且构网

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

解决c#中的相似方程

更新时间:2023-07-21 09:38:22

取决于你如何评估你的表达式 - 你没有这么说我们必须猜测 - 你要么需要重做你的解析器,要么实际实现你自己的。如果你依赖于有效的编译并且在C#中运行表达式来解决这个问题,那么你从一开始就失败了 - 你无法修改编译器。



你可以拿这样的东西:微小的表达评估器 [ ^ ]并调整它允许你的(非标准的)输入格式。



或者只是告诉你的用户添加一个运算符......
Depending on how you are evaluating your expressions - and you don't say so we have to guess - you will either need to rework your parser or actually implement your own. If you are relying on effectively compiling and running expressions in C# to solve this, then you are on a loser from teh start - you can't modify the compiler.

You could however take something like this: A Tiny Expression Evaluator[^] and tweak it to allow your (rather non-standard) input format.

Or just tell your users to add an operator...


只替换'之后的数字序列('with *(

这样 3((482/100) )* 2)将变为 3 *((482/100)* 2)

[而不是 3 *(*(482/100) )* 2)]



Best,H
Replace only a sequence of a digit following '(' with "*("
This way 3((482/100)*2) will become 3*((482/100)*2)
[instead of 3*(*(482/100)*2)]

Best, H


在你解析语法之前,我建议你做它更常规,例如使乘法运算符'*'是必需的。

一旦你有一个好的语法,那么你可能会找到几个建立的选项实际的解析器(你可以找到现成的解析器,使用工具,在你的应用程序中嵌入一个脚本语言)。
Before you parse the grammar, I suggest you to make it more regular, for instance making the multiplicative operator '*' mandatory.
Once you have a good grammar then you may find several options for building the actual parser (you may find ready-to-use one, use a tool, embed a script language in your application).