-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP227.cpp
131 lines (128 loc) · 3.84 KB
/
P227.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
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
#include "header.h"
class Solution {
bool A_first(char a, char b) {
int v1,v2;
switch (a) {
case '(':
{v1 = 3;break;}
case '*':
{v1 = 4;break;}
case '/':
{v1 = 5;break;}
case '+':
{v1 = 6;break;}
case '-':
{v1 = 7;break;}
case ')':
{v1 = 8;break;}
}
switch (b) {
case '(':
{v2 = 3;break;}
case '*':
{v2 = 4;break;}
case '/':
{v2 = 5;break;}
case '+':
{v2 = 6;break;}
case '-':
{v2 = 7;break;}
case ')':
{v2 = 8;break;}
}
return v1/2<=v2/2;
}
public:
int calculate(string s) {
vector<int> digit, dig_head, opt_head;
vector<char> opt;
// opt.push_back(100);
dig_head.push_back(0);
opt_head.push_back(0);
for (int i=0; i<s.length(); ) {
if (s[i] == ' ') {
i++;
continue;
}
if (s[i]>='0' && s[i]<='9') {
int t=i, val=0;
while (t<s.length() && s[t]>='0' && s[t]<='9') {
val= val*10 + (s[t]-'0');
t++;
}
i = t;
digit.push_back(val);
}
else {
while (opt.size()-opt_head.back()>0 && (A_first(opt.back(), s[i]))) {
int n1=0,n2;
if (digit.size()-dig_head.back()>=2) {
n2 = digit.back(); digit.pop_back();
n1 = digit.back(); digit.pop_back();
}
else {
n2 = digit.back(); digit.pop_back();
}
switch (opt.back()) {
case '+':
n1 = n1+n2; break;
case '-':
n1 = n1-n2; break;
case '*':
n1 = n1*n2; break;
case '/':
n1 = n1/n2; break;
}
digit.push_back(n1);
opt.pop_back();
}
if (s[i]=='(') {
dig_head.push_back(digit.size());
opt_head.push_back(opt.size());
}
else if (s[i]==')') {
dig_head.pop_back();
opt_head.pop_back();
}
else opt.push_back(s[i]);
i++;
}
}
// cout << "--- " << opt.size() <<" --- "<< digit.size() << " --- " << endl;
// print(opt);
// print(digit);
while (opt.size()-opt_head.back()>0) {
int n1=0,n2;
if (digit.size()-dig_head.back()>=2) {
n2 = digit.back(); digit.pop_back();
n1 = digit.back(); digit.pop_back();
}
else {
n2 = digit.back(); digit.pop_back();
}
switch (opt.back()) {
case '+':
n1 = n1+n2; break;
case '-':
n1 = n1-n2; break;
case '*':
n1 = n1*n2; break;
case '/':
n1 = n1/n2; break;
}
digit.push_back(n1);
opt.pop_back();
}
return digit.back();
}
};
int main() {
string s;
s = "3+2*2";
cout << Solution().calculate(s) << endl;
s = " 3/2 ";
cout << Solution().calculate(s) << endl;
s = " 3+5 / 2 ";
cout << Solution().calculate(s) << endl;
return 0;
}