Click to See Complete Forum and Search --> : eval in java??
Trainwreck
March 18th, 2008, 03:29 PM
I'm trying to make this basic calculator in java just to expand my knowledge a bit. But i've already came across a problem.
The application has a textfield where you can enter a sum like: 4+(2*8)
What i wanted to use is a funtion called eval(), but Java doesn't seem to have this. Is there an equivallent? Or how could i else calculate stuff like that?
Thanks for any help.
Londbrok
March 18th, 2008, 03:40 PM
I dont know if there is eval() equalent in Java. However that kind of calculation is algorithvise quite doable. In fact it is not uncommon a practise, and quite surely the net is full of Java calculator how to´s and tutorials.
You could, for example, break the whole thing to charArray, or use String.indexOf to find the first occurence of... whats it called... these ->( ), parse its contents to a calculation, calculate that and proceed untill all those have been dealt with.
So in that idea... you need to construct a parser, to which you offer a String, it will determine the numbers and different operators, plus minus and so forth. If the string is calculatable, parser returns the result.
Surely you have this and that readily made in the bubbling foam of Java community. Short googling gives you eg. beanShell, that appears to do what you need.
Object result = i.eval("2*2"); // Integer
where i is instance of beanShells interpreter. Not hundred percent sure it can evaluate (3*(5+6)) but would be a little suprised if it could not.
Check that out, or any of the other available parsers, in case coding your own is not topmost priority right now.
spoon!
March 18th, 2008, 08:27 PM
something like eval() is really difficult in a compiled language, because it would need access to the compiler at runtime
ProgramThis
March 19th, 2008, 07:18 AM
something like eval() is really difficult in a compiled language, because it would need access to the compiler at runtime
Not true at all. You can write an algorithm that parses the string into a tree where the leaves are numbers and the parents are operators. If you parse the information correctly into the tree then you can simply evaluate the tree.
I did this in ANSI C a while back for a programming assignment that took the derivate of a function. I'ld post the code but it's a bit messy, and I believe that it was for prefix operations not infix.
spoon!
March 19th, 2008, 05:48 PM
eval() (http://en.wikipedia.org/wiki/Eval) usually means a function that can take arbitrary source code as a string and evaluate it
ProgramThis
March 19th, 2008, 10:15 PM
eval() usually means a function that can take arbitrary source code as a string and evaluate it
I understand, I have taken two functional programming courses. However, for the purposes of a calculator eval can be done relatively simply. You can recursively parse through the string inserting the nodes into a tree and then perform the evaluation. There needs not be any "on the fly" compiling or reworking of any code.
*
+ 5
2 3
would be the tree for (2 + 3) * 5. With no parens it would look like this:
+
2 *
3 5
for 2 + 3 * 5
Figuring out how to recurse through the string and correctly insert the items into a tree and then execute when the idea is a simple calculator (with + - * /) does not need to be as complicated as the link you provided.
As a matter of fact, you can perform infix, prefix and postfix operations on the tree depending on whether you decided to go left-read-right, right-read-left, or read-left-right.
Trainwreck
March 20th, 2008, 11:54 AM
Im trying to write my own eval function.
Tearing a string apart isn't the difficult part but the calculation is.
Suppose i have the following string:
String str = 2*(5+5);
I mean i can take it apart but how do i eventually calculate it since there is no function at all for that...?? I can't parse it back into an int and put that in a println().
Deliverance
March 20th, 2008, 12:25 PM
ProgramThis gave you an excellent example of a function you could write to parse the string, and return you your result (as an int, float or double, whatever you decide to implement) which you can put in your println
ProgramThis
March 20th, 2008, 02:49 PM
Once you have the string parsed (whether in a tree, or however you want to do it) you can try to evaluate it, or you can write a dirty, easy hack:
int evaluateOperator(int a, char op, int b) {
int val = 0;
switch(op)
case '+':
val = a + b;
break;
case '-':
...//you get the point
return val;
}
I know that isn't a true eval, but for a simple calculator it serves the purpose. It will work as a sort of eval emulator. Parse the string into a series of actions to be performed, iterate through your data structure containing the elements, call this method when you have an operator (using recursion if you have nested operators).
The point is: you are not going to get Java to work like Lisp, Scheme, Prolog, ML. It's just not going to happen, but you CAN emulate it.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.