如何计算由字符串表示的数学表达式?

发布于 2024-08-21 02:33:44 字数 81 浏览 9 评论 0原文

实现一个“计算器”来解析字符串(例如,2 ^ 3 / 2)并计算运算结果很容易。但是,是否有一个图书馆已经能够做到这一点?

It's easy to implement a "Calculator" to parse a string (e.g., 2 ^ 3 / 2) and compute the result of operations. But, is there a library already capable of doing this?

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

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

发布评论

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

评论(6

渡你暖光 2024-08-28 02:33:44

dotMath 库可以执行此操作。

The dotMath library does this.

花想c 2024-08-28 02:33:44

为了做到这一点,你将需要某种数学解析器。我之前使用过 使用 DeepEddie 的 RPN 的 C# 表达式解析器,或者你也可以如果您使用的表达式的复杂性范围更有限,请自行创建。

不要让它吓到你,它实际上很容易制作。

You are going to need some kind of math parser in order to do that. I've used C# Expression Parser using RPN by DeepEddie before, or you could make your own if the complexity of the expressions you use are of more limited scope.

Don't let it scare you, it is actually quite easy to make.

愁杀 2024-08-28 02:33:44

将ironpython嵌入到您的应用程序中,然后您可以要求它评估任意复杂的字符串,

我认为它们甚至有相同事物的样本

embed ironpython in your app, you can then ask it to evaluate arbitrarily complex strings

i think they even have a sample of the same thing

柏拉图鍀咏恒 2024-08-28 02:33:44

查看逆波兰表示法。它广泛用于现代计算器

Check out Reverse Polish notation. It's widely used in modern calculators

淡淡離愁欲言轉身 2024-08-28 02:33:44

您还可以使用 JScript 库,尽管它已被弃用。不是说你应该,只是说你可以。

Microsoft.JScript.Eval.JScriptEvaluate

You can also use the JScript library although its deprecated. Not saying you should, just that you could.

Microsoft.JScript.Eval.JScriptEvaluate

苍白女子 2024-08-28 02:33:44

我用过这个:

using System;
using System.CodeDom.Compiler;
using System.Reflection;
using Microsoft.JScript;


  public class JScriptEvaluator
  {
        public   int EvalToInteger(string statement)
        {
              string s = EvalToString(statement);
              return int.Parse(s.ToString());
        }

        public   double EvalToDouble(string statement)
        {
              string s = EvalToString(statement);
              return double.Parse(s);
        }

        public   string EvalToString(string statement)
        {
            object o = "-1";
            try
            {
             o=  EvalToObject(statement);
            }
            catch { o = "-1"; }
              return o.ToString();
        }

        public   object EvalToObject(string statement)
        {
              return _evaluatorType.InvokeMember(
                                "Eval",
                                BindingFlags.InvokeMethod,
                                null,
                                _evaluator,
                                new object[] { statement }
                          );
        }

        public JScriptEvaluator()
        {
              CodeDomProvider provider = new Microsoft.JScript.JScriptCodeProvider();

              CompilerParameters parameters;
              parameters = new CompilerParameters();
              parameters.GenerateInMemory = true;

              CompilerResults results;
              results = provider.CompileAssemblyFromSource(parameters, _jscriptSource);

              Assembly assembly = results.CompiledAssembly;
              _evaluatorType = assembly.GetType("Evaluator.Evaluator");

              _evaluator = Activator.CreateInstance(_evaluatorType);
        }

        private   object _evaluator = null;
        private   Type _evaluatorType = null;
        private   readonly string _jscriptSource =

              @"package Evaluator
              {
                 class Evaluator
                 {
                       public function Eval(expr : String) : String 
                       { 
                          return eval(expr); 
                       }
                 }
              }";
  }

I have used this :

using System;
using System.CodeDom.Compiler;
using System.Reflection;
using Microsoft.JScript;


  public class JScriptEvaluator
  {
        public   int EvalToInteger(string statement)
        {
              string s = EvalToString(statement);
              return int.Parse(s.ToString());
        }

        public   double EvalToDouble(string statement)
        {
              string s = EvalToString(statement);
              return double.Parse(s);
        }

        public   string EvalToString(string statement)
        {
            object o = "-1";
            try
            {
             o=  EvalToObject(statement);
            }
            catch { o = "-1"; }
              return o.ToString();
        }

        public   object EvalToObject(string statement)
        {
              return _evaluatorType.InvokeMember(
                                "Eval",
                                BindingFlags.InvokeMethod,
                                null,
                                _evaluator,
                                new object[] { statement }
                          );
        }

        public JScriptEvaluator()
        {
              CodeDomProvider provider = new Microsoft.JScript.JScriptCodeProvider();

              CompilerParameters parameters;
              parameters = new CompilerParameters();
              parameters.GenerateInMemory = true;

              CompilerResults results;
              results = provider.CompileAssemblyFromSource(parameters, _jscriptSource);

              Assembly assembly = results.CompiledAssembly;
              _evaluatorType = assembly.GetType("Evaluator.Evaluator");

              _evaluator = Activator.CreateInstance(_evaluatorType);
        }

        private   object _evaluator = null;
        private   Type _evaluatorType = null;
        private   readonly string _jscriptSource =

              @"package Evaluator
              {
                 class Evaluator
                 {
                       public function Eval(expr : String) : String 
                       { 
                          return eval(expr); 
                       }
                 }
              }";
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文