如何评估(数学)字符串表达式,vb.net [英] how to evaluate ( math ) a string expression, vb.net

查看:28
本文介绍了如何评估(数学)字符串表达式,vb.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定我使用的术语是否正确.但我有一个字符串表达式,我想计算它.这是一个例子

I'm not sure the term I used is correct. but I have a string expression and I want it to be calculated. this an example

Dim S = "4+4"
dim result = evaluate(S) 'some sort of treatment // that return 8

我不确定这将如何运作.我熟悉 JS eval 函数.但似乎我需要添加某种图书馆.我不想那样做.我找到了一些关于使用

I'm not sure how this is going to work. I'm familiar with the JS eval function. but it seems that I need to add some sort of a library. and I don't want to do that. I have found some links about using

dim s = new expression("4+4") 

并使用

s.evaluate()

但这需要添加另一个库.正如我之前所说,我不想使用任何图书馆.我只想要一个如何进行的解决方案?我撞墙了.顺便说一句,我仍然是初学者,请尽量回答简单,我将不胜感激.

but that require to add another library. and as I said before I don't want to use any library. I just want a solution on how to proceed? I have hit a wall. BTW I'm still a beginner try to answer as simple as you can I would appreciate it.

推荐答案

您可以使用 DataTable.Compute-技巧":

You can use the DataTable.Compute-"trick":

Dim tbl = new DataTable()
Dim result = Convert.ToDouble(tbl.Compute("4+4", Nothing))

表达式支持以下算术运算符:

The following arithmetic operators are supported in expressions:

+ (addition)
- (subtraction)
* (multiplication)
/ (division)
% (modulus)

更多信息:DataColumn.Expression表达式语法.

这篇关于如何评估(数学)字符串表达式,vb.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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