// // NAME // asgn2.cpp // // DESCRIPTION // This file contains the functions in the Main module. // This module contains the top-level functions of the program. // // // Include files. // #include <iostream.h> #include "bool.h" #include "expr.h" // // Function prototypes. // static void displayMenu(); static int chooseExpr(); static void doEnter(expr * exprs[]); static void doDisplay(expr * exprs[]); static void doEvaluate(expr * exprs[]); static void doClear(expr * exprs[]); static void doJoin(expr * exprs[]); // // NAME // main // // DESCRIPTION // This function is the top-level function of the program. // // PARAMETERS // (none) // // RETURNS // 0 // int main() { char choice; bool done = false; expr * exprs[2]; exprs[0] = new expr(0.0); exprs[1] = new expr(0.0); while (!done) { displayMenu(); cin >> choice; switch (choice) { case '1': doEnter(exprs); break; case '2': doDisplay(exprs); break; case '3': doEvaluate(exprs); break; case '4': doClear(exprs); break; case '5': doJoin(exprs); break; case '6': done = true; break; } } delete exprs[0]; delete exprs[1]; return 0; } // // NAME // displayMenu // // DESCRIPTION // This function displays the menu to the standard output. // // PARAMETERS // (none) // // RETURNS // (none) // static void displayMenu() { cout << endl; cout << "1. Enter expression" << endl; cout << "2. Display expression" << endl; cout << "3. Evaluate expression" << endl; cout << "4. Clear expression" << endl; cout << "5. Join expressions" << endl; cout << "6. Quit" << endl; cout << endl; cout << "> "; return; } // // NAME // chooseExpr // // DESCRIPTION // This function prompts the user to choose an expression, then // returns value entered. // // PARAMETERS // (none) // // RETURNS // value entered // static int chooseExpr() { int choice; cout << endl; cout << "Which expression (1 or 2): "; cin >> choice; return choice-1; } // // NAME // doEnter // // DESCRIPTION // This function reads in an expression from the user. // // PARAMETERS // exprs (in/out) - array of expressions. // // RETURNS // (none) // static void doEnter(expr * exprs[]) { char str[100]; int index; cout << endl << "Enter an expression: "; cin.getline(str, 100); cin.getline(str, 100); index = chooseExpr(); delete exprs[index]; exprs[index] = new expr(str); return; } // // NAME // doDisplay // // DESCRIPTION // This function displays an expression // // PARAMETERS // exprs (in/out) - array of expressions. // // RETURNS // (none) // static void doDisplay(expr * exprs[]) { int index; index = chooseExpr(); cout << endl; exprs[index]->display(); cout << endl; return; } // // NAME // doEvaluate // // DESCRIPTION // This function evaluates an expression // // PARAMETERS // exprs (in/out) - array of expressions. // // RETURNS // (none) // static void doEvaluate(expr * exprs[]) { int index; index = chooseExpr(); cout << endl << exprs[index]->evaluate() << endl; return; } // // NAME // doClear // // DESCRIPTION // This function resets an expression to zero. // // PARAMETERS // exprs (in/out) - array of expressions. // // RETURNS // (none) // static void doClear(expr * exprs[]) { int index; index = chooseExpr(); delete exprs[index]; exprs[index] = new expr(0.0); return; } // // NAME // doJoin // // DESCRIPTION // This function joins two expressions // // PARAMETERS // exprs (in/out) - array of expressions. // // RETURNS // (none) // static void doJoin(expr * exprs[]) { char choice; expr::operation oper; expr temp(*(exprs[0])); cout << endl << "Which operation?" << endl; cout << " 1. Addition" << endl; cout << " 2. Subtraction" << endl; cout << " 3. Multiplication" << endl; cout << " 4. Division" << endl; cout << endl; cout << "> "; cin >> choice; switch (choice) { case '1': oper = expr::Addition; break; case '2': oper = expr::Subtraction; break; case '3': oper = expr::Multiplication; break; case '4': oper = expr::Division; break; default: oper = expr::Addition; break; } exprs[0] = new expr(oper, temp, *(exprs[1])); return; } // // End of file. //