Python has a function called eval() used to solve mathematical expressions in a string but Java has nothing like this, so i developed this algorithm which can do so.
You can use this package to create a calculator which can solve complex mathematical expressions.
import evaluater.Evaluater;
import evaluater.EvaluaterExceptions;
public class sample {
public static void main(String[] args)
{
try
{
Evaluater eval = new Evaluater("1280.33/(12*-10.11)");
System.out.println(eval.evaluate());
}
catch(EvaluaterExceptions.InvalidMathExpression excep)
{
System.out.println(excep);
}
}
}| Operators | Name | Precedence |
|---|---|---|
| + | Addition | 1 |
| - | Subtraction | 1 |
| x,*,X | Multiplication | 2 |
| /,÷ | Division | 2 |
| % | Modulus | 2 |
| ^ | Power | 3 |
| ! | Not | 4 |