Write a rational number class. This problem will be revisited in chapter 11, whe
ID: 3625486 • Letter: W
Question
Write a rational number class. This problem will be revisited in chapter 11, where operate 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 function input and output that take an istream and ostream argument, respectively, and fetch or write rational numbers in the for 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 funciton 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.
Explanation / Answer
Dear... Sample code for given class: using namespace std;class Rational { private: int num; int den; public: void setNumerator(int); void setDenominator(int); int getNumerator(); int getDenominator(); void simplify(); Rational operator+(Rational); Rational operator-(Rational); Rational operator*(Rational); Rational operator/(Rational); bool operator==(Rational); bool operator!=(Rational); bool operator>(Rational); bool operator<(Rational); bool operator>=(Rational); bool operator<=(Rational); Rational operator-(); }; int main() { int ratNum1 = 0; cout << "Enter the denominator of the first rational number: "; cin >> ratNum1; int ratDen1 = 0; cout << "Enter the numerator of the first rational number: "; cin >> ratDen1; Rational rat1; rat1.setNumerator(ratNum1); rat1.setDenominator(ratDen1); int ratNum2 = 0; cout << "Enter the denominator of the second rational number: "; cin >> ratNum2; int ratDen2 = 0; cout << "Enter the numerator of the second rational number: "; cin >> ratDen2; Rational rat2; rat2.setNumerator(ratNum2); rat2.setDenominator(ratDen2); Rational rat3 = rat1 + rat2; cout << rat1.getNumerator() << "/" << rat1.getDenominator() << " + "; cout << rat2.getNumerator() << "/" << rat2.getDenominator(); cout << " = " << rat3.getNumerator() << "/" << rat3.getDenominator() << endl; cout << "Simplified " << rat3.getNumerator() << "/" << rat3.getDenominator() << " is "; rat3.simplify(); cout << rat3.getNumerator() << "/" << rat3.getDenominator() << endl; if(rat1 > rat2) { cout << rat1.getNumerator() << "/" << rat1.getDenominator() << " is bigger than " << rat2.getNumerator() << "/" << rat2.getDenominator() << endl; } else { cout << rat1.getNumerator() << "/" << rat1.getDenominator() << " is NOT bigger than " << rat2.getNumerator() << "/" << rat2.getDenominator() << endl; } system("PAUSE"); return 0; } void Rational::setNumerator(int n) { num = n; } void Rational::setDenominator(int n) { den = n; } int Rational::getNumerator() { return num; } int Rational::getDenominator() { return den; } Rational Rational::operator+(Rational ratPassed) { Rational ratResult ratResult.num = num * ratPassed.den + den * ratPassed.num; ratResult.den = den * ratPassed.den; return ratResult; } Rational Rational::operator-(Rational ratPassed) { Rational ratResult; ratResult.num = num * ratPassed.den - den * ratPassed.num; ratResult.den = den * ratPassed.den; return ratResult; Rational Rational::operator*(Rational ratPassed) { Rational ratResult; ratResult.num = num * ratPassed.num; ratResult.den = den * ratPassed.den; return ratResult; } Rational Rational::operator/(Rational ratPassed) { Rational ratResult; ratResult.num = num * ratPassed.den; ratResult.den = den * ratPassed.num; return ratResult; } bool Rational::operator==(Rational ratPassed) { simplify(); ratPassed.simplify(); return (ratPassed.num == num) && (ratPassed.den == den); } bool Rational::operator!=(Rational ratPassed) { simplify(); ratPassed.simplify(); return (ratPassed.num != num) && (ratPassed.den != den); } bool Rational::operator>(Rational ratPassed) { simplify(); ratPassed.simplify(); return (num * ratPassed.den) > (ratPassed.num * den); } bool Rational::operator<(Rational ratPassed) { simplify(); ratPassed.simplify(); return (num * ratPassed.den) < (ratPassed.num * den); } bool Rational::operator>=(Rational ratPassed) { simplify(); ratPassed.simplify(); return (num * ratPassed.den) >= (ratPassed.num * den); } bool Rational::operator<=(Rational ratPassed) { simplify(); ratPassed.simplify(); return (num * ratPassed.den) <= (ratPassed.num * den); } Rational Rational::operator-() { Rational ratResult; ratResult.den = den * -1; return ratResult; }
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.