Solved

C++

Posted on 1998-11-28
3
199 Views
Last Modified: 2010-04-01
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;
}


0
Comment
Question by:demami
  • 3
3 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 1178771
You've done all the hard work.  

>> a)addition of two rational numbers
Done.  Use +
>> b)subtraction of two rational numbers
Done.  Use -
>> c)multiplication of two rational numbers
Done.  Use *
>> d)division of two rational numbers
Done Use /
>>  e)printing rational numbers in the form a/b where a is the numerator and
>>  b is the denominator
done use <<

0
 
LVL 22

Expert Comment

by:nietod
ID: 1178772
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.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1178773
So, did this help?  Do you need more help?
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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.
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.

830 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