#include <iostream>
#include <stack>
#include <vector>
#include <string>
#include <map>
using namespace std;
string s;
map<char, int> prec;
stack<char> stk;
string ans = "";
void set_prec(){
prec['*'] = 3;
prec['/'] = 3;
prec['+'] = 2;
prec['-'] = 2;
prec['('] = 1;
}
void input(){
cin >> s;
}
void solve(){
for(int i = 0; i < s.length(); i++){
char c = s[i];
if(c == '('){
stk.push('(');
}
else if(c == ')'){
while(stk.top() != '('){
ans += stk.top();
stk.pop();
}
stk.pop(); // '(도 없애야지'
}
else if(c != '*' && c != '/' && c!= '+' && c != '-'){
//피연산자이면
ans += c;
}
else if(c == '*' || c == '/'){
while(!stk.empty() and (stk.top()=='*' ||stk.top() =='/')){
ans += stk.top();
stk.pop();
}
stk.push(c);
}
else if(c == '+' || c == '-'){
while(!stk.empty() and stk.top() != '('){
ans += stk.top();
stk.pop();
}
stk.push(c);
}
else{
stk.push(c);
}
}
while(!stk.empty()){
ans += stk.top();
stk.pop();
}
}
void print_(){
cout << ans << "\n";
}
int main(void){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
set_prec();
input();
solve();
print_();
return 0;
}
후위 표기식과 스택 잘 이해하면 풀 수 있는 문제.
+와 -보다 *와 / 의 우선순위가 높다는 점
그리고 (와 )를 어떻게 처리할 지만 이해하면 된다.
