C#中有一个eval函数吗? [英] Is there an eval function In C#?

查看:89
本文介绍了C#中有一个eval函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一个方程式输入到TextBox中,该方程式将生成给定抛物线的图形。可以使用eval函数吗?我使用的是C#2010,它没有Microsoft.JScript

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 Microsoft.JScript

推荐答案

C#没有类似的Eval函数,但创建一个真的很简单:

C# doesn't have a comparable Eval function but creating one is really easy:

public static double Evaluate(string expression)  
       {  
           System.Data.DataTable table = new System.Data.DataTable();  
           table.Columns.Add("expression", string.Empty.GetType(), expression);  
           System.Data.DataRow row = table.NewRow();  
           table.Rows.Add(row);  
           return double.Parse((string)row["expression"]);  
       }

现在简单地这样称呼:

Console.WriteLine(Evaluate("9 + 5"));

这篇关于C#中有一个eval函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆