Question: Write pseudo code to validate and evaluate a given arithematic expression Example: a+(b*c)+(d/e) If the input is syntactically wrong, display an error message else display the result
Anónimo
The interviewer was probably looking for the particular answer only. With a stack to be used for parentheses validation and simultaneously evaluating the expression. You take 2 stacks, one for (operators,braces) and another for operands. In the operand stack, you push the values and in the operator stack, you push the operators and braces until you encounter a closing brace. Then you pop the last operator and pop two values from the operand stack, perform the operation and push the result back to value stack. Do this until you pop the opening brace from the operator stack. At the end, you will find the operand stack empty and value stack having one value, which is the result of the expression.