Question: Using the previous assignment, expand it to include methods for the other rational number arithmetic operations, according to the class declaration below. class FullFraction {
Using the previous assignment, expand it to include methods for the other rational number arithmetic operations, according to the class declaration below.
class FullFraction { int numerator; int denominator; int gcd(int x, int y); public: FullFraction(); FullFraction(int n, int d); FullFraction add(FullFraction rop); FullFraction subtract(FullFraction rop); FullFraction multiply(FullFraction rop); FullFraction divide(FullFraction rop); int getNumerator() const; void setFullFraction(int num, int denom); int getDenominator() const; float getDecimal() const; std::string toString(); }; int FullFraction::gcd(int x, int y) { if (x <= 1 || y <= 1) return 1; while (x != y) { if (x > y) { if (0 == (x = x % y)) return y; } else { if (0 == (y = y % x)) return x; } } return x; }
Guidelines
Maintain fractions internally in lowest terms - the best place to do this is in setFraction()
Be sure to maintain the sign of the fraction correctly
Avoid a 0 in the denominator by forcing the fraction to 0
Enhance toString() to display a mixed fraction (whole numbers and fraction) when the numerator is greater than the denominator
Use the following test driver to test the class.
static int assign_FullFraction() { FullFraction f0; FullFraction f1(2, 2); FullFraction f2(2, 7); FullFraction f3(1, -3); cout << f1.toString() << " + " << f0.toString() << " = " << f1.add(f0).toString()<< endl; cout << f0.toString() << " + " << f1.toString() << " = " << f0.add(f1).toString()<< endl; cout << f1.toString() << " * " << f2.toString() << " = " << f1.multiply(f2).toString()<< endl; cout << f1.toString() << " / " << f3.toString() << " = " << f1.divide(f3).toString()<< endl; cout << f2.toString() << " + " << f3.toString() << " = " << f2.add(f3).toString()<< endl; cout << f2.toString() << " - " << f3.toString() << " = " << f2.subtract(f3).toString()<< endl; cout << f2.toString() << " * " << f3.toString() << " = " << f2.multiply(f3).toString()<< endl; cout << f3.toString() << " / " << f2.toString() << " = " << f3.divide(f2).toString()<< endl; cout << f2.toString() << " / " << f3.toString() << " = " << f2.divide(f3).toString()<< endl; cout << "the numerator of " << f3.toString() << " is " << f3.getNumerator()<< endl; cout << "the denominator of " << f3.toString() << " is " << f3.getDenominator()<< endl; cout << "the decimal value of " << f3.toString() << " is " << f3.getDecimal()<< endl; return 0; } The output of the test driver is:
1 + 0 = 1 0 + 1 = 1 1 * 2/7 = 2/7 1 / -1/3 = -3 2/7 + -1/3 = -1/21 2/7 - -1/3 = 13/21 2/7 * -1/3 = -2/21 -1/3 / 2/7 = -1 1/6 2/7 / -1/3 = -6/7 the numerator of -1/3 is -1 the denominator of -1/3 is 3 the decimal value of -1/3 is -0.333333
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
