//************************************************************************ // Program to do fraction arithmetic // // Written by : Rex Forsyth //************************************************************************ #include #include #include "fraction.h" #include "myio.h" using namespace std; // function prototypes void instruct(); void getQuestion(fraction& frac1, fraction& frac2, char& oper); void getOperator(char& oper); fraction doCalculation(const fraction& frac1,const fraction& frac2,char oper); void displayResult(const fraction& frac1,const fraction& frac2,char oper, const fraction& result); void getValid(fraction& frac); //************************************************************************ //************************************************************************ int main() { fraction first; // first fraction fraction second; // second fraction char oper; fraction result; // the result instruct(); do { getQuestion(first, second, oper); if (oper == '/' && second.num() == 0) cout << "Division by zero is not allowed!!!" << endl; else { result = doCalculation(first, second, oper); displayResult(first, second, oper, result); } clrStream(); // clear the new line if nothing else // to prepare for character input cout << "Do you want to continue (n to stop)? "; } while (tolower(getResponse('y')) != 'n'); cout << "Just testing the comparison and printMixed methods" << endl; first.printMixed(cout); if (first.sameAs(second)) cout << " = "; else cout << " != "; second.printMixed(cout); cout << endl; first.printMixed(cout); if (first.isLessThan(result)) cout << " < "; else cout << " >= "; result.printMixed(cout); cout << endl; return 0; } //************************************************************************ // function to explain how to use this program //************************************************************************ void instruct() { cout << "This program will ask for a fraction expression involving" << endl; cout << "two fractions and an operator (+,-,* or /). It will then" << endl; cout << "calculate and print the result. The fraction should be" << endl; cout << "entered in the form a/b where a and b are integers. If " << endl; cout << "denominator is 1, the fraction can be entered without" << endl; cout << "the / and the denominator. All fractions will be reduced" << endl; cout << "to lowest terms with the denominator positive. Examples " << endl; cout << "of valid input are : " << endl; cout << " 3/4" << endl; cout << " 3" << endl; cout << " 10/12" << endl; cout << " -3/4" << endl; cout << " 3/-4" << endl; cout << " 0" << endl; cout << endl << endl; } //************************************************************************ // function to get the question // // parameter usage // frac1 -- exports the first fraction // frac2 -- exports the second fraction // oper -- exports the operator to apply // // Post-Conditions : fractions will be valid and reduced to lowest terms // with the denominator positive. // the operator will be one of + - * / //************************************************************************ void getQuestion(fraction& frac1, fraction& frac2, char& oper) { cout << "Enter first fraction -- "; getValid(frac1); cout << "Enter the operator (one or + - * /) -- "; getOperator(oper); cout << "Enter second fraction -- "; getValid(frac2); } //*********************************************** // function to get a valid fraction from the user // //*********************************************** void getValid(fraction& frac) { while (true) { frac.read(cin); if (cin.good()) break; cout << " Not a valid fraction!! Try again -- "; cin.clear(); clrStream(); } } //************************************************************************ // function to get an operator // // parameter usage // oper -- exports the operator // // Post-Conditions : the operator will be one of + - * / //************************************************************************ void getOperator(char& oper) { cin >> oper; while (oper != '+' && oper != '-' && oper != '*' && oper != '/') { cout << "Invalid operator entered!! Must be one of +,-,*,/. Reenter -- "; cin >> oper; } } //************************************************************************ // function to apply the operator to the two fraction to get the result // // parameter usage // frac1 -- imports the first fraction // frac2 -- imports the second fraction // oper -- imports the operator to apply // // Pre-Conditions : the two fractions will be valid and reduced to lowest // terms with the denominator positive. // if the oper is '/' then num2 is not 0 // the operator will be one of + - * / // Post-Conditions : the returned result will be reduced to lowest terms // with the denominator positive. //************************************************************************ fraction doCalculation(const fraction& frac1,const fraction& frac2,char oper) { switch (oper) { case '+' : return frac1.add(frac2); break; case '-' : return frac1.sub(frac2); break; case '*' : return frac1.mult(frac2); break; case '/' : return frac1.div(frac2); break; } } //************************************************************************ // function to display the expression and the result // // parameter usage // frac1 -- imports the first fraction // frac2 -- imports the second fraction // oper -- imports the operator to apply // result -- imports the result fraction // // Pre-Conditions : all fractions will be valid and reduced to lowest // terms with the denominator positive. // the operator will be one of + - * / //************************************************************************ void displayResult(const fraction& frac1,const fraction& frac2,char oper, const fraction& result) { frac1.print(cout); cout << ' ' << oper << ' '; frac2.print(cout); cout << " = "; result.print(cout); cout << endl << endl; }