.NET中有字符串数学评估器吗?(Is there a string math evaluator in .NET?)

编程入门 行业动态 更新时间:2024-10-23 09:26:27
.NET中有字符串数学评估器吗?(Is there a string math evaluator in .NET?)

如果我有一个具有有效数学表达式的字符串,例如:

String s = "1 + 2 * 7";

在.NET中有内置的库/函数,将解析和评估该表达式并返回结果? 在这种情况下15。

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 parse and evaluate that expression for me and return the result? In this case 15.

最满意答案

您可以添加对Microsoft脚本控制库(COM)的引用,并使用此代码来评估表达式。 (也适用于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 Script Control 1.0”,然后选择“确定”。

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)

Edit - C# version.

MSScriptControl.ScriptControl sc = new MSScriptControl.ScriptControl(); sc.Language = "VBScript"; string expression = "1 + 2 * 7"; object result = sc.Eval(expression); MessageBox.Show(result.ToString());

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.

更多推荐

本文发布于:2023-08-07 19:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465970.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中有   字符串   数学   NET   math

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!