calculating user defined formulas (with c++)

前端 未结 8 1719
萌比男神i
萌比男神i 2020-12-29 12:07

We would like to have user defined formulas in our c++ program. e.g. The value v = x + ( y - (z - 2)) / 2. Later in the program the user would define x,y and z -> t

8条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-29 12:14

    Using Spirit (for example) to parse (and the 'semantic actions' it provides to construct an expression tree that you can then manipulate, e.g., evaluate) seems like quite a simple solution. You can find a grammar for arithmetic expressions there for example, if needed... (it's quite simple to come up with your own).

    Note: Spirit is very simple to learn, and quite adapted for such tasks.

提交回复
热议问题