Solved

class RationalNumber

Posted on 2004-04-20
8
745 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10875104
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 500 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

820 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