|
发表于 2015-1-16 09:45:32
|
显示全部楼层
回帖奖励 +3 鱼币
不知道要求的是怎么个短法?
- def rpn():
- expression = input('请输入中缀表达式:')
- stack = []
- for i in expression:
- if i.isalnum(): print(i, end=' ')
- else:
- if i == '*' or i == '/' or i == '(': stack.append(i)
- elif i == '+' or i == '-':
- if len(stack):
- while True:
- temp = stack.pop()
- stack.append(temp) if '(' == temp else print(temp, end=' ')
- if not len(stack) or temp == '(': break
- stack.append(i)
- else : #i == ')'
- temp = stack.pop()
- while temp != '(':
- print(temp, end=' ')
- temp = stack.pop()
- while len(stack):
- print(stack.pop(), end=' ')
复制代码 |
评分
-
查看全部评分
|