Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

class RationalNumber

Posted on 2004-04-20
8
Medium Priority
?
761 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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 viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

636 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