1: // Filename: COMPFRAC.CPP 2: // Purpose: A program that uses a Fraction class to permit the user 3: // to compute the sum, difference, product and/or quotient 4: // of two fractional quantities, and also to compare two 5: // fractional quantities. 7: #include <iostream> 8: #include <iomanip> 9: using namespace std; 11: #include "FRACTION.H" 12: #include "MENU.H" 13: #include "TXITEMS.H" 14: #include "PAUSE.H" 16: void ComputeWithFractions(); 17: void CompareFractions(); 19: int main() 20: { 21: Menu m("Main Menu"); 22: m.AddOption("Quit"); 23: m.AddOption("Get information"); 24: m.AddOption("Compute the value of a fractional expression"); 25: m.AddOption("Compare the values of two fractions"); 27: TextItems compfracText("compfrac.dat"); 28: int menuChoice; 30: do 31: { 32: m.Display(); 33: menuChoice = m.Choice(); 35: switch (menuChoice) 36: { 37: case -1: 38: cout << "\nProgram now terminating.\n\n"; 39: break; 40: case 1: 41: cout << endl; 42: break; 43: case 2: 44: compfracText.DisplayItem("Program Description"); 45: break; 46: case 3: 47: ComputeWithFractions(); 48: break; 49: case 4: 50: CompareFractions(); 51: break; 52: } 54: } while (menuChoice != 1 && menuChoice != -1); 55: cout << endl; 57: return 0; 58: } 61: void ComputeWithFractions() 62: // Pre: none 63: // Post: The user has entered one or more fractional expressions and 64: // the value of each expression has been computed and displayed. 65: // The user has entered any character other than 'Y' or 'y'. 66: { 67: bool finished; 68: Fraction f1, f2, result; 69: char op, response; 71: do 72: { 73: cout << "\nEnter fractional expression to be evaluated here: "; 74: cin >> f1; cin >> op; cin >> f2; 75: cin.ignore(80, '\n'); cout << endl; 76: cout << "Answer: "; 77: cout << f1; cout << " " << op << " "; cout << f2; 78: cout << " = "; 79: switch (op) 80: { 81: case '+': result = f1 + f2; break; 82: case '-': result = f1 - f2; break; 83: case '*': result = f1 * f2; break; 84: case '/': result = f1 / f2; break; 85: } 86: cout << result; cout << endl; 88: cout << "Would you like to do another calculation? (Y/N): "; 89: cin >> response; cin.ignore(80, '\n'); cout << endl; 90: finished = (response != 'Y' && response != 'y'); 92: } while (!finished); 93: } 96: void CompareFractions() 97: // Pre: none 98: // Post: The user has entered an even number of fractions, the values 99: // of each pair have been compared and a message has been 100: // displayed indicating whether the two values are equal, and, 101: // if not, which is greater and by how much. 102: { 103: bool finished; 104: Fraction f1, f2; 105: char response; 107: do 108: { 109: cout << "\nEnter two fractions to be compared here: "; 110: cin >> f1; cin >> f2; 111: cin.ignore(80, '\n'); cout << endl; 113: if (f1 == f2) 114: cout << "\nThe two fractions are equal.\n"; 115: else if (f1 < f2) 116: cout << f2 << " is greater than " << f1 117: << " and the difference is " << f2 - f1 << ".\n"; 118: else // f1 must be > f2 if you make it to this point 119: cout << f1 << " is greater than " << f2 120: << " and the difference is " << f1 - f2 << ".\n"; 122: cout << "Would you like to do another comparison? (Y/N): "; 123: cin >> response; cin.ignore(80, '\n'); cout << endl; 124: finished = (response != 'Y' && response != 'y'); 126: } while (!finished); 127: }