题解 | #简单表达式计算#
简单表达式计算
https://www.nowcoder.com/practice/6221faa383fc49f1b10dffcb62c866bf
# 读入表达式,调用eval函数,三行搞定。
import sys
inp = sys.stdin.read().strip().split("\n")[:-1]
for s in inp: print(eval(s))
# def calc(a, b, op):
# # print(a,b,op)
# if '+'==op: return a+b
# if '-'==op: return a-b
# if '*'==op: return a*b
# assert("error")
# op = set('+-*')
# for s in inp:
# # print(eval(s))
# stack0 = []
# stack1 = []
# i = 0
# while i<len(s):
# if s[i] not in op:
# j=i+1
# while j<len(s) and s[j] not in op: j+=1
# stack0.append(int(s[i:j]))
# i = j
# else:
# o = s[i]
# if o=="*": stack1.append(o)
# else:
# while len(stack1):
# b, a = stack0.pop(), stack0.pop()
# c = calc(a,b,stack1.pop())
# stack0.append(c)
# stack1.append(o)
# i+=1
# while len(stack1):
# b, a = stack0.pop(), stack0.pop()
# c = calc(a,b,stack1.pop())
# stack0.append(c)
# print(stack0[0])

查看23道真题和解析