题目描述
![](https://img-blog.csdnimg.cn/c5c0de3a098c4158af7de84f05e689e9.png)
代码实现
# coding:utf-8
# 仿LISP运算
# https://www.nowcoder.com/discuss/360006188531032064?sourceSSR=search
import systry:while True:line = sys.stdin.readline().replace('(', '( ').replace(')', ' )').strip().split(' ')if line == '':breakstack = []op = []error_flag = Falsedef cal(operation, p1, p2):res = 0if operation == 'add':res = p1 + p2if operation == 'sub':res = p1 - p2if operation == 'mul':res = p1 * p2if operation == 'div':if p2 == 0:global error_flagerror_flag = Trueelse:res = p1 // p2return resfor i in range(len(line)):if line[i] == '(':stack.append(line[i])elif line[i] == ')':if not stack:error_flag = Truebreakstack.pop()p2 = int(op.pop())p1 = int(op.pop())operation = op.pop()res = cal(operation, p1, p2)op.append(res)else:op.append(line[i])if error_flag:breakif error_flag:print("error")else:print(res)except:print('error')