
-----------------------------------
efus
Sun Nov 30, 2008 3:44 pm

recursive function problem
-----------------------------------
Hi, I am having some troubles with a recursive function.
The function needs to get a string as an input. The string contains both symbols and numbers. What the function does is doing some operation on the two children of a node, the node being the operation(+,-,*,/). 
The thing is, I need the function to return a double. This couses a problem since I cant just convert the string to a double becouse of the operators. 
This is the algoritm:

function Evaluate {
if node is an operator(+,-,*,/) then
    evaluate the right and left child
    apply the operator of the node to the two children
else if the node is a value then
    the result is the value


-----------------------------------
Tony
Sun Nov 30, 2008 3:52 pm

RE:recursive function problem
-----------------------------------
typecast the value to Double. Then you are always doing operations on two Double's, so you can just return the same type.

It doesn't make sense to convert a String, since "+" in Double is meaningless.

-----------------------------------
Vermette
Sun Nov 30, 2008 6:18 pm

RE:recursive function problem
-----------------------------------
In case you're not sure what Tony is referring to:

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Double.html

Make note of the valueOf(String s) method.
