'''
1918 - 후위 표기식 / gold 2 / 스택, 자료구조
'''
infix = input()
op_stack = []
postfix = ''
for v in infix:
if v.isalpha():
postfix += v
else:
if v == '(':
op_stack.append(v)
elif v == '*' or v == '/':
while op_stack and (op_stack[-1] == '*' or op_stack[-1] == '/'):
postfix += op_stack.pop()
op_stack.append(v)
elif v == '+' or v == '-':
while op_stack and op_stack[-1] != '(':
postfix += op_stack.pop()
op_stack.append(v)
elif v == ')':
while op_stack[-1] != '(':
postfix += op_stack.pop()
op_stack.pop()
while op_stack:
postfix += op_stack.pop()
print(postfix)
입력받은 infix(중위 표기식)을 for문 통해 비교하기
1. 피연산자 (알파벳) 일 때
- postfix 문자열에 추가
2. 연산자 일 때
- v == ( 일 때, stack에 추가
- v == * / 일 때, stack에 있던 * / 을 pop해서 postfix에 추가하기 ☞ v를 stack에 추가
- v == + - 일 때, stack에 있던 * / + - 을 pop 해서 postfix에 추가 ☞ v를 stack에 추가
- v == ) 일 때, stack에 있던 * / + - 을 pop 해서 postfix에 추가 ☞ stack 한번 더 pop해서 ( 지우기
op_stack에 남은 연산자가 있으면, postfix에 추가하기
'백준 문제풀이' 카테고리의 다른 글
1806번 - 부분합 / gold 4 / 투 포인터 (1) | 2022.12.09 |
---|---|
12852번 - 1로 만들기 2 / silver 1 / DP (0) | 2022.12.08 |
13172 - Σ / gold 4 / 분할 정복을 이용한 거듭제곱 (0) | 2022.12.05 |
2638 - 치즈 / gold 3 / BFS (0) | 2022.12.04 |
2263 - 트리의 순회 / gold 2 / 분할 정복 (1) | 2022.12.03 |