如何创建数学公式解释器

发布于 2024-11-28 15:56:48 字数 108 浏览 0 评论 0原文

我必须用 PHP 创建一个简单的公式解释器。它必须支持 4 个运算符:exp、ln、加法、减法和括号。

我应该从哪里开始?我听说用户输入的公式必须转化为树,是真的吗?也许口译员已经存在?

I have to create a simple formula interpreter with PHP. It has to support 4 operators: exp, ln, addition, subtraction and brackets.

Where should I start? I've heard that the formula entered by the user must be transformed into a tree, is that true? Maybe interpreters already exist?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

瞄了个咪的 2024-12-05 15:56:48

是的,正确的是用户输入的公式必须转换成抽象语法树

数学公式通常使用 中缀 表示法编写。您需要将其转换为 postfixprefix 表示法。 后缀表示法也称为反向波兰表示法

您可以使用调车场算法来完成此操作。 查看详细示例

stackoverflow 问题链接PHP 中的实现。

Yes, correct the formula entered by the user must be converted into an abstract syntax tree.

Mathematical formulas are normally written using the infix notation. You need to convert this to postfix or prefix notation. The postfix notation is also known as the reverse polish notation.

You can use the shunting yard algorithm to accomplish this. See a detailed example.

This stackoverflow question links to a implementation in PHP.

白衬杉格子梦 2024-12-05 15:56:48

您可能可以使用基本的字符串操作将公式重写为 PHP 表达式并对其进行eval。根据语法,您甚至可以单独保留表达式,只为 expln 定义 PHP 函数,因此当您 eval 输入时可以直接评价。

这比为如此简单的语言编写自己的解析器和解释器要简单得多。

如果这是课堂作业,你的老师可能会因为这样做而让你不及格。

You could probably use basic string manipulation to rewrite the formula into a PHP expression and eval it. Depending on the syntax, you could even leave the expression alone and just define PHP functions for exp and ln, so when you eval the input it can be evaluated directly.

That'd be a lot more straightforward than writing your own parser and interpreter for such a simple language.

If this is classwork, your teacher will likely fail you for doing it that way.

聚集的泪 2024-12-05 15:56:48

我创建了这个,请查看:公式解释器

它是如何工作的?

首先,使用公式及其参数创建 FormulaInterpreter 实例。

$formulaInterpreter = new FormulaInterpreter("x + y", ["x" => 10, "y" => 20]);

使用 execute() 方法解释公式。它将返回结果:

echo $formulaInterpreter->execute();

在一行中

echo (new FormulaInterpreter("x + y", ["x" => 10, "y" => 20]))->execute();

示例

# Formula: speed = distance / time
$speed = (new FormulaInterpreter("distance/time", ["distance" => 338, "time" => 5]))->execute() ;
echo $speed;


#Venezuela night overtime (ordinary_work_day in hours): (normal_salary * days_in_a_work_month)/ordinary_work_day
$parameters = ["normal_salary" => 21000, "days_in_a_work_month" => 30, "ordinary_work_day" => 8];
$venezuelaLOTTTArt118NightOvertime = (new FormulaInterpreter("(normal_salary/days_in_a_work_month)/ordinary_work_day", $parameters))->execute();
echo $venezuelaLOTTTArt118NightOvertime;


#cicle area
$cicleArea = (new FormulaInterpreter("3.1416*(radio*radio)", ["radio" => 10]))->execute();
echo $cicleArea;

关于公式

  1. 它必须至少包含两个操作数和一个运算符。
  2. 操作数的名称可以是大写或小写。
  3. 到目前为止,sin、cos、pow 等数学函数不包括在内。我正在努力将他们纳入其中。
  4. 如果您的公式无效,您将收到一条错误消息,例如:错误,您的公式 (single_variable) 无效。
  5. 参数的值必须是数字。

如果你愿意的话,你可以改进它!

I created this, check it out: Formula Interpreter

How does it work ?

First, create an instance of FormulaInterpreter with the formula and its parameters

$formulaInterpreter = new FormulaInterpreter("x + y", ["x" => 10, "y" => 20]);

Use the execute() method to interpret the formula. It will return the result:

echo $formulaInterpreter->execute();

in a single line

echo (new FormulaInterpreter("x + y", ["x" => 10, "y" => 20]))->execute();

Examples

# Formula: speed = distance / time
$speed = (new FormulaInterpreter("distance/time", ["distance" => 338, "time" => 5]))->execute() ;
echo $speed;


#Venezuela night overtime (ordinary_work_day in hours): (normal_salary * days_in_a_work_month)/ordinary_work_day
$parameters = ["normal_salary" => 21000, "days_in_a_work_month" => 30, "ordinary_work_day" => 8];
$venezuelaLOTTTArt118NightOvertime = (new FormulaInterpreter("(normal_salary/days_in_a_work_month)/ordinary_work_day", $parameters))->execute();
echo $venezuelaLOTTTArt118NightOvertime;


#cicle area
$cicleArea = (new FormulaInterpreter("3.1416*(radio*radio)", ["radio" => 10]))->execute();
echo $cicleArea;

About the formulas

  1. It must contain at least two operands and an operator.
  2. Operands' name could be in upper or lower case.
  3. By now, math functions as sin, cos, pow… are not included. I'm working to include them.
  4. If your formula is not valid, you will get an error message like: Error, your formula (single_variable) is not valid.
  5. Parameters' values must be numeric.

You can improve it if you want to!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文