From 0269b97746affff743291b46bc632a434a7aa6dd Mon Sep 17 00:00:00 2001 From: kanghmhmhmhm <129865445+kanghmhmhmhm@users.noreply.github.com> Date: Mon, 24 Mar 2025 05:28:55 +0900 Subject: [PATCH] Create baekjoon1918 --- .../baekjoon1918" | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 "week1/datastructure/\355\233\204\354\234\204_\355\221\234\352\270\260\354\213\235/baekjoon1918" diff --git "a/week1/datastructure/\355\233\204\354\234\204_\355\221\234\352\270\260\354\213\235/baekjoon1918" "b/week1/datastructure/\355\233\204\354\234\204_\355\221\234\352\270\260\354\213\235/baekjoon1918" new file mode 100644 index 00000000..e3464c43 --- /dev/null +++ "b/week1/datastructure/\355\233\204\354\234\204_\355\221\234\352\270\260\354\213\235/baekjoon1918" @@ -0,0 +1,52 @@ +#include +#include +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 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(); + } +}