Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C++

Posted on 1998-11-28
3
Medium Priority
?
215 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 100 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

715 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