且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我将如何评估某个公式?

更新时间:2023-12-04 22:38:10

一种可能性是编写一种解析器.***使用二叉树结构来表示表达式,而不是列表.>

每个非叶子节点都是一个操作,每个叶子节点都是操作数.

I have a multidimension arrayList and I ask the user for a formula and than I evaluate it. The problem is that I get user input like this:

  ((a1+a2)/12)*a3

the problem is that a1 and a2 and a3 refer to columns and I have to evaluate it to a certain value to it and I am totally lost on how to approach this problem any advice or guidance would be great. Also this calculate value has to update every time the value in any of the columns Update. The thing is the formula isn't hard coded.

A possibility is write a sort of parser. It's better to use a binary tree structure to represent an expression, rather than a list.

Each non-leaf node is an operation and every leaf is operand.