C# 数学计算器
可能的重复:
.NET 中有字符串数学计算器吗?
使用 C# 将字符串表达式转换为整数值
计算数学表达式的最佳和最短方法
c# 计算字符串“3*(4+2)”产量 int 18
有没有一种方法可以以与此处介绍的方式不同的方式计算 (2-3/4*12) 等数学表达式?
Possible Duplicates:
Is there a string math evaluator in .NET?
Converting string expression to Integer Value using C#
Best and shortest way to evaluate mathematical expressions
c# evaluating string “3*(4+2)” yield int 18
Is there a way to calculate math expressions like (2-3/4*12) in a different way than presented here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
DataTable 有一个 Compute 方法,允许您写这个:
请注意,这仅限于简单的数学表达式。
其他选项包括在 DLR 中使用动态语言,例如 IronPython 和 IronRuby。查看这篇文章 :
您还可以查看GitHub 上的NCalc 库。
DataTable has a Compute method that allows you to write this:
Note that this is limited to simple math expressions.
Other option consist in using a dynamic language in the DLR such as IronPython and IronRuby. Check-out this post:
You may also check the NCalc library on GitHub.
有一些有趣的选项可供您选择。
NCalc - C# 使用 ANTLR 构建的词法分析器。这将解析您的文本并允许您为参数/变量分配值。解释器是 C#,因此您不必在应用程序域等中加载其他程序集。
JINT - 由 ECalc 的同一作者使用 ANTLR 创建语法的基于 C# 的 Javascript 解释器。目前尚处于测试阶段,但可以很好地处理计算和函数。
CS-Script.Net - 来自网站:“CS-Script 是一个 CLR(公共语言运行时) )基于脚本系统,使用符合 ECMA 标准的 C# 作为编程语言,目前以 Microsoft 的 CLR 实现 (.NET 2.0/3.0/3.5) 为目标,对 Mono 的支持有限。”加载脚本并在内存和单独的应用程序域中创建程序集。它非常强大,我在生产中使用它来进行嵌入式脚本编写。
There are some interesting options available to you.
NCalc - a C# Lexer Parser built with ANTLR. This will parse your text and allow you to assign values to parameters / variables. The interpreter is C#, so you do not have to load additional assemblies in an app domain, etc.
JINT - a C# based Javascript interpreter built by the same author of ECalc using ANTLR to create the grammar. This is currently in beta but works well with calculations and functions.
CS-Script.Net - From the site: "CS-Script is a CLR (Common Language Runtime) based scripting system which uses ECMA-compliant C# as a programming language. CS-Script currently targets Microsoft implementation of CLR (.NET 2.0/3.0/3.5) with limited support on Mono." The load scripts and create assemblies in memory and separate app domain. It is quite robust, and I use it in production for embedded scripting.
查看 FLEE(快速轻量级表达式评估器) - http://flee.codeplex.com/
它是免费且快速的,我已经在几个项目中使用了它。
Check out FLEE (Fast Lightweight Expression Evaluator) - http://flee.codeplex.com/
It's free and fast and I've used it in a couple of projects.
注意:这个答案只是为了完整性。这绝对不是我推荐的方法。
可以直接从 C# 访问(已弃用的)JScript 库,这意味着您可以使用 JScript 的
eval
函数的等效功能。NB: This answer is just for completeness. It's definitely not an approach that I'd recommend.
It's possible to access the (deprecated) JScript libraries directly from C#, which means that you can use the equivalent of JScript's
eval
function.绝对属于“不推荐”类别,但为了完整起见 - 如果您有一个可以方便连接的数据库,请向其发送查询“SELECT 表达式”。
Definitely in the "do not recommend" category, but for completeness -- if you've got a database you can conveniently connect to, send it the query "SELECT expression".
呃,这似乎是一个非常夸张的解决方案。
你真正想要的是一个简单的解析器。
您需要将字符串分解为标记,然后对它们进行评估。这将帮助您开始研究。
http://en.wikipedia.org/wiki/Parsing#Overview_of_process
Uh that seems like a very over the top solution.
What you really want is a simple parser.
You need to break the string up into tokens and then evaluate them. This will get you started researching.
http://en.wikipedia.org/wiki/Parsing#Overview_of_process
最好的选择是构建表达式树。首先构建表达式树,然后使用 http:// msdn.microsoft.com/en-us/library/system.linq.expressions.aspx 您可以使用 http://msdn.microsoft.com/en-us/library/bb356928(v=VS.100).aspx
best option is to build Expression tree. First you build tree of your expression, then use http://msdn.microsoft.com/en-us/library/system.linq.expressions.aspx you can compile it then easily using http://msdn.microsoft.com/en-us/library/bb356928(v=VS.100).aspx