// ********* // proj4.cpp // ********* #include #include #include "infixPostfix.h" using namespace std; int main() { // input infix and postfix strings; string infixExp, postfixExp; // object used for evaluating postfix expressions postfixEval pexp; // object for evaluating infix conversions infixEval iexp; // object for conversion from infix to postfix infix2Postfix i2pexp; // postfix expression input cout << "Enter a postfix expression: "; getline(cin, postfixExp); while (postfixExp != ""){ try{ // assign the expression to exp pexp.setPostfixExp(postfixExp); // call evaluate() in a try block in case an error occurs cout << "The postfix expression " << pexp << " has value " << pexp.evaluate() << endl << endl; } catch (const expressionError& ee) { cout << ee.what() << endl << endl; } // input another expression cout << "Enter another postfix expression: "; getline(cin, postfixExp); } // input and evaluate infix expressions until the // user enters an empty string // get the first expression cout << "Enter an infix expression: "; getline(cin, infixExp); while (infixExp != "") { try { // an exception may occur. enclose the conversion // to postfix and the output of the expression // value in a try block iexp.setInfixExp(infixExp); cout << "The infix expression is " << iexp << endl; // convert to postfix i2pexp.setInfixExp(infixExp); postfixExp = i2pexp.postfix(); // output the postfix expression cout << "Its postfix form is " << postfixExp << endl; cout << "It has value " << iexp.evaluate() << endl << endl; } // catch an exception and output the error catch (const expressionError& ee) { cout << ee.what() << endl << endl; } // input another expression cout << "Enter an infix expression: "; getline(cin, infixExp); } return 0; }