
def cal(x,y,op):
if op=="+":
s=x+y
elif op=="-":
s=x-y
elif op=="*":
s=x*y
else:
s=x/y
①
exp=input("输入表达式:").split () #输入一串字符,通过空格隔开,使用split函数转换为列表
print(exp)
st1=[""]*2 #存储数字
st2=[""]*2 #存储运算符
top1=top2=-1
for i in exp:
if ②
if top2==-1:
top2+=1
elif i in "*/":
top2+=1
else:
st1[top1-1]=cal(float(st1[top1-1]),float(st1[top1]),st2[top2])
top1-=1
③
else:
if top1==-1:
top1+=1
st1[top1]=i
elif top2>-1:
if st2[top2] in "*/":
④
⑤
elif st2[top2] in "+-":
top1+=1
st1[top1]=i #将数字压入st1栈中
if top2>-1: #处理剩余未运算的数据
st1[top1-1]=cal(float(st1[top1-1]),float(st1[top1]),st2[top2])
top1-=1
print(st1[top1])#’’’

同类型试题

y = sin x, x∈R, y∈[–1,1],周期为2π,函数图像以 x = (π/2) + kπ 为对称轴
y = arcsin x, x∈[–1,1], y∈[–π/2,π/2]
sin x = 0 ←→ arcsin x = 0
sin x = 1/2 ←→ arcsin x = π/6
sin x = √2/2 ←→ arcsin x = π/4
sin x = 1 ←→ arcsin x = π/2


y = sin x, x∈R, y∈[–1,1],周期为2π,函数图像以 x = (π/2) + kπ 为对称轴
y = arcsin x, x∈[–1,1], y∈[–π/2,π/2]
sin x = 0 ←→ arcsin x = 0
sin x = 1/2 ←→ arcsin x = π/6
sin x = √2/2 ←→ arcsin x = π/4
sin x = 1 ←→ arcsin x = π/2

