-
Notifications
You must be signed in to change notification settings - Fork 3
/
736.cpp
76 lines (75 loc) · 2.23 KB
/
736.cpp
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
class Solution {
public:
deque<unordered_map<string, int>> scopes_;
string getToken(string& expression, int& pos) {
string token;
while (pos < expression.size() && expression[pos] != ')' && expression[pos] != ' ') {
token.push_back(expression[pos]);
pos++;
}
return token;
}
int getValue(string& token) {
for (auto& scope : scopes_) {
if (scope.find(token) != scope.end()) return scope[token];
}
return 0;
}
int eval(string& expression, int& pos) {
scopes_.push_front(unordered_map<string, int>());
int value = 0;
if (expression[pos] == '(') pos++;
string token = getToken(expression, pos);
if (token == "add") {
pos++;
int v1 = eval(expression, pos);
pos++;
int v2 = eval(expression, pos);
value = v1 + v2;
}
else if (token == "mult") {
pos++;
int v1 = eval(expression, pos);
pos++;
int v2 = eval(expression, pos);
value = v1 * v2;
}
else if (token == "let") {
string var;
while (expression[pos] != ')') {
pos++;
if (expression[pos] == '(') {
pos++;
value = eval(expression, pos);
break;
}
var = getToken(expression, pos);
if (expression[pos] == ')') {
if (isalpha(var[0])) {
value = getValue(var);
}
else {
value = stoi(var);
}
break;
}
pos++;
value = scopes_.front()[var] = eval(expression, pos);
}
}
else if (isalpha(token[0])) {
value = getValue(token);
}
else {
value = stoi(token);
}
if (expression[pos] == ')') pos++;
scopes_.pop_front();
return value;
}
int evaluate(string expression) {
scopes_.clear();
int position = 0;
return eval(expression, position);
}
};