#include // for access to logic_error #include #include // define classes Expr, Operand, Operator, Plus and Mult here! // file separation is not required. Expr* top_and_pop(stack& s) { if ( s.empty() ) throw logic_error{"Too few operands"}; Expr* e{ s.top() }; s.pop(); return e; } int main() { stack exprs; string input; while ( cin >> input ) { switch( input.at(0) ) { case '+': { Expr* right{ top_and_pop( exprs ) }; Expr* left{ top_and_pop( exprs ) }; exprs.push( new Plus{left, right} ); } break; case '*': { Expr* right{ top_and_pop( exprs ) }; Expr* left{ top_and_pop( exprs ) }; exprs.push( new Mult{left, right} ); } break; default: { exprs.push( new Operand{ stod(input) } ); } break; } } Expr* e{ exprs.top() }; e->print( cout ); cout << endl << "Answer: " << e->evaluate() << endl; while ( ! exprs.empty() ) { delete top_and_pop( exprs ); } return 0; }