0% found this document useful (0 votes)
54 views7 pages

Evaluating Postfix in C

Evaluating postfix expressions involves using a stack. When scanning a postfix expression from left to right, variables and constants are pushed onto the stack, while operators pop the top two elements, perform the operation, and push the result back onto the stack. Each operator refers to the previous two operands in the postfix string. An algorithm uses a stack to evaluate postfix expressions by pushing operands and popping and operating on elements when operators are encountered.

Uploaded by

Junald Bayog
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views7 pages

Evaluating Postfix in C

Evaluating postfix expressions involves using a stack. When scanning a postfix expression from left to right, variables and constants are pushed onto the stack, while operators pop the top two elements, perform the operation, and push the result back onto the stack. Each operator refers to the previous two operands in the postfix string. An algorithm uses a stack to evaluate postfix expressions by pushing operands and popping and operating on elements when operators are encountered.

Uploaded by

Junald Bayog
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Evaluating Postfix

J u n a l d B a y o g R o d e l R e b u c as
Evaluating Postfix Expression
For example:

Given : 5 6 + 9 * 2 +

Result: 101
Evaluating Postfix Expression
Use a stack to evaluate an expression in postfix notation.
The postfix expression to be evaluated is scanned from left to right.
Variables or constants are pushed onto the stack.
When an operator is encountered, the indicated action is performed
using the top elements of the stack, and the result replaces the
operands on the stack.

Evaluating Postfix Expression
Each operator in a postfix string refers to the previous two operands
in the string.
Suppose that each time we read an operand we push it into a stack.
When we reach an operator, its operands will then be top two
elements on the stack
We can then pop these two elements, perform the indicated
operation on them, and push the result on the stack.
So that it will be available for use as an operand of the next operator.

Postfix expressions:
Algorithm using stacks (cont.)
Another Example
Algorithm for evaluating a postfix
expression (Cond.)
WHILE more input items exist
{
If symb is an operand
then push (opndstk,symb)
else //symbol is an operator
{
Opnd1=pop(opndstk);
Opnd2=pop(opndnstk);
Value = result of applying symb to opnd1 & opnd2
Push(opndstk,value);
} //End of else
} // end while
Result = pop (opndstk);

You might also like