Question: Please finish the a c++ program for a fraction calculator that doesnt use decimal approximations to do the computation. The program should represent the fractions

Please finish the a c++ program for a fraction calculator that doesnt use decimal approximations to do the computation. The program should represent the fractions as structures containing two integers, the numerator and the denominator, and should express all results as reduced fractions. Each fraction should be printed as an integer part and a proper fraction if it is greater than or equal to one, instead of as an improper fraction. The program should read in two fractions and an operation, perform that operation, and print the result. It should then loop until the user enters zero over zero as the first fraction, and then exit. You should have separate functions to add two fractions, subtract two fractions, multiply two fractions, and divide two fractions, as well as to print a fraction. Please do error checking to make sure that none of the calculations would require division by zero, and print out an appropriate error when that occurs.

#include  #include  using namespace std; struct frac { int num; // numerator int den; // denominator }; frac add(frac x, frac y); frac sub(frac x, frac y); frac mul(frac x, frac y); frac div(frac x, frac y); int gcd(int x, int y); void read(frac &x); void print(frac x); int main() { frac a, b, c; char op; bool legal; cout << "Enter a two fractions and an operator: "; read(a); cin >> op; read(b); while ((a.num != 0) || (a.den != 0)) { legal = true; if (op == '+') c = add(a, b); else if (op == '-') c = sub(a, b); else if (op == '*') c = mul(a, b); else if (op == '/') c = div(a, b); else { cout << op << " is not a legal operation "; legal = false; } if (legal) { print(a); cout << ' ' << op << ' '; print(b); cout << " = "; print(c); cout << endl; } cout << "Enter another calculation (0/0 to exit): "; read(a); cin >> op; read(b); } return 0; } frac add(frac x, frac y) { frac result; int divisor; result.num = x.num * y.den + y.num*x.den; result.den = x.den*y.den; divisor = gcd(result.num, result.den); result.num /= divisor; result.den /= divisor; return result; } frac sub(frac x, frac y) { frac result; int divisor; result = x; divisor = gcd(result.num, result.den); result.num /= divisor; result.den /= divisor; return result; } frac mul(frac x, frac y) { frac result; int divisor; result = x; divisor = gcd(result.num, result.den); result.num /= divisor; result.den /= divisor; return result; } frac div(frac x, frac y) { frac result; int divisor; result = x; divisor = gcd(result.num, result.den); result.num /= divisor; result.den /= divisor; return result; } int gcd(int x, int y) { int rem; rem = x % y; while (rem != 0) { x = y; y = rem; rem = x % y; } return y; } void read(frac &x) { char tmp; cin >> x.num; cin >> tmp; cin >> x.den; return; } void print(frac x) { cout << x.num << "/" << x.den; return; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!