stack
来源:互联网 发布:服装搭配软件 编辑:程序博客网 时间:2024/06/04 19:00
def calx(op, a, b): if op == 'mul': return str(int(a) * int(b)) elif op == 'add': return str(int(a) + int(b)) elif op == 'div': if b == '0': return 'error' else: return str(int(a) / int(b)) elif op == 'sub': return str(int(a) - int(b))def funx(ss): ops = [] num = [] for p in ss.split(' '): if p[0] == '(': ops += [p.strip('(')] elif p[-1] == ')': b = p.strip(')') a = num.pop() res = calx(ops.pop(), a, b) if res == 'error': return res if ops: num += [res] else: return str(res) else : num += [p.strip(')')] while ops: b = num.pop() a = num.pop() res = calx(ops.pop(), a, b) if res == 'error': return res if ops: num += [res] else: return str(res) ss = '(sub (add 1 4) (div 9 (sub 11 8)))'#parts = ss.split(' ')funx(ss)
import redef func(ss): patt = re.compile('\(\w+ \d+ \d+\)') while True: outs = patt.findall(ss) if len(outs) == 0: return ss for i in outs: tmp_i = i[1:-1].split(' ') tmp = calx(tmp_i[0], tmp_i[1], tmp_i[2]) print tmp if tmp == 'error': return 'error' ss = ss.replace(i, tmp)
阅读全文
0 0
- stack
- stack
- stack
- stack
- Stack
- Stack
- Stack
- Stack
- stack
- Stack
- stack
- stack
- Stack
- stack
- Stack
- Stack
- stack
- stack.
- Android Fk-PKMS(2) PackageManagerService之应用的安装与卸载
- 夜长,梦短
- pat-a1018. Public Bike Management (30)
- 初见Lambda表达式
- Python小问题总结
- stack
- 错误总结
- 如何将在Excel中设计的实体对象字段表导入PowerDesigner
- CQOI2016 手机号码 数位DP
- css背景图片自适应
- mysql安装及配置
- POJ: Drying(二分)
- C++设计模式:装饰模式
- M进制转N进制(C++)