Luca
Luca

Reputation: 11961

How can I evaluate a math expression represented by a string?

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?

Upvotes: 4

Views: 2376

Answers (6)

fishhead
fishhead

Reputation: 6109

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

Upvotes: 0

Chris Haas
Chris Haas

Reputation: 55417

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

Microsoft.JScript.Eval.JScriptEvaluate

Upvotes: 0

dev-null-dweller
dev-null-dweller

Reputation: 29462

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

Upvotes: 0

Dynami Le Savard
Dynami Le Savard

Reputation: 5036

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.

Upvotes: 2

Timothy S. Van Haren
Timothy S. Van Haren

Reputation: 8966

The dotMath library does this.

Upvotes: 4

pm100
pm100

Reputation: 50120

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

Upvotes: 1

Related Questions