-
Notifications
You must be signed in to change notification settings - Fork 0
/
200425-1.cpp
61 lines (59 loc) · 1.28 KB
/
200425-1.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
// https://leetcode-cn.com/problems/basic-calculator/
#include <iostream>
#include <string>
using namespace std;
class Solution {
public:
int calculate(string s) {
int pos = 0;
return eval_exp(s.c_str(), pos);
}
private:
int eval_exp(const char* s, int& pos) {
int x = eval_item(s, pos);
for (;;) {
skip_spaces(s, pos);
if (s[pos] == '+') {
int x2 = eval_item(s, ++pos);
x += x2;
} else if (s[pos] == '-') {
int x2 = eval_item(s, ++pos);
x -= x2;
} else {
break;
}
}
return x;
}
int eval_item(const char* s, int& pos) {
skip_spaces(s, pos);
if (s[pos] == '(') {
int x = eval_exp(s, ++pos);
skip_spaces(s, pos);
if (s[pos] == ')') ++pos;
return x;
} else if (s[pos] == '+' || s[pos] == '-') {
bool minus = (s[pos] == '-');
int x = eval_item(s, ++pos);
if (minus) x = -x;
return x;
} else {
int x = s[pos] - '0';
for (++pos; s[pos] >= '0' && s[pos] <= '9'; ++pos) {
x = x * 10 + (s[pos] - '0');
}
return x;
}
}
void skip_spaces(const char* s, int& pos) {
while (s[pos] == ' ') ++pos;
}
};
int main()
{
Solution s;
cout << s.calculate("1 + 1") << endl; // answer: 2
cout << s.calculate(" 2-1 + 2 ") << endl; // answer: 3
cout << s.calculate("(1+(4+5+2)-3)+(6+8)") << endl; // answer: 23
return 0;
}