问题描述
我目前正在开发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?
推荐答案
- 采用表达式:
2 *(2x + 5)-5
- 添加*符号,使计算机更易理解:
2 *(2 * x + 5)-5
- 使用Shunting Yard算法对其进行解析,结果变为:
2 2 x * 5 + * 5-
(每个字符都可以看作是数组的元素). - 使用已解析的表达式,创建二叉树:
- Take the expression:
2 * (2x + 5) - 5
- Add the * symbol to make it more understandable for the computer:
2 * (2*x + 5) - 5
- 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). - 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.
这篇关于带变量的调车场算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!