解题思路:
注意事项:
参考代码:
syouxianji = {'^':6,'/':4,'*':3,'-':2,'+':1,'':0,'(':0,')':0}
try:
s = input()
while s!='#':
slis = []
t = ''
for i in range(len(s)):
if s[i].isdigit():
t+=s[i]
if i==len(s)-1 and t!='':
slis.append(t)
else:
if t!='':
slis.append(t)
slis.append(s[i])
t=''
ss = []
t = 2
i=0
while i<len(slis):
if slis[i] == '(':
ss.append(i)
elif slis[i]==')':
l=ss.pop()
try:
youfuhao = slis[i + 1]
except:
youfuhao = ''
if l==0:
zuofuhao = ''
else:zuofuhao = slis[l - 1]
try:
zhongfuhao=slis[i-t]
except:zhongfuhao=''
if i-2==l or \
(zuofuhao == '(' and youfuhao == ')')\
or ( (zhongfuhao == '*') and ((syouxianji[zhongfuhao] >= syouxianji[zuofuhao]) and (syouxianji[zhongfuhao]+1 >= syouxianji[youfuhao]))) \
or ((zhongfuhao == '/') and ((syouxianji[zhongfuhao] > syouxianji[zuofuhao]) and (
syouxianji[zhongfuhao] >= syouxianji[youfuhao]))) \
or ((zhongfuhao=='-' or zhongfuhao == '^') and ((syouxianji[zhongfuhao] > syouxianji[zuofuhao]) and (
syouxianji[zhongfuhao] > syouxianji[youfuhao]))) \
or ((zhongfuhao == '+' ) and ( syouxianji[zhongfuhao] >= syouxianji[zuofuhao]) and (
syouxianji[zhongfuhao] >= syouxianji[youfuhao])):
if youfuhao==')':
t+=2
slis.pop(i)
slis.pop(l)
i-=2
else:
if youfuhao == ')':
t += 4
i+=1
print(''.join(slis))
s = input()
except:pass
0.0分
0 人评分
C语言程序设计教程(第三版)课后习题5.8 (C语言代码)浏览:572 |
printf基础练习2 (C语言代码)浏览:741 |
【简单计算】 (C语言代码)浏览:622 |
a+b浏览:433 |
C语言程序设计教程(第三版)课后习题1.6 (C语言代码)浏览:472 |
A+B for Input-Output Practice (C语言代码)浏览:467 |
蓝桥杯历届试题-翻硬币 (C++代码)浏览:878 |
用筛法求之N内的素数。 (C语言代码)浏览:529 |
分糖果 (C语言代码)浏览:916 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:469 |