学进去-教育应平等而普惠
试题
类型:操作题
难度系数:0.40
所属科目:高中信息技术
编写程序,实现四则混合运算(算术表达式只含有数字和“+-*/”运算符)。算法设计如下:通过枚举表达式的各种情况,推导算法规则,如表达式“5*3……”则不管后续运算符是什么必定先计算5*3;如表达式“5+3……”则需判断“3”后面的运算符,若为“*2”,则需先计算“3*2”,若为“+2”,则需计算“5+3”。程序运行界面如图所示,请在划线处填入合适的代码。

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

用户名称
2019-09-19

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

用户名称
2019-09-19
我要答疑
编写解析
解析:

奖学金将在审核通过后自动发放到帐

提交
我要答疑
我要答疑:
提交