0% found this document useful (0 votes)
26 views17 pages

DS - 3.3stacks (Applications)

Uploaded by

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

DS - 3.3stacks (Applications)

Uploaded by

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

STACK

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.

In infix notation, the operators have precedence.


We must evaluate expressions from
left to right, and
multiplication and division have higher precedence than addition and subtraction

If we want to evaluate the expression in a different order, we must include parentheses.


For example, in the expression a + b * c, we first evaluate * using the operands b and c, and then we
evaluate + using the operand a and the result of b * c.
INTRODUCTION
INTRODUCTION
In the early 1920s, the Polish mathematician Jan discovered that if operators were written before the
operands (prefix or Polish notation; for example, + a b), the parentheses can be omitted.
Reverse Polish notation
In the late 1950s, an early computer scientist proposed a scheme in which the operators follow the
operands (postfix operators)

This has the advantage that the operators appear in the order required for computation.

For example, the expression:


a+b*c
In a postfix expression is:
abc*+
POSTFIX EXPRESSIONS

Infix expression Equivalent postfix expression

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.

Postfix expressions can be evaluated using the following algorithm:

1. Scan the expression from left to right.

2. When an operator is found, back up to get the required number of operands,

3. Perform the operation,

4. and continue.
POSTFIX EXPRESSION EVALUATION

Consider the following


postfix expression:
63+2*=

Evaluating expression
using a stack and the
previous algorithm can be
shown as.

=, which is the equal sign, indicating the end of the expression.


POSTFIX EXPRESSION EVALUATION
If a symbol is read other than a number, the following cases arise:
o The symbol we read is one of the following: +, -, *, /, or =
o If the symbol is +, -, *, or /, the symbol is an operator and so must evaluate it.
Because an operator requires two operands, the stack must have at least two
elements; otherwise, the expression has an error.
o If the symbol is = (an equal sign), the expression ends and print the answer. At this
step, the stack must contain exactly one element; otherwise, the expression has an
error.
o The symbol we read is something other than +, -, *, /, or =. In this case, the expression
contains an illegal operator.
When an operand (number) is encountered in an expression, it is pushed onto the stack because the
operator comes after the operands.
POSTFIX EXPRESSION EVALUATION

Consider the following expressions:

Expression 1: 7 6 + 3 ; 6 - =
Expression 2: 14 + 2 3 * =
Expression 3: 14 2 3 +

Which expression is Legal and which is Illegal Expression

Expression 1 has an illegal operator, expression


Expression 2 does not have enough operands for +,
Expression 3 has too many operands.
In the case of expression 3, when encounter the equal sign (=), the stack will have two elements and this error
cannot be discovered until we are ready to print the value of the expression.
POSTFIX EXPRESSION EVALUATION

To make the input easier to read, we assume that the postfix expressions are in the following form:

#6 #3 + #2 * =

The symbol # precedes each number in the expression.

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

Read the first character

while not the end of input data


{
a. initialize the stack
b. process the expression
c. output result
d. get the next expression
}

four functions — evaluateExpression, evaluateOpr, discardExp, and printResult.

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

This function (if possible) evaluates an expression.


Two operands are needed to evaluate an operation
and operands are saved in the stack.

Therefore, the stack must contain at least two


numbers.

If the stack contains fewer than two numbers, the


expression has an error.
POSTFIX EXPRESSION

discardExp

This function is called whenever an error is discovered in the expression.

It reads and writes the input data only until the input is '=', the end of the expression.
POSTFIX EXPRESSION

printResult

If the postfix expression contains no errors, the


function printResult prints the result

Otherwise, it outputs an appropriate message.

You might also like