6

I'm looking for a formula interpreter that I can use in a C# application. It needs to be able to interpret a string like this:

max(1+2, 4) * x

I found Writing a fast formula interpreter (codeproject.com) which almost does what I need but it doesn't allow for functions with multiple parameters. I could probably add that functionality to it but I was just wondering if something like this already exists.

Thanks

Justin
  • 84,773
  • 49
  • 224
  • 367
rob
  • 17,995
  • 12
  • 69
  • 94
  • 1
    possible duplicate: http://stackoverflow.com/questions/4629/c-eval-equivalent – Jean-Bernard Pellerin Mar 29 '11 at 22:02
  • Another potential duplicate: http://stackoverflow.com/questions/355062/is-there-a-string-math-evaluator-in-net – Andy West Mar 29 '11 at 22:04
  • 1
    Those questions are relatively old - Corbins answer already links to two projects not mentioned in either of those two questions, so I say keep the question open. – Justin Mar 29 '11 at 22:30
  • 1
    This question has been done to death. There have been no revolutionary developments in the past, oh, 10 years. – Hans Passant Mar 29 '11 at 22:52

3 Answers3

7

A couple I've used in the past with no problems:

Pontus Magnusson
  • 632
  • 1
  • 10
  • 25
Corbin March
  • 25,526
  • 6
  • 73
  • 100
1

You can actually build a very effective interpreter by parsing and replacing certain functional keywords such as max with Math.Max and then dynamically building and executing the formula as a C# class and method. So actually you would be parsing and wrapping the formula and allowing the C# compiler to interpret and execute it.

So, taking your sample formula of max(1+2, 4) * x would turn into:

public class MyFormula
{
    public double calc(double x)
    {
        return Math.Max(1+2, 4) * x;
    }
}

Which you would compile on the fly and then execute per the linked article. You still have to parse for and pass the x value of course.

Paul Sasik
  • 79,492
  • 20
  • 149
  • 189
  • This seems dangerous if certain security precautions aren't met. – Ozzah Mar 29 '11 at 22:10
  • @Ozzah: This isn't like SQL Injection. For one thing, you would limit the amount of input, and then you would be parsing for math terms such as max, sin, cos etc. Anything that's not kosher would fail as a syntax error. – Paul Sasik Mar 29 '11 at 22:13
1

A long time ago in one project i had to create some booking with formulas, and i used VsaEngine. To use this engine you need to add reference to Microsoft.JScript. Here is example:

Code for usage is very simple, just replace formula parameters like:

string formula = "x+y";
formula=  formula.Replace("x","100").Replace("y","200");
string result = CalculateFormula(formula);

And here is core method, CalculateFormula:

public string CalculateFormula(string evaluationString)
{ 
      VsaEngine en = VsaEngine.CreateEngine();
      Object result = Eval.JScriptEvaluate(evaluationString, en);
      return result.ToString();
}

With this you can create your custom formula interpreter engine.

buda
  • 2,372
  • 1
  • 18
  • 23
  • VsaEngine is a nice thing. The problem is that it is obsolete and deprecated and Microsoft states that there will be no replacement for this feature. – Elmue Dec 13 '22 at 00:54