A simple equation parser for Java.
This code calculates the sum of "2+2". (= 4)
String equation = "2+2";
MathParser mathParser = new MathParser();
System.out.println(mathParser.parse(equation));
Brackets? No problem! "18*(36-50)" = -252
String equation = "18*(36-50)";
MathParser mathParser = new MathParser();
System.out.println(mathParser.parse(equation));
You need functions? Too easy! "root(3,8)" (3th root of 8) = 2
String equation = "root(3,8)";
MathParser mathParser = new MathParser();
System.out.println(mathParser.parse(equation));
Not enough functions? Try this! fooBar(1,2,3) = 1 + 2 + 3 = 6
String equation = "fooBar(1,2,3)";
MathParser mathParser = new MathParser();
mathParser.addFunction("fooBar", new MathFunction() {
@Override
public Double execute(Double[] parameter) {
return parameter[0] + parameter[1] + parameter[2];
}
});
System.out.println(mathParser.parse(equation));
Still no problem! 5((2+4)-2)+sin(root(1)*pi) = 20
String equation = "(( (5* ((2 +4)-2) +sin(root(2,1)*pi())))";
MathParser mathParser = new MathParser();
System.out.println(mathParser.parse(equation));
Gradle:
dependencies {
implementation 'de.sematre.mathparser:MathParser:1.1'
}
Maven:
<dependency>
<groupId>de.sematre.mathparser</groupId>
<artifactId>MathParser</artifactId>
<version>1.1</version>
</dependency>
- 1.1
- Code cleanup
- 1.0
- Initial version
© Sematre 2019
Distributed under the MIT License. See LICENSE
for more information.