C# Formula (123/2*15+22) evaluation [duplicate]
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.
精彩评论