/****************************************************** ** Name: ** Filename: TreeDrv.cpp ** Project #: Data Structures And Other Objects Using C++ 2nd Edition (Micheal Main & Walter Savich) ** Project Description: For this project, implement a class for expression trees, including operations for building expression trees. Also include a function to "evaluate" a non-empty expression tree. ** Output: - A simple tree diagram using the input string - The solution to the evaluated input expression string. ** Input: An expression to be evaluated i.e. 14*(7+3) ** Algorithm: 1. Instruct user to input an expression 2. Check data input scanf() 3. Evaluate input expression 4. Print simple tree diagram using the input string 5. Print out solution to the evaluated expression and tree. 6. Ask user to try another expression or end program ******************************************************/ // Include files #include // used for cin, cout #include #include "Tree.h" #include "Create.h" using namespace std; // Global Type Declarations // Function Prototypes bool toQuit(); void instruct (void); void pause (); //Global Variables - should not be used without good reason. int main () { // Declaration section bool done = false; // User Decides To Quit CTree *head; char expression[1000]; double res; // Executable section instruct (); while (!done) // While Not False Repeat { printf("\nNOTE: This program only evaluates expressions containing + and *"); printf("\nEnter Expression: "); // Printf used instead of cout // The string specifier %s scans the string and terminates when a space is found scanf("%s",expression); if(!HandleSum(expression,&head)) { printf("error parsing expression\n"); return -1; } printf("\nExpression tree:\n\n"); res=head->Evaluate(0); printf("\nFinal result: %.0f\n",res); done = toQuit(); // Calls toQuit() Function To Continue Or End Prog. } pause (); return 0; } bool toQuit () { bool cont = true; // While Will Repeat Unless cont = false bool toReturn = true; // Default Value Of True Unless Changed char response; // Users Decision To Continue Or End Prog. while (cont) { cout <<"\nWould you like to enter another expression? (Y/N) "; cin >> response; cout <<"\n"; if ((response != 'Y') && (response != 'y') && (response != 'N') && (response != 'n')) { // Error Because Did Not Use Y or N cout <<"Invalid entry. Try again" << endl; } if (response == 'Y' || response == 'y') { cont = false; // User Continues toReturn = false; } else if (response == 'N' || response == 'n') { cont = false; // Stop Function toReturn = true; } }//End While return toReturn; // Returns True Or False To End } // End Quit void instruct (void) { // Declaration section cout << "This program will take an input expression use it to create an " << "expression tree\nand then evaluate the expression tree. It will " << "also print out a simple diagram\nof an expression tree " << "using the input and provide the solution to the evaluated expression." << " Input the expression in the following format: 14*(7+3) NO SPACES." << "\n----------------------------------------------------" << "---------------------------" << endl; // Executable section } void pause () { // Declaration section // Executable section cout << "\nPress any key to continue..."; getch(); cout << "\r"; cout << " "; cout << "\r"; } /* Program Output This program will take an input expression use it to create an expression tree and then evaluate the expression tree. It will also print out a simple diagram of an expression tree using the input and provide the solution to the evaluated expression. Input the expression in the following format: 14*(7+3) NO SPACES. ------------------------------------------------------------------------------- NOTE: This program only evaluates expressions containing + and * Enter Expression: 14*(7+3) Expression tree: 14 *< 7 +< 3 Final result: 140 Would you like to enter another expression? (Y/N) */