Click here to Skip to main content
15,941,973 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
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

For CPP :

int ExpressEval(char * expression);

For C Sharp :

public int ExpressEval(string 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 your function must return -5.
Posted
Updated 2-Jun-11 22:41pm
v2
Comments
walterhevedeich 3-Jun-11 4:26am    
Seems like a homework. What kind of help do you need on this one?
Sergey Alexandrovich Kryukov 3-Jun-11 5:04am    
What kind of help?
--SA
Rob Branaghan 3-Jun-11 5:08am    
Looks a little bit like this

http://www.cis.upenn.edu/~matuszek/cit594-2002/Assignments/5-expressions.html
Sandeep Mewara 3-Jun-11 11:18am    
What have you tried so far? Update the question with the effort.

1 solution

Well, skipping the most external '[]', you have the following grammar (if I'm right):

start: exp

exp: DIGIT | DIGIT ',' exp | '[' exp ']'    

NUM: '0' | '1' | .. | '9'


It looks simple enough in order to implement manually the parser.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900