Is there a string math evaluator in .NET?

前端 未结 16 1449
长情又很酷
长情又很酷 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:36

    Flee Fast Lightweight Expression Evaluator

    https://flee.codeplex.com

    Language Reference

    • ArithmeticOperators Example: a*2 + b ^ 2 - 100 % 5
    • ComparisonOperators Example: a <> 100
    • AndOrXorNotOperators Example (logical): a > 100 And Not b = 100
    • ShiftOperators Example: 100 >> 2
    • Concatenation Example: "abc" + "def"
    • Indexing Example: arr[i + 1] + 100
    • Literals
    • Casting Example: 100 + cast(obj, int)
    • ConditionalOperator Example: If(a > 100 and b > 10, "both greater", "less")
    • InOperator Example (List): If(100 in (100, 200, 300, -1), "in", "not in")
    • Overloaded Operators On Types

    Example :

    Imports Ciloci.Flee
    Imports Ciloci.Flee.CalcEngine
    Imports System.Math
    

        Dim ec As New Ciloci.Flee.ExpressionContext
        Dim ex As IDynamicExpression
        ec.Imports.AddType(GetType(Math))
    
        ec.Variables("a") = 10            
        ec.Variables("b") = 40               
        ex = ec.CompileDynamic("a+b")
    
        Dim evalData    
        evalData = ex.Evaluate()
        Console.WriteLine(evalData)
    

    The output : 50

提交回复
热议问题