Write a rational number class. This problem will be revisited in Chapter11, where operator overloading will make the problem much easier. For now we will use member functions add, sub, mul, div, and less that each carry out the operations +, -, *, /, and <. For example, a + b will be written a.add(b), and a < b will be written a.less(b).
Define a class for rational numbers. A rational number is a “ratio-nal” number, composed of two integers with division indicated. The division is not carried out, it is only indicated, as in 1/2, 2/3, 15/32, 65/4, 16/5. You should represent rational numbers by two int values, numerator and denominator.
A principle of abstract data type construction is that constructors must be present to create objects with any legal values. You should provide constructors to make objects out of pairs of int values; this is a constructor with two int parameters. Since every int is also a rational number, as in 2/1 or 17/1, you should provide a constructor with a single int parameter.
Provide member functions input and output that take an istream and ostream argument, respectively, and fetch or write rational numbers in the form 2/3 or 37/51 to or from the keyboard (and to or from a file).
Provide member functions add, sub, mul, and div that return a rational value. Provide a function less that returns a bool value. These functions should do the operation suggested by the name. Provide a member function neg that has no parameters and returns the negative of the calling object.
Provide a main function that thoroughly tests your class implementation. The following formulas will be useful in defining functions.
a/b + c/d = (a * d + b * c) / (b * d)
a/b - c/d = (a * d - b * c) / (b * d)
(a/b) * (c/d) = (a * c) / (b * d)
(a/b) / (c/d) = (a * d) / (c * b)
-(a/b) = (-a/b)
(a/b) < (c/d) means (a * d) < (c * b)
(a/b) == (c/d) means (a * d) == (c * b)
Let any sign be carried by the numerator; keep the denominator positive.
This is what I have so far, I know Im wrong with a lot, I just need somebody to just slowly talk me through the problem. Thanks.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
#include <iostream>
using namespace std;
class Rational
{
public :
Rational ( int initNum, unsigned int initDenom ) ;
int getNumerator ( ) ;
unsigned int getDenominator ( ) ;
Rational add ( Rational secondSummand ) ;
void output ( ostream & outputStream ) ;
private :
int numerator ;
unsigned int denominator ;
int getNumerator () const
{ return(numerator);
}
int getDenominator () const
{ return(denominator);
}
};
Rational Rational :: add ( Rational secondSummand )
{
Rational result(left.getNumerator() * right.getDemoninator() +
left.getDemoninator() * right.getNumerator(),
left.getDemoninator() * right.getDemoninator()
);
return result;
}
int main()
{
int Rational :: getNumerator ()
{ return this -> numerator;}
int Rational :: getDenominator ()
{ return this -> denominator;}
return 0;
}
|