recursive function problem
Author |
Message |
efus
|
Posted: 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

|
|
 |
Tony

|
Posted: 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. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
Vermette

|
|
|
|
 |
|
|