带变量的调车场算法 [英] Shunting Yard Algorithm with Variables

查看:69
本文介绍了带变量的调车场算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发Shunting Yard Algorithm的修改版本,该版本可以使用变量,但是我不知道如何使它起作用.例如,我希望算法重新编写2 *(2x + 5)-5到4x +5.是否有任何想法/指向已经实现的算法的链接?

I'm currently working on a modified version of the Shunting Yard Algorithm that would work with variables, but I cant figure out how to get it to work. For example, I would want the algorithm to re-write 2 * (2x + 5) - 5 to 4x + 5. Any ideas / links to already implemented algorithms that does this already?

推荐答案

  1. 采用表达式: 2 *(2x + 5)-5
  2. 添加*符号,使计算机更易理解: 2 *(2 * x + 5)-5
  3. 使用Shunting Yard算法对其进行解析,结果变为: 2 2 x * 5 + * 5-(每个字符都可以看作是数组的元素).
  4. 使用已解析的表达式,创建二叉树:
  1. Take the expression: 2 * (2x + 5) - 5
  2. Add the * symbol to make it more understandable for the computer: 2 * (2*x + 5) - 5
  3. Parse it using the Shunting Yard Algorithm, it becomes: 2 2 x * 5 + * 5 - (Each character could be seen as an element of an array).
  4. With the parsed expression, create the binary tree:

<代码>--/\* 5/\2 +/\* 5/\2倍5.定义代数规则并将其应用于树.例如,一条规则能够将 2 节点与 2 * x + 5 子树相乘".

- / \ * 5 / \ 2 + / \ * 5 / \ 2 x 5. Define and apply algebraic rules to the tree. For example, a rule to be able to 'multiply' the 2 node with the 2 * x + 5 subtree.

这篇关于带变量的调车场算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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