Link to home
Start Free TrialLog in
Avatar of demami
demami

asked on

C++

Hi,
I would like to modify my program to do the following:
  I need to use the following definitions to create 4 rational objects:
Rational P(70,36)
const Rational Q(51,-90)
const Rational R(-34,-60)
Rational S=Q.add(R); //member function add() adds Q and R
The print out should look like this:
P = 70/36 = 35/18 = 1.94444
Q = 50/(-90) = -17/30 = -0.566667
R = -34/(-60) = 17/30 = 0.566667
S = Q + R = 0/1 = 0
P + Q = 62/45 = 1.37778
Q - R = -17/15 = -1.13333
Q * R = -289/900 = -0.321111
P/R = 175/51 = 3.43137
P/S = Error: Divide by zero. Default value 0/1 is returned.
I know I need these public member functions:
    a)addition of two rational numbers
    b)subtraction of two rational numbers
    c)multiplication of two rational numbers
    d)division of two rational numbers
        and each of these should be stored in reduced form.
    e)printing rational numbers in the form a/b where a is the numerator and
b is the denominator
    f)printing rational numbers in floating point format
and they are already in my rational.h but I need help in implementing them
in the rational.cpp program.
Thanks for the help.
//Rational.h
#ifndef __RATIONAL
#define __RATIONAL
//
// =, +=, -=, /=, *=      --> Usual assignment
// +, -, *, /             --> Usual binary arithmetic
// <, <=, >, >=, ==, !=   --> Usual relational and equality
// << and >>              --> Input and output
// double LongDecimal( )  --> Return double equivalent
#include <iostream.h>
typedef long IntType;
class Rational
{
  public:
        // Constructors
    Rational( const IntType & Numerator = 0 ) :
        Numer( Numerator ), Denom( 1 ) { }
    Rational( const IntType & Numerator,
              const IntType & Denominator ) :
        Numer( Numerator ), Denom( Denominator )
            { FixSigns( ); Reduce( ); }
    Rational( const Rational & Rhs ) :
        Numer( Rhs.Numer ), Denom( Rhs.Denom ) { }
        // Destructor
    ~Rational( ) { }
        // Assignment Operators
    const Rational & operator= ( const Rational & Rhs );
    const Rational & operator+=( const Rational & Rhs );
    const Rational & operator-=( const Rational & Rhs );
    const Rational & operator/=( const Rational & Rhs );
    const Rational & operator*=( const Rational & Rhs );
        // Mathematical Binary Operators
    Rational operator+( const Rational & Rhs ) const;
    Rational operator-( const Rational & Rhs ) const;
    Rational operator/( const Rational & Rhs ) const;
    Rational operator*( const Rational & Rhs ) const;
        // Relational and Equality Operators
    int operator< ( const Rational & Rhs ) const;
    int operator<=( const Rational & Rhs ) const;
    int operator> ( const Rational & Rhs ) const;
    int operator>=( const Rational & Rhs ) const;
    int operator==( const Rational & Rhs ) const;
    int operator!=( const Rational & Rhs ) const;
        // Unary Operators
    const Rational & operator++( );            // Prefix
    Rational operator++( int );                // Postfix
    const Rational & operator--( );            // Prefix
    Rational operator--( int );                // Postfix
    const Rational & operator+( ) const;
    Rational operator-( ) const;
    int operator!( ) const;
        // Member Function
    double LongDecimal( ) const   // Do the division
        { return double( Numer ) / double( Denom ); }
        // Friends of the class: privacy is waived for these
    friend ostream & operator<<
            ( ostream & Out, const Rational & Value );
    friend istream & operator>>
            ( istream & In,  Rational & Value );
  private:
    IntType Numer;             // The numerator
    IntType Denom;             // The denominator
    void FixSigns( );          // Ensures Denom >= 0
    void Reduce( );            // Ensures lowest form
};
#endif
//Rational.cpp
#include "Rational.h"
// N is guaranteed non-negative
IntType
Gcd1( const IntType & N, const IntType & M )
{
    if( N % M == 0 )
        return M;
    else
        return Gcd1( M, N % M );
}
IntType
Gcd( const IntType & M, const IntType & N )
{
    if( M > 0 )
        return Gcd1( N, M );
    else
        return Gcd1( N, -M );
}
void
Rational::FixSigns( )
{
    if( Denom < 0 )
    {
        Denom = -Denom;
        Numer = -Numer;
    }
}
void
Rational::Reduce( )
{
    IntType D = 1;
    if( Denom != 0 && Numer != 0 )
        D = Gcd( Numer, Denom );
    if( D > 1 )
    {
        Numer /= D;
        Denom /= D;
    }
}
const Rational &
Rational::operator=( const Rational & Rhs )
{
    if( this != &Rhs )
    {
        Numer = Rhs.Numer;
        Denom = Rhs.Denom;
    }
    return *this;
}
const Rational &
Rational::operator+=( const Rational & Rhs )
{
    Numer = Numer * Rhs.Denom + Rhs.Numer * Denom;
    Denom = Denom * Rhs.Denom;
    Reduce( );
    return *this;
}
const Rational &
Rational::operator-=( const Rational & Rhs )
{
    Numer = Numer * Rhs.Denom - Rhs.Numer * Denom;
    Denom = Denom * Rhs.Denom;
    Reduce( );
    return *this;
}
const Rational &
Rational::operator*=( const Rational & Rhs )
{
    IntType NewNumer = Numer * Rhs.Numer;
    IntType NewDenom = Denom * Rhs.Denom;
    Numer = NewNumer;
    Denom = NewDenom;
    Reduce( );
    return *this;
}
const Rational &
Rational::operator/=( const Rational & Rhs )
{
    IntType NewNumer = Numer * Rhs.Denom;
    IntType NewDenom = Denom * Rhs.Numer;
    Numer = NewNumer;
    Denom = NewDenom;
    FixSigns( );
    Reduce( );
    return *this;
}
Rational
Rational::operator+( const Rational & Rhs ) const
{
    Rational Answer( *this );
    Answer += Rhs;
    return Answer;
}
Rational
Rational::operator-( const Rational & Rhs ) const
{
    Rational Answer( *this );
    Answer -= Rhs;
    return Answer;
}
Rational
Rational::operator*( const Rational & Rhs ) const
{
    Rational Answer( *this );
    Answer *= Rhs;
    return Answer;
}
Rational
Rational::operator/( const Rational & Rhs ) const
{
    Rational Answer( *this );
    Answer /= Rhs;
    return Answer;
}
int
Rational::operator==( const Rational & Rhs ) const
{
    return Numer * Rhs.Denom == Denom * Rhs.Numer;
}
int
Rational::operator!=( const Rational & Rhs ) const
{
    return Numer * Rhs.Denom != Denom * Rhs.Numer;
}
int
Rational::operator<=( const Rational & Rhs ) const
{
    return Numer * Rhs.Denom <= Denom * Rhs.Numer;
}
int
Rational::operator<( const Rational & Rhs ) const
{
    return Numer * Rhs.Denom < Denom * Rhs.Numer;
}
int
Rational::operator>=( const Rational & Rhs ) const
{
    return Numer * Rhs.Denom >= Denom * Rhs.Numer;
}
int
Rational::operator>( const Rational & Rhs ) const
{
    return Numer * Rhs.Denom > Denom * Rhs.Numer;
}
const Rational &
Rational::operator++( )
{
    Numer += Denom;
    return *this;
}
Rational
Rational::operator++( int )
{
    Rational Tmp = *this;
    Numer += Denom;
    return Tmp;
}
const Rational &
Rational::operator--( )
{
    Numer -= Denom;
    return *this;
}
Rational
Rational::operator--( int )
{
    Rational Tmp = *this;
    Numer -= Denom;
    return Tmp;
}
int
Rational::operator!( ) const
{
    return !Numer;
}
const Rational &
Rational::operator+( ) const
{
    return *this;
}
Rational
Rational::operator-( ) const
{
    return Rational( -Numer, Denom );
}
istream &
operator>>( istream & In, Rational & Value )
{
    In >> Value.Numer;

    char Ch = ' ';
    In.get( Ch );
    if( Ch == '/' )
    {
        In >> Value.Denom;
        Value.FixSigns( );
        Value.Reduce( );
    }
    else
    {
        Value.Denom = 1;
        In.putback( Ch );
    }
    return In;
}
ostream &
operator<<( ostream & Out, const Rational & Value )
{
    if( Value.Denom != 0 )
    {
        Out << Value.Numer;
        if( Value.Denom != 1 )
            Out << '/' << Value.Denom;
        return Out;
    }
    if( Value.Numer == 0 )
        Out << "indeterminate";
    else
    {
        if( Value.Numer < 0 )
            Out << '-';
        Out << "infinity";
    }
    return Out;
}


ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nietod
nietod

All you need now is to use them from main, like

int main()
{
   Rational P(70,36);
   const Rational Q(51,-90);
   const Rational R(-34,-60);  
   Rational S=Q + R;  // NOTE + instead of "add()".

  // To produce the first line of ouput.
  cout << "P = " << P << " = ";
  P.Reduce(); //// Note for this Reduce() must be made public.
  cout << P << " = " << P.LongDecimal();
}

You should be able to figure out the other lines for yourself.  (You will pretty much have to, as we can only provide very limited on school assignments.)  If you have questions, let me know.
So, did this help?  Do you need more help?