开发者

C# Formula (123/2*15+22) evaluation [duplicate]

This question already has answers here: operators as strings 开发者_Go百科 (13 answers) Is there a string math evaluator in .NET? (18 answers) Closed 9 years ago.

Does someone know the best way to evaluate a string formula like this one: (123/2*15+22) within c#.

I have read that i should use an ICodeCompiled, but havent been able to find any good implementations to do this very simply thing.

Any suggestions are appreciated.


class Program {
    static void Main(string[] args) {
        var calc = new System.Data.DataTable();
        Console.WriteLine(calc.Compute("(123/2*15+22)", ""));
        Console.ReadLine();
    }
}

Output: 944.5


I would take a look at MSDN's documentation on ICodeCompiler.


You could use an expression parser, such as this one at CodeProject.


For this type of problem I typically recommend FLEE; I've had nothing but good experiences using it to do equation evaluation and some basic DSL work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜