Solved

C++

Posted on 1998-11-28
3
202 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
[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
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

734 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