Adventure Time - Finn 3
본문 바로가기
코테연습/python

후위표기식 만들기

by hyun9_9 2026. 4. 28.

중위표기식이 입력되면 후위표기식으로 변환하는 프로그램을 작성하세요. 중위표기식은 우리가 흔히 쓰은 표현식입니다. 즉 3+5 와 같이 연산자가 피연산자 사이에 있 으면 중위표기식입니다. 후위표기식은 35+ 와 같이 연산자가 피연산자 뒤에 있는 표기식입니다. 예를 들어 중위표기식이 3+5*2 를 후위표기식으로 표현하면 352*+ 로 표현됩니다. 만약 다음과 같이 연산 최우선인 괄호가 표현된 식이라면 (3+5)*2 이면 35+2* 로 바꾸어야 합니다

import sys
sys.stdin=open("input.txt","rt")
# n,m = map(int,input().split())
# n = int(input())
# arr = list(map(int,input().split()))

# n = input()
# arr = [list(map(int,input().split())) for _ in range(n)]
# arr = [int(input()) for _ in range(n)]
arr = list(input())
print(arr)
num =[]
ex = []
res =''

for a in arr:
    if a.isdigit():
        res+= a

    else:
        if a =='(':
            ex.append(a)
        elif a == '*' or a =='/':
            while ex and (ex[-1] == '*' or ex[-1]=='/'):
                res+=ex.pop()
            ex.append(a)
        elif a=='+' or a=='-':
            while ex and (ex[-1] != '('):
                res+=ex.pop()
            ex.append(a)
        elif a==')':
            while ex and(ex[-1]!='('):
                res+=ex.pop()
            ex.pop()

while ex:
    res+=ex.pop()



print(res)

'코테연습 > python' 카테고리의 다른 글

응급실 (큐)  (0) 2026.04.29
공주 구하기(큐 자료구조로 해결)  (0) 2026.04.29
나누어 떨어지는 숫자 배열  (0) 2026.04.28
가운데 글자 가져오기  (0) 2026.04.27
가장 큰 수  (1) 2026.04.26