How would I Evaluate a certain formula?

后端 未结 4 1353
旧时难觅i
旧时难觅i 2021-01-27 09:24

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
         


        
4条回答
  •  感动是毒
    2021-01-27 09:32

    You will need to turn the formula into something like a Binary Expression Tree. This should be not that difficult.

    Then you will need to traverse this tree to evaluate the expression value at the start and each time a value in the arrayList changes. Concentrate first on building the tree and getting correct values when you evaluate it. Don't forget negative numbers and variables! Watching the arrayList for changes should be trivial after that.

提交回复
热议问题