Click to See Complete Forum and Search --> : is there an eval function?


crazycoder
02-10-2006, 05:42 PM
I'm trying to create a equation grapher. I was thinking you would type in the equation as a string, then convert it to an integer. I was wondering if there is some sort of eval function in java like in javascript.

I know for sines and things they would have to use Math.~~();, I won't need to worry about that because I won't be selling it. :)

Jeff Mott
02-10-2006, 07:12 PM
Nope. And you probably shouldn't even use the one in JavaScript unless absolutely necessary.

If you're accepting a string, then you'll actually have to parse and interpret that string: pick out the operands, operators, and then work in the order of precedence.

crazycoder
02-10-2006, 09:55 PM
Do you have any idea how to go about this? because I'm clueless

Cytael
02-12-2006, 08:05 PM
This should be close to what you want, albeit at a very basic level. It should handle any equation that contains only integers and the 4 basic operators, provided they're seperated by spaces, as in "4 + 2 / 3 - 6 * 7"

Note that it doesn't check for errors in the formatting of the string, and it may not even compile (haven't tested it), but it should give you a more-than-ample nudge in the right direction. The basic algorithm is: look for an operator (following the order-of-operations, which I only halfway implemented). If you find one, perform that operation on the numbers on either side of the operator, store the result in the position of the left operand, and delete the operator and right operand. This eventually leaves just one entry in the list, which is your final answer.

Here's the code:

public class EquationParser{
String equation;
public EquationParser(String equation) { this.equation = equation; }

public int ParseMe()
{
String[] split = equation.split(" ");
ArrayList pieces = new ArrayList();
for (int i=0; i<split.length; i++) pieces.add(split[i]);

for(int i=0; i<pieces.size(); i++)
{
int temp;
if (pieces.get(i).equals("/"))
{
temp = Integer.parseInt((String)pieces.get(i-1)) / Integer.parseInt((String)pieces.get(i+1));
pieces.set(i-1, Integer.toString(temp));
pieces.removeRange(i, i+2);
i--;
}
}
for(int i=0; i<pieces.size(); i++)
{
int temp;
if (pieces.get(i).equals("*"))
{
temp = Integer.parseInt((String)pieces.get(i-1)) * Integer.parseInt((String)pieces.get(i+1));
pieces.set(i-1, Integer.toString(temp));
pieces.removeRange(i, i+2);
i--;
}
}
for(int i=0; i<pieces.size(); i++)
{
int temp;
if (pieces.get(i).equals("-"))
{
temp = Integer.parseInt((String)pieces.get(i-1)) - Integer.parseInt((String)pieces.get(i+1));
pieces.set(i-1, Integer.toString(temp));
pieces.removeRange(i, i+2);
i--;
}
}
for(int i=0; i<pieces.size(); i++)
{
int temp;
if (pieces.get(i).equals("+"))
{
temp = Integer.parseInt((String)pieces.get(i-1)) + Integer.parseInt((String)pieces.get(i+1));
pieces.set(i-1, Integer.toString(temp));
pieces.removeRange(i, i+2);
i--;
}
}

int result = Integer.parseInt((String)pieces.get(0));
return(result);
}
}

crazycoder
02-19-2006, 10:01 AM
Thanks