Parse Math Expression in PHP

喜夏-厌秋 提交于 2020-01-01 19:55:18

问题


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.

What I currently want to do is to support

  • 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

Can somebody tell me hints how to implement this?


回答1:


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.



来源:https://stackoverflow.com/questions/16572153/parse-math-expression-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!