[후위표기식]계산

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
oper = ['+', '-', '*', '/']
for t in range(int(input())):
    s = list(input().split())
    stack = list()
    for i in s:
        if i in oper:
            try:
                a = stack.pop(-1)
                b = stack.pop(-1)
                calc = eval(b+i+a)
                stack.append(str(calc))
            except:
                print("#{} error".format(t+1))
                break
        else:
            if i == '.':
                print("#{} {}".format(t+1, stack[0]))
            else:
                stack.append(i)

댓글

이 블로그의 인기 게시물

[python]섬의 둘레구하기

백트래킹으로 부분집합구하기(Get Powerset usiing Backtracking)

[패턴매칭][Python]보이어 무어 알고리즘