在 vb.net 中做数学就像在 javascript 中做 Eval [英] Doing math in vb.net like Eval in javascript

查看:20
本文介绍了在 vb.net 中做数学就像在 javascript 中做 Eval的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以解析 vb.net 中的字符串(例如内置方法),可以像 Eval 一样进行数学运算?例如,3+(7/3.5) 作为字符串将返回 2.

我不是要您为我编写代码,我只是想知道是否有内置方法可以做到这一点,如果没有,我会自己编写代码.>

我敢打赌它不能自己解析像 Sin(90) 这样的东西,我知道这需要用 Math.Sin(90) 代替.

如果有内置方法,你如何使用它?

解决方案

使用 DataTable.Compute 方法.显然,这不是健壮的(功能有限),并且为此目的滥用 DataTable 感觉很糟糕,但我想我会添加到当前的答案中.

示例:

var result = new DataTable().Compute("3+(7/3.5)", null);//5

"Sin(90)" 不适用于这种方法.请参阅DataColumn.Expression 属性页获取支持的函数列表,特别是在聚合"部分下.

使用 System.CodeDom 命名空间是一种选择.

一些有用的链接:

<小时>

为了解决您的评论,这里有一种方法来演示用等效的 Math 类方法替换三角函数.

C#

字符串表达式 = "(Sin(0) + Cos(0)+Tan(0)) * 10";string updatedExpression = Regex.Replace(expression, @"(?Sin|Cos|Tan)((?.*?))", match =>match.Groups["func"].Value == "Sin" ?Math.Sin(Int32.Parse(match.Groups["arg"].Value)).ToString():match.Groups["func"].Value == "Cos" ?Math.Cos(Int32.Parse(match.Groups["arg"].Value)).ToString():Math.Tan(Int32.Parse(match.Groups["arg"].Value)).ToString());var result = new DataTable().Compute(updatedExpression, null);//10

VB.NET

Dim expression As String = "(Sin(0) + Cos(0)+Tan(0)) * 10"Dim updatedExpression As String = Regex.Replace(expression, "(?Sin|Cos|Tan)((?.*?))", Function(match As Match) _If(match.Groups("func").Value = "Sin", Math.Sin(Int32.Parse(match.Groups("arg").Value)).ToString(), _If(match.Groups("func").Value = "Cos", Math.Cos(Int32.Parse(match.Groups("arg").Value)).ToString(), _Math.Tan(Int32.Parse(match.Groups("arg").Value)).ToString())) _)Dim 结果 = New DataTable().Compute(updatedExpression,Nothing)

但是请注意,您需要知道arg"组的内容.我知道它们是整数,所以我对它们使用了 Int32.Parse.如果它们是项目的组合,那么这种简单的方法将不起作用.我怀疑如果解决方案因更多不受支持的函数调用而变得过于复杂,您将不断需要创可贴,在这种情况下,CodeDom 方法或其他方法可能更合适.

Is there any way to parse a string in vb.net (like, built in methods), that can do math like Eval can? For example, 3+(7/3.5) as a string would return 2.

I am not asking for you to code this for me, I just want to know if there is a built in way to do this, if there is not I will code it myself.

I can wager that it would not be able to parse stuff like Sin(90) on its own, and I understand that would need to be replaced by Math.Sin(90).

If there is a built in method, how do you use it?

解决方案

There's a shortcut for limited (ie. simple) math expressions by using the DataTable.Compute method. Obviously, this isn't robust (limited functionality) and feels hackish to misuse the DataTable for this purpose, but I figured I would add to the current answers.

Example:

var result = new DataTable().Compute("3+(7/3.5)", null); // 5

"Sin(90)" wouldn't work with this approach. Refer to the DataColumn.Expression Property page for a list of supported functions, specifically under the "Aggregates" section.

Using the System.CodeDom namespace is an option.

Some helpful links:


EDIT: to address your comment, here is an approach to demonstrate replacing trigonometric functions with their equivalent Math class methods.

C#

string expression = "(Sin(0) + Cos(0)+Tan(0)) * 10";
string updatedExpression = Regex.Replace(expression, @"(?<func>Sin|Cos|Tan)((?<arg>.*?))", match =>
            match.Groups["func"].Value == "Sin" ? Math.Sin(Int32.Parse(match.Groups["arg"].Value)).ToString() :
            match.Groups["func"].Value == "Cos" ? Math.Cos(Int32.Parse(match.Groups["arg"].Value)).ToString() :
            Math.Tan(Int32.Parse(match.Groups["arg"].Value)).ToString()
        );
var result = new DataTable().Compute(updatedExpression, null); // 10

VB.NET

Dim expression As String = "(Sin(0) + Cos(0)+Tan(0)) * 10"
Dim updatedExpression As String = Regex.Replace(expression, "(?<func>Sin|Cos|Tan)((?<arg>.*?))", Function(match As Match) _
        If(match.Groups("func").Value = "Sin", Math.Sin(Int32.Parse(match.Groups("arg").Value)).ToString(), _
        If(match.Groups("func").Value = "Cos", Math.Cos(Int32.Parse(match.Groups("arg").Value)).ToString(), _
        Math.Tan(Int32.Parse(match.Groups("arg").Value)).ToString())) _
        )
Dim result = New DataTable().Compute(updatedExpression, Nothing)

Note, however, that you need to know the contents of the "arg" group. I know they are ints, so I used Int32.Parse on them. If they are a combination of items then this simple approach won't work. I suspect you will constantly need to band-aid the solution if it gets too complicated with more unsupported function calls, in which case the CodeDom approach or others may be more suitable.

这篇关于在 vb.net 中做数学就像在 javascript 中做 Eval的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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