This is from \"Problem Solving with C++\" by Walter Savitch, chapter 10, problem
ID: 3625693 • Letter: T
Question
This is from "Problem Solving with C++" by Walter Savitch, chapter 10, problem 10."Write a rational number class. This problem will be revisited in Chapter 11, where operator overloading will make the problem much easier. For now we will use the 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 from 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 number 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 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 a main function that thoroughly tests yours 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, Here is the code #include using namespace std; class Rational { public: Rational(int numerator, int denominator); Rational(int numerator); // sets denominator to 1 Rational(); // sets numerator to 0, denominator to 1 friend Rational operator+(const Rational&,const Rational&); friend Rational operator-(const Rational&,const Rational&); friend Rational operator*(const Rational&,const Rational&); friend Rational operator/(const Rational&,const Rational&); friend bool operator=(const Rational&,const Rational&); friend bool operator ==(const Rational&,const Rational&); friend ostream& operator (istream&,Rational&); private: int n; int d; }; void normalize(int &n, int &d); Rational::Rational(int numer, int denom) { normalize(numer, denom); n = numer; d = denom; } //sets denominator to 1 Rational::Rational(int numer): n(numer), d(1) // See the initializer appendix { //body deliberately empty } // sets numerator to 0, denominator to 1 Rational::Rational():n(0), d(1) // see initializer appendix { //body deliberately empty } Rational operator +(const Rational& left, const Rational& right) { int numer = left.n * right.d + left.d * right.n; int denom = left.d * right.d; normalize(numer, denom); Rational local(numer, denom); return local; } Rational operator -(const Rational& left, const Rational& right) { int numer = left.n * right.d - left.d * right.n; int denom = left.d * right.d; normalize(numer, denom); Rational local (numer, denom); return local; } Rational operator *(const Rational& left, const Rational& right) { Rational product; int numer = left.n * right.n; int denom = left.d * right.d; normalize(numer, denom); product = Rational(numer, denom); return product; } Rational operator/(const Rational& left, const Rational& right) { Rational quotient; int numer = left.n * right.d; int denom = left.d * right.n; normalize(numer, denom); quotient = Rational(numer, denom); return quotient; } //precondition: all relational operators require d > 0 bool operator = right.n * left.d; } bool operator==(const Rational& left,const Rational& right) { return left.n * right.d == right.n * left.d; } istream& operator >>(istream& in_str, Rational& right) { char ch; in_str >> right.n >> ch >> right.d; if (ch != '/')// properly done, we would set iostream {// to fail here in case of error. coutRelated 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.