#五則運算
#後置式處理
import sys
def change_postfix(expression): #後置式函數
output = [] #存數字
operator = [] #存運算元
priority = {'+':1, '-':1, '*':2, '/':2, '%':2} #優先處理對照表
for r in expression:
if r.isdigit(): #判斷式否為數字
output.append(r)
elif r == '(':
operator.append(r)
elif r == ')':
while operator and operator[-1] != '(':
output.append(operator.pop())
operator.pop()
else:
while operator and operator[-1] != '(' and priority[r] <= priority[operator[-1]]:
output.append(operator.pop())
operator.append(r)
while operator: #特例: 3+4,所以要多這一行
output.append(operator.pop())
return output
def profix_calculate(solution): #後置式記算
calculate = [] #存結果
for t in solution:
if t.isdigit(): #判斷式否為數字
calculate.append(int(t))
else:
a = calculate.pop()
b = calculate.pop()
if t == '+':
calculate.append(b+a)
elif t == '-':
calculate.append(b-a)
elif t == '*':
calculate.append(a*b)
elif t == '/':
calculate.append(b//a)
elif t == '%':
calculate.append(b%a)
return calculate[0]
for line in sys.stdin:
expression = line.strip().split()
solution = change_postfix(expression)
print(profix_calculate(solution))