// Massingill, Berna L. and Oldham, Jeffrey D. // 2000 Mar 29 // CS1321 // Boolean-expression evaluator. // Input: // // A boolean expression, read from standard input (terminated by // EOF), in postfix notation. Terms are separated by whitespace, // and each term is either an operator (&&, ||, !, =>, or ==) or // a constant (true or false). // // Output: // // A message to standard output giving the value of the expression // if it is well-formed, or an error message if it is not. #include #include // has assert() #include // has EXIT_SUCCESS, EXIT_FAILURE #include #include typedef vector expr; // ADD CODE HERE? // ---- Function prototypes. ---- void readInput(istream & in, expr & inputExpression); bool evaluate(const expr & expression, string & expressionValue); // ADD CODE HERE? // ---- Main program. ---- int main(void) { cout << "Enter the expression to evaluate " "(multiple lines okay, control-D to end):\n"; expr inputExpression; readInput(cin, inputExpression); string result; if (evaluate(inputExpression, result)) cout << "Result = " << result << endl; else cout << "Expression is not well-formed\n"; return EXIT_SUCCESS; } // ---- Function definitions. ---- // Pre: "in" contains a sequence of strings. // Post: strings read from inputExpression have been appended to // inputExpression. void readInput(istream & in, expr & inputExpression) { string word; while (in >> word) inputExpression.push_back(word); } // Pre: inputExpression contains a boolean expression (as described in // header comments). // Post: if input expression is well-formed, expressionValue // contains its value and return value is "true"; // otherwise return value is "false". // ADD CODE HERE.