DS - 3.3stacks (Applications)
DS - 3.3stacks (Applications)
APPLICATION
POSTFIX
EXPRESSION
CALCULATOR
DATA STRUCTURES
INTRODUCTION
Infix notation
Usual notation for writing arithmetic expressions, in which the operator is written between the
operands.
For example, in the expression a + b, the operator + is between the operands a and b.
This has the advantage that the operators appear in the order required for computation.
a+b ab+
a+b*c abc*+
a*b+c ab*c+
(a + b ) * c ab+c*
(a – b) * (c + d) ab–cd+*
(a + b) * (c – d / e) + f ab+cde/–*f+
Infix to Postfix Converter
1) Examine the next element in the input
2) If it is operand, output it
3) If it is opening parenthesis, push it on stack.
4) If it is an operator, then
i) If stack is empty, push operator on stack.
ii) If the top of stack is opening parenthesis, push operator on stack
iii) If it has higher priority than the top of stack, push operator on stack.
iv) Else pop the operator from the stack and output it
5) If it is a closing parenthesis, pop operators from stack and output them until an opening parenthesis
is encountered. pop and discard the opening parenthesis.
6) If there is more input go to step 1
7) If there is no more input, pop the remaining operators to output.
Infix to Postfix Converter Cont..
Expression Stack Output
2 Empty 2
Convert
* * 2
2*3/(2-1)+5*3 3 * 23
into Postfix form / / 23*
( /( 23*
2 /( 23*2
- /(- 23*2
1 /(- 23*21
) / 23*21-
+ + 23*21-/
5 + 23*21-/5
* +* 23*21-/53
3 +* 23*21-/53
Empty 23*21-/53*+
POSTFIX EXPRESSION EVALUATION
Many compilers use stacks to first translate infix expressions into some form of
postfix notation and then translate this postfix expression into machine code.
4. and continue.
POSTFIX EXPRESSION EVALUATION
Evaluating expression
using a stack and the
previous algorithm can be
shown as.
Expression 1: 7 6 + 3 ; 6 - =
Expression 2: 14 + 2 3 * =
Expression 3: 14 2 3 +
To make the input easier to read, we assume that the postfix expressions are in the following form:
#6 #3 + #2 * =
If the symbol scanned is #, the next input is a number (that is, an operand).
If the symbol scanned is not #, it is either an operator (might be illegal) or an equal sign (indicating the
end of the expression).
Furthermore, we assume that each expression contains only the +, -, *, and / operators.
POSTFIX EXPRESSION EVALUATION
MAIN ALGORITHM
evaluateExpression, if possible, evaluates the expression and leaves the result in the stack.
If postfix expression is error free, the function printResult outputs the result.
The function evaluateOpr evaluates an operator, and
Function discardExp discards the current expression if there is any error in the expression.
POSTFIX EXPRESSION EVALUATION
evaluateExpression
Function evaluateExpression
evaluates each postfix
expression. Each expression
ends with the symbol =. The
general algorithm in
pseudocode is as follows
POSTFIX EXPRESSION
evaluateOpr
discardExp
It reads and writes the input data only until the input is '=', the end of the expression.
POSTFIX EXPRESSION
printResult