有没有在.NET中字符串的数学评估? [英] Is there a string math evaluator in .NET?
问题描述
如果我有一个有效的数学EX pression如字符串:
If I have a string with a valid math expression such as:
String s = "1 + 2 * 7";
有没有在.NET中,将分析和评估前pression我和返回结果,一个内置的库/函数?在这种情况下,15
Is there a built in library/function in .NET that will parse and evaluate that expression for me and return the result? In this case 15.
推荐答案
您可以添加到Microsoft脚本控制库(COM),并使用code引用这样来评价一个前pression。 (也可用于JScript的。)
You could add a reference to Microsoft Script Control Library (COM) and use code like this to evaluate an expression. (Also works for JScript.)
Dim sc As New MSScriptControl.ScriptControl()
sc.Language = "VBScript"
Dim expression As String = "1 + 2 * 7"
Dim result As Double = sc.Eval(expression)
修改 - C#版
MSScriptControl.ScriptControl sc = new MSScriptControl.ScriptControl();
sc.Language = "VBScript";
string expression = "1 + 2 * 7";
object result = sc.Eval(expression);
MessageBox.Show(result.ToString());
修改 - 该ScriptControl的是一个COM对象。在该项目的添加引用对话框中选择COM选项卡,然后向下滚动到Microsoft脚本控制1.0,并选择确定。
Edit - The ScriptControl is a COM object. In the "Add reference" dialog of the project select the "COM" tab and scroll down to "Microsoft Script Control 1.0" and select ok.
这篇关于有没有在.NET中字符串的数学评估?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!