All pastes #609053 Raw Edit

Stuff

public text v1 · immutable
·published 2007-07-08 17:18 UTC
rendered paste body
 
Do Exercise 5 on page 674 in Savitch: 
 
Adapt the Rational class you defined in question 2 of this assignment as follows: 
Ensure that you have three constructors:   a constructor with two arguments that can be used to set the member variables of an object to any legitimate 
values;   a constructor with a single parameter of type int called whole_number so that the constructor initialises 
the object to the rational number whole_number/1;   a default constructor that initialises an object to 0 (that is 0/1). 
 
Overload the input and output operators >> and <<.  Numbers are to be input and output in the form 1/2, 15/32, 
300/401, and so forth.  Note that the numerator, the denominator, or both may contain a minus sign, so -1/2,  
15/-32, and -300/-401 are also possible inputs.  Overload all of the following operators so that they correctly 
apply to the type Rational: ==, <, <=, >, >=, +, -, *, and /.  Also write a test program to test your class. 
 
Hints: Include a function to normalize the values stored so that, after normalization, the denominator is positive and 
the numerator and denominator are as small as possible.  For example, after normalization 4/-8 would be presented 
the same as -1/2.  The rational number can be reduced to its smallest value by computing the greatest common 
divisor (GCD) of the numerator and denominator, and then dividing each by this number. 
 
A recursive definition to compute the GCD of two integers follows: 
 
              | n if n divides m 
gcd(m,n) =    |
              | gcd(n, remainder of m divided by n) otherwise