//--------------------------------------------------------- // Homework07.cpp // // Converting an infix expression to a postfix expression. // // D. Searls // REPLACE THIS LINE WITH YOUR NAME!! // Asbury University //--------------------------------------------------------- #include #include #include #include #include "list.cpp" using namespace std; template class stack { YOUR STACK CLASS IMPLEMENTATION GOES HERE }; struct tokenType { bool isOperand; double operand; string op; }; //----------------------------------------------- // precedence // // Returns the precedence of the specified // operator based on this table: // // Operator Precedence // -------- ---------- // * 2 // / 2 // + 1 // - 1 // ( 0 // "" 0 Null Operator // // Precondition: the operator is "+", "-", "*", // "/", "(", or "". // // In Parameter: op //----------------------------------------------- int precedence(string op) { int result; if (op == "*" || op == "/") { result = 2; } else if (op == "+" || op == "-") { result = 1; } else { result = 0; } return result; } //----------------------------------------------- // Function: convertToPostfix // // Convert the infix expression (read from the // specified string stream) into a postfix // expression. // // Precondition: the infix expression consists of // space-delimited tokens (numbers and arithmetic // operators) and has no errors. // // In Parameter: infix // Out Parameter: postfix //----------------------------------------------- void convertToPostfix(stringstream& infix, list& postfix) { } //--------------------------------------------------------- // valueOf // // Return the value of the postfix expression. // // In Parameter: postfix //--------------------------------------------------------- double valueOf(list& postfix) { } //********************************************************* // M A I N D R I V E R //********************************************************* int main() { list postfix; tokenType* ptr; string infix; stringstream infixStream; cout << "Given an infix expression, this application will\n"; cout << "display the corresponding postfix expression and\n"; cout << "its value.\n\n"; cout << "To terminate, just tap the Enter key when asked\n"; cout << "for an infix expression.\n\n"; cout << " Infix: "; getline(cin, infix); while (infix.length() > 0) { infixStream.str(infix); postfix.clear(); convertToPostfix(infixStream, postfix); infixStream.clear(); cout << "Postfix: "; for (ptr = postfix.first(); ptr != postfix.end(); ptr = postfix.next()) { if (ptr->isOperand) { cout << ptr->operand; } else { cout << ptr->op; } cout << ' '; } cout << " = " << valueOf(postfix) << endl << endl; cout << " Infix: "; getline(cin, infix); } return 0; }