Is there a string math evaluator in .NET?

前端 未结 16 1439
长情又很酷
长情又很酷 2020-11-22 01:01

If I have a string with a valid math expression such as:

String s = \"1 + 2 * 7\";

Is there a built in library/function in .NET that will p

相关标签:
16条回答
  • 2020-11-22 01:42

    For anybody developing in C# on Silverlight here's a pretty neat trick that I've just discovered that allows evaluation of an expression by calling out to the Javascript engine:

    double result = (double) HtmlPage.Window.Eval("15 + 35");
    
    0 讨论(0)
  • 2020-11-22 01:42

    Actually there is kind of a built in one - you can use the XPath namespace! Although it requires that you reformat the string to confirm with XPath notation. I've used a method like this to handle simple expressions:

        public static double Evaluate(string expression)
        {
            var xsltExpression = 
                string.Format("number({0})", 
                    new Regex(@"([\+\-\*])").Replace(expression, " ${1} ")
                                            .Replace("/", " div ")
                                            .Replace("%", " mod "));
    
            return (double)new XPathDocument
                (new StringReader("<r/>"))
                    .CreateNavigator()
                    .Evaluate(xsltExpression);
        }
    
    0 讨论(0)
  • 2020-11-22 01:42

    Initially I used the c# wrapper for muparser. This was very fast. The only faster solution I know is exprtk. If you are looking for other solutions you can checkout the benchmark.

    But in case of .Net you can use the builtin support to compile code at runtime. The idea is to have a "template" source file as e.g. embedded resource where you can replace the formula for the evaluation. Then you pass this prepared class-source-code to the compiler.

    A basic template could look like this:

    public class CSCodeEvaler
    {
        public double EvalCode()
        {
            return last = Convert.ToDouble(%formula%);
        }
    
        public double last = 0;
        public const double pi = Math.PI;
        public const double e = Math.E;
        public double sin(double value) { return Math.Sin(value); }
        public double cos(double value) { return Math.Cos(value); }
        public double tan(double value) { return Math.Tan(value); }
        ...
    

    Notice the %formula% where the expression will be put in.

    To compile use the class CSharpCodeProvider. I do not want to put in the complete source here. But this answer might help:

    After you have the in memory assembly loaded you can create an instance of your class and call EvalCode.

    0 讨论(0)
  • 2020-11-22 01:42

    Recently I was using mXparser, which is a math parser library for .NET and JAVA. mXparser supports basic formulas as well as very fancy / complicated ones (including variables, functions, operators, iteration and recursion).

    https://mxparser.codeplex.com/

    https://mathparser.org/

    A few usage examples:

    Example 1:

    Expression e = new Expression("1+2*7 + (sin(10) - 2)/3");
    double v = e.calculate();
    

    Example 2:

    Argument x = new Argument("x = 5");
    Expression e = new Expression("2*x+3", x);
    double v = e.calculate();
    

    Example 3:

    Function f = new Function("f(x,y) = sin(x) / cos(y)");
    Expression e = new Expression("f(pi, 2*pi) - 2", f);
    double v = e.calculate();
    

    Found recently - in case you would like to try the syntax (and see the advanced use case) you can download the Scalar Calculator app that is powered by mXparser.

    Best regards

    0 讨论(0)
提交回复
热议问题