Math Pars expression

Is there a simple way to parse a simple math expression represented as a string, for example (x + (2 * x) / (1-x)), provide a value for x and get the result?

I looked at VSAEngine for a few online examples, but I get a warning that this assembly is deprecated and not using it.

If that makes any difference, I am using .NET 4.0.

+7
source share
9 answers

You can try using DataTable.Compute .

Associated with it is DataColumn.Expression .

Also check: Doing math in vb.net as an Eval in javascript

Note: I did not use them myself.

+5
source share

I urge you to caution against choosing an existing generalized expression evaluator over the target mathematical evaluator. The reason for this is that expression evaluators are not limited to mathematics. A smart person can use this to instantiate any type in the structure and call any method in the type, and this will allow him to do some obviously undesirable things. For example: new System.Net.WebClient().DownloadFile("illegalchildpornurl", "C:\openme.gif") in most of them will be perfectly evaluated and do what it seems (and make you a criminal at the same time).

This does not mean that you are not looking for something that has already been written. It just means being careful. You want to do math, and only math. Most of what is already there is not so legible.

+10
source share

I recently used mXparser, which is a math analyzer library. This gives you great flexibility, such as variables, functions, constants, operators. Below you will find some examples of use:

Example 1 - a simple formula

 Expression e = new Expression("1 + pi"); double v = e.calculate(); 

Example 2 - a formula with variables, functions, etc.

 Argument x = new Argument("x = 2"); Constant a = new Constant("a = sin(10)"); Function f = new Function("f(t) = t^2"); Expression e = new Expression("2*x + a - f(10)", x, a, f); double v = e.calculate(); 

https://mxparser.codeplex.com/

http://mathparser.org/

Best wishes

+5
source share

I would also look at Jace ( https://github.com/pieterderycke/Jace ). Jace is a high-performance mathematical analyzer and computing engine that supports all varieties of .NET (.NET 4.x, Windows Phone, Windows Store, ...). Jace is also available through NuGet: https://www.nuget.org/packages/Jace

+3
source share

Another opportunity you can explore is the Spring.NET Framework expression evaluation function . It can do a lot more than math.

However, the entire Spring.NET Framework may be a bit crowded for your needs if you don't need other functions.

+1
source share

Related: priority expression parser .

+1
source share

Here is one way to do it. This code is written in Java. Note that it does not handle negative numbers right now, but you can add this.

 public class ExpressionParser { public double eval(String exp, Map<String, Double> vars){ int bracketCounter = 0; int operatorIndex = -1; for(int i=0; i<exp.length(); i++){ char c = exp.charAt(i); if(c == '(') bracketCounter++; else if(c == ')') bracketCounter--; else if((c == '+' || c == '-') && bracketCounter == 0){ operatorIndex = i; break; } else if((c == '*' || c == '/') && bracketCounter == 0 && operatorIndex < 0){ operatorIndex = i; } } if(operatorIndex < 0){ exp = exp.trim(); if(exp.charAt(0) == '(' && exp.charAt(exp.length()-1) == ')') return eval(exp.substring(1, exp.length()-1), vars); else if(vars.containsKey(exp)) return vars.get(exp); else return Double.parseDouble(exp); } else{ switch(exp.charAt(operatorIndex)){ case '+': return eval(exp.substring(0, operatorIndex), vars) + eval(exp.substring(operatorIndex+1), vars); case '-': return eval(exp.substring(0, operatorIndex), vars) - eval(exp.substring(operatorIndex+1), vars); case '*': return eval(exp.substring(0, operatorIndex), vars) * eval(exp.substring(operatorIndex+1), vars); case '/': return eval(exp.substring(0, operatorIndex), vars) / eval(exp.substring(operatorIndex+1), vars); } } return 0; } 

}

You need to import java.util.Map.

Here is how I use this code:

  ExpressionParser p = new ExpressionParser(); Map vars = new HashMap<String, Double>(); vars.put("x", 2.50); System.out.println(p.eval(" 5 + 6 * x - 1", vars)); 
+1
source share

As I replied in this thread ( Best Free C # Math Parser using variables, user-defined functions, user-defined operators ), you can use Mathos Parser , which can be simply pasted into the source code.

  Mathos.Parser.MathParser parser = new Mathos.Parser.MathParser(); string expr = "(x+(2*x)/(1-x))"; // the expression decimal result = 0; // the storage of the result parser.LocalVariables.Add("x", 41); // 41 is the value of x result = parser.Parse(expr); // parsing Console.WriteLine(result); // 38.95 
0
source share

I recommend you use MEEL for this.

 // parse string to IExpression (symbolic type) IExpression expression = BaseExpression.Parse("(x+(2*x)/(1-x))"); // create your own collection for attributes var attributes = new MathAttributeCollection(); // create local variable named "x" with value 5 var attributeX = new ScalarAttrInt("x") {Value = new ScalarConstInt(5)}; attributes.Add(attributeX); // execute math expression where x=5 var result = expression.Execute(attributes); MessageBox.Show(result.GetText()); // result: 2.5 
0
source share

All Articles