-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path35_boj_1918.cpp
88 lines (76 loc) · 1.85 KB
/
35_boj_1918.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
#include <cstdio>
#include <iostream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int main(int argc, char **argv) {
ios_base::sync_with_stdio(false);
char oper[6] = {'+', '-', '*', '/','(',')'};
stack<char> st;
vector<char> v;
string s;
cin >> s;
int leng = s.length();
for (int i=0; i<leng; i++)
{
// puts("=============================");
// for (auto iter=v.begin(); iter != v.end(); iter++)
// {
// printf("%c ", *iter);
// }
// puts("");
// puts("=============================");
if ('A' <= s[i] && s[i] <= 'Z')
{
v.push_back(s[i]);
}
else
{
if (s[i] == '(')
{
st.push(s[i]);
}
else if (s[i] == '*' || s[i] == '/')
{
while (!st.empty() && (st.top()=='*' || st.top()=='/'))
{
v.push_back(st.top());
st.pop();
}
st.push(s[i]);
}
else if (s[i] == '+' || s[i] == '-')
{
while (!st.empty() && st.top() != '(')
{
v.push_back(st.top());
st.pop();
}
st.push(s[i]);
}
else if (s[i] == ')')
{
while (!st.empty() && st.top() != '(')
{
v.push_back(st.top());
st.pop();
}
st.pop();
}
}
}
while (!st.empty())
{
v.push_back(st.top());
st.pop();
}
for (auto iter=v.begin(); iter != v.end(); iter++)
{
printf("%c", *iter);
}
return 0;
}
/*
4*(5+2)
*/