4

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?

4

1 回答 1

4
  1. 取表达式:2 * (2x + 5) - 5
  2. 添加 * 符号以使其更易于计算机理解:2 * (2*x + 5) - 5
  3. 使用 Shutting Yard 算法对其进行解析,它变为:(2 2 x * 5 + * 5 -每个字符都可以看作是数组的一个元素)。
  4. 使用解析后的表达式,创建二叉树:

- / \ * 5 / \ 2 + / \ * 5 / \ 2 x 5. 定义代数规则并将其应用于树。例如,能够将2节点与2 * x + 5子树“相乘”的规则。

于 2014-08-15T03:20:39.430 回答