-
Notifications
You must be signed in to change notification settings - Fork 22
/
0054.cpp
69 lines (64 loc) · 1.51 KB
/
0054.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
// 0054.表达式求值
#include <iostream>
#include <stack>
using namespace std;
string mp = "+-*/)";
bool cmp(char c1, char c2)
{
if (c1 =='(') {
return false;
} else if((c1=='+' || c1=='-') && (c2=='*' || c2=='/')){
return false;
}
return true;
}
void doCal(stack<double> &st, stack<char> &so)
{
double b = st.top();
st.pop();
double a = st.top();
st.pop();
int op = so.top();
so.pop();
if(op == '+') a = a+b;
else if(op == '-') a = a-b;
else if(op == '*') a = a*b;
else if(op == '/') a = a/b;
st.push(a);
return ;
}
int main()
{
string s;
while(getline(cin, s))
{
stack<double> st;
stack<char> so;
so.push('(');
s += ')';
bool nextIsOp = false;
for(int i = 0; i < s.size(); i++)
{
if(s[i]=='(') {
so.push('(');
} else if(s[i]==')') {
while(so.top() != '(') doCal(st, so);
so.pop();
} else if (nextIsOp) {
while(cmp(so.top(), s[i])) doCal(st, so);
so.push(s[i]);
nextIsOp = false;
} else {
int j = i;
if(s[j] == '-' || s[j] == '+') i++;
while(mp.find(s[i]) == mp.npos) i++;
string t = s.substr(j, i-j);
st.push((double)stoi(t));
i--;
nextIsOp = true;
}
}
cout << st.top() << endl;
}
return 0;
}