pseudo nos

Would be really grateful if anyone could throw light on how to write the method.

Expression Evaluation

Consider a pseudo code which takes following expression as input and produces given outputs.

Input Output

[ 0 ] 0
[ 1 ] 1
[ 0, 1 ] 1
[ 1, 1 ] 2
[ 1, [ 1, 2 ] ] -2
[ 1, 2, [ 8 ] ] -5
[ 1, [ 2 ], [ 3 ] ] -4
[ 9, [ 3, 4, [ [ 2 ] ] ] ] 0

Design a function/methos to implement above pseudo code by following prototype as

int ExpressEval(char * expression);

Input:

Accept expression to be evaluated as a string.

Output :

Return result of evaluation as an integer.

For e.g

consider input string to be "[1, 2 , [5 , 3 ] ]"

then the function must return -5.



Please help...Thnx in advance..
this is easy once you see whats going on.
except for the outermost [], all the inner [] are -ve

and numbers within [] should just be added;

so [1,1] = 1+1 = 2
[1,2,[8]] = 1+2 - 8 = -5

you can use a stack to push the numbers and the opening [ and as and when you see a ] you can pop the values off the stack to compute the value between [ ] and push it back onto the stack.
Topic archived. No new replies allowed.