C++ A rational number is of the form a/b where a and b are integers with b not e
ID: 3747625 • Letter: C
Question
C++
A rational number is of the form a/b where a and b are integers with b not equal to zero. Develop a class for processing rational numbers. The class should have a numerator data member and a denominator data member. It should read and display all rational numbers in the format a/b; for output, just display a if the denominator is 1. The following examples illustrate the operations that should be provided:
A rational number x can be entered in the form
a or a/b. (e.g. 4, 3/4, 5/2)
x + y addition
x - y subtraction
x * y multiplication
x / y division
Explanation / Answer
#include using namespace std; class Rational { // not fully commented public: Rational(int, int); // parameterized constructor Rational(); // default constructor Rational(const Rational &x); // copy constructor Rational operator+(const Rational); Rational operator-(const Rational); Rational operator*(const Rational); Rational operator/(const Rational); Rational operator=(const Rational); void printRational(); private: int numerator; int denominator; void reduce(); // utility function, reduce to lowest terms }; // parameterized constructor: parameters are numerator and denominator respectively // if the number is negative, the negative is always stored in the numerator Rational::Rational(int n, int d) { numerator = (d < 0 ? -n : n); denominator = (d < 0 ? -d : d); reduce(); } // default constructor: Rational::Rational() { numerator = 0; denominator = 1; } // copy constructor Rational::Rational(const Rational &x) { numerator = x.numerator; denominator = x.denominator; reduce(); } //--------------------------------- add //-------------------------------------- // overloaded +: addition of 2 Rationals, current object and parameter Rational Rational::operator+(const Rational a) { Rational t; t.numerator = a.numerator * denominator + a.denominator * numerator; t.denominator = a.denominator * denominator; t.reduce(); return t; } //------------------------------ subtract //------------------------------------ Rational Rational::operator-(const Rational a) { Rational t; t.numerator = a.numerator * denominator - a.denominator * numerator; t.denominator = a.denominator * denominator; t.reduce(); return t; } //------------------------------ multiply //------------------------------------ Rational Rational::operator*(const Rational a) { Rational t; t.numerator = a.numerator * numerator; t.denominator = a.denominator * denominator; t.reduce(); return t; } //-------------------------------- divide //------------------------------------ Rational Rational::operator/(const Rational a) { Rational t; t.numerator = a.denominator * numerator; t.denominator = a.numerator * denominator; t.reduce(); return t; } //---------------------------- printRational //--------------------------------- void Rational::printRational() { if (denominator == 1) coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.