#include // Provides isdigit #include // Provides cout, cin, peek, ignore #include // Provides strchr #include "stack2.h" // Provides the Stack template class void evaluate_stack_tops(Stack& numbers, Stack& operations) { double operand1, operand2; operand2 = numbers.pop( ); operand1 = numbers.pop( ); switch (operations.pop( )) { case '+': numbers.push(operand1 + operand2); break; case '-': numbers.push(operand1 - operand2); break; case '*': numbers.push(operand1 * operand2); break; case '/': numbers.push(operand1 / operand2); break; } } double read_and_evaluate(istream& ins) { const char DECIMAL = '.'; const char RIGHT_PARENTHESIS = ')'; Stack numbers; Stack operations; double number; char symbol; while (!ins.eof( ) && ins.peek( ) != '\n') { if (isdigit(ins.peek( )) || (ins.peek( ) == DECIMAL)) { ins >> number; numbers.push(number); } else if (strchr("+-*/", ins.peek( )) != NULL) { ins >> symbol; operations.push(symbol); } else if (ins.peek( ) == RIGHT_PARENTHESIS) { cin.ignore( ); evaluate_stack_tops(numbers, operations); } else cin.ignore( ); } return numbers.pop( ); } void main( ) { double answer; cout << "Type a fully parenthesized arithmetic expression:" << endl; answer = read_and_evaluate(cin); cout << "That evaluates to " << answer << endl; }