how to convert a string to a mathematical expression programmatically

后端 未结 5 1993
时光说笑
时光说笑 2020-12-20 23:32

I am a beginner at C#. I am facing a problem while converting a string to a mathematical expression. I have a UI where user can create formula using random formula field. An

5条回答
  •  猫巷女王i
    2020-12-21 00:14

    The most structural approach which comes to my mind would be to define a grammar consisting out of operator symbols (which are apparently +,-,*,/ and ^ in your case) and operands; then, if a derivation of the input in the defined grammar exists, the derivation basically is the expression tree which can then be traversed recursively while the operators are directry translated to the actual operations. I admit that the description is a bit vague, but good parsing can be a bit difficult. Perhaps a look at LL parser can help a bit.

提交回复
热议问题