Question: Please Help!! Write a C++ program: include methods for the other rational number arithmetic operations, according to the class declaration below Guidelines -Maintain fractions internally
Please Help!! Write a C++ program: include methods for the other rational number arithmetic operations, according to the class declaration below
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
This is the coding I have so far:-
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 numerator, int denominator); int getDenominator() const; float getDecimal() const; std::string toString(); };
FullFraction FullFraction::add(FullFraction rop) { FullFraction addFraction; addFraction.numerator = (numerator * rop.denominator) + (rop.numerator*denominator); addFraction.denominator = denominator * rop.denominator; return addFraction; } FullFraction FullFraction::subtract(FullFraction rop) { }
FullFraction FullFraction::multiply(FullFraction rop) { }
FullFraction FullFraction::divide(FullFraction rop) { }
int FullFraction::getNumerator() const { return numerator; }
void FullFraction::setFullFraction(int num, int den) { numerator = num; denominator = den; } int FullFraction::getDenominator() const { return denominator; } float FullFraction::getDecimal()const {
}
std::string FullFraction::toString() { std::stringstream ss; ss << numerator << "/" << denominator; std::string str = ss.str(); return str; }
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; }
int main() {
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 will look like :-
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.33333
Thank you so much for helping. Much appreciated!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
