1

I have used EvalEx (https://github.com/uklimaschewski/EvalEx) and modified it to make a calculator without packages like math . so now it can get an input in string form from the user and print out the result.

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    

now the user will enter expressions that contain variables instead of constants

ADD(DIV(SIN(FACT(X1)),CEIL(TAN(MUL(1.5,FIB(X2))))),GCD(Y,10))    

and the overall code should work like below when run :

Enter an Expression: ADD(DIV(SIN(FACT(X1)),CEIL(TAN(MUL(1.5,FIB(X2))))),GCD(Y,10))     
Enter Variables: X1,X2,Y    
Enter values for X1, X2 and Y by this order(separate the values by space): 3 4 2    
The Result is: 1.94        

note that the user first enters the expression then will tell the machine what are the variables (as in " enter variables : x1,x2,y" ) also I have to use objective programming concepts ( so each function of the expression and its constants or variables should be kept as an object )

so how can I change the existing code from EvalEx to make the program to identify the variables ?

Vicarious
  • 131
  • 18

1 Answers1

1

You can set up a Map<String, Double> that associates variable names to values, like this:

Map<String, Double> values = new HashMap<String, Double>();
values.put("X", 13.5);
values.put("Y", -3);

Then you can replace all of the variables in the expression by their corresponding values, like this:

for (Map.Entry<String, Double> entry : values.entrySet())
    expression = expression.replace(entry.getKey(), entry.getValue().toString());

Then you can just apply the method you've already written to expression.

This approach is simple, but not very robust. For example, if one of your variable names names is "C" you will mess up the function "CEIL". For this reason, you could insist that all variables begin with "_" or something similar.

Paul Boddington
  • 37,127
  • 10
  • 65
  • 116
  • thank you for your answer but can you tell me another way without using map and hashmap ? It would be much better if I could do this by changing the eval method in EvalEx as I should avoid using packages actually I'm looking for ways to make the code work without any prepared packages like map, math etc (except for required ones like java.lang of course) – Vicarious Dec 24 '14 at 16:06
  • You don't have to use a `HashMap`. You could use a pair of arrays instead (a `String[]` for the variable names and a `double[]` for the corresponding values). But I don't know what you've got against `Map` - it's in java.util, so it's hardly exotic! If you want us to help you modify the code for `EvalEx` you will have to include the code for that method in your question. – Paul Boddington Dec 24 '14 at 16:39
  • I don't have anything against Map I've just started learning java so I thought it was a package that I shouldn't use and this the link to the code , this is essentially how it should look like https://www.dropbox.com/sh/0zeqdc6sn5ouvcm/AAAdjl9XCf6TQrNqyu5wUCJTa?dl=0 – Vicarious Dec 24 '14 at 16:57
  • and those comments are in my own language I haven't change the methods much ,I've mainly deleted parts related to boolean parsing – Vicarious Dec 24 '14 at 17:03