Title: Representation of Arithmetic Expressions
1Representation 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 -
2Stack Application 1Evaluation of Postfix
Expressions
- An Example
- 6 3 7 2 -
- 9
- 9 7 2 -
- 5
- 9 5
- 45
3Evaluation 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
4An Example - Stack is empty
5 2 - 4 6
stack operands
5An Example - Push 5
5 2 - 4 6
operands
operands. push (5)
5
6An Example - Push 2
5 2 - 4 6
operands
operands.push(2)
2
5
7An 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
8An 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
9An 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
10An 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
11An Example - Push 4
5 2 - 4 6
operands
operands.push(4)
4
3
12An Example - Push 6
5 2 - 4 6
operands
operands.push(6)
6
4
3
13An 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
14An 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
15An 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
16An 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
17An 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
18An 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
19An 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
20An 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
21Stack 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
22Dealing 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.
23How 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.
24An Example Infix to Postfix
stack operator char token, stTop Cstring
postExpr bool done false
25An Example Infix to Postfix
operator
token
postExprtoken
A
done
false
stTop
postExpr
A
26An Example Infix to Postfix
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
27An Example Infix to Postfix
while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
28An Example Infix to Postfix
postExprtoken
29An Example Infix to Postfix
while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
30An Example Infix to Postfix
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
31An Example Infix to Postfix
while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
32An Example Infix to Postfix
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
33An Example Infix to Postfix
while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
34An Example Infix to Postfix
postExprtoken
35An Example Infix to Postfix
while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
36An Example Infix to Postfix
while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
37An Example Infix to Postfix
while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
38An Example Infix to Postfix
while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
39An Example Infix to Postfix
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
40An Example Infix to Postfix
while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
41An Example Infix to Postfix
while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
42An Example Infix to Postfix
while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
-
43An Example Infix to Postfix
postExprtoken
-
44An Example Infix to Postfix
while (!operator.empty()) stTop
operator.top() operator.pop()
postExpr stTop
-
45An Example Infix to Postfix
while (!operator.empty()) stTop
operator.top() operator.pop()
postExpr stTop
46An Example Infix to Postfix
while (!operator.empty()) stTop
operator.top() operator.pop()
postExpr stTop
47Example 2 Infix to Postfix
stack operator char token, stTop Cstring
postExpr bool done false
48Example 2 Infix to Postfix
if (token '(' ) operator.push(token)
49Example 2 Infix to Postfix
postExprtoken
50Example 2 Infix to Postfix
while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
51Example 2 Infix to Postfix
while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
operator
52Example 2 Infix to Postfix
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
53Example 2 Infix to Postfix
while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
54Example 2 Infix to Postfix
while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
-
55Example 2 Infix to Postfix
postExprtoken
56Example 2 Infix to Postfix
if (token ')' ) stTop
operator.top() while (stTop!'(' )
postExprstTop
operator.pop() stTop operator.top()
operator.pop() //
discard '('
57Example 2 Infix to Postfix
if (token ')' ) stTop
operator.top() while (stTop!'(' )
postExprstTop
operator.pop() stTop operator.top()
operator.pop() //
discard '('
58Example 2 Infix to Postfix
if (token ')' ) stTop
operator.top() while (stTop!'(' )
postExprstTop
operator.pop() stTop operator.top()
operator.pop() //
discard '('
59Example 2 Infix to Postfix
if (token ')' ) stTop
operator.top() while (stTop!'(' )
postExprstTop
operator.pop() stTop operator.top()
operator.pop() //
discard '('
)
false
(
('
AB-
60Example 2 Infix to Postfix
if (token ')' ) stTop
operator.top() while (stTop!'(' )
postExprstTop
operator.pop() stTop operator.top()
operator.pop() //
discard '('
61Example 2 Infix to Postfix
while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
62Example 2 Infix to Postfix
while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
/
63Example 2 Infix to Postfix
operator
token
if (token '(' ) operator.push(token)
'('
done
false
(
stTop
/
('
postExpr
AB-
64Example 2 Infix to Postfix
operator
token
postExprtoken
D
done
false
(
stTop
/
('
postExpr
AB-D
65Example 2 Infix to Postfix
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
66Example 2 Infix to Postfix
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
67Example 2 Infix to Postfix
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
68Example 2 Infix to Postfix
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
69Example 2 Infix to Postfix
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
70Example 2 Infix to Postfix
postExprtoken
E
false
(
/
'('
AB-DE
71Example 2 Infix to Postfix
if (token ')' ) stTop
operator.top() while (stTop!'(' )
postExprstTop
operator.pop() stTop operator.top()
operator.pop() //
discard '('
)
false
(
/
''
AB-DE
72Example 2 Infix to Postfix
if (token ')' ) stTop
operator.top() while (stTop!'(' )
postExprstTop
operator.pop() stTop operator.top()
operator.pop() //
discard '('
)
false
(
/
''
AB-DE
73Example 2 Infix to Postfix
if (token ')' ) stTop
operator.top() while (stTop!'(' )
postExprstTop
operator.pop() stTop operator.top()
operator.pop() //
discard '('
)
false
(
/
'('
AB-DE
74Example 2 Infix to Postfix
operator
token
if (token ')' ) stTop
operator.top() while (stTop!'(' )
postExprstTop
operator.pop() stTop operator.top()
operator.pop() //
discard '('
)
done
false
(
stTop
/
'('
postExpr
AB-DE
75Example 2 Infix to Postfix
if (token ')' ) stTop
operator.top() while (stTop!'(' )
postExprstTop
operator.pop() stTop operator.top()
operator.pop() //
discard '('
)
false
/
'('
AB-DE
76Example 2 Infix to Postfix
while (!operator.empty()) stTop
operator.top() operator.pop()
postExpr stTop
operator
token
\0
done
false
stTop
/
'('
postExpr
AB-DE
77Example 2 Infix to Postfix
while (!operator.empty()) stTop
operator.top() operator.pop()
postExpr stTop
AB-DE/
78Example 2 Infix to Postfix
while (!operator.empty()) stTop
operator.top() operator.pop()
postExpr stTop
operator
token
\0
done
false
stTop
/
postExpr
AB-DE/