forked from tony9402/baekjoon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
50 lines (45 loc) · 1.21 KB
/
main.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
// Authored by : tony9402
// Co-authored by : -
// Link : http://boj.kr/1bff0abd9f2646ceb07d7fdb4b5ace97
#include<bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
string s; cin >> s;
stack<char> st;
string answer = "";
for(auto &ch: s) {
if('A' <= ch && ch <= 'Z') answer += ch;
else {
if(ch == '(') st.push(ch);
else if(ch == ')') {
while(!st.empty() && st.top() != '(') {
answer += st.top();
st.pop();
}
st.pop();
}
else if(ch == '*' || ch == '/') {
while(!st.empty() && (st.top() == '*' || st.top() == '/')) {
answer += st.top();
st.pop();
}
st.push(ch);
}
else if(ch == '+' || ch == '-') {
while(!st.empty() && st.top() != '(') {
answer += st.top();
st.pop();
}
st.push(ch);
}
}
}
while(!st.empty()) {
answer += st.top();
st.pop();
}
cout << answer;
return 0;
}