在vb.net中进行数学运算,例如javascript中的Eval [英] Doing math in vb.net like Eval in javascript
问题描述
有什么方法可以解析vb.net中的字符串(例如,内置方法),可以像Eval一样进行数学运算吗?例如,3+(7/3.5)作为字符串将返回2.
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.
我敢打赌它将无法自行解析诸如Sin(90)之类的东西,而且我知道需要将其替换为Math.Sin(90).
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.
示例:
var result = new DataTable().Compute("3+(7/3.5)", null); // 5
"Sin(90)"不适用于此方法.请参考 DataColumn.Expression属性页有关受支持功能的列表,特别是在聚合"部分下.
"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.
使用System.CodeDom命名空间是一种选择.
Using the System.CodeDom namespace is an option.
一些有用的链接:
- CodeDom计算器
- 使用CodeDom评估数学表达式
- 相关的SO问题: .NET中是否存在字符串数学评估器?
- CodeDom Calculator
- Evaluating mathematical expressions using CodeDom
- Related SO question: Is there a string math evaluator in .NET?
以解决您的评论,这是一种演示用等效的Math类方法替换三角函数的方法.
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)
但是,请注意,您需要了解"arg"组的内容.我知道它们是整数,因此我在它们上使用了Int32.Parse.如果它们是项目的组合,那么这种简单的方法将行不通.我怀疑如果由于无法支持更多函数调用而使解决方案变得过于复杂,那么您将经常需要对这些解决方案进行创可贴.在这种情况下,使用CodeDom方法或其他方法可能更合适.
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屋!