후위식 연산

이세진·2022년 4월 15일
0

코테준비

목록 보기
35/87

생성일: 2022년 1월 25일 오후 7:58

구현 코드

# 후위식 연산
import sys
#sys.stdin = open("input.txt", "rt")
s = input()
stack = []

for x in s:
    if x.isdecimal():
        stack.append(x)
    else:
        a = int(stack.pop())
        b = int(stack.pop())
        if x == '+':
            stack.append(b+a)
        elif x == '-':
            stack.append(b-a)
        elif x == '*':
            stack.append(b*a)
        else:
            stack.append(b/a)
print(stack.pop())
profile
나중은 결코 오지 않는다.

0개의 댓글