C# 数学计算器 [英] C# Math calculator

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

问题描述

可能的重复:
.NET 中是否有字符串数学计算器?
使用 C# 将字符串表达式转换为整数值
评估数学表达式的最佳和最短方法
c#求值字符串“3*(4+2)”产量 int 18

有没有一种方法可以用不同于这里介绍的方式来计算 (2-3/4*12) 这样的数学表达式?

Is there a way to calculate math expressions like (2-3/4*12) in a different way than presented here?

http://www.c-sharpcorner.com/uploadfile/mgold/codedomcalculator08082005003253am/codedomcalculator.aspx

推荐答案

DataTable 有一个 计算 方法,允许你写这个:

DataTable has a Compute method that allows you to write this:

var result = new DataTable().Compute("2-3/4*12", null);

请注意,这仅限于简单的数学表达式.

Note that this is limited to simple math expressions.

其他选项包括在 DLR 中使用动态语言,例如 IronPython 和 IronRuby.查看这篇文章:

Other option consist in using a dynamic language in the DLR such as IronPython and IronRuby. Check-out this post:

var engine = new IronPython.Hosting.PythonEngine();
double result = pythonEngine.EvaluateAs<double>("2-3/4*12");

您也可以查看 GitHub 上的 NCalc 库.

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

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