#include #include #include #include #include #include // Implement the classes here // Implement the make_literal(), make_print() and make_operator() functions here void evaluate_expression(std::vector const& expression) { std::stack stack { }; for (/* Action */ action : expression) { action.evaluate(stack); } } /* Associations that should be be stored in parse_table: "." -> make_print(std::cout) "+" -> make_operator([](int x, int y) { return x + y; }) "*" -> make_operator([](int x, int y) { return x * y; }) "min" -> make_operator([](int x, int y) { return std::min(x, y); }) "max" -> make_operator([](int x, int y) { return std::max(x, y); } // Note: It should be very easy to for example add something like: "^" -> make_operator([](int x, int y) { return std::pow(x, y); } */ /* Action */ parse_token(std::string const& token) { if (/* token exists in parse_table */) { return /* a clone of the Action object associated with token */; } else { int value { /* convert token to int */ }; return make_literal(value); } } std::vector parse_expression(std::istream& is) { std::vector expression { }; std::string token; while (is >> token) { expression.push_back(parse_token(token)); } return expression; } int main() { std::string line; std::getline(std::cin, line); std::istringstream iss { line }; auto expression = parse_expression(iss); evaluate_expression(expression); } /* Example runs: $ ./a.out 1 2 + . 3 $ ./a.out 1 . 2 3 + . 1 5 $ ./a.out 11 12 13 max min . 11 $ ./a.out 1 2 + 10 * . 30 */