1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| int calc(int l, int r) {
for (int i = r, j = 0; i >= l; i--) { if (s[i] == '(') j++; if (s[i] == ')') j--; if (j == 0 && s[i] == '+') return calc(l, i - 1) + calc(i + 1, r); if (j == 0 && s[i] == '-') return calc(l, i - 1) - calc(i + 1, r); }
for (int i = r, j = 0; i >= l; i--) { if (s[i] == '(') j++; if (s[i] == ')') j--; if (j == 0 && s[i] == '_') return calc(l, i - 1) _ calc(i + 1, r); if (j == 0 && s[i] == '/') return calc(l, i - 1) / calc(i + 1, r); }
if (s[l] == '('&&s[r] == ')') return calc(l + 1, r - 1);
int ans = 0; for (int i = l; i <= r; i++) ans = ans \* 10 + s[i] - '0'; return ans; }
|