|
发表于 2015-1-15 13:36:21
|
显示全部楼层
本帖最后由 挥舞乾坤 于 2015-1-21 11:37 编辑
龌龊的牺牲效率,换来减少行数....
- import re;s,oper,result = input('输入表达式:').replace(' ',''),{'+':2,'-':2,'*':1,'/':1,'#':3,'(':4,')':4},[];operlist,slist, = ['#'],re.findall(r'\w+[.]?\w*|^\-\w+[.]?\w*|(?<=[\-\+\*\/\(])\-\w+[.]?\w*|[\-\+\*\/\(\)]',s)
- for each in slist:
- if each == '(':operlist.append(each);continue
- if each == ')':
- while operlist[-1] != '(':result.append(operlist.pop())
- operlist.pop();continue
- if each not in oper:result.append(each);continue
- while oper[operlist[-1]] <= oper[each]:result.append(operlist.pop())
- operlist.append(each)
- while len(operlist) > 1:result.append(operlist.pop())
- print(' '.join([str(x) for x in result]))
复制代码 |
评分
-
参与人数 2 | 荣誉 +1 |
鱼币 +6 |
收起
理由
|
戴宇轩
| + 1 |
+ 1 |
短 &gt; 效率 |
~风介~
| |
+ 5 |
不错不错,功能齐全了,接下来就是改短了~ |
查看全部评分
|