串计算器 [英] String calculator

查看:284
本文介绍了串计算器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

喜同胞的程序员,

我创建的 C#

和我有一个字符串变量数学包含 100 * 5 - 2

and I have a string variable math which contains 100 * 5 - 2

我怎样才能显示它的输出是 498 在我的控制台?

How can I display its output which is 498 in my console?

我的代码是这样的:

String math = "100 * 5 - 2"; 

Console.WriteLine(math);

Console.ReadLine(); // For Pause



所以基本上,我的代码会给我是<$ c中的字符串本身$ C> 100 * 5 - 2

但我希望它给我 498 作为一个结果。

but I want it to give me 498 as a result.

有关这种想法是非常赞赏。

Idea about this is pretty much appreciated.

感谢

推荐答案

正则表达式的评价可以用数据表的计算方法来实现。

Regular Expression evaluation can be done using DataTable COmpute method.

从MSDN:<一HREF =http://msdn.microsoft.com/en-us/library/system.data.datatable.compute%28v=vs.110%29.aspx相对=nofollow> DataTable.Compute()

计算上通过过滤器
条件的当前行中给定的表达式。

Computes the given expression on the current rows that pass the filter criteria.

试试这个:

using System.Data;//import this namespace

 string math = "100 * 5 - 2";
 string value = new DataTable().Compute(math, null).ToString();

这篇关于串计算器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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