Assignment 3
Due Tuesday, February 18 Thursday, February 20, 2003
Make sure you notice the updates (using operators)
discussed at the bottom of this page.
You are going to create a nice class for holding Rationals and working
with them. You should store the numerator and denominator in lowest terms.
You will need (all except #10 are member functions.)
-
Rational(); a default constructor that sets a rational to 1/1.
-
Rational(int num, int den); a constructor that takes values for the numerator
and denominator. It should store values in reduced form.
-
bool set(int num, int den); a function that allows you to set the value
of a rational. It should store values in reduced form.
-
Rational plus(Rational right); that returns the sum of two Rationals -
the calling Rational plus the parameter Rational, storing the answer in
a third Rational. The two summands should not be changed.
-
Rational minus(Rational right); that returns the difference of two Rationals
- the calling Rational minus the parameter Rational, storing the answer
in a third Rational. The two operands should not be changed.
-
Rational times(Rational right); that returns the product of two Rationals
- the calling Rational times the parameter Rational, storing the answer
in a third Rational. The two operands should not be changed.
-
Rational divide(Rational right); that returns the quotient of two Rationals
- the calling Rational divided by the parameter Rational, storing the answer
in a third Rational. The two operands should not be changed.
-
bool equal(Rational right); that compares the calling Rational to the argument.
Returns true if they are equal.
-
bool equal(int right); that compares the calling Rational to the argument.
Returns true if they are mathematically equal.
-
bool equal(int left, Rational right); non-member function that compares
the first argument to the second argument, returning true if they are mathematically
equal.
-
bool greaterthan(Rational right); that returns true if the calling Rational
is greater than the parameter.
-
void printquotient(); that prints the calling Rational as a quotient in
the form 7/9.
-
void printdecimal(); that prints the calling Rational as a decimal in the
form 2.54.(You decide how to deal with number of digits.)
-
void read(); that reads in a Rational entered in the form int/int, for
example 37/67. It should store values in reduced form.
-
int gcd(int a, int b); a member function, private to the Rational class,
to perform Euclid's Algorithm, so you can store all your Rationals in reduced
form. It should return the gcd of the two parameters.
You can use Euclid's Algorithm to find the gcd (greaatest common divisor),
as follows.
ex. to find the gcd of 234 and 318:
318/234 = 1 with remainder 84
234/84 = 2 with remainder 66
84/66 = 1 with remainder 18
66/18 = 3 with remainder 12
18/12 = 1 with remainder 6
12/6 = 2 with remainder 0
Since 6 is the last nonzero remainder, 6 is the gcd.
Once you know the gcd, divide both the numerator and the denominator by
it to put the rational in lowest terms, i.e. to reduce your rational.
The rational 318/234 should be reduced to 53/39.
Don't forget about negative numbers. Don't forget about dividing
by zero or zero denominators.
Write a good test program, that really works your class. It should
demonstrate all the features, showing how the class handles both good and
bad situations.
This program will be in five files: a header and .cpp file for the class,
a header and .cpp file for the global functions, and a .cpp file for the
main program. They must all be in one project.
Once you have this up and running, change all the funny function names
to good operators:
plus, minus, times, divide should be replaced by
operator+, operator-, operator*, and operator/
equal and greaterthan should be replaced by
operator== and operator>
Of course, the calls to these functions will have to be changed as well.