在PHP中解析数学表达式 [英] Parse Math Expression in PHP

查看:297
本文介绍了在PHP中解析数学表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将数学表达式解析成表达式树。
但我被困在我需要实现功能和否定的舞台上。我不明白使用Shunting-Yard算法的逻辑。

I'm currently trying to parse math expression into expression tree. But I'm stuck on the stage where I need to implement functions and negates. I don't understand logic to do it using Shunting-Yard algorithm.

我目前想做的是支持


  • 否定,像 - (x + 5)

  • 函数调用,如min(x,y)

  • 功能刚刚在函数名称后面,像cos ^ 2(x)

  • 隐含乘法,像2x一样2 * x

  • 科学符号

  • 常量e和pi

  • Negates, like -(x+5)
  • Function calls, like min(x,y)
  • Power just after function name, like cos^2(x)
  • Implicit multiplication, like 2x is same as 2*x
  • Scientific notation
  • Constants e and pi

有人可以告诉我如何实现这个吗? p>

Can somebody tell me hints how to implement this?

推荐答案

工作,PSR-0兼容的分流码算法的实现可以在这里找到: https://github.com/andig/php-shunting-yard/tree/dev
它支持常量,自定义功能等。

An working, PSR-0 compatible implementation of the shunting yard algorithm can be found here: https://github.com/andig/php-shunting-yard/tree/dev. It supports constants, custom functions etc.

这篇关于在PHP中解析数学表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆