c# - 字符串计算器

标签 c# console-application calculator

<分区>

各位程序员好,

我正在用 C# 创建一个计算器

我有一个字符串变量 math,其中包含 100 * 5 - 2

如何在我的控制台中显示其输出 498

我的代码是这样的:

String math = "100 * 5 - 2"; 

Console.WriteLine(math);

Console.ReadLine(); // For Pause

所以基本上,我的代码会给我的是 100 * 5 - 2

的字符串本身

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

关于这个的想法非常感谢。

谢谢

最佳答案

可以使用 DataTable.Compute 方法(来自 MSDN)完成正则表达式计算:

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();

关于c# - 字符串计算器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21950093/

相关文章:

c# - 在控制台应用程序下找不到 System.drawing 命名空间

c# - C#控制台应用程序,无法隐式将 'double'类型转换为 'string'吗?

c# - 在 Windows 10 中将计算器窗口置于前面

python - 如何在QLCD数字屏上显示多个数字?

c# - 在浏览器中以 45 度 Angular 显示文本的选项

c# - 没有方法和参数描述的 Visual Studio 2013 智能感知

c# - NPGSQL with .netCore 如何登录到模式而不仅仅是数据库

.net - 如何在 C# 中使用操作动词进行命令行解析?

c# - 可以用 C# 中的用户输入计算年龄,但不知道为什么会这样

c# - 错误 MSB3027 : Could not copy "C:\pagefile.sys" to "bin\roslyn\pagefile.sys". 超过重试次数 10。失败