-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
evaluate_rpn.cc
45 lines (40 loc) · 1.37 KB
/
evaluate_rpn.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <functional>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include "test_framework/generic_test.h"
using std::function;
using std::stack;
using std::string;
using std::stringstream;
using std::unordered_map;
int Evaluate(const string& expression) {
stack<int> intermediate_results;
stringstream ss(expression);
string token;
const char kDelimiter = ',';
const unordered_map<string, function<int(int, int)>> kOperators = {
{"+", [](int x, int y) { return x + y; }},
{"-", [](int x, int y) { return x - y; }},
{"*", [](int x, int y) { return x * y; }},
{"/", [](int x, int y) { return x / y; }}};
while (getline(ss, token, kDelimiter)) {
if (kOperators.count(token)) {
const int y = intermediate_results.top();
intermediate_results.pop();
const int x = intermediate_results.top();
intermediate_results.pop();
intermediate_results.emplace(kOperators.at(token)(x, y));
} else { // token is a number.
intermediate_results.emplace(stoi(token));
}
}
return intermediate_results.top();
}
int main(int argc, char* argv[]) {
std::vector<std::string> args{argv + 1, argv + argc};
std::vector<std::string> param_names{"expression"};
return GenericTestMain(args, "evaluate_rpn.cc", "evaluate_rpn.tsv", &Evaluate,
DefaultComparator{}, param_names);
}