Is there an eval function In C#?

前端 未结 7 1940
[愿得一人]
[愿得一人] 2020-12-05 03:41

I\'m typing an equation into a TextBox that will generate the graph of the given parabola. Is it possible to use an eval function? I\'m using C# 2010 and it doesn\'t have Mi

7条回答
  •  旧时难觅i
    2020-12-05 04:11

    You can easily do this with the "Compute" method of the DataTable class.

    static Double Eval(String expression)
    {
        System.Data.DataTable table = new System.Data.DataTable();
        return Convert.ToDouble(table.Compute(expression, String.Empty));
    }
    

    Pass a term in form of a string to the function in order to get the result.

    Double result = Eval("7 * 6");
    result = Eval("17 + 4");
    ...
    

提交回复
热议问题