以下是支持括号与优先级的表达式求值器的Python实现,使用双栈法(操作数栈和运算符栈)处理运算符优先级:
class ExpressionEvaluator:
def __init__(self):
self.precedence = {'+':1, '-':1, '*':2, '/':2, '^':3}
def evaluate(self, expression):
def apply_op(a, b, op):
if op == '+': return a + b
if op == '-': return a - b
if op == '*': return a * b
if op == '/': return a // b
if op == '^': return a ** b
values = []
ops = []
i = 0
while i < len(expression):
if expression[i] == ' ':
i += 1
continue
if expression[i] == '(':
ops.append(expression[i])
elif expression[i].isdigit():
val = 0
while i < len(expression) and expression[i].isdigit():
val = val * 10 + int(expression[i])
i += 1
values.append(val)
continue
elif expression[i] == ')':
while len(ops) != 0 and ops[-1] != '(':
val2 = values.pop()
val1 = values.pop()
op = ops.pop()
values.append(apply_op(val1, val2, op))
ops.pop() # 弹出左括号
else:
while (len(ops) != 0 and ops[-1] != '(' and
self.precedence[ops[-1]] >= self.precedence[expression[i]]):
val2 = values.pop()
val1 = values.pop()
op = ops.pop()
values.append(apply_op(val1, val2, op))
ops.append(expression[i])
i += 1
while len(ops) != 0:
val2 = values.pop()
val1 = values.pop()
op = ops.pop()
values.append(apply_op(val1, val2, op))
return values[-1]
# 测试用例
evaluator = ExpressionEvaluator()
print(evaluator.evaluate("10 + 2 * 6")) # 输出22
print(evaluator.evaluate("(10 + 2) * 6")) # 输出72
print(evaluator.evaluate("100 * 2 + 12")) # 输出212
print(evaluator.evaluate("100 * (2 + 12)")) # 输出1400
print(evaluator.evaluate("2 ^ 3 + 5 * 2")) # 输出18
该实现支持加减乘除和指数运算,正确处理括号优先级。核心算法是Dijkstra的双栈法,时间复杂度O(n)。使用时创建ExpressionEvaluator实例并调用evaluate方法即可。
http://keji.liyintong.com
http://keji.naqimai.cn
http://keji.kucedu.cn
http://keji.yueluyan.cn
http://keji.huayuke.cn
http://keji.haizichu.cn
http://keji.yawanmei.cn
http://keji.biaolele.cn
http://keji.shenhebu.cn
http://keji.zimeiren.cn
http://keji.qishouka.cn
http://keji.ruanding.cn
http://keji.xjhsdsc.cn
http://keji.itoren.cn
http://keji.iseebest.cn
http://keji.bndaye.cn
http://keji.rustler.cn
http://keji.excelta.cn
http://keji.diaolift.cn
http://keji.jxpfbyjs.cn
http://keji.banans.cn
http://keji.aspira.cn
http://keji.bxhqw.cn
http://keji.pudiweng.cn
http://keji.tingbu.cn
http://keji.ouhei.cn
http://keji.huiha.cn
http://keji.miuling.cn
http://keji.podang.cn
http://keji.fenkun.cn
http://keji.liangran.cn
http://keji.zouliu.cn
http://keji.xuhou.cn
http://keji.kuopao.cn
http://keji.lunkai.cn
http://keji.zhaiti.cn
http://keji.fogei.cn
http://keji.gengluo.cn
http://keji.wadiao.cn
http://keji.hunjun.cn
http://keji.huanken.cn
http://keji.chuancong.cn
http://keji.buzun.cn
http://keji.zhuozou.cn
http://keji.lazai.cn
http://keji.zengle.cn
http://keji.suidun.cn
http://keji.zhaojunji.cn
http://keji.huihuoban.cn
http://keji.wanjiahua.cn
http://keji.conglinyi.cn
http://keji.henyoupin.cn
http://keji.wuwenkang.cn
http://keji.tujiachen.cn
http://keji.zilaoweng.cn
http://keji.baolema.cn
http://keji.shumeilin.cn
http://keji.anhetong.cn
http://keji.wenjishu.cn
http://keji.kansande.cn
http://keji.yueshijie.cn
http://keji.tihujiu.cn
http://keji.huatoutou.cn
http://keji.xiaolaige.cn
http://keji.huguangu.cn
http://keji.lvdate.cn
http://keji.kesini.cn
http://keji.soubianlu.cn
http://keji.fuenbu.cn
http://keji.liuyakun.cn
http://keji.zouyizou.cn
http://keji.juyingba.cn
http://keji.namahu.cn
http://keji.dadudu.cn
http://keji.xuewenzi.cn
http://keji.lazhuyong.cn
http://keji.aizishu.cn
http://keji.nianjiepo.cn
http://keji.baisuijie.cn
http://keji.wanyuecun.cn
http://keji.shoupashu.cn
http://keji.hetongmei.cn
http://keji.ouenming.cn
http://keji.qianyiduo.cn
http://keji.yidingzhi.cn
http://keji.zouyuming.cn
http://keji.mofaya.cn
http://keji.hexiangru.cn
http://keji.quyouban.cn
http://keji.mingyinsi.cn
http://keji.junepan.cn
http://keji.qiyuehong.cn
http://keji.ledatong.cn
http://keji.chenqinga.cn
http://keji.ebuyun.cn
http://keji.gayijiu.cn
http://keji.liqinge.cn
http://keji.liubawan.cn
http://keji.huabaohan.cn
http://keji.aiguandan.cn
http://keji.judoubang.cn
http://keji.huachenyu.cn
http://keji.hexiaolia.cn
http://keji.feiyuxuan.cn
http://keji.zhenwasai.cn
http://keji.maoweilai.cn
http://keji.yunyuewei.cn
http://keji.kemensen.cn
http://keji.anxinyuan.cn
http://keji.deyisheji.cn
http://keji.ximaguohe.cn
http://keji.gewukeji.cn
http://keji.rehuang.cn