|
发表于 2015-1-18 11:23:40
|
显示全部楼层
回帖奖励 +3 鱼币
- operator_pre, operator, getin, putout,express = ({'+': 1, '-': 1, '*': 2, '/': 2, '^': 3, '%': 3, '(': 0, '#': 0}, ['+', '-', '*', '/', '%', '^'], [], [], raw_input("请输入表达式: "))
- for index in range(0,len(express)):
- if express[index] in ['+','-'] and express[index-1] == '(' or index == 0: express = express[:index] + '0' + express[index:]
- for ch in express:
- if ch <= '9' and ch >= '0': putout.append(ch)
- elif ch in operator:
- if len(getin) != 0 and operator_pre[ch] > operator_pre[getin[-1]]: getin.append(ch)
- elif len(getin) != 0 and operator_pre[ch] <= operator_pre[getin[-1]]:
- while len(getin) != 0 and operator_pre[getin[-1]] >= operator_pre[ch]:putout.append(getin.pop())
- getin.append(ch)
- else:getin.append(ch)
- elif ch == '(':getin.append(ch)
- elif ch == ')':
- while getin[-1] != '(' and len(getin) != 0:putout.append(getin.pop())
- getin.pop()
- else:putout.append(ch)
- while len(getin) != 0:putout.append(getin.pop())
- print(''.join(putout))
|
|