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

问题描述

可能显示的文件:结果
  

  <一href=\"http://stackoverflow.com/questions/2607798/converting-string-ex$p$pssion-to-integer-value-using-c\">Converting使用C#结果字符串前pression为整数值
  <一href=\"http://stackoverflow.com/questions/1437964/best-and-shortest-way-to-evaluate-mathematical-ex$p$pssions\">Best和最短的方法来评估数学EX pressions 结果
  

有没有办法在比这里psented $ P $以不同的方式来计算数学EX pressions像(2-3 / 4 * 12)?

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

<一个href=\"http://www.c-sharpcorner.com/uploadfile/mgold/$c$cdomcalculator08082005003253am/$c$cdomcalculator.aspx\">http://www.c-sharpcorner.com/uploadfile/mgold/$c$cdomcalculator08082005003253am/$c$cdomcalculator.aspx

推荐答案

DataTable中有一个的,使您可以这样写:

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

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

请注意,这是仅限于简单的数学EX pressions。

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

您还可以检查codePLEX 的 NCalc库。

You may also check the NCalc library on CodePlex.

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