OOP  - Assignment 4

Topic:              Overloaded Operators and Rational Numbers

Points:             25

Due:                2-12-2001

Instructor:      Dana Steil

Files:

Rational.h

Rational.cpp

Assignment4.cpp

Create a class to represent rational numbers with the following functionality:

 

Constructor(s): 

The user of the class should be able to create an instance of a rational number passing in 0-2 arguments. If no arguments are passed in the value should be 0, for example 0/1.  If one argument is passed in, it should be considered a whole number and thus be represented over one in radical form, for example 5/1.  If two numbers are passed in, the first should be considered the numerator the second the denominator.  Be sure to reduce the fraction completely.

 

Unary Operators:

++ and --  (both the post and pre increment and decrement operators) four in all.  These operators should add or remove one from the numerator.  Be sure to reduce the resulting radical.

 

Binary Operators:

+,-,*,/,=, and == should be overloaded.  Each of these operators should work with whole numbers and other rational numbers.  Be sure to reduce the resulting radical.

 

Friend Operators:

The stream insertion << and stream extraction >> operators should get and show the value of the radical. 

 

The stream-extraction operator should prompt the user as follows:

Numerator? 7

Denominator? 9

 

The stream insertion operator should show the radical with a forward slash ‘/’ appearing between the numerator and denominator with one exception.  If the denominator is 1, only the numerator should be displayed.

 

Private Function(s):

A private reduce fraction and common denominator functions will be useful.

 

Watch out for negative values in both the numerator and denominator.


 

#ifndef RATIONAL_H

#define RATIONAL_H

 

#include<iostream>

 

using std::ostream;

using std::istream;

 

class CRational

{

       friend ostream &operator<<(ostream &, const CRational & Fraction);

       friend istream &operator>>(istream &, CRational &Fraction);

 

       friend CRational &operator++(CRational &Rational, int Garbage);

       friend CRational &operator--(CRational &Rational, int Garbage);

 

       friend const CRational operator+ (const long lValue, const CRational & rValue);

       friend const CRational operator- (const long lValue, const CRational & rValue);

       friend const CRational operator* (const long lValue, const CRational & rValue);

       friend const CRational operator/ (const long lValue, const CRational & rValue);

 

       public:

              CRational(long NewNumerator = 0, int NewDenominator = 1);

 

              const CRational & operator=(const CRational & rValue);

              const bool operator==(const CRational & rValue) const;

              const CRational operator+ (const CRational & rValue) const;

              const CRational operator- (const CRational & rValue) const;

              const CRational operator* (const CRational & rValue) const;

              const CRational operator/ (const CRational & rValue) const;

 

              CRational &operator++();

              CRational &operator--();

       private:

              long m_Numerator;

              long m_Denominator;

 

              long LeastCommonMultiple(long x, long y) const;

              long GreatestCommonDivisor(long x, long y) const;

              void Reduce();

             

              //any other private functions you may need

};

 

#endif


 

long CRational::LeastCommonMultiple(long x, long y) const

{

       bool Continue = true;

       long Guess = x;

 

       while (Continue)

       {

              if(Guess % y == 0)

              {

                     Continue = false;

              }

              else

              {     

                     Guess += x;

              }

       }

 

       return Guess;

}

 

long CRational::GreatestCommonDivisor(long x, long y) const

{

       long Remainder = x % y;

 

       while(Remainder != 0)

       {

              x = y;

              y = Remainder;

              Remainder = x % y;        

       }

 

       return y;

}

 

void CRational::Reduce()

{

       long GCD = GreatestCommonDivisor(m_Numerator, m_Denominator);

      

       m_Numerator = m_Numerator/GCD;

       m_Denominator = m_Denominator/GCD;

}