Process mathematical equations in php

后端 未结 8 1918
陌清茗
陌清茗 2020-11-30 14:14

A user is allowed to enter any mathematical equation they like (with one variable):

x + 5

1 - x/2

(x/3) * (56/13)

8条回答
  •  孤城傲影
    2020-11-30 14:27

    Depends...

    What is the complexity it will accept? Because for common mathematical equations (like the ones you posted), I do not see too much problem in writing a parser. The main problematic question would be round the numbers and place correct parenthesis.

    But if the equations are going to accept "advanced" inputs, like {[()]}, or X², X³, or getting further, diferential calculus and college maths, so things can go crazy.

    If the complexity reach symbolic handling, try read and search something about CAS (Calculate Algebra Systems).

    Of course, I extremely recommend you to make your own system for inputs, validate against it, and evangelize users to tie inputs to it. Nothing too complex, but enough to make you (and others) comfortable and safe to reach what you need.

提交回复
热议问题