C# 数学计算器

发布于 2024-09-02 07:41:04 字数 887 浏览 7 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

夏夜暖风 2024-09-09 07:41:04

DataTable 有一个 Compute 方法,允许您写这个:

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

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

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

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

您还可以查看GitHub 上的NCalc 库

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.

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

You may also check the NCalc library on GitHub.

千寻… 2024-09-09 07:41:04

有一些有趣的选项可供您选择。

  1. NCalc - C# 使用 ANTLR 构建的词法分析器。这将解析您的文本并允许您为参数/变量分配值。解释器是 C#,因此您不必在应用程序域等中加载其他程序集。

  2. JINT - 由 ECalc 的同一作者使用 ANTLR 创建语法的基于 C# 的 Javascript 解释器。目前尚处于测试阶段,但可以很好地处理计算和函数。

  3. 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.

  1. 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.

  2. 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.

  3. 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.

两个我 2024-09-09 07:41:04

查看 FLEE(快速轻量级表达式评估器) - http://flee.codeplex.com/

Flee 是 .NET 框架的表达式解析器和求值器。它允许您在运行时计算字符串表达式的值,例如 sqrt(a^2 + b^2)。它使用自定义编译器、强类型表达式语言和轻量级代码生成器将表达式直接编译为 IL。这意味着表达式求值极其快速且高效。尝试一下演示,它可以让您根据表达式生成图像,并亲自查看。

它是免费且快速的,我已经在几个项目中使用了它。

Check out FLEE (Fast Lightweight Expression Evaluator) - http://flee.codeplex.com/

Flee is an expression parser and evaluator for the .NET framework. It allows you to compute the value of string expressions such as sqrt(a^2 + b^2) at runtime. It uses a custom compiler, strongly-typed expression language, and lightweight codegen to compile expressions directly to IL. This means that expression evaluation is extremely fast and efficient. Try out the demo, which lets you generate images based on expressions, and see for yourself.

It's free and fast and I've used it in a couple of projects.

森林迷了鹿 2024-09-09 07:41:04

注意:这个答案只是为了完整性。这绝对不是我推荐的方法。

可以直接从 C# 访问(已弃用的)JScript 库,这意味着您可以使用 JScript 的 eval 函数的等效功能。

using Microsoft.JScript;        // needs a reference to Microsoft.JScript.dll
using Microsoft.JScript.Vsa;    // needs a reference to Microsoft.Vsa.dll

// ...

string expr = "2 - 3 / 4 * 12";
Console.WriteLine(JScriptEval(expr));    // displays -7

// ...

public static VsaEngine _engine = VsaEngine.CreateEngine();

public static double JScriptEval(string expr)
{
    // error checking etc removed for brevity

    return double.Parse(Eval.JScriptEvaluate(expr, _engine).ToString());
}

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.

using Microsoft.JScript;        // needs a reference to Microsoft.JScript.dll
using Microsoft.JScript.Vsa;    // needs a reference to Microsoft.Vsa.dll

// ...

string expr = "2 - 3 / 4 * 12";
Console.WriteLine(JScriptEval(expr));    // displays -7

// ...

public static VsaEngine _engine = VsaEngine.CreateEngine();

public static double JScriptEval(string expr)
{
    // error checking etc removed for brevity

    return double.Parse(Eval.JScriptEvaluate(expr, _engine).ToString());
}
沫尐诺 2024-09-09 07:41:04

绝对属于“不推荐”类别,但为了完整起见 - 如果您有一个可以方便连接的数据库,请向其发送查询“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".

谢绝鈎搭 2024-09-09 07:41:04

呃,这似乎是一个非常夸张的解决方案。

你真正想要的是一个简单的解析器。

您需要将字符串分解为标记,然后对它们进行评估。这将帮助您开始研究。
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

素染倾城色 2024-09-09 07:41:04

最好的选择是构建表达式树。首先构建表达式树,然后使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文