Skip to content

Create baekjoon1918 #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions week1/datastructure/후위_표기식/baekjoon1918
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <iostream>
#include <stack>
using namespace std;
void solution();
//A+(B*C)-(D/E)
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);

solution();
return 0;
}
void solution(){
string str;
stack<char> st;
cin >> str;
for (int i = 0; i < str.size(); ++i) {
if(str[i]>='A'&&str[i]<='Z'){//알파뱃일때 처리
cout << str[i];
}
else{
if(str[i]=='('){
st.push(str[i]);
}
else if(str[i]==')'){// 닫힘 괄호 들어오면 스택에 잇던것들 열림괄호 전까지 pop
while(st.top()!='('){
cout << st.top();
st.pop();
}
st.pop();// 열림괄호 삭제
}
else if(str[i]=='*'||str[i]=='/'){// 우선순위가 높은 곱하기와 나눗셈을 우선순위가 낮은 것들 전까지 pop
while(!st.empty()&&(st.top()=='*'||st.top()=='/')){
cout << st.top();
st.pop();
}
st.push(str[i]);
}
else if(str[i]=='+'||str[i]=='-'){// + - 처리
while(!st.empty() && st.top() != '('){
cout << st.top();
st.pop();
}
st.push(str[i]);
}
}
}
while(!st.empty()){// 남은 스택 처리
cout << st.top();
st.pop();
}
}