forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathG.cpp
More file actions
145 lines (121 loc) · 3.9 KB
/
G.cpp
File metadata and controls
145 lines (121 loc) · 3.9 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--)
#define REP(i,a) for(int i=0,_a=(a); i<_a; i++)
#define EACH(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define DEBUG(x) { cout << #x << " = "; cout << (x) << endl; }
#define PR(a,n) { cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl; }
#define PR0(a,n) { cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl; }
#define sqr(x) ((x) * (x))
#define ll long long
using namespace std;
map<string,string> to;
string define(string a) {
if (!to.count(a)) return a;
return to[a] = define(to[a]);
}
bool isOp(char c) {
if (c == '+' || c == '-') return true;
if (c == '*' || c == '/' || c == '%') return true;
if (c == '^') return true;
return false;
}
int toInt(string s) {
int res = 0, sign = 1;
for(int i = 0; i < s.length(); ++i) {
if (i == 0 && s[i] == '-') sign = -1;
else if (s[i] >= '0' && s[i] <= '9') res = res * 10 + s[i] - '0';
else return 0;
}
return res * sign;
}
int power(int a, int b) {
if (b == 0) return 1;
if (b == 1) return a;
int mid = power(a * a, b >> 1);
if (b & 1) return mid * a;
else return mid;
}
int _div(int a, int b) {
int res = abs(a) / abs(b);
if (a < 0) res = -res;
if (b < 0) res = -res;
return res;
}
int _mod(int a, int b) {
int res = abs(a) % abs(b);
if (a < 0) res = -res;
if (b < 0) res = -res;
return res;
}
int f(const string& s, int l, int r) {
int level = 0;
// binary operator + and -
for(int i = r-1; i > l; --i) {
if (s[i] == '(') ++level;
else if (s[i] == ')') --level;
else if (s[i] == '+' || s[i] == '-') {
if (level == 0 && !isOp(s[i-1])) {
int left = f(s, l, i);
int right = f(s, i+1, r);
if (s[i] == '+') return left + right;
else return left - right;
}
}
}
// binary operator * / %
level = 0;
for(int i = r-1; i > l; --i) {
if (s[i] == '(') ++level;
else if (s[i] == ')') --level;
else if (s[i] == '*' || s[i] == '/' || s[i] == '%') {
if (level == 0) {
int left = f(s, l, i);
int right = f(s, i+1, r);
if (s[i] == '*') return left * right;
else if (s[i] == '/') {
return _div(left, right);
}
else return _mod(left, right);
}
}
}
// binary operator ^
level = 0;
for(int i = l; i < r-1; ++i) {
if (s[i] == '(') ++level;
else if (s[i] == ')') --level;
else if (s[i] == '^') {
if (level == 0) {
int left = f(s, l, i);
int right = f(s, i+1, r);
return power(left, right);
}
}
}
// bracket
if (s[l] == '(') return f(s, l+1, r-1);
// unary operator + and -
if (s[l] == '+') return f(s, l+1, r);
if (s[l] == '-') return -f(s, l+1, r);
return toInt(define(s.substr(l, r - l)));
}
int main() {
ios :: sync_with_stdio(false);
freopen("plcool.in", "r", stdin);
freopen("plcool.out", "w", stdout);
string op;
while (cin >> op) {
if (op == "print") {
string expr; getline(cin, expr);
expr.erase(remove(expr.begin(), expr.end(), ' '), expr.end());
transform(expr.begin(), expr.end(), expr.begin(), ::tolower);
printf("%d\n", f(expr, 0, expr.length()));
} else { // op == "define"
string a, b; cin >> a >> b;
if (to.count(a)) continue;
if (define(b) == a) continue;
to[a] = b;
}
}
}