| Arithmetic Expressions : | |||||||||||||||||||
Algorithm for evaluation of an expression E which is in prefix notation : |
|||||||||||||||||||
|
|||||||||||||||||||
| Evaluate (E) | |||||||||||||||||||
| Else | |||||||||||||||||||
| { | |||||||||||||||||||
| { | |||||||||||||||||||
| Top = 0; | |||||||||||||||||||
| If (x = = operand) | |||||||||||||||||||
| While (1) | |||||||||||||||||||
| PUSH (Q, top, n, x); | |||||||||||||||||||
| { | |||||||||||||||||||
| If (x = = operator) | |||||||||||||||||||
| x= next token (E) | |||||||||||||||||||
| { | |||||||||||||||||||
| If (x = = infinity) | |||||||||||||||||||
| Pop correct number of operands according to the operator (unary/binary)
and then perform the operation and store result onto the stack |
|||||||||||||||||||
| { | |||||||||||||||||||
| Print value of stack [top] as the output of the expression | |||||||||||||||||||
| } | |||||||||||||||||||
| } | |||||||||||||||||||
| } | |||||||||||||||||||
| } | |||||||||||||||||||
| Prev | |||||||||||||||||||