评估数学EX pressions最佳和最短的路 [英] Best and shortest way to evaluate mathematical expressions

查看:184
本文介绍了评估数学EX pressions最佳和最短的路的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多算法来评估EX pressions,例如:

There are many algorithms to evaluate expressions, for example:

  1. 通过递归下降
  2. 调度场算法
  3. 逆波兰式
  1. By Recursive Descent
  2. Shunting-yard algorithm
  3. Reverse Polish notation

有没有办法来评价使用C#.NET的反射或其他现代.NET技术的所有数学EX pression?

Is there any way to evaluate any mathematical expression using C# .net reflection or other modern .net technology?

推荐答案

继托马斯的答案,它实际上可以访问(DE precated)的JScript库直接从C#中,这意味着您可以使用JScript中的等效评估的功能。

Further to Thomas's answer, it's actually possible to access the (deprecated) JScript libraries directly from C#, which means 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 = "7 + (5 * 4)";
Console.WriteLine(JScriptEval(expr));    // displays 27

// ...

public static double JScriptEval(string expr)
{
    // error checking etc removed for brevity
    return double.Parse(Eval.JScriptEvaluate(expr, _engine).ToString());
}

private static readonly VsaEngine _engine = VsaEngine.CreateEngine();

这篇关于评估数学EX pressions最佳和最短的路的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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