I have written a project that calculates different functions like sine,MCM etc without using existing packages like math in java
now I want to get an expression from the user in form of a string and then print out the result
like :
import java.util.Scanner;
public class Phase2main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String expression ;
double result = 0 ;
System.out.println(" Enter your desired expression from the available functions ");
expression = s.nextLine();
System.out.println("Result is : " + result);
}
}
then it should run like this: Enter an Expression: ADD(DIV(SIN(FACT(3)),CEIL(TAN(MUL(1.5,FIB(4))))),GCD(2,10)) The Result is: 1.94
how can I make the program to identify my functions like CEIL and their input ? I've checked many of the similar questions but the ones that I found are rather libraries that are too complex for me to understand or do basic arithmetic without identifying functions and their inputs
so how can I write a simple evaluator for this specific problem?
May be use JavaScript interpreter?
First create engine instance and init:
And then you can evaluate expressions in the "scope" many times:
Be warned:
You can also use, for example, Jython or JRuby as interpreter.