// Author : Rex Forsyth // Date : Jan 16, 2001 // client program to test the fraction class #include #include #include #include "fraction.h" #include "myio.h" using namespace std; #define MAX 10 // function prototypes void printMenu(); int choice(); void doIt(int c, fraction t[]); void printAll(fraction t[]); void read(fraction t[]); void print(fraction t[]); void arithmetic(fraction t[]); void reciprocate(fraction t[]); void compare(fraction t[]); void getValid(fraction& t); void getOp(char& op); void getInRange(int& i); //**************************************** //**************************************** int main() { fraction fractionList[MAX]; int what; printMenu(); do { what = choice(); doIt(what,fractionList); if (what < 7 ) cout <<" What now ? "; } while (what != 8); return 0; } //**************************************** // function to print available options //**************************************** void printMenu() { cout << "You may choose one of the following options:" << endl; cout << endl; cout << " 1. (R)ead a new fraction. " << endl; cout << " 2. (P)rint a fraction. " << endl; cout << " 3. (T)est reciprocal method." << endl; cout << " 4. Test (A)ritmetic methods."<< endl; cout << " 5. Test (C)omparison methods." << endl; cout << " 6. (L)ist all fractions." << endl; cout << " 7. (H)elp -- print this list. " << endl; cout << " 8. (Q)uit -- exit the program. " << endl; cout << endl; cout << " Enter your choice -- "; } //**************************************** // function to obtain the users choice //**************************************** int choice() { char ans; ans = cin.get(); if (ans != '\n') clrStream(); switch (toupper(ans)) { case '1': case 'R': return 1; case '2': case 'P': return 2; case '3': case 'T': return 3; case '4': case 'A': return 4; case '5': case 'C': return 5; case '6': case 'L': return 6; case '7': case '?': case 'H': return 7; // Help or '?' case '8': case 'E': case 'D': case 'Q': return 8;// Quit, Done or Exit default : cout << "Invalid option entered!! "; return 7; // cause menu to be printed } } //**************************************** // function to do users choice //**************************************** void doIt(int c, fraction t[]) { switch (c) { case 1 : read(t); break; case 2 : print(t); break; case 3 : reciprocate(t); break; case 4 : arithmetic(t); break; case 5 : compare(t); break; case 6 : printAll(t); break; case 7 : printMenu(); break; case 8 : break; // do nothing } } //**************************************** // function to obtain a valid fraction //**************************************** void getValid(fraction& t) { while (true) { t.read(cin); if (cin.good()) break; cout << "Invalid fraction!! " << endl << "Fractions must be one of the following forms :" << endl << " 1. integer" << endl << " 2. integer/integer" << endl << "Note: no spaces are allowed in the fraction" << endl << "Enter the fraction -- "; cin.clear(); clrStream(); } } //**************************************** // function print all fractions //**************************************** void printAll(fraction t[]) { cout << "This is a list of the fractions available." << endl; cout << "NUMBER fraction" << endl; for (int i=0; i< MAX; i++) { cout << setw(4) << i+1 << " : "; t[i].print(cout); cout << endl; } } //**************************************** // function to obtain a valid fraction //**************************************** void read(fraction t[]) { int loc; cout << "Which location do you wish to put it in (1--" << MAX << ")? "; getInRange(loc); clrStream(); cout << "Enter the fraction -- "; getValid(t[loc-1]); clrStream(); } //**************************************** // function to print a specific fraction //**************************************** void print(fraction t[]) { int loc; cout << "Which fraction do you wish to print (1--" << MAX << ")? "; getInRange(loc); clrStream(); cout << "fraction " << loc << " is : "; if (t[loc-1].num() > t[loc-1].den()) t[loc-1].printMixed(cout); else t[loc-1].print(cout); cout << endl; } //**************************************** // function to test the negation function //**************************************** void reciprocate(fraction t[]) { int loc; cout << "Enter number of fraction to use (1--" << MAX << ")? "; getInRange(loc); clrStream(); if (t[loc-1].num() == 0) cout << "Can not find the reciprocal of O!!!!"; else { cout << "Printing the reciprocal of the fraction -- "; t[loc-1].reciprocal().print(cout); } cout << endl; } //**************************************** // function to test the binary operators //**************************************** void arithmetic(fraction t[]) { int loc1, loc2, loc3; char op; cout << "Enter number of first fraction to use (1--" << MAX << ")? "; getInRange(loc1); clrStream(); cout << "Enter number of second fraction to use (1--" << MAX << ")? "; getInRange(loc2); clrStream(); cout << "Enter the operator to use (+,-,*,/) -- "; getOp(op); clrStream(); if (op == '/' && t[loc2-1].num() == 0) cout<< "Unable to do '" << op << "'!!! Divisor is 0!!!"<< endl; else { cout << "Enter location to store the result (1--" << MAX << ")? "; getInRange(loc3); clrStream(); switch (op) { case '+' : t[loc3-1] = t[loc1-1].add(t[loc2-1]); break; case '-' : t[loc3-1] = t[loc1-1].sub(t[loc2-1]); break; case '*' : t[loc3-1] = t[loc1-1].mult(t[loc2-1]); break; case '/' : t[loc3-1] = t[loc1-1].div(t[loc2-1]); break; } } } //******************************************************* // function to obtain an operator +, -, or * //******************************************************* void getOp(char & op) { while (true) { cin >> op; if (op == '+' || op == '-' || op == '*' || op == '/') break; cout << "Only +, -,* or / accepted!!! Try again -- "; clrStream(); } } //********************************************* // function to get an int in the range 1 to MAX //********************************************* void getInRange(int& i) { while (true) { getValid(i); // obtain a valid int if (i >= 1 && i <= MAX) break; // if in range, we are happy; outta here cout << "The integer must be between 1 and " << MAX << ". Try again -- "; clrStream(); } } //****************************************** // function to test the comparison functions //****************************************** void compare(fraction t[]) { int loc1, loc2; string op; bool result; cout << "Enter number of first fraction to use (1--" << MAX << ")? "; getInRange(loc1); clrStream(); cout << "Enter number of second fraction to use (1--" << MAX << ")? "; getInRange(loc2); clrStream(); cout << "Enter the comparison to use (==, !=, <, <=, >, >=) -- "; cin >> op; clrStream(); if (op == "==") result = t[loc1-1].sameAs(t[loc2-1]); else if (op == "!=") result = !t[loc1-1].sameAs(t[loc2-1]); else if (op == "<") result = t[loc1-1].isLessThan(t[loc2-1]); else if (op == "<=") result = t[loc1-1].sameAs(t[loc2-1]) || t[loc1-1].isLessThan(t[loc2-1]); else if (op == ">") result = !t[loc1-1].sameAs(t[loc2-1]) && !t[loc1-1].isLessThan(t[loc2-1]); else if (op == ">=") result = !t[loc1-1].isLessThan(t[loc2-1]); else { cout << op << " is not a valid comparison operator" << endl; return; } t[loc1-1].print(cout); cout << " " << op << " "; t[loc2-1].print(cout); cout << " is "; if (result) cout << "true."; else cout << "false."; cout << endl; }