Solved

class RationalNumber

Posted on 2004-04-20
8
734 Views
Last Modified: 2012-06-27
Need to create a class RationalNumber(fractions) with the following capabilities:

* Create a constructor that prevents a 0 denominator in a fraction.

* Design the add, sub, mul, and division functions for this class.

* Design the functions for the relational and equality operation in this class.

I am looking for the ACTUAL WORKING CODE(not snippets) that will generate this output:

5/3 addition 3/2 = 19/6
5/3 subtraction 3/2 = 1/6
5/3 multiplication 3/2 = 5/2
5/3 division 3/2 = 10/9

5/3 is:
         != 3/2
         != 3/2

 This is what I have so far.  Please incorporate this format if you can.  Please Advise.  Del

// rational.h

#ifndef RATIONAL_H
#define RATIONAL_H

class RationalNumber {

public:

   RationalNumber(int=0, int=1);
   RationalNumber add(const RationalNumber &);
   RationalNumber sub(const RationalNumber &);
   RationalNumber mul(const RationalNumber &);
   RationalNumber div(const RationalNumber &);

   bool equal(const RationalNumber &) const;
   bool notEqual(const RationalNumber &) const;
   void printRational() const;
   
private:

   int numerator;
   int denominator;

};

#endif

// rational.cpp

#include <iostream>

using namespace std;

#include "rational.h"

RationalNumber::RationalNumber(int n, int d)
{

   numerator = n;
   denominator = d;

}

RationalNumber RationalNumber::add(const RationalNumber &a)
{

   RationalNumber t;

   t.numerator = a.numerator * denominator;
   t.numerator += a.denominator * numerator;
   t.denominator = a.denominator * denominator;

   return t;

}

RationalNumber RationalNumber::sub(const RationalNumber &s)
{

   RationalNumber t;

   t.numerator = s.denominator * numerator;
   t.numerator -= denominator * s.numerator;
   t.denominator = s.denominator * denominator;

   return t;

}

RationalNumber RationalNumber::mul(const RationalNumber &m)
{

   RationalNumber t;

   t.numerator = m.numerator * numerator;
   t.denominator = m.denominator * denominator;
      
   return t;

}

RationalNumber RationalNumber::div(const RationalNumber &v)
{

   RationalNumber t;

   t.numerator = v.denominator * numerator;
   t.denominator = denominator * v.numerator;

   return t;

}

void RationalNumber::printRational() const
{

   if(denominator == 0)
     cout << "\nDivide by zero error!!!\n";

   else
      if(numerator == 0)
         cout << 0;
      else
         cout << numerator << '/' << denominator;

}

bool RationalNumber::equal(const RationalNumber &e) const
{

   ...................................................

}

bool RationalNumber::notEqual(const RationalNumber &n) const
{

   ...................................................

}

// rational_driver.cpp

#include <iostream>

using namespace std;

#include "rational.h"

int main()
{

   RationalNumber c(5,3);
   RationalNumber d(3,2);
   RationalNumber x;
      
   c.printRational();
   cout << " addition ";
   d.printRational();
   x = c.add(d);
   cout << " = ";
   x.printRational();
   cout << '\n';
   x.printRational();
   cout << " = ";
   cout << "\n\n";

   c.printRational();
   cout << " subtraction ";
   d.printRational();
   x = c.sub(d);
   cout << " = ";
   x.printRational();
   cout << '\n';
   x.printRational();
   cout << " = ";
   cout << "\n\n";

   c.printRational();
   cout << " multiplication ";
   d.printRational();
   x = c.mul(d);
   cout << " = ";
   x.printRational();
   cout << '\n';
   x.printRational();
   cout << " = ";
   cout << "\n\n";

   c.printRational();
   cout << " division ";
   d.printRational();
   x = c.div(d);
   cout << " = ";
   x.printRational();
   cout << '\n';
   x.printRational();
   cout << " = ";
   cout << endl;

   return 0;

} // end driver

      






     
0
Comment
Question by:edelossantos
8 Comments
 
LVL 30

Expert Comment

by:Axter
Comment Utility
What is your question?
0
 

Author Comment

by:edelossantos
Comment Utility
I need the two function definitions written for:

bool RationalNumber::equal(const RationalNumber &e) const
{

   ...................................................

}

bool RationalNumber::notEqual(const RationalNumber &n) const
{

   ...................................................

}

and then I need the code tested and possibly corrected so it will compile.
0
 

Author Comment

by:edelossantos
Comment Utility
In other words I need the above code modified to generate the output listed.  Please advise.  Del

5/3 addition 3/2 = 19/6
5/3 subtraction 3/2 = 1/6
5/3 multiplication 3/2 = 5/2
5/3 division 3/2 = 10/9

5/3 is:
         != 3/2
         != 3/2

0
 
LVL 10

Expert Comment

by:Sys_Prog
Comment Utility
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 500 total points
Comment Utility
Take this


// rational.h

#ifndef RATIONAL_H
#define RATIONAL_H
class RationalNumber {

public:

   RationalNumber();
   RationalNumber(int num, int denom = 1);
   RationalNumber(const RationalNumber &);

   RationalNumber add(const RationalNumber &) const;
   RationalNumber sub(const RationalNumber &) const;
   RationalNumber mul(const RationalNumber &) const;
   RationalNumber div(const RationalNumber &) const;
   bool equal(const RationalNumber &) const;
   bool notEqual(const RationalNumber &) const;

   bool operator == (const RationalNumber& rn);
   bool operator != (const RationalNumber& rn);
   bool operator <  (const RationalNumber& rn);
   bool operator >  (const RationalNumber& rn);
   bool operator <= (const RationalNumber& rn);
   bool operator >= (const RationalNumber& rn);
   
   RationalNumber& operator = (const RationalNumber& rn);
   RationalNumber& operator = (int n);
   RationalNumber& operator += (const RationalNumber& rn);
   RationalNumber& operator += (int n);
   RationalNumber& operator -= (const RationalNumber& rn);
   RationalNumber& operator -= (int n);
   RationalNumber& operator *= (const RationalNumber& rn);
   RationalNumber& operator *= (int n);
   RationalNumber& operator /= (const RationalNumber& rn);
   RationalNumber& operator /= (int n);

   RationalNumber operator + (const RationalNumber& rn) const;
   RationalNumber operator + (int n)  const;
   RationalNumber operator - (const RationalNumber& rn)  const;
   RationalNumber operator - (int n)  const;
   RationalNumber operator * (const RationalNumber& rn)  const;
   RationalNumber operator * (int n)  const;
   RationalNumber operator / (const RationalNumber& rn)  const;
   RationalNumber operator / (int n)  const;


   void printRational() const;

   friend RationalNumber operator+ (int n, const RationalNumber& rn);
   friend RationalNumber operator- (int n, const RationalNumber& rn);
   friend RationalNumber operator* (int n, const RationalNumber& rn);
   friend RationalNumber operator/ (int n, const RationalNumber& rn);
   
private:

   int numerator;
   int denominator;

};

#endif

// rational.cpp

#include <iostream>
#include <assert.h>

using namespace std;

#include "rational.h"

RationalNumber::RationalNumber()
: numerator(0), denominator(1)
{
}

RationalNumber::RationalNumber(int n, int d)
: numerator(n), denominator(d)
{
   if (d == 0)
   {
       assert(d != 0);
       denominator = 1;
   }
}

RationalNumber::RationalNumber(const RationalNumber & rn)
:   numerator  (rn.numerator),
    denominator(rn.denominator)
{
}

bool RationalNumber::operator == (const RationalNumber& rn)
{
    return (numerator * rn.denominator == rn.numerator * denominator);
}

bool RationalNumber::operator != (const RationalNumber& rn)
{
    return !(*this == rn);
}

bool RationalNumber::operator <  (const RationalNumber& rn)
{
    return (numerator * rn.denominator < rn.numerator * denominator);
}

bool RationalNumber::operator >  (const RationalNumber& rn)
{
    return (numerator * rn.denominator > rn.numerator * denominator);
}

bool RationalNumber::operator <= (const RationalNumber& rn)
{
    return !(*this > rn);
}

bool RationalNumber::operator >= (const RationalNumber& rn)
{
    return !(*this < rn);
}


RationalNumber RationalNumber::operator + (const RationalNumber& rn) const
{
   return add(rn);
}
RationalNumber RationalNumber::operator + (int n)  const
{
   return add(RationalNumber(n));
}
RationalNumber RationalNumber::operator - (const RationalNumber& rn)  const
{
   return sub(rn);
}
RationalNumber RationalNumber::operator - (int n)  const
{
   return sub(RationalNumber(n));
}
RationalNumber RationalNumber::operator * (const RationalNumber& rn)  const
{
   return mul(rn);
}
RationalNumber RationalNumber::operator * (int n)  const
{
   return mul(RationalNumber(n));
}
RationalNumber RationalNumber::operator / (const RationalNumber& rn)  const
{
   return div(rn);
}
RationalNumber RationalNumber::operator / (int n)  const
{
   return div(RationalNumber(n));
}

RationalNumber& RationalNumber::operator = (const RationalNumber& rn)
{
    numerator   = rn.numerator;
    denominator = rn.denominator;
    return *this;
}

RationalNumber& RationalNumber::operator = (int n)
{
    numerator   = n;
    denominator = 1;
    return *this;
}

RationalNumber& RationalNumber::operator += (const RationalNumber& rn)
{
    *this = add(rn);
    return *this;
}

RationalNumber& RationalNumber::operator += (int n)
{
    *this = add(n);
    return *this;
}

RationalNumber& RationalNumber::operator -= (const RationalNumber& rn)
{
    *this = sub(rn);
    return *this;
}

RationalNumber& RationalNumber::operator -= (int n)
{
    *this = sub(n);
    return *this;
}

RationalNumber& RationalNumber::operator *= (const RationalNumber& rn)
{
    *this = mul(rn);
    return *this;
}

RationalNumber& RationalNumber::operator *= (int n)
{
    *this = mul(n);
    return *this;
}


RationalNumber& RationalNumber::operator /= (const RationalNumber& rn)
{
    *this = div(rn);
    return *this;
}

RationalNumber& RationalNumber::operator /= (int n)
{
    *this = div(n);
    return *this;
}

RationalNumber operator+ (int n, const RationalNumber& rn)
{
    return rn.add(n);
}

RationalNumber operator- (int n, const RationalNumber& rn)
{
    RationalNumber t(n);
    return t.sub(rn);
}

RationalNumber operator* (int n, const RationalNumber& rn)
{
    return rn.mul(n);
}

RationalNumber operator/ (int n, const RationalNumber& rn)
{
    RationalNumber t(n);
    return t.div(rn);
}


RationalNumber RationalNumber::add(const RationalNumber &a)  const
{

   RationalNumber t;

   t.numerator = a.numerator * denominator;
   t.numerator += a.denominator * numerator;
   t.denominator = a.denominator * denominator;

   return t;
}

RationalNumber RationalNumber::sub(const RationalNumber &s)  const
{

   RationalNumber t;

   t.numerator = s.denominator * numerator;
   t.numerator -= denominator * s.numerator;
   t.denominator = s.denominator * denominator;

   return t;

}

RationalNumber RationalNumber::mul(const RationalNumber &m)  const
{

   RationalNumber t;

   t.numerator = m.numerator * numerator;
   t.denominator = m.denominator * denominator;
     
   return t;

}

RationalNumber RationalNumber::div(const RationalNumber &v)  const
{

   RationalNumber t;

   t.numerator = v.denominator * numerator;
   
   if (v.numerator != 0)
   {
       t.denominator = denominator * v.numerator;
   }
   else
   {
       assert(v.numerator != 0);
   }

   return t;

}

void RationalNumber::printRational() const
{

     if(numerator == 0)
        cout << 0;
     else
        cout << numerator << '/' << denominator;

}

bool RationalNumber::equal(const RationalNumber &e) const
{
    return (numerator*e.denominator == e.numerator*denominator);
}

bool RationalNumber::notEqual(const RationalNumber &n) const
{
    return !equal(n);

}

// rational_driver.cpp
#include <iostream>

using namespace std;

#include "rational.h"

int main()
{

   RationalNumber c(5,3);
   RationalNumber d(3,2);
   RationalNumber x;
     
   c.printRational();
   cout << " addition ";
   d.printRational();
   x = c.add(d);
   cout << " = ";
   x.printRational();
   cout << '\n';
   x.printRational();
   cout << " = ";
   cout << "\n\n";

   c.printRational();
   cout << " subtraction ";
   d.printRational();
   x = c.sub(d);
   cout << " = ";
   x.printRational();
   cout << '\n';
   x.printRational();
   cout << " = ";
   cout << "\n\n";

   c.printRational();
   cout << " multiplication ";
   d.printRational();
   x = c.mul(d);
   cout << " = ";
   x.printRational();
   cout << '\n';
   x.printRational();
   cout << " = ";
   cout << "\n\n";

   c.printRational();
   cout << " division ";
   d.printRational();
   x = c.div(d);
   cout << " = ";
   x.printRational();
   cout << '\n';
   x.printRational();
   cout << " = ";
   cout << endl;

   return 0;

} // end driver

Regards, Alex
0
 

Author Comment

by:edelossantos
Comment Utility
itsmeandnobodyelse,

Excellent!!!!!!!!!! Would you modify the above code to also include istream and ostream?  A million thanks.  Del
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
Thank's for the points:

I added a function reduceFraction(), that might be called after any update of a RationalNumber. You may test the function using that code snippet at the end of main():

int main()
{
   ...
 
   char  temp[128];
   char* pszDenom;
   while ( x != -1 )
   {
       cout << "Enter RationalNumber [e. g. 17/5] ==>";
       cin  >> temp;

       pszDenom = strchr(temp, '/');
       if (pszDenom == NULL)
           pszDenom = " 1";
       x = RationalNumber(atoi(temp), atoi(++pszDenom));
       x.reduceFraction();
       x.printRational();
       cout << endl << endl;
   }
   return 0;
}

RationalNumber& RationalNumber::reduceFraction()
{
    int  i   = numerator;
    int  j   = denominator;
    // save sign of number
    bool neg = i * j < 0;
    // make all numbers positive
    if ( i < 0 )
        i = -i;
    if ( j < 0 )
        j = -j;

    // get minimum
    int m = (i < j)? i : j;

    // we check 2 and all odd numbers til m
    // so, we need a step of 1 first and a step of 2 else
    for (int n = 2; n <= m; n += (n == 2)? 1 : 2)
    {
        // check if both i and j can be divided by n without rest
        while ((i % n) == 0 && (j % n) == 0)
        {
            // ok, divide both and minimum also (as m is either i or j)
            i   /= n;
            j   /= n;
            m   /= n;
        }
    }
    numerator   = (neg)? -i : i;
    denominator = j;
    return *this;
}

Regards, Alex


0
 

Author Comment

by:edelossantos
Comment Utility
My pleasure.  Once again, excellent!!!!!!!!!! Thank you.  Del
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now