Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 recursive function problem
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
efus




PostPosted: Sun Nov 30, 2008 3:44 pm   Post subject: 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:
code:

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
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Sun Nov 30, 2008 3:52 pm   Post subject: 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.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Vermette




PostPosted: Sun Nov 30, 2008 6:18 pm   Post subject: 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.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: