[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

class RationalNumber

Posted on 2004-04-20
8
Medium Priority
?
766 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
ID: 10874171
What is your question?
0
 

Author Comment

by:edelossantos
ID: 10874468
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
ID: 10874901
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10875104
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 2000 total points
ID: 10876282
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
ID: 10877030
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
ID: 10877035
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
ID: 10877088
My pleasure.  Once again, excellent!!!!!!!!!! Thank you.  Del
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

831 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