Python实现Shuntingyard (调度场)算法

本算法具体实现了数学表达式的中缀转后缀(逆波兰),并且对表达式进行了估值,支持基本的四则运算和若干数学函数。最大的缺点是不支持负数并且不能检查表达式的合法性,考虑到其算法的复杂性,笔者决定学习正则表达式来解决这一问题。

具体算法参考http://en.wikipedia.org/wiki/Shunting-yard_algorithm以及相关代码https://github.com/ekg/shuntingyard/blob/master/shuntingyard.py


import sys
import math
import cmath
import operator

# define operator
operator_ad = "+"
operator_mi = "-"
operator_mu = "*"
operator_di = "/"
operator_eq = "="
operator_lp = "("
operator_rp = ")"

# define operator implementation
operator_implement = {
    "+": operator.add,
    "-": operator.sub,
    "*": operator.mul,
    "/": operator.div,
    }

# define function implementation
function_implement = {
    "ceil": math.ceil, "floor": math.floor,
    "fabs": math.fabs,
    "factorial": math.factorial,
    "pow": math.pow,
    "sin": math.sin, "cos": math.cos, "tan": math.tan, "asin": math.asin, "acos": math.acos, "atan": math.atan,
    "rad2deg": math.degrees, "deg2rad": math.radians,
    "sinh": math.sinh, "cosh": math.cosh, "tanh": math.tanh, "asinh": math.asinh, "acosh": math.acosh, "atanh": math.atanh,
    "erf": math.erf, "erfc": math.erfc, "gamma&#
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值