-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy path9.52.cpp
62 lines (58 loc) · 887 Bytes
/
9.52.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
#include<iostream>
#include<string>
#include<vector>
#include<stack>
int countOne(std::stack<int> &st)
{
int t1,t2,t3;
t1 = st.top();
st.pop();
st.pop();
t2= st.top();
st.pop();
t3 = t1+t2;
//std::cout << t3 << std::endl;
return t3;
}
int main()
{
int co = ((5+3)+6+4)+2+(2+4+4)+4;
std::string s("((5+3)+6+4)+2+(2+4+4)+4");
std::stack<int> st;
int t1;
for(char c : s)
{
switch(c)
{
case ')':
t1 = countOne(st);
while(!st.empty() && st.top() != -2)
{
st.push(t1);
t1 = countOne(st);
}
if(st.top() == -2)
st.pop();
st.push(t1);
break;
case '+':
st.push(-1);
break;
case '(':
st.push(-2);
break;
default:
st.push(c-'0');
break;
}
}
t1 = st.top();
st.pop();
while(!st.empty())
{
st.push(t1);
t1 = countOne(st);
}
std::cout << co << " " << t1 << std::endl;
return 0;
}