Opensouce C/C++ Math Expression Parser Library

Open source mathematical expression parser?

I would take a look at NCalc. Here is a description of the project:

NCalc is a mathematical expressions evaluator in .NET. NCalc can parse any expression and evaluate the result, including static or dynamic parameters and custom functions. For additional information on the technique we used to create this framework please read this article: http://www.codeproject.com/KB/recipes/sota_expression_evaluator.aspx

If this is too complex for your purpose, you can take a look at any "parser generator" for C#. These tools allow you to specify the grammar of your expression and generate code that will parse it. Writing parser for numerica expressions should be pretty straightforward. See for example

  • Five minute introduction to ANTLR
  • You could also try using F#, which is a great language for this kind of problem.

    See for example FsLex Sample by Chris Smith

What is the best way to evaluate mathematical expressions in C++?

Boost.Spirit is a C++ parser library.

Examples:

  • in its distribution: classic version and current version (look for "calc");
  • on Rosetta wiki;
  • some applications using it.

parsing math expression in c++

Use the Shunting-yard algorithm. The wikipedia description is quite comprehensive, I hope it will suffice.

You can also try to write a formal grammar, for example a parsing-expression grammar, and use a tool to generate a parser. This site about PEGs lists 3 C/C++ libraries for PEG parsing.

How to evaluate a math expression given in string form?

With JDK1.6, you can use the built-in Javascript engine.

import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
import javax.script.ScriptException;

public class Test {
public static void main(String[] args) throws ScriptException {
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
String foo = "40+2";
System.out.println(engine.eval(foo));
}
}

Java math expression parser that can take complex numbers as a variable?

As mentioned by PhiLo, you can use generics. Try this Processing sketch:

import java.util.*;
java.util.List<String> list = Arrays.asList("a", "b", "c");
textFont(loadFont("UMingCN-30.vlw"));
for(int i = 0; i < list.size(); i++) {
text(list.get(i), 5, int(i*30)+30);
}

And there's a non commercial version of JEP available (GPL). Download it here and add it to your Processing classpath (import it).
After successfully doing so, you can use JEP like this:

void setup() {
org.nfunk.jep.JEP parser = new org.nfunk.jep.JEP();
parser.addComplex();
try {
parser.parseExpression("(1+2*i) + (3+8*i)");
println(parser.getComplexValue());
} catch(Exception e) {
e.printStackTrace();
}
}

which produces the (expected) output: (4.0, 10.0)

Convert Expression from a Textbox to Math Expression in Code Behind

solved with this library http://www.codeproject.com/Articles/21137/Inside-the-Mathematical-Expressions-Evaluator

my final code

Calculator Cal = new Calculator();
txt_LambdaNoot.Text = (Cal.Evaluate(txt_C.Text) / fo).ToString();

now when some one type 3*10^11 he will get 300000000000



Related Topics



Leave a reply



Submit