Representation of Arithmetic Expressions - PowerPoint PPT Presentation

1 / 78
About This Presentation
Title:

Representation of Arithmetic Expressions

Description:

A stack is an ordered group of homogeneous items (elements), in which the ... Stack to determine where to put it placing each value popped onto temp Stack. ... – PowerPoint PPT presentation

Number of Views:63
Avg rating:3.0/5.0
Slides: 79
Provided by: ruthu
Category:

less

Transcript and Presenter's Notes

Title: Representation of Arithmetic Expressions


1
Representation of Arithmetic Expressions
  • Prefix Notation - operator precedes its
    operands. A B - C D
  • Infix Notation - operator is between its
    operands. ( A B) ( C - D )
  • Postfix Notation - operator follows its operands.
    A B C D -

2
Stack Application 1Evaluation of Postfix
Expressions
  • An Example
  • 6 3 7 2 -
  • 9
  • 9 7 2 -
  • 5
  • 9 5
  • 45

3
Evaluation of Postfix Expressions
  • Operands are placed onto the stack until their
    operator is located.
  • Operators are executed when they are located.
  • Pop stack to remove operands
  • Evaluate expression
  • Place result onto stack

4
An Example - Stack is empty
5 2 - 4 6
stack operands
5
An Example - Push 5
5 2 - 4 6
operands

operands. push (5)
5
6
An Example - Push 2
5 2 - 4 6
operands

operands.push(2)
2
5
7
An Example - Process -
5 2 - 4 6
operands

op2 operands.top() operands.pop()
op1 operands.top() operands.pop() ans op1 -
op2 operands.push (ans)
op1 op2 ans
5
2
8
An Example - Process -
5 2 - 4 6
operands

op2 operands.top() operands.pop() op1
operands.top() operands.pop() ans op1 -
op2 operands.push (ans)
Op1 Op2 Ans
5
2
9
An Example - Process -
5 2 - 4 6
operands

op2 operands.top() operands.pop() op1
operands.top() operands.pop() ans op1 -
op2 operands.push (ans)
Op1 Op2 Ans
5
2
3
10
An Example - Process -
5 2 - 4 6
operands
Private Data Top maxItems-1 . . .
3 2
1 items0

op2 operands.top() operands.pop() op1
operands.top() operands.pop() ans op1 -
op2 operands.push (ans)
0
Op1 Op2 Ans
2
3
3
5
2
3
11
An Example - Push 4
5 2 - 4 6
operands

operands.push(4)
4
3
12
An Example - Push 6
5 2 - 4 6
operands

operands.push(6)
6
4
3
13
An Example - Process
5 2 - 4 6
operands

op2 operands.top() operands.pop() op1
operands.top() operands.pop() ans op1
op2 operands.push (ans)
4
Op1 Op2 Ans
3
6
14
An Example - Process
5 2 - 4 6
operands

op2 operands.top() operands.pop() op1
operands.top() operands.pop() ans op1
op2 operands.push (ans)
Op1 Op2 Ans
3
4
6
15
An Example - Process
5 2 - 4 6
operands

op2 operands.top() operands.pop() op1
operands.top() operands.pop() ans op1
op2 operands.push (ans)
Op1 Op2 Ans
3
4
6
10
16
An Example - Process
5 2 - 4 6
operands

op2 operands.top() operands.pop() op1
operands.top() operands.pop() ans op1 op2
operands.push (ans)
10
Op1 Op2 Ans
3
4
6
10
17
An Example - Process
5 2 - 4 6
operands

op2 operands.top() operands.pop() op1
operands.top() operands.pop() ans op1
op2 operands.push (ans)
Op1 Op2 Ans
3
10
18
An Example - Process
5 2 - 4 6
operands

op2 operands.top() operands.pop() op1
operands.top() operands.pop() ans op1
op2 operands.push (ans)
Op1 Op2 Ans
3
10
19
An Example - Process
5 2 - 4 6
operands

op2 operands.top() operands.pop() op1
operands.top() operands.pop() ans op1
op2 operands.push (ans)
Op1 Op2 Ans
3
10
30
20
An Example - Process
5 2 - 4 6
operands

op2 operands.top() operands.pop() op1
operands.top() operands.pop() ans op1
op2 operands.push (ans)
Op1 Op2 Ans
30
3
10
30
21
Stack Application 2Conversion from Infix to
Postfix
  • Operands placed in postfix expression as they are
    encountered.
  • Operators need to be stored on stack until they
    should be executed.
  • Precedence
  • Parentheses
  • Current Operator pushed onto stack
  • Dealing with the stack at end of expression

22
Dealing with operators
  • While the operator at the top of the stack must
    be executed before the current operator, pop the
    stack and place operator into postfix expression.
  • If current token is open parentheses just push it
    onto the stack, it acts as a barrier. The only
    operator to remove it is a close parentheses.
  • If current token is a close parentheses, pop the
    stack while top is not an open parentheses. Then
    pop and discard the open parentheses.
  • Always push all current operators (except close
    parentheses) onto the stack.

23
How to finish processing after last token
  • Pop all remaining operators off the stack after
    the last token has been processed. Place each
    operator onto the postfix expression as it is
    popped.

24
An Example Infix to Postfix
  • A B C - D

stack operator char token, stTop Cstring
postExpr bool done false
25
An Example Infix to Postfix
  • A B C - D


operator
token
postExprtoken
A
done
false
stTop
postExpr
A
26
An Example Infix to Postfix
  • A B C - D

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false

operator
token

done
false
stTop
postExpr
A
27
An Example Infix to Postfix
  • A B C - D

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
28
An Example Infix to Postfix
  • A B C - D

postExprtoken
29
An Example Infix to Postfix
  • A B C - D

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
30
An Example Infix to Postfix
  • A B C - D

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false

operator
token

done
false
stTop


postExpr
AB
31
An Example Infix to Postfix
  • A B C - D

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
32
An Example Infix to Postfix
  • A B C - D

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false

operator
token

done
true
stTop


postExpr
AB
33
An Example Infix to Postfix
  • A B C - D

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false

34
An Example Infix to Postfix
  • A B C - D

postExprtoken
35
An Example Infix to Postfix
  • A B C - D

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
36
An Example Infix to Postfix
  • A B C - D

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
37
An Example Infix to Postfix
  • A B C - D

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
38
An Example Infix to Postfix
  • A B C - D

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
39
An Example Infix to Postfix
  • A B C - D

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false

operator
token
-
done
false
stTop


postExpr
ABC
40
An Example Infix to Postfix
  • A B C - D

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
41
An Example Infix to Postfix
  • A B C - D

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
42
An Example Infix to Postfix
  • A B C - D

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
-
43
An Example Infix to Postfix
  • A B C - D

postExprtoken
-
44
An Example Infix to Postfix
  • A B C - D

while (!operator.empty()) stTop
operator.top() operator.pop()
postExpr stTop
-
45
An Example Infix to Postfix
  • A B C - D

while (!operator.empty()) stTop
operator.top() operator.pop()
postExpr stTop
46
An Example Infix to Postfix
  • A B C - D

while (!operator.empty()) stTop
operator.top() operator.pop()
postExpr stTop
47
Example 2 Infix to Postfix
  • (A - B) / (D E)

stack operator char token, stTop Cstring
postExpr bool done false
48
Example 2 Infix to Postfix
  • (A - B) / (D E)

if (token '(' ) operator.push(token)
49
Example 2 Infix to Postfix
  • (A - B) / (D E)

postExprtoken
50
Example 2 Infix to Postfix
  • (A - B) / (D E)

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
51
Example 2 Infix to Postfix
  • (A - B) / (D E)

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
operator
52
Example 2 Infix to Postfix
  • (A - B) / (D E)

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
-
true
(
'('
A
53
Example 2 Infix to Postfix
  • (A - B) / (D E)

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
54
Example 2 Infix to Postfix
  • (A - B) / (D E)

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
-
55
Example 2 Infix to Postfix
  • (A - B) / (D E)

postExprtoken
56
Example 2 Infix to Postfix
  • (A - B) / (D E)

if (token ')' ) stTop
operator.top() while (stTop!'(' )
postExprstTop
operator.pop() stTop operator.top()
operator.pop() //
discard '('
57
Example 2 Infix to Postfix
  • (A - B) / (D E)

if (token ')' ) stTop
operator.top() while (stTop!'(' )
postExprstTop
operator.pop() stTop operator.top()
operator.pop() //
discard '('
58
Example 2 Infix to Postfix
  • (A - B) / (D E)

if (token ')' ) stTop
operator.top() while (stTop!'(' )
postExprstTop
operator.pop() stTop operator.top()
operator.pop() //
discard '('
59
Example 2 Infix to Postfix
  • (A - B) / (D E)

if (token ')' ) stTop
operator.top() while (stTop!'(' )
postExprstTop
operator.pop() stTop operator.top()
operator.pop() //
discard '('
)
false
(
('
AB-
60
Example 2 Infix to Postfix
  • (A - B) / (D E)

if (token ')' ) stTop
operator.top() while (stTop!'(' )
postExprstTop
operator.pop() stTop operator.top()
operator.pop() //
discard '('
61
Example 2 Infix to Postfix
  • (A - B) / (D E)

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
62
Example 2 Infix to Postfix
  • (A - B) / (D E)

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
/
63
Example 2 Infix to Postfix
  • (A - B) / (D E)


operator
token
if (token '(' ) operator.push(token)
'('
done
false
(
stTop
/
('
postExpr
AB-
64
Example 2 Infix to Postfix
  • (A - B) / (D E)


operator
token
postExprtoken
D
done
false
(
stTop
/
('
postExpr
AB-D
65
Example 2 Infix to Postfix
  • (A - B) / (D E)

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false

operator
token

done
false
(
stTop
/
('
postExpr
AB-D
66
Example 2 Infix to Postfix
  • (A - B) / (D E)

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false

operator
token

done
false
(
stTop
/
'('
postExpr
AB-D
67
Example 2 Infix to Postfix
  • (A - B) / (D E)

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false

true
(
/
'('
AB-D
68
Example 2 Infix to Postfix
  • (A - B) / (D E)

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false

true
(
/
'('
AB-D
69
Example 2 Infix to Postfix
  • (A - B) / (D E)

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false


false
(
/
'('
AB-D
70
Example 2 Infix to Postfix
  • (A - B) / (D E)

postExprtoken
E

false
(
/
'('
AB-DE
71
Example 2 Infix to Postfix
  • (A - B) / (D E)

if (token ')' ) stTop
operator.top() while (stTop!'(' )
postExprstTop
operator.pop() stTop operator.top()
operator.pop() //
discard '('
)

false
(
/
''
AB-DE
72
Example 2 Infix to Postfix
  • (A - B) / (D E)

if (token ')' ) stTop
operator.top() while (stTop!'(' )
postExprstTop
operator.pop() stTop operator.top()
operator.pop() //
discard '('
)

false
(
/
''
AB-DE
73
Example 2 Infix to Postfix
  • (A - B) / (D E)

if (token ')' ) stTop
operator.top() while (stTop!'(' )
postExprstTop
operator.pop() stTop operator.top()
operator.pop() //
discard '('
)
false
(
/
'('
AB-DE
74
Example 2 Infix to Postfix
  • (A - B) / (D E)


operator
token
if (token ')' ) stTop
operator.top() while (stTop!'(' )
postExprstTop
operator.pop() stTop operator.top()
operator.pop() //
discard '('
)
done
false
(
stTop
/
'('
postExpr
AB-DE
75
Example 2 Infix to Postfix
  • (A - B) / (D E)

if (token ')' ) stTop
operator.top() while (stTop!'(' )
postExprstTop
operator.pop() stTop operator.top()
operator.pop() //
discard '('
)
false
/
'('
AB-DE
76
Example 2 Infix to Postfix
  • (A - B) / (D E)

while (!operator.empty()) stTop
operator.top() operator.pop()
postExpr stTop

operator
token
\0
done
false
stTop
/
'('
postExpr
AB-DE
77
Example 2 Infix to Postfix
  • (A - B) / (D E)

while (!operator.empty()) stTop
operator.top() operator.pop()
postExpr stTop
AB-DE/
78
Example 2 Infix to Postfix
  • (A - B) / (D E)

while (!operator.empty()) stTop
operator.top() operator.pop()
postExpr stTop

operator
token
\0
done
false
stTop
/
postExpr
AB-DE/
Write a Comment
User Comments (0)
About PowerShow.com