objectives - cs.trinity.eduthicks/2320-2004-s/lecture-pdf/infixprefix... · # 1 1 2 3 objectives 1....

37
# 1 1 2 3 Objectives 1. Evaluate Mathematical Expressions 2. Infix Expression Operators, Operands, Scope Openers, Scope Closers 3. Mathematical Order Of Operations 4. Generate Postfix From Infix 5. Generate Prefix From Infix 6. Evaluate Infix Expression With Respect To Parentheses, Brackets, & Braces 7. Create Prefix Expression From Infix Expression 8. Evaluate Postfix Expression 3 4 5 6 6

Upload: phamthuan

Post on 03-Apr-2018

222 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Objectives - cs.trinity.eduthicks/2320-2004-S/Lecture-PDF/InfixPrefix... · # 1 1 2 3 Objectives 1. Evaluate Mathematical Expressions 2. Infix Expression Operators, Operands, Scope

# 1

1 2

3

Objectives1. Evaluate Mathematical Expressions

2. Infix Expression Operators, Operands, Scope Openers, Scope Closers

3. Mathematical Order Of Operations

4. Generate Postfix From Infix

5. Generate Prefix From Infix

6. Evaluate Infix Expression With Respect To Parentheses, Brackets, & Braces

7. Create Prefix Expression From Infix Expression

8. Evaluate Postfix Expression

34

5 66

Page 2: Objectives - cs.trinity.eduthicks/2320-2004-S/Lecture-PDF/InfixPrefix... · # 1 1 2 3 Objectives 1. Evaluate Mathematical Expressions 2. Infix Expression Operators, Operands, Scope

# 2

7 8

Evaluate Mathematical ExpressionStep 1 –Is It A Valid Infix

Is It Valid With Respect To Parentheses, Brackets, & Braces

# | Expression | Valid | Invalid

1 | 5 + 2 ^ 3 ^ 2 | |

2 | (2 + 3) * (5 – 2) | |

3 | 3 * (2 + [ 12 / [ 4 * {5 – 4} -1 ] – 1 ] ) | |

X

X

X

What makes a Mathematical Expression valid with respectto Parentheses, Brackets, and Braces?

4 | 3 * )2 + 3 ( | | X

5 | 3 * (2 + 12 / {4 * [5 – 4} -1 ]) | | X

96 | ( 3 + 2 ^ 3 ^ 2 / {4 * [5 – 4] * 1 } * 2 | | X10

Now that we can manually verify valid infix parenthese,

brackets, and braces, how do we get the computer to do so?

Info

Max

Top-1

5

0

1

2

3

4

5

11

# | Expression [Infix String] | Valid | Invalid

0 | 2 + (3 * 5) | | X

Info

Max

Top-1

5

0

1

2

3

4

5“2 + (3 * 5 )”

We are going to evaluate expressions with constants –Extrapolate it to variables!

Define Scope Openers: ( { [

Define Scope Closers: ) } ]

Stack <char> ValidParen(5);

Traverse Infix String One Character At A Time

•If the character is a Scope Opener Push It To The Stack

•If the character is a Scope Closer, Pop the Stack; if the Closer and the top element of the stack are a pair, continue; otherwise return Invalid!

•When done, return Valid if stack is empty; otherwise return Invalid!

12

Page 3: Objectives - cs.trinity.eduthicks/2320-2004-S/Lecture-PDF/InfixPrefix... · # 1 1 2 3 Objectives 1. Evaluate Mathematical Expressions 2. Infix Expression Operators, Operands, Scope

# 3

# | Expression [Infix String] | Valid | Invalid

0 | 2 + (3 * 5) | | X

Info

Max

Top-1

5

0

1

2

3

4

5“2+ (3 * 5 )”

We are going to evaluate expressions with constants –Extrapolate it to variables!

Stack <char> ValidParen(5);

What do we do with the 2?

Skip Constants/OperandsNothing –Skip to next Character

13

# | Expression [Infix String] | Valid | Invalid

0 | 2 + (3 * 5) | | X

Info

Max

Top-1

5

0

1

2

3

4

5“2 +(3 * 5 )”

We are going to evaluate expressions with constants –Extrapolate it to variables!

Stack <char> ValidParen(5);

What do we do with the +?

Skip OperatorsNothing –Skip to next Character

14

# | Expression [Infix String] | Valid | Invalid

0 | 2 + (3 * 5) | | X

Info

Max

Top-1

5

0

1

2

3

4

5“2 + (3 * 5 )”

We are going to evaluate expressions with constants –Extrapolate it to variables!

Stack <char> ValidParen(5);

What do we do with the (?

Push Scope Openers

15

# | Expression [Infix String] | Valid | Invalid

0 | 2 + (3 * 5) | | X

Info

Max

Top0

5

0

1

2

3

4

5“2 + (3* 5 )”

We are going to evaluate expressions with constants –Extrapolate it to variables!

Stack <char> ValidParen(5);

What do we do with the 3?(

Skip Constants/OperandsNothing –Skip to next Character

16

# | Expression [Infix String] | Valid | Invalid

0 | 2 + (3 * 5) | | X

Info

Max

Top0

5

0

1

2

3

4

5“2 + (3 *5 )”

We are going to evaluate expressions with constants –Extrapolate it to variables!

Stack <char> ValidParen(5);

What do we do with the *?(

Skip OperatorsNothing –Skip to next Character

17

# | Expression [Infix String] | Valid | Invalid

0 | 2 + (3 * 5) | | X

Info

Max

Top0

5

0

1

2

3

4

5“2 + (3 * 5)”

We are going to evaluate expressions with constants –Extrapolate it to variables!

Stack <char> ValidParen(5);

What do we do with the 5?(

Skip Constants/OperandsNothing –Skip to next Character

18

Page 4: Objectives - cs.trinity.eduthicks/2320-2004-S/Lecture-PDF/InfixPrefix... · # 1 1 2 3 Objectives 1. Evaluate Mathematical Expressions 2. Infix Expression Operators, Operands, Scope

# 4

# | Expression [Infix String] | Valid | Invalid

0 | 2 + (3 * 5) | | X

Info

Max

Top0

5

0

1

2

3

4

5“2 + (3 * 5 )”

We are going to evaluate expressions with constants –Extrapolate it to variables!

Stack <char> ValidParen(5);

What do we do with the )?

(Pop The Stack And See If Set

) Goes With (If the Set Matches ContinueReturn INVALID If No Match

19

# | Expression [Infix String] | Valid | Invalid

0 | 2 + (3 * 5) | | X

Info

Max

Top-1

5

0

1

2

3

4

5“2 + (3 * 5 )”

We are going to evaluate expressions with constants –Extrapolate it to variables!

Stack <char> ValidParen(5);

Done With The Infix String

If Stack Empty Return VALID

If Stack Not Empty Return INVALID

20

21

# | Expression [Infix String] | Valid | Invalid

X

Info

Max

Top-1

5

0

1

2

3

4

5

“5 + 2 ̂ 3 ̂ 2”

We are going to evaluate expressions with constants –Extrapolate it to variables!

Stack <char> ValidParen(5);

What do we do with the 5?

Skip Constants/OperandsNothing –Skip to next Character

1 | 5 + 2 ^ 3 ^ 2 | | X

22

# | Expression [Infix String] | Valid | Invalid

X

Info

Max

Top-1

5

0

1

2

3

4

5

We are going to evaluate expressions with constants –Extrapolate it to variables!

Stack <char> ValidParen(5);

What do we do with the +?

Skip OperatorsNothing –Skip to next Character

1 | 5 + 2 ^ 3 ^ 2 | | X

“5 +2 ̂ 3 ̂ 2”

23

# | Expression [Infix String] | Valid | Invalid

X

Info

Max

Top-1

5

0

1

2

3

4

5

We are going to evaluate expressions with constants –Extrapolate it to variables!

Stack <char> ValidParen(5);

What do we do with the 2?

X

Skip Constants/OperandsNothing –Skip to next Character

1 | 5 + 2 ^ 3 ^ 2 | |

“5 + 2 ̂3 ̂ 2”

24

Page 5: Objectives - cs.trinity.eduthicks/2320-2004-S/Lecture-PDF/InfixPrefix... · # 1 1 2 3 Objectives 1. Evaluate Mathematical Expressions 2. Infix Expression Operators, Operands, Scope

# 5

# | Expression [Infix String] | Valid | Invalid

X

Info

Max

Top-1

5

0

1

2

3

4

5

We are going to evaluate expressions with constants –Extrapolate it to variables!

Stack <char> ValidParen(5);

What do we do with the ^?

Skip OperatorsNothing –Skip to next Character

1 | 5 + 2 ^ 3 ^ 2 | | X

“5 + 2 ^3 ̂ 2”

^ or $ often represents exponenation 25

# | Expression [Infix String] | Valid | Invalid

X

Info

Max

Top-1

5

0

1

2

3

4

5

We are going to evaluate expressions with constants –Extrapolate it to variables!

Stack <char> ValidParen(5);

What do we do with the 3?

X

Skip Constants/OperandsNothing –Skip to next Character

1 | 5 + 2 ^ 3 ^ 2 | |

“5 + 2 ^ 3 ̂2”

26

# | Expression [Infix String] | Valid | Invalid

X

Info

Max

Top-1

5

0

1

2

3

4

5

We are going to evaluate expressions with constants –Extrapolate it to variables!

Stack <char> ValidParen(5);

What do we do with the ^?

Skip OperatorsNothing –Skip to next Character

X1 | 5 + 2 ^ 3 ^ 2 | |

“5 + 2 ^ 3 ^2”

27

# | Expression [Infix String] | Valid | Invalid

X

Info

Max

Top-1

5

0

1

2

3

4

5

We are going to evaluate expressions with constants –Extrapolate it to variables!

Stack <char> ValidParen(5);

What do we do with the 2?

X

Skip Constants/OperandsNothing –Skip to next Character

1 | 5 + 2 ^ 3 ^ 2 | |

“5 + 2 ^ 3 ^ 2”

28

# | Expression [Infix String] | Valid | Invalid

X

Info

Max

Top-1

5

0

1

2

3

4

5

“2 + 3 * 5 -2”

We are going to evaluate expressions with constants –Extrapolate it to variables!

Stack <char> ValidParen(5);

Infix Expression CompleteWhat Is Returned?

VALID –Stack is Empty!

X1 | 5 + 2 ^ 3 ^ 2 | |

29 30

Page 6: Objectives - cs.trinity.eduthicks/2320-2004-S/Lecture-PDF/InfixPrefix... · # 1 1 2 3 Objectives 1. Evaluate Mathematical Expressions 2. Infix Expression Operators, Operands, Scope

# 6

# | Expression [Infix String] | Valid | Invalid

Info

Max

Top-1

5

0

1

2

3

4

5

“(2 + 3) * (5 –2)”

We are going to evaluate expressions with constants –Extrapolate it to variables!

Stack <char> ValidParen(5);

What do we do with the (?

2 | (2 + 3) * (5 – 2) | | X

Push Scope Openers

31

# | Expression [Infix String] | Valid | Invalid

Info

Max

Top0

5

0

1

2

3

4

5

(

“(2 + 3) * (5 –2)”

We are going to evaluate expressions with constants –Extrapolate it to variables!

Stack <char> ValidParen(5);

Skip

2 | (2 + 3) * (5 – 2) | | X

Skip

SkipPop Scope

Closer

Set Match( )

Continue32

# | Expression [Infix String] | Valid | Invalid

Info

Max

Top-1

5

0

1

2

3

4

5

“(2 + 3) * (5 –2)”

We are going to evaluate expressions with constants –Extrapolate it to variables!

Stack <char> ValidParen(5);

2 | (2 + 3) * (5 – 2) | | X

Skip

Push

33

# | Expression [Infix String] | Valid | Invalid

Info

Max

Top0

5

0

1

2

3

4

5

(

“(2 + 3) * (5–2)”

We are going to evaluate expressions with constants –Extrapolate it to variables!

Stack <char> ValidParen(5);

2 | (2 + 3) * (5 – 2) | | X

Skip

Skip

Skip

Pop

Set Match( )

Continue

34

# | Expression [Infix String] | Valid | Invalid

Info

Max

Top-1

5

0

1

2

3

4

5

“(2 + 3) * (5 –2)”

We are going to evaluate expressions with constants –Extrapolate it to variables!

Stack <char> ValidParen(5);

2 | (2 + 3) * (5 – 2) | | X

Infix Expression CompleteWhat Is Returned?

VALID –Stack is Empty!

35 36

Page 7: Objectives - cs.trinity.eduthicks/2320-2004-S/Lecture-PDF/InfixPrefix... · # 1 1 2 3 Objectives 1. Evaluate Mathematical Expressions 2. Infix Expression Operators, Operands, Scope

# 7

# | Expression [Infix String] | Valid | Invalid

Info

Max

Top-1

5

0

1

2

3

4

5“3 * (2 + [ 12 / [ 4 * {5 –4} -1 ] –1 ] )”

We are going to evaluate expressions with constants –Extrapolate it to variables!

3 | 3 * (2 + [ 12 / [ 4 * {5 – 4} -1 ] – 1 ] ) | | X

Skip Skip Push

37

# | Expression [Infix String] | Valid | Invalid

Info

Max

Top0

5

0

1

2

3

4

5

(

“3 * (2 + [ 12 / [ 4 * {5 –4} -1 ] –1 ] )”

We are going to evaluate expressions with constants –Extrapolate it to variables!

3 | 3 * (2 + [ 12 / [ 4 * {5 – 4} -1 ] – 1 ] ) | | X

Skip Skip Push

38

# | Expression [Infix String] | Valid | Invalid

Info

Max

Top1

5

0

1

2

3

4

5

(

[

“3 * (2 + [ 12 / [ 4 * {5 –4} -1 ] –1 ] )”

We are going to evaluate expressions with constants –Extrapolate it to variables!

3 | 3 * (2 + [ 12 / [ 4 * {5 – 4} -1 ] – 1 ] ) | | X

Skip Skip Push

39

# | Expression [Infix String] | Valid | Invalid

Info

Max

Top2

5

0

1

2

3

4

5

(

[

[

“3 * (2 + [ 12 / [ 4 * { 5 –4} -1 ] –1 ] )”

We are going to evaluate expressions with constants –Extrapolate it to variables!

3 | 3 * (2 + [ 12 / [ 4 * {5 – 4} -1 ] – 1 ] ) | | X

Skip Skip Push

40

# | Expression [Infix String] | Valid | Invalid

Info

Max

Top3

5

0

1

2

3

4

5

(

[

[

{

“3 * (2 + [ 12 / [ 4 * { 5–4 } -1 ] –1 ] )”

We are going to evaluate expressions with constants –Extrapolate it to variables!

3 | 3 * (2 + [ 12 / [ 4 * {5 – 4} -1 ] – 1 ] ) | | X

Skip Skip PopSkip

Set Match{ }

Continue

41

# | Expression [Infix String] | Valid | Invalid

Info

Max

Top2

5

0

1

2

3

4

5

(

[

[

“3 * (2 + [ 12 / [ 4 * { 5 –4 } - 1 ] –1 ] )”

We are going to evaluate expressions with constants –Extrapolate it to variables!

3 | 3 * (2 + [ 12 / [ 4 * {5 – 4} -1 ] – 1 ] ) | | X

Skip Skip Pop

Set Match[ ]

Continue

42

Page 8: Objectives - cs.trinity.eduthicks/2320-2004-S/Lecture-PDF/InfixPrefix... · # 1 1 2 3 Objectives 1. Evaluate Mathematical Expressions 2. Infix Expression Operators, Operands, Scope

# 8

# | Expression [Infix String] | Valid | Invalid

Info

Max

Top1

5

0

1

2

3

4

5

(

[

“3 * (2 + [ 12 / [ 4 * { 5 –4 } - 1 ] –1 ] )”

We are going to evaluate expressions with constants –Extrapolate it to variables!

3 | 3 * (2 + [ 12 / [ 4 * {5 – 4} -1 ] – 1 ] ) | | X

Skip Skip Pop

Set Match[ ]

Continue

43

# | Expression [Infix String] | Valid | Invalid

Info

Max

Top0

5

0

1

2

3

4

5

(

“3 * (2 + [ 12 / [ 4 * { 5 –4 } - 1 ] –1 ] )”

We are going to evaluate expressions with constants –Extrapolate it to variables!

3 | 3 * (2 + [ 12 / [ 4 * {5 – 4} -1 ] – 1 ] ) | | X

Pop

Set Match( )

Continue

44

# | Expression [Infix String] | Valid | Invalid

Info

Max

Top-1

5

0

1

2

3

4

5“3 * (2 + [ 12 / [ 4 * { 5 –4 } - 1 ] –1 ] )”

We are going to evaluate expressions with constants –Extrapolate it to variables!

3 | 3 * (2 + [ 12 / [ 4 * {5 – 4} -1 ] – 1 ] ) | | X

Infix Expression CompleteWhat Is Returned?

VALID –Stack is Empty!

45 46

# | Expression [Infix String] | Valid | Invalid

Info

Max

Top-1

5

0

1

2

3

4

5“3 * ) 2 + 3 (”

We are going to evaluate expressions with constants –Extrapolate it to variables!

Skip Skip Pop

4 | 3 * ) 2 + 3 ( | | X

UnsuccessfulPop –Return

INVALID

47 48

Page 9: Objectives - cs.trinity.eduthicks/2320-2004-S/Lecture-PDF/InfixPrefix... · # 1 1 2 3 Objectives 1. Evaluate Mathematical Expressions 2. Infix Expression Operators, Operands, Scope

# 9

# | Expression [Infix String] | Valid | Invalid

Info

Max

Top-1

5

0

1

2

3

4

5“3 * ( 2 + 12 / { 4 * [ 5 –4 } -1 ] )”

We are going to evaluate expressions with constants –Extrapolate it to variables!

Skip Skip Push

5 | 3 * (2 + 12 / {4 * [5 – 4} -1 ]) | | X

49

# | Expression [Infix String] | Valid | Invalid

Info

Max

Top0

5

0

1

2

3

4

5

(

“3 * ( 2 + 12 / { 4 * [ 5 –4 } -1 ] )”

We are going to evaluate expressions with constants –Extrapolate it to variables!

Skip Skip

Push

5 | 3 * (2 + 12 / {4 * [5 – 4} -1 ]) | | X

Skip Skip

50

# | Expression [Infix String] | Valid | Invalid

Info

Max

Top1

5

0

1

2

3

4

5

( {

“3 * ( 2 + 12 / { 4 * [ 5 –4 } -1 ] )”

We are going to evaluate expressions with constants –Extrapolate it to variables!

Push

5 | 3 * ( 2 + 12 / { 4 * [ 5 – 4 } -1 ] ) | | X

SkipSkip

51

# | Expression [Infix String] | Valid | Invalid

Info

Max

Top1

5

0

1

2

3

4

5

( {[

“3 * ( 2 + 12 / { 4 * [ 5–4 } -1 ] )”

We are going to evaluate expressions with constants –Extrapolate it to variables!

5 | 3 * ( 2 + 12 / { 4 * [ 5 – 4 } -1 ] ) | | X

Skip Skip PopSkip

[ } Not Match Return INVALID

52

53

# | Expression [Infix String] | Valid | Invalid

Info

Max

Top-1

5

0

1

2

3

4

5“( 3 + 2 ^ 3 ^ 2 / { 4 * [ 5 –4 ] * 1 } * 2”

We are going to evaluate expressions with constants –Extrapolate it to variables!

Push

X6 | ( 3 + 2 ^ 3 ^ 2 / {4 * [5 – 4] * 1 } * 2 | |

54

Page 10: Objectives - cs.trinity.eduthicks/2320-2004-S/Lecture-PDF/InfixPrefix... · # 1 1 2 3 Objectives 1. Evaluate Mathematical Expressions 2. Infix Expression Operators, Operands, Scope

# 10

# | Expression [Infix String] | Valid | Invalid

Info

Max

Top0

5

0

1

2

3

4

5

(

“( 3 + 2 ^ 3 ^ 2 / { 4 * [ 5 –4 ] *1 } * 2”

We are going to evaluate expressions with constants –Extrapolate it to variables!

Skip Skip

Push

X6 | ( 3 + 2 ^ 3 ^ 2 / {4 * [5 – 4] -1 } * 2 | |

Skip Skip

Skip Skip Skip Skip

55

# | Expression [Infix String] | Valid | Invalid

Info

Max

Top1

5

0

1

2

3

4

5

( {

“( 3 + 2 ^ 3 ^ 2 / { 4 * [ 5 –4 ] *1 } * 2”

We are going to evaluate expressions with constants –Extrapolate it to variables!

Push

X6 | ( 3 + 2 ^ 3 ^ 2 / {4 * [5 – 4] -1 } * 2 | |

Skip

Skip

56

# | Expression [Infix String] | Valid | Invalid

Info

Max

Top2

5

0

1

2

3

4

5

( { [

“( 3 + 2 ^ 3 ^ 2 / { 4 * [ 5 – 4 ] * 1 } * 2”

We are going to evaluate expressions with constants –Extrapolate it to variables!

Pop

X6 | ( 3 + 2 ^ 3 ^ 2 / {4 * [5 – 4] -1 } * 2 | |

Skip

Skip Skip

[ ] Set Match Continue

57

# | Expression [Infix String] | Valid | Invalid

Info

Max

Top1

5

0

1

2

3

4

5

( {

“( 3 + 2 ^ 3 ^ 2 / { 4 * [ 5 – 4 ] * 1 } * 2”

We are going to evaluate expressions with constants –Extrapolate it to variables!

Pop

X6 | ( 3 + 2 ^ 3 ^ 2 / {4 * [5 – 4] -1 } * 2 | |

Skip

Skip

{ } Set Match Continue

58

# | Expression [Infix String] | Valid | Invalid

Info

Max

Top0

5

0

1

2

3

4

5

(

“( 3 + 2 ^ 3 ^ 2 / { 4 * [ 5 – 4 ] * 1 } * 2”

We are going to evaluate expressions with constants –Extrapolate it to variables!

X6 | ( 3 + 2 ^ 3 ^ 2 / {4 * [5 – 4] -1 } * 2 | |

Skip

SkipIf Stack Empty Return VALID

If Stack Not Empty Return INVALID

59

Stack <char> ValidParen(5);

What type of stack was to Verify A Valid Infix?

Is It Correct Parentheses, Brackets,

and Braces?

60

Page 11: Objectives - cs.trinity.eduthicks/2320-2004-S/Lecture-PDF/InfixPrefix... · # 1 1 2 3 Objectives 1. Evaluate Mathematical Expressions 2. Infix Expression Operators, Operands, Scope

# 11

6161

62

Must Get Same Answer As Manual Solution!

Preserve The Mathematical Order Of Operations

5 + 2 ^ 3 ^ 2 = _______69 ?

517 ?Other ?

63

Maybe A Spreadsheet Will Help?

69 ?

517 ?Maybe Not!

5 + 2 ^ 3 ^ 2 = _______

64

Create Valid Postfix ExpressionStep 3

Preserve The Mathematical Order Of Operations

+

-* /

^

( ) # 1 Innermost First

Parenthes, Brackets, Braces

# 2 Right To Left# 4 Left To Right

# 4 Left To Right

# 3 Left To Right# 3 Left To Right

65

Create Valid Postfix Expression Step 2 Preserve The Mathematical Order Of Operations

1 | 5 + 2 ^ 3 ^ 2

2 | ( 2 + 3 ) * (5 – 2)

3 | 3 * 2 + 3 – 4 / 2 / 2 - 3

66

4 | ( 3 + 2 ^ 3 ^ 2 / {4 * [ 5 – 4 ] -1 } ) * 2

5 + 2 ^ 3 ^ 2

Create Postfix By Hand –Must Be Able To Do So To Verify Correctness Of Algorithm

Infix

Prefix

Postfix

5 2 3 2

5 2 3 2

Page 12: Objectives - cs.trinity.eduthicks/2320-2004-S/Lecture-PDF/InfixPrefix... · # 1 1 2 3 Objectives 1. Evaluate Mathematical Expressions 2. Infix Expression Operators, Operands, Scope

# 12

67

5 2 3 2

68

5 + 2 ^ 3 ^ 2

1 –Write Down All Of The Operands

Infix

Prefix

Postfix

5 2 3 2

5 2 3 2^

69

5 + 2 ^ 3 ^ 2

2 –Solve As If Doing Mathematically –Order Of Operations

Infix

Prefix

Postfix

5 2 ^3 2

Put Operator In Front Of Respective Operands For Prefix

Put Operator In Rear Of Respective Operands For Prefix

5 + 2 ^ 3 ^ 2

5 2 3 2^^

70

2 –Solve As If Doing Mathematically –Order Of Operations

Infix

Prefix

Postfix

5 ^2 ^3 2

5 2 3 2^

5 2 ^3 2

5 2 3 2^^+

71

5 + 2 ^ 3 ^ 2

2 –Solve As If Doing Mathematically –Order Of Operations

Infix

Prefix

Postfix

+5 ^2 ^3 2

5 2 3 2^^

5 ^2 ^3 2

72

Page 13: Objectives - cs.trinity.eduthicks/2320-2004-S/Lecture-PDF/InfixPrefix... · # 1 1 2 3 Objectives 1. Evaluate Mathematical Expressions 2. Infix Expression Operators, Operands, Scope

# 13

( 2 + 3 ) * ( 5 – 2 )

73

Infix

Prefix

Postfix

+ 2 3 5 2

2 3 + 5 2

2 3 5 2

2 3 5 2

Solve As If Doing Mathematically –Order Of Operations

( 2 + 3 ) * ( 5 – 2 )

74

Infix

Prefix

Postfix

+ 2 3 -5 2

2 3 + 5 2-

+ 2 3 5 2

2 3 + 5 2

Solve As If Doing Mathematically –Order Of Operations

( 2 + 3 ) * ( 5 – 2 )

75

Infix

Prefix

Postfix

* + 2 3 -5 2

2 3 + 5 2 - *

+ 2 3 -5 2

2 3 + 5 2-

Solve As If Doing Mathematically –Order Of Operations

76

77

Infix

Prefix

Postfix

3 * 2 + 3 – 4 / 2 / 2 - 3

3 2 3 4 2 2 3

3 2 3 4 2 2 3

* 3 2 3 4 2 2 3

3 2* 3 4 2 2 3

Solve As If Doing Mathematically –Order Of Operations

* 3 2 3 4 2 2 3

78

Infix

Prefix

Postfix

3 * 2 + 3 – 4 / 2 / 2 - 3

3 2* 3 4 2 2 3

* 3 2 3 /4 2 2 3

3 2* 3 4 2/ 2 3

Solve As If Doing Mathematically –Order Of Operations

Page 14: Objectives - cs.trinity.eduthicks/2320-2004-S/Lecture-PDF/InfixPrefix... · # 1 1 2 3 Objectives 1. Evaluate Mathematical Expressions 2. Infix Expression Operators, Operands, Scope

# 14

79

Infix

Prefix

Postfix

3 * 2 + 3 – 4 / 2 / 2 - 3

* 3 2 3 //4 2 2 3

3 2* 3 4 2/ 2/ 3

* 3 2 3 /4 2 2 3

3 2* 3 4 2/ 2 3

Solve As If Doing Mathematically –Order Of Operations

80

Infix

Prefix

Postfix

3 * 2 + 3 – 4 / 2 / 2 - 3

+* 3 2 3 //4 2 2 3

3 2* 3+ 4 2/ 2/ 3

* 3 2 3 //4 2 2 3

3 2* 3 4 2/ 2/ 3

Solve As If Doing Mathematically –Order Of Operations

81

Infix

Prefix

Postfix

3 * 2 + 3 – 4 / 2 / 2 - 3

- +* 3 2 3 //4 2 2 3

3 2* 3+ 4 2/ 2/- 3

+* 3 2 3 //4 2 2 3

3 2* 3+ 4 2/ 2/ 3

Solve As If Doing Mathematically –Order Of Operations

82

Infix

Prefix

Postfix

3 * 2 + 3 – 4 / 2 / 2 - 3

- - +* 3 2 3 //4 2 2 3

3 2* 3+ 4 2/ 2/- 3 -

- +* 3 2 3 //4 2 2 3

3 2* 3+ 4 2/ 2/- 3

Solve As If Doing Mathematically –Order Of Operations

83

( 3 + 2 ^ 3 ^ 2 / {4 * [ 5 – 4 ] * 1 } ) * 2

84

Infix

Prefix

Postfix

Solve As If Doing Mathematically –Order Of Operations

3 2 3 2 4 5 4 1 2

3 2 3 2 4 5 4 1 2

3 2 3 2 4 -5 4 1 2

3 2 3 2 4 5 4- 1 2

Page 15: Objectives - cs.trinity.eduthicks/2320-2004-S/Lecture-PDF/InfixPrefix... · # 1 1 2 3 Objectives 1. Evaluate Mathematical Expressions 2. Infix Expression Operators, Operands, Scope

# 15

( 3 + 2 ^ 3 ^ 2 / {4 * [ 5 – 4 ] * 1 } ) * 2

85

Infix

Prefix

Postfix

Solve As If Doing Mathematically –Order Of Operations

3 2 3 2 4 -5 4 1 2

3 2 3 2 4 5 4- 1 2

3 2 3 2 *4 -5 4 1 2

3 2 3 2 4 5 4-* 1 2

( 3 + 2 ^ 3 ^ 2 / {4 * [ 5 – 4 ] * 1 } ) * 2

86

Infix

Prefix

Postfix

Solve As If Doing Mathematically –Order Of Operations

3 2 3 2 **4 -5 4 1 2

3 2 3 2 4 5 4-* 1* 2

3 2 3 2 *4 -5 4 1 2

3 2 3 2 4 5 4-* 1 2

( 3 + 2 ^ 3 ^ 2 / {4 * [ 5 – 4 ] * 1 } ) * 2

87

Infix

Prefix

Postfix

Solve As If Doing Mathematically –Order Of Operations

3 2 3 2 **4 -5 4 1 2

3 2 3 2 4 5 4-* 1* 2

3 2 ^3 2 **4 -5 4 1 2

3 2 3 2^ 4 5 4-* 1* 2

( 3 + 2 ^ 3 ^ 2 / {4 * [ 5 – 4 ] * 1 } ) * 2

88

Infix

Prefix

Postfix

Solve As If Doing Mathematically –Order Of Operations

3 2 ^3 2 **4 -5 4 1 2

3 2 3 2^ 4 5 4-* 1* 2

3 ^2 ^3 2 **4 -5 4 1 2

3 2 3 2^^ 4 5 4-* 1* 2

( 3 + 2 ^ 3 ^ 2 / {4 * [ 5 – 4 ] * 1 } ) * 2

89

Infix

Prefix

Postfix

Solve As If Doing Mathematically –Order Of Operations

3 ^2 ^3 2 **4 -5 4 1 2

3 2 3 2^^ 4 5 4-* 1* 2

3 /^2 ^3 2 **4 -5 4 1 2

3 2 3 2^^ 4 5 4-* 1*/ 2

( 3 + 2 ^ 3 ^ 2 / {4 * [ 5 – 4 ] * 1 } ) * 2

90

Infix

Prefix

Postfix

Solve As If Doing Mathematically –Order Of Operations

+3 /^2 ^3 2 **4 -5 4 1 2

3 2 3 2^^ 4 5 4-* 1*/+ 2

3 /^2 ^3 2 **4 -5 4 1 2

3 2 3 2^^ 4 5 4-* 1*/ 2

Page 16: Objectives - cs.trinity.eduthicks/2320-2004-S/Lecture-PDF/InfixPrefix... · # 1 1 2 3 Objectives 1. Evaluate Mathematical Expressions 2. Infix Expression Operators, Operands, Scope

# 16

( 3 + 2 ^ 3 ^ 2 / {4 * [ 5 – 4 ] * 1 } ) * 2

91

Infix

Prefix

Postfix

Solve As If Doing Mathematically –Order Of Operations

*+3 /^2 ^3 2 **4 -5 4 1 2

3 2 3 2^^ 4 5 4-* 1*/+ 2*

+3 /^2 ^3 2 **4 -5 4 1 2

3 2 3 2^^ 4 5 4-* 1*/+ 2

92

Now that we can manually create the postfix expressions,

how do we do this with a computer program?

Info

Max

Top-1

5

0

1

2

3

4

5

93 94

95 96

Info

Max

Top-1

5

0

1

2

3

4

5( 2 + 3 ) * ( 5 – 2 )

( ICP = 7 Always PUSH opener to stack)

( ISP of Empty Stack = -1)

Stack <char> Postfix(5);

To Form Postfix•Examine one Infix character at a time –left to right

•If Operand, Write it to the Postfix String•If Scope Opener or Operator

•If the In-Coming Priority (ICP) is greater than the In-StackPriority (ISP) Push

•If ICP < ISP Pop –Write Values To Postfix Until Can Push•If Scope Closer, Pop till find opener –Write values to

Postfix –Throw out scope opener!•Empty Stack –Write Values To Postfix

2 3 + 5 2 - *

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2

Page 17: Objectives - cs.trinity.eduthicks/2320-2004-S/Lecture-PDF/InfixPrefix... · # 1 1 2 3 Objectives 1. Evaluate Mathematical Expressions 2. Infix Expression Operators, Operands, Scope

# 17

97

Info

Max

Top0

5

0

1

2

3

4

5

(

( 2 + 3 ) * ( 5 – 2 )

Write Operand To Postfix String

( ISP = That Of Top Char = 0

Postfix = 2

To Form Postfix•Examine one Infix character at a time –left to right

•If Operand, Write it to the Postfix String•If Scope Opener or Operator

•If the In-Coming Priority (ICP) is greater than the In-StackPriority (ISP) Push

•If ICP < ISP Pop –Write Values To Postfix Until Can Push•If Scope Closer, Pop till find opener –Write values to

Postfix –Throw out scope opener!•Empty Stack –Write Values To Postfix

2 3 + 5 2 - *

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2

98

Info

Max

Top0

5

0

1

2

3

4

5

(

( 2 + 3 ) * ( 5 – 2 )

ICP = 1 > ISP = 0 Push Operator

( ISP = That Of Top Char = 0

Postfix = 2

To Form Postfix•Examine one Infix character at a time –left to right

•If Operand, Write it to the Postfix String•If Scope Opener or Operator

•If the In-Coming Priority (ICP) is greater than the In-StackPriority (ISP) Push

•If ICP < ISP Pop –Write Values To Postfix Until Can Push•If Scope Closer, Pop till find opener –Write values to

Postfix –Throw out scope opener!•Empty Stack –Write Values To Postfix

2 3 + 5 2 - *

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2

99

Info

Max

Top1

5

0

1

2

3

4

5

(+

( 2 + 3 ) * ( 5 – 2 )

( ISP = That Of Top Char = 2

Postfix = 2

Write Operand To Postfix String

3

To Form Postfix•Examine one Infix character at a time –left to right

•If Operand, Write it to the Postfix String•If Scope Opener or Operator

•If the In-Coming Priority (ICP) is greater than the In-StackPriority (ISP) Push

•If ICP < ISP Pop –Write Values To Postfix Until Can Push•If Scope Closer, Pop till find opener –Write values to

Postfix –Throw out scope opener!•Empty Stack –Write Values To Postfix

2 3 + 5 2 - *

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2

100

Info

Max

Top1

5

0

1

2

3

4

5

(+

( 2 + 3 ) * ( 5 – 2 )

( ISP = That Of Top Char = 2

Postfix = 2 3

Scope Closer –Pop & Add To Postfix

+

To Form Postfix•Examine one Infix character at a time –left to right

•If Operand, Write it to the Postfix String•If Scope Opener or Operator

•If the In-Coming Priority (ICP) is greater than the In-StackPriority (ISP) Push

•If ICP < ISP Pop –Write Values To Postfix Until Can Push•If Scope Closer, Pop till find opener –Write values to

Postfix –Throw out scope opener!•Empty Stack –Write Values To Postfix

2 3 + 5 2 - *

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2

101

Info

Max

Top-1

5

0

1

2

3

4

5( 2 + 3 ) * ( 5 – 2 )

( ISP -1

Postfix = 2 3 +

ICP = 3 > ISP = -1 Push

To Form Postfix•Examine one Infix character at a time –left to right

•If Operand, Write it to the Postfix String•If Scope Opener or Operator

•If the In-Coming Priority (ICP) is greater than the In-StackPriority (ISP) Push

•If ICP < ISP Pop –Write Values To Postfix Until Can Push•If Scope Closer, Pop till find opener –Write values to

Postfix –Throw out scope opener!•Empty Stack –Write Values To Postfix

2 3 + 5 2 - *

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2

102

Info

Max

Top0

5

0

1

2

3

4

5

*

( 2 + 3 ) * ( 5 – 2 )

( ISP 4

Postfix = 2 3 +

( ICP = 7 Always PUSH opener to stack)

To Form Postfix•Examine one Infix character at a time –left to right

•If Operand, Write it to the Postfix String•If Scope Opener or Operator

•If the In-Coming Priority (ICP) is greater than the In-StackPriority (ISP) Push

•If ICP < ISP Pop –Write Values To Postfix Until Can Push•If Scope Closer, Pop till find opener –Write values to

Postfix –Throw out scope opener!•Empty Stack –Write Values To Postfix

2 3 + 5 2 - *

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2

Page 18: Objectives - cs.trinity.eduthicks/2320-2004-S/Lecture-PDF/InfixPrefix... · # 1 1 2 3 Objectives 1. Evaluate Mathematical Expressions 2. Infix Expression Operators, Operands, Scope

# 18

103

Info

Max

Top1

5

0

1

2

3

4

5

*(

( 2 + 3 ) * ( 5 – 2 )

( ISP 0

Postfix = 2 3 +

Write Operand To Postfix String

5

To Form Postfix•Examine one Infix character at a time –left to right

•If Operand, Write it to the Postfix String•If Scope Opener or Operator

•If the In-Coming Priority (ICP) is greater than the In-StackPriority (ISP) Push

•If ICP < ISP Pop –Write Values To Postfix Until Can Push•If Scope Closer, Pop till find opener –Write values to

Postfix –Throw out scope opener!•Empty Stack –Write Values To Postfix

2 3 + 5 2 - *

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2

104

Info

Max

Top1

5

0

1

2

3

4

5

*(

( 2 + 3 ) * ( 5 – 2 )

( ISP 0

Postfix = 2 3 + 5

ICP = 1 > ISP = 0 Push

To Form Postfix•Examine one Infix character at a time –left to right

•If Operand, Write it to the Postfix String•If Scope Opener or Operator

•If the In-Coming Priority (ICP) is greater than the In-StackPriority (ISP) Push

•If ICP < ISP Pop –Write Values To Postfix Until Can Push•If Scope Closer, Pop till find opener –Write values to

Postfix –Throw out scope opener!•Empty Stack –Write Values To Postfix

2 3 + 5 2 - *

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2

105

Info

Max

Top2

5

0

1

2

3

4

5

*( -

( 2 + 3 ) * ( 5 – 2 )

( ISP 1

Postfix = 2 3 + 5

Write Operand To Postfix String

2

To Form Postfix•Examine one Infix character at a time –left to right

•If Operand, Write it to the Postfix String•If Scope Opener or Operator

•If the In-Coming Priority (ICP) is greater than the In-StackPriority (ISP) Push

•If ICP < ISP Pop –Write Values To Postfix Until Can Push•If Scope Closer, Pop till find opener –Write values to

Postfix –Throw out scope opener!•Empty Stack –Write Values To Postfix

2 3 + 5 2 - *

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2

106

Info

Max

Top2

5

0

1

2

3

4

5

*( -

( 2 + 3 ) * ( 5 – 2 )

( ISP 1

Postfix = 2 3 + 5 2

Scope Closer –Pop & Add To Postfix

-

To Form Postfix•Examine one Infix character at a time –left to right

•If Operand, Write it to the Postfix String•If Scope Opener or Operator

•If the In-Coming Priority (ICP) is greater than the In-StackPriority (ISP) Push

•If ICP < ISP Pop –Write Values To Postfix Until Can Push•If Scope Closer, Pop till find opener –Write values to

Postfix –Throw out scope opener!•Empty Stack –Write Values To Postfix

2 3 + 5 2 - *

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2

107

Info

Max

Top0

5

0

1

2

3

4

5

*

( 2 + 3 ) * ( 5 – 2 )

( ISP 2

Postfix = 2 3 + 5 2 -

Done –Pop Stack & Add To Postfix

*

To Form Postfix•Examine one Infix character at a time –left to right

•If Operand, Write it to the Postfix String•If Scope Opener or Operator

•If the In-Coming Priority (ICP) is greater than the In-StackPriority (ISP) Push

•If ICP < ISP Pop –Write Values To Postfix Until Can Push•If Scope Closer, Pop till find opener –Write values to

Postfix –Throw out scope opener!•Empty Stack –Write Values To Postfix

2 3 + 5 2 - *

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2

108

Info

Max

Top-1

5

0

1

2

3

4

5

( 2 + 3 ) * ( 5 – 2 )

( ISP 2

Postfix = 2 3 + 5 2 - *

To Form Postfix•Examine one Infix character at a time –left to right

•If Operand, Write it to the Postfix String•If Scope Opener or Operator

•If the In-Coming Priority (ICP) is greater than the In-StackPriority (ISP) Push

•If ICP < ISP Pop –Write Values To Postfix Until Can Push•If Scope Closer, Pop till find opener –Write values to

Postfix –Throw out scope opener!•Empty Stack –Write Values To Postfix

2 3 + 5 2 - *

It is finished –Success! –Stack Is Empty!

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2

Page 19: Objectives - cs.trinity.eduthicks/2320-2004-S/Lecture-PDF/InfixPrefix... · # 1 1 2 3 Objectives 1. Evaluate Mathematical Expressions 2. Infix Expression Operators, Operands, Scope

# 19

109 110

Info

Max

Top-1

5

0

1

2

3

4

5

Infix = 5 + 2 ^ 3 ^ 2

ISP = -1

5 2 3 2 ^ ^ +

Postfix =

Write Operand To Postfix String

5

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2

111

Info

Max

Top-1

5

0

1

2

3

4

5

Infix = 5 + 2 ^ 3 ^ 2

ISP = -1

5 2 3 2 ^ ^ +

Postfix = 5

ICP = 1 > ISP -1 PUSH

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2

112

Info

Max

Top0

5

0

1

2

3

4

5

+

Infix = 5 + 2 ^ 3 ^ 2

ISP = 1

5 2 3 2 ^ ^ +

Postfix = 5

Write Operand To Postfix String

2

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2

113

Info

Max

Top0

5

0

1

2

3

4

5

+

Infix = 5 + 2 ^ 3 ^ 2

ISP = 2

5 2 3 2 ^ ^ +

Postfix = 5 2

ICP = 6 > ISP 2 PUSH

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2

114

Info

Max

Top1

5

0

1

2

3

4

5

+^

Infix = 5 + 2 ^ 3 ^ 2

ISP = 5

5 2 3 2 ^ ^ +

Postfix = 5 2

Write Operand To Postfix String

3

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2

Page 20: Objectives - cs.trinity.eduthicks/2320-2004-S/Lecture-PDF/InfixPrefix... · # 1 1 2 3 Objectives 1. Evaluate Mathematical Expressions 2. Infix Expression Operators, Operands, Scope

# 20

115

Info

Max

Top1

5

0

1

2

3

4

5

+^

Infix = 5 + 2 ^ 3 ^ 2

ISP = 5

5 2 3 2 ^ ^ +

Postfix = 5 2 3

ICP = 6 > ISP 5 Push

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2

116

Info

Max

Top2

5

0

1

2

3

4

5

+^ ^

Infix = 5 + 2 ^ 3 ^ 2

ISP = 5

5 2 3 2 ^ ^ +

Postfix = 5 2 3

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2

Write Operand To Postfix String

2

117

Info

Max

Top2

5

0

1

2

3

4

5

+^ ^

Infix = 5 + 2 ^ 3 ^ 2

ISP = 5

5 2 3 2 ^ ^ +

Postfix = 5 2 3 2

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2

^ ^ +

Done –Pop Stack & Add To Postfix

118

119

Info

Max

Top-1

5

0

1

2

3

4

5

Infix = 3 * 2 + 3 – 4 / 2 / 2 – 3

ISP = -1

3 2 * 3 + 4 2 / 2 / - 3 -

Postfix =

Write Operand To Postfix String

3

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2

120

Info

Max

Top-1

5

0

1

2

3

4

5

Infix = 3 * 2 + 3 – 4 / 2 / 2 - 3

ISP = -1

Postfix = 3

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2

ICP = 3 > ISP = -1 Push *

3 2 * 3 + 4 2 / 2 / - 3 -

Page 21: Objectives - cs.trinity.eduthicks/2320-2004-S/Lecture-PDF/InfixPrefix... · # 1 1 2 3 Objectives 1. Evaluate Mathematical Expressions 2. Infix Expression Operators, Operands, Scope

# 21

121

Info

Max

Top-1

5

0

1

2

3

4

5

*

Infix = 3 * 2 + 3 – 4 / 2 / 2 - 3

ISP = 4

Postfix = 3

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2

Write Operand To Postfix String

2

3 2 * 3 + 4 2 / 2 / - 3 -

122

Info

Max

Top-1

5

0

1

2

3

4

5

*

Infix = 3 * 2 + 3 – 4 / 2 / 2 - 3

ISP = 4

Postfix = 3 2

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2

ICP = 1 < ISP = 4 Pop * Add To Postfix & Try Again! Push +

*

3 2 * 3 + 4 2 / 2 / - 3 -

123

Info

Max

Top-1

5

0

1

2

3

4

5

+

Infix = 3 * 2 + 3 – 4 / 2 / 2 - 3

ISP = 2

Postfix = 3 2 *

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2

3

Write Operand To Postfix String

3 2 * 3 + 4 2 / 2 / - 3 -

124

Info

Max

Top-1

5

0

1

2

3

4

5

+

Infix = 3 * 2 + 3 – 4 / 2 / 2 - 3

ISP = 2

Postfix = 3 2 * 3

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2

+

ICP = 1 < ISP = 2 Pop + Add To Postfix & Try Again! Push -

3 2 * 3 + 4 2 / 2 / - 3 -

125

Info

Max

Top-1

5

0

1

2

3

4

5

-

Infix = 3 * 2 + 3 – 4 / 2 / 2 - 3

ISP = 2

Postfix = 3 2 * 3 +

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2

4

Write Operand To Postfix String

3 2 * 3 + 4 2 / 2 / - 3 -

126

Info

Max

Top-1

5

0

1

2

3

4

5

-

Infix = 3 * 2 + 3 – 4 / 2 / 2 - 3

ISP = 2

Postfix = 3 2 * 3 + 4

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2

ICP = 3 > ISP = 2 Push /

3 2 * 3 + 4 2 / 2 / - 3 -

Page 22: Objectives - cs.trinity.eduthicks/2320-2004-S/Lecture-PDF/InfixPrefix... · # 1 1 2 3 Objectives 1. Evaluate Mathematical Expressions 2. Infix Expression Operators, Operands, Scope

# 22

127

Info

Max

Top0

5

0

1

2

3

4

5

-/

Infix = 3 * 2 + 3 – 4 / 2 / 2 - 3

ISP = 2

Postfix = 3 2 * 3 + 4

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2

2

3 2 * 3 + 4 2 / 2 / - 3 -

Write Operand To Postfix String

128

Info

Max

Top1

5

0

1

2

3

4

5

-/

Infix = 3 * 2 + 3 – 4 / 2 / 2 - 3

ISP = 4

Postfix = 3 2 * 3 + 4 2

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2

/

3 2 * 3 + 4 2 / 2 / - 3 -

ICP = 3 < ISP = 4 Pop / Add To Postfix & Try Again! Push /

129

Info

Max

Top1

5

0

1

2

3

4

5

-/

Infix = 3 * 2 + 3 – 4 / 2 / 2 - 3

ISP = 4

Postfix = 3 2 * 3 + 4 2 /

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2

2

3 2 * 3 + 4 2 / 2 / - 3 -

Write Operand To Postfix String

130

Info

Max

Top1

5

0

1

2

3

4

5

-/

Infix = 3 * 2 + 3 – 4 / 2 / 2 - 3

ISP = 4

Postfix = 3 2 * 3 + 4 2 / 2

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2

/

3 2 * 3 + 4 2 / 2 / - 3 -

ICP = 1 < ISP = 4 Pop / Add To Postfix & Try Again! Pop Add To Postfix & Try Again! Push -

-

131

Info

Max

Top0

5

0

1

2

3

4

5

-

Infix = 3 * 2 + 3 – 4 / 2 / 2 - 3

ISP = 2

Postfix = 3 2 * 3 + 4 2 / 2 / -

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2

3

3 2 * 3 + 4 2 / 2 / - 3 -

Write Operand To Postfix String

132

Info

Max

Top0

5

0

1

2

3

4

5

-

Infix = 3 * 2 + 3 – 4 / 2 / 2 - 3

ISP = 2

Postfix = 3 2 * 3 + 4 2 / 2 / - 3

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2

-

3 2 * 3 + 4 2 / 2 / - 3 -

Done Pop Stack & Add To Postfix

Page 23: Objectives - cs.trinity.eduthicks/2320-2004-S/Lecture-PDF/InfixPrefix... · # 1 1 2 3 Objectives 1. Evaluate Mathematical Expressions 2. Infix Expression Operators, Operands, Scope

# 23

133 134

Info

Max

Top-1

5

0

1

2

3

4

5

ISP = -1

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *

Postfix =

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2( 3 + 2 ^ 3 ^ 2 / { 4 * [ 5 – 4 ] * 1 } ) * 2

( ICP = 7 Always PUSH opener to stack)

135

Info

Max

Top0

5

0

1

2

3

4

5

(

ISP = 0

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *

Postfix = 3

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2( 3 + 2 ^ 3 ^ 2 / { 4 * [ 5 – 4 ] * 1 } ) * 2

Write Operand To Postfix String

136

Info

Max

Top0

5

0

1

2

3

4

5

(

ISP = 0

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *

Postfix = 3

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2( 3 + 2 ^ 3 ^ 2 / { 4 * [ 5 – 4 ] * 1 } ) * 2

ICP = 1 > ISP = 0 Push +

137

Info

Max

Top1

5

0

1

2

3

4

5

( +

ISP = 2

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *

Postfix = 3

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2( 3 + 2 ^ 3 ^ 2 / { 4 * [ 5 – 4 ] * 1 } ) * 2

2

Write Operand To Postfix String

138

Info

Max

Top1

5

0

1

2

3

4

5

( +

ISP = 2

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *

Postfix = 3 2

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2( 3 + 2 ^ 3 ^ 2 / { 4 * [ 5 – 4 ] * 1 } ) * 2

ICP = 6 > ISP = 2 Push ^

Page 24: Objectives - cs.trinity.eduthicks/2320-2004-S/Lecture-PDF/InfixPrefix... · # 1 1 2 3 Objectives 1. Evaluate Mathematical Expressions 2. Infix Expression Operators, Operands, Scope

# 24

139

Info

Max

Top2

5

0

1

2

3

4

5

( +^

ISP = 5

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *

Postfix = 3 2

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2( 3 + 2 ^ 3 ^ 2 / { 4 * [ 5 – 4 ] * 1 } ) * 2

Write Operand To Postfix String

3

140

Info

Max

Top2

5

0

1

2

3

4

5

( +^

ISP = 5

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *

Postfix = 3 2 3

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2( 3 + 2 ^ 3 ^ 2 / { 4 * [ 5 – 4 ] * 1 } ) * 2

ICP = 6 > ISP = 5 Push ^

141

Info

Max

Top2

5

0

1

2

3

4

5

( +^

ISP = 5

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *

Postfix = 3 2 3

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2( 3 + 2 ^ 3 ^ 2 / { 4 * [ 5 – 4 ] * 1 } ) * 2

2

Write Operand To Postfix String

^

142

Info

Max

Top3

5

0

1

2

3

4

5

( +^

ISP = 5

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *

Postfix = 3 2 3 2

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2( 3 + 2 ^ 3 ^ 2 / { 4 * [ 5 – 4 ] * 1 } ) * 2

^

^

ICP = 3 < ISP = 5 Pop ^Add To Postfix & Try Again! Pop ^Add To Postfix & Try Again! Push /

^

143

Info

Max

Top2

5

0

1

2

3

4

5

( +/

ISP = 4

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *

Postfix = 3 2 3 2 ^ ^

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2( 3 + 2 ^ 3 ^ 2 / { 4 * [ 5 – 4 ] * 1 } ) * 2

(ICP = 7 > ISP = 4 PushAlways PUSH opener to stack

144

Info

Max

Top3

5

0

1

2

3

4

5

( +/

ISP = 0

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *

Postfix = 3 2 3 2 ^ ^

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2( 3 + 2 ^ 3 ^ 2 / { 4 * [ 5 – 4 ] * 1 } ) * 2

4

{

Write Operand To Postfix String

Page 25: Objectives - cs.trinity.eduthicks/2320-2004-S/Lecture-PDF/InfixPrefix... · # 1 1 2 3 Objectives 1. Evaluate Mathematical Expressions 2. Infix Expression Operators, Operands, Scope

# 25

145

Info

Max

Top3

5

0

1

2

3

4

5

( +/

ISP = 0

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *

Postfix = 3 2 3 2 ^ ^ 4

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2( 3 + 2 ^ 3 ^ 2 / { 4 * [ 5 – 4 ] * 1 } ) * 2

{

ICP = 3 > ISP = 0 Push *

146

Info

Max

Top4

5

0

1

2

3

4

5

( +/

ISP = 4

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *

Postfix = 3 2 3 2 ^ ^ 4

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2( 3 + 2 ^ 3 ^ 2 / { 4 * [ 5 – 4 ] * 1 } ) * 2

{*

(ICP = 7 > ISP = 4 PushAlways PUSH opener to stack

147

Info

Max

Top5

5

0

1

2

3

4

5

( +/

[

ISP = 0

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *

Postfix = 3 2 3 2 ^ ^ 4

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2( 3 + 2 ^ 3 ^ 2 / { 4 * [ 5 – 4 ] * 1 } ) * 2

{*

5

Write Operand To Postfix String

148

Info

Max

Top5

5

0

1

2

3

4

5

( +/

[

ISP = 0

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *

Postfix = 3 2 3 2 ^ ^ 4 5

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2( 3 + 2 ^ 3 ^ 2 / { 4 * [ 5 – 4 ] * 1 } ) * 2

{*

(ICP = 1 > ISP = 0 PushAlways PUSH opener to stack

149

Info

Top5

0

1

2

3

4

5

( +/

[

ISP = 1

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *

Postfix = 3 2 3 2 ^ ^ 4 5

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2( 3 + 2 ^ 3 ^ 2 / { 4 * [ 5 – 4 ] * 1 } ) * 2

{*

4

6 -

Write Operand To Postfix String

150

Info

Top5

0

1

2

3

4

5

( +/

[

ISP = 1

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *

Postfix = 3 2 3 2 ^ ^ 4 5

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2( 3 + 2 ^ 3 ^ 2 / { 4 * [ 5 – 4 ] * 1 } ) * 2

{*

-

6 -

Pop Stack Till Find Matching Opener Add - To Postfix

Page 26: Objectives - cs.trinity.eduthicks/2320-2004-S/Lecture-PDF/InfixPrefix... · # 1 1 2 3 Objectives 1. Evaluate Mathematical Expressions 2. Infix Expression Operators, Operands, Scope

# 26

151

Info

Top4

0

1

2

3

4

5

( +/

ISP = 4

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *

Postfix = 3 2 3 2 ^ ^ 4 5 -

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2( 3 + 2 ^ 3 ^ 2 / { 4 * [ 5 – 4 ] * 1 } ) * 2

{*

6

(ICP = 1 > ISP = 4 Pop *Add To Postfix & Try Again! Push -

*

152

Info

Top4

0

1

2

3

4

5

( +/

ISP = 2

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *

Postfix = 3 2 3 2 ^ ^ 4 5 - *

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2( 3 + 2 ^ 3 ^ 2 / { 4 * [ 5 – 4 ] * 1 } ) * 2

{-

6

1

Write Operand To Postfix String

153

Info

Top4

0

1

2

3

4

5

( +/

ISP = 2

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *

Postfix = 3 2 3 2 ^ ^ 4 5 - * 1

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2( 3 + 2 ^ 3 ^ 2 / { 4 * [ 5 – 4 ] * 1 } ) * 2

{-

6

-

Pop Stack Till Find Matching Opener Add - To Postfix

154

Info

Top2

0

1

2

3

4

5

( +/

ISP = 4

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *

Postfix = 3 2 3 2 ^ ^ 4 5 - * 1 -

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2( 3 + 2 ^ 3 ^ 2 / { 4 * [ 5 – 4 ] * 1 } ) * 2

6

/

Pop Stack Till Find Matching Opener Add - To Postfix

+

155

Info

Top-1

0

1

2

3

4

5

ISP = -1

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *

Postfix =

3 2 3 2 ^ ^ 4 5 - * 1 - / +

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2( 3 + 2 ^ 3 ^ 2 / { 4 * [ 5 – 4 ] * 1 } ) * 2

6

(ICP = 3 > ISP = -1 Push *

156

Info

Top0

0

1

2

3

4

5

*

ISP = 4

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *

Postfix =

3 2 3 2 ^ ^ 4 5 - * 1 - / +

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2( 3 + 2 ^ 3 ^ 2 / { 4 * [ 5 – 4 ] * 1 } ) * 2

6

Write Operand To Postfix String

2

Page 27: Objectives - cs.trinity.eduthicks/2320-2004-S/Lecture-PDF/InfixPrefix... · # 1 1 2 3 Objectives 1. Evaluate Mathematical Expressions 2. Infix Expression Operators, Operands, Scope

# 27

157

Info

Top0

0

1

2

3

4

5

*

ISP = 4

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *

Postfix =

3 2 3 2 ^ ^ 4 5 - * 1 - / + 2

ICP ISP

+ -* /^({[)}]

1

3

6

7

-

0

5

4

2( 3 + 2 ^ 3 ^ 2 / { 4 * [ 5 – 4 ] * 1 } ) * 2

6

*

Done Pop Stack & Add To Postfix

Stack <char> Postfix(5);

What type of stack was to

create the Postfix from the Infix?

158

159 160

Create Valid Postfix ExpressionStep 3

Preserve The Mathematical Order Of Operations

+

-* /

^

( ) # 1 Innermost First

Parenthes, Brackets, Braces

# 2 Right To Left# 4 Left To Right

# 4 Left To Right

# 3 Left To Right# 3 Left To Right

161

Evaluate Postfix Expression Step 2 Preserve The Mathematical Order Of Operations

1 | 5 + 2 ^ 3 ^ 2

2 | ( 2 + 3 ) * (5 – 2)

3 | 3 * 2 + 3 – 4 / 2 / 2 – 3

4 | ( 3 + 2 ^ 3 ^ 2 / {4 * [ 5 – 4 ] * 1 } ) * 2

5 2 3 2 ^ ^ +

2 3 + 5 2 - *

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *

3 2 * 3 + 4 2 / 2 / - 3 -

= _______

= _______

= _______

= _______

15

517

5

262

162

Page 28: Objectives - cs.trinity.eduthicks/2320-2004-S/Lecture-PDF/InfixPrefix... · # 1 1 2 3 Objectives 1. Evaluate Mathematical Expressions 2. Infix Expression Operators, Operands, Scope

# 28

163

1 | 5 + 2 ^ 3 ^ 2

5 2 3 2 ^ ^ += _______517

Info

Max

Top-1

5

0

1

2

3

4

5

5 2 3 2 ^ ^ +

Push Operands To Stack

Stack <double> Eval (5);

164

1 | 5 + 2 ^ 3 ^ 2

5 2 3 2 ^ ^ += _______517

Info

Max

Top0

5

0

1

2

3

4

5

5

5 2 3 2 ^ ^ +

Push Operands To Stack

Stack <double> Eval (5);

165

1 | 5 + 2 ^ 3 ^ 2

5 2 3 2 ^ ^ += _______517

Info

Max

Top1

5

0

1

2

3

4

5

52

5 2 3 2 ^ ^ +

Push Operands To Stack

Stack <double> Eval (5);

166

1 | 5 + 2 ^ 3 ^ 2

5 2 3 2 ^ ^ += _______517

Info

Max

Top2

5

0

1

2

3

4

5

52 3

5 2 3 2 ^ ^ +

Push Operands To Stack

Stack <double> Eval (5);

167

1 | 5 + 2 ^ 3 ^ 2

5 2 3 2 ^ ^ += _______517

Info

Max

Top3

6

0

1

2

3

4

5

52 3 2

5 2 3 2 ^ ^ +

Operator –Pop Stack Twice

Stack <double> Eval (5);

23 ^

Apply Operator

Evaluate Expression & Push

= 9

Push 9

Info

Max

Top2

5

0

1

2

3

4

5

52 9

168

Page 29: Objectives - cs.trinity.eduthicks/2320-2004-S/Lecture-PDF/InfixPrefix... · # 1 1 2 3 Objectives 1. Evaluate Mathematical Expressions 2. Infix Expression Operators, Operands, Scope

# 29

1 | 5 + 2 ^ 3 ^ 2

5 2 3 2 ^ ^ += _______517

5 2 3 2 ^ ^ +

Operator –Pop Stack Twice

Stack <double> Eval (5);

Pop –Evaluate 2 ^ 9Push 512

Info

Max

Top2

5

0

1

2

3

4

5

52 9

169

1 | 5 + 2 ^ 3 ^ 2

5 2 3 2 ^ ^ += _______517

5 2 3 2 ^ ^ +

Operator –Pop Stack Twice

Stack <double> Eval (5);

Pop –Evaluate 5 + 512Push 517

Info

Max

Top1

5

0

1

2

3

4

5

5512

170

1 | 5 + 2 ^ 3 ^ 2

5 2 3 2 ^ ^ += _______517

5 2 3 2 ^ ^ +

Done –Only Item In Stack Contains Evaluated Expression

Stack <double> Eval (5);

Info

Max

Top0

5

0

1

2

3

4

5

517

171 172

= _______

Info

Max

Top-1

5

0

1

2

3

4

5

Push

2 | ( 2 + 3 ) * (5 – 2)

2 3 + 5 2 - * 15

2 3 + 5 2 - *

173

= _______

Info

Max

Top0

5

0

1

2

3

4

5

2Push

2 | ( 2 + 3 ) * (5 – 2)

2 3 + 5 2 - * 15

2 3 + 5 2 - *

174

Page 30: Objectives - cs.trinity.eduthicks/2320-2004-S/Lecture-PDF/InfixPrefix... · # 1 1 2 3 Objectives 1. Evaluate Mathematical Expressions 2. Infix Expression Operators, Operands, Scope

# 30

= _______

Info

Max

Top1

5

0

1

2

3

4

5

23

Pop Evaluate 2 + 3 Push 5

2 | ( 2 + 3 ) * (5 – 2)

2 3 + 5 2 - * 15

2 3 + 5 2 - *

175

= _______

Info

Max

Top0

5

0

1

2

3

4

5

5

2 | ( 2 + 3 ) * (5 – 2)

2 3 + 5 2 - * 15

2 3 + 5 2 - *

Push

176

= _______

Info

Max

Top1

5

0

1

2

3

4

5

55

2 | ( 2 + 3 ) * (5 – 2)

2 3 + 5 2 - * 15

2 3 + 5 2 - *

Push

177

= _______

Info

Max

Top2

5

0

1

2

3

4

5

55 2

2 | ( 2 + 3 ) * (5 – 2)

2 3 + 5 2 - * 15

2 3 + 5 2 - *

Pop –Evaluate 5 - 2 Push 3

178

= _______

Info

Max

Top1

5

0

1

2

3

4

5

53

2 | ( 2 + 3 ) * (5 – 2)

2 3 + 5 2 - * 15

2 3 + 5 2 - *

Pop –Evaluate 5 * 3 Push 15

179

= _______

Info

Max

Top0

5

0

1

2

3

4

5

15

2 | ( 2 + 3 ) * (5 – 2)

2 3 + 5 2 - * 15

2 3 + 5 2 - *

Done –Only Item In Stack Contains Evaluated Expression

180

Page 31: Objectives - cs.trinity.eduthicks/2320-2004-S/Lecture-PDF/InfixPrefix... · # 1 1 2 3 Objectives 1. Evaluate Mathematical Expressions 2. Infix Expression Operators, Operands, Scope

# 31

181

= _______

Info

Max

Top-1

5

0

1

2

3

4

5

Push

3 2 * 3 + 4 2 / 2 / - 3 -

3 | 3 * 2 + 3 – 4 / 2 / 2 – 3

3 2 * 3 + 4 2 / 2 / - 3 -

5

3 2 * 3 + 4 2 / 2 / - 3 -

182

= _______

Info

Max

Top0

5

0

1

2

3

4

5

3 Push

3 2 * 3 + 4 2 / 2 / - 3 -

3 | 3 * 2 + 3 – 4 / 2 / 2 – 3

3 2 * 3 + 4 2 / 2 / - 3 -

5

3 2 * 3 + 4 2 / 2 / - 3 -

183

= _______

Info

Max

Top1

5

0

1

2

3

4

5

3 2

3 2 * 3 + 4 2 / 2 / - 3 -

3 | 3 * 2 + 3 – 4 / 2 / 2 – 3

3 2 * 3 + 4 2 / 2 / - 3 -

5

3 2 * 3 + 4 2 / 2 / - 3 -

Pop Evaluate 3 * 2 Push 6

184

= _______

Info

Max

Top0

5

0

1

2

3

4

5

6

3 2 * 3 + 4 2 / 2 / - 3 -

3 | 3 * 2 + 3 – 4 / 2 / 2 – 3

3 2 * 3 + 4 2 / 2 / - 3 -

5

3 2 * 3 + 4 2 / 2 / - 3 -

Push

185

= _______

Info

Max

Top1

5

0

1

2

3

4

5

6 3

3 2 * 3 + 4 2 / 2 / - 3 -

3 | 3 * 2 + 3 – 4 / 2 / 2 – 3

3 2 * 3 + 4 2 / 2 / - 3 -

5

3 2 * 3 + 4 2 / 2 / - 3 -

Pop Evaluate 6 + 3 Push 9

186

Page 32: Objectives - cs.trinity.eduthicks/2320-2004-S/Lecture-PDF/InfixPrefix... · # 1 1 2 3 Objectives 1. Evaluate Mathematical Expressions 2. Infix Expression Operators, Operands, Scope

# 32

= _______

Info

Max

Top0

5

0

1

2

3

4

5

9

3 2 * 3 + 4 2 / 2 / - 3 -

3 | 3 * 2 + 3 – 4 / 2 / 2 – 3

3 2 * 3 + 4 2 / 2 / - 3 -

5

3 2 * 3 + 4 2 / 2 / - 3 -

Push

187

= _______

Info

Max

Top1

5

0

1

2

3

4

5

9 4

3 2 * 3 + 4 2 / 2 / - 3 -

3 | 3 * 2 + 3 – 4 / 2 / 2 – 3

3 2 * 3 + 4 2 / 2 / - 3 -

5

3 2 * 3 + 4 2 / 2 / - 3 -

Push

188

= _______

Info

Max

Top2

5

0

1

2

3

4

5

9 4 2

3 2 * 3 + 4 2 / 2 / - 3 -

3 | 3 * 2 + 3 – 4 / 2 / 2 – 3

3 2 * 3 + 4 2 / 2 / - 3 -

5

3 2 * 3 + 4 2 / 2 / - 3 -

Pop Evaluate 4 / 2 Push 2

189

= _______

Info

Max

Top1

5

0

1

2

3

4

5

9 2

3 2 * 3 + 4 2 / 2 / - 3 -

3 | 3 * 2 + 3 – 4 / 2 / 2 – 3

3 2 * 3 + 4 2 / 2 / - 3 -

5

3 2 * 3 + 4 2 / 2 / - 3 -

Push

190

= _______

Info

Max

Top2

5

0

1

2

3

4

5

9 2 2

3 2 * 3 + 4 2 / 2 / - 3 -

3 | 3 * 2 + 3 – 4 / 2 / 2 – 3

3 2 * 3 + 4 2 / 2 / - 3 -

5

3 2 * 3 + 4 2 / 2 / - 3 -

Pop Evaluate 2 / 2 Push 1

191

= _______

Info

Max

Top1

5

0

1

2

3

4

5

9 1

3 2 * 3 + 4 2 / 2 / - 3 -

3 | 3 * 2 + 3 – 4 / 2 / 2 – 3

3 2 * 3 + 4 2 / 2 / - 3 -

5

3 2 * 3 + 4 2 / 2 / - 3 -

Pop Evaluate 9 - 1 Push 8

192

Page 33: Objectives - cs.trinity.eduthicks/2320-2004-S/Lecture-PDF/InfixPrefix... · # 1 1 2 3 Objectives 1. Evaluate Mathematical Expressions 2. Infix Expression Operators, Operands, Scope

# 33

= _______

Info

Max

Top0

5

0

1

2

3

4

5

8

3 2 * 3 + 4 2 / 2 / - 3 -

3 | 3 * 2 + 3 – 4 / 2 / 2 – 3

3 2 * 3 + 4 2 / 2 / - 3 -

5

3 2 * 3 + 4 2 / 2 / - 3 -

Push

193

= _______

Info

Max

Top1

5

0

1

2

3

4

5

8 3

3 2 * 3 + 4 2 / 2 / - 3 -

3 | 3 * 2 + 3 – 4 / 2 / 2 – 3

3 2 * 3 + 4 2 / 2 / - 3 -

5

3 2 * 3 + 4 2 / 2 / - 3 -

Pop Evaluate 8 - 3 Push 5

194

= _______

Info

Max

Top0

5

0

1

2

3

4

5

5

3 2 * 3 + 4 2 / 2 / - 3 -

3 | 3 * 2 + 3 – 4 / 2 / 2 – 3

3 2 * 3 + 4 2 / 2 / - 3 -

5

3 2 * 3 + 4 2 / 2 / - 3 -Done –Only Item In Stack Contains Evaluated Expression

195 196

= _______

Info

Max

Top-1

5

0

1

2

3

4

53 2 * 3 + 4 2 / 2 / - 3 -

4 | ( 3 + 2 ^ 3 ^ 2 / {4 * [ 5 – 4 ] * 1 } ) * 2

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *262

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *

Push

197

= _______

Info

Max

Top0

5

0

1

2

3

4

5

3

3 2 * 3 + 4 2 / 2 / - 3 -

4 | ( 3 + 2 ^ 3 ^ 2 / {4 * [ 5 – 4 ] * 1 } ) * 2

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *262

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *

Push

198

Page 34: Objectives - cs.trinity.eduthicks/2320-2004-S/Lecture-PDF/InfixPrefix... · # 1 1 2 3 Objectives 1. Evaluate Mathematical Expressions 2. Infix Expression Operators, Operands, Scope

# 34

= _______

Info

Max

Top1

5

0

1

2

3

4

5

32

3 2 * 3 + 4 2 / 2 / - 3 -

4 | ( 3 + 2 ^ 3 ^ 2 / {4 * [ 5 – 4 ] * 1 } ) * 2

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *262

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *

Push

199

= _______

Info

Max

Top2

5

0

1

2

3

4

5

323

3 2 * 3 + 4 2 / 2 / - 3 -

4 | ( 3 + 2 ^ 3 ^ 2 / {4 * [ 5 – 4 ] * 1 } ) * 2

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *262

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *

Push

200

= _______

Info

Max

Top3

5

0

1

2

3

4

5

3232

3 2 * 3 + 4 2 / 2 / - 3 -

4 | ( 3 + 2 ^ 3 ^ 2 / {4 * [ 5 – 4 ] * 1 } ) * 2

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *262

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *

Pop Evaluate 3 ^ 2 Push 9

201

= _______

Info

Max

Top2

5

0

1

2

3

4

5

329

3 2 * 3 + 4 2 / 2 / - 3 -

4 | ( 3 + 2 ^ 3 ^ 2 / {4 * [ 5 – 4 ] * 1 } ) * 2

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *262

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *

Pop Evaluate 2 ^ 9 Push 512

202

= _______

Info

Max

Top1

5

0

1

2

3

4

5

3512

3 2 * 3 + 4 2 / 2 / - 3 -

4 | ( 3 + 2 ^ 3 ^ 2 / {4 * [ 5 – 4 ] * 1 } ) * 2

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *262

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *

Push

203

= _______

Info

Max

Top2

5

0

1

2

3

4

5

3512

4

4 | ( 3 + 2 ^ 3 ^ 2 / {4 * [ 5 – 4 ] * 1 } ) * 2

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *262

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *

Push

204

Page 35: Objectives - cs.trinity.eduthicks/2320-2004-S/Lecture-PDF/InfixPrefix... · # 1 1 2 3 Objectives 1. Evaluate Mathematical Expressions 2. Infix Expression Operators, Operands, Scope

# 35

= _______

Info

Max

Top3

5

0

1

2

3

4

5

3512

45

4 | ( 3 + 2 ^ 3 ^ 2 / {4 * [ 5 – 4 ] * 1 } ) * 2

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *262

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *

Push

205

= _______

Info

Max

Top4

5

0

1

2

3

4

5

3512

454

4 | ( 3 + 2 ^ 3 ^ 2 / {4 * [ 5 – 4 ] * 1 } ) * 2

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *262

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *

Pop Evaluate 5 - 4 Push 1

206

= _______

Info

Max

Top3

5

0

1

2

3

4

5

3512

41

4 | ( 3 + 2 ^ 3 ^ 2 / {4 * [ 5 – 4 ] * 1 } ) * 2

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *262

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *

Pop Evaluate 4 * 1 Push 4

207

= _______

Info

Max

Top2

5

0

1

2

3

4

5

3512

4

4 | ( 3 + 2 ^ 3 ^ 2 / {4 * [ 5 – 4 ] * 1 } ) * 2

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *262

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *

Push

208

= _______

Info

Max

Top3

5

0

1

2

3

4

5

3512

41

4 | ( 3 + 2 ^ 3 ^ 2 / {4 * [ 5 – 4 ] * 1 } ) * 2

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *262

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *

Pop Evaluate 4 * 1 Push 4

209

= _______

Info

Max

Top2

5

0

1

2

3

4

5

3512

4

4 | ( 3 + 2 ^ 3 ^ 2 / {4 * [ 5 – 4 ] * 1 } ) * 2

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *262

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *

Pop Evaluate 512 / 4 Push 128

210

Page 36: Objectives - cs.trinity.eduthicks/2320-2004-S/Lecture-PDF/InfixPrefix... · # 1 1 2 3 Objectives 1. Evaluate Mathematical Expressions 2. Infix Expression Operators, Operands, Scope

# 36

= _______

Info

Max

Top1

5

0

1

2

3

4

5

3128

4 | ( 3 + 2 ^ 3 ^ 2 / {4 * [ 5 – 4 ] * 1 } ) * 2

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *262

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *

Pop Evaluate 3 +128 Push 131

211

= _______

Info

Max

Top0

5

0

1

2

3

4

5

131

4 | ( 3 + 2 ^ 3 ^ 2 / {4 * [ 5 – 4 ] * 1 } ) * 2

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *262

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *

Push

212

= _______

Info

Max

Top1

5

0

1

2

3

4

5

1312

4 | ( 3 + 2 ^ 3 ^ 2 / {4 * [ 5 – 4 ] * 1 } ) * 2

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *262

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *

Pop Evaluate 131 * 2 Push 262

213

= _______

Info

Max

Top0

5

0

1

2

3

4

5

262

4 | ( 3 + 2 ^ 3 ^ 2 / {4 * [ 5 – 4 ] * 1 } ) * 2

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *262

3 2 3 2 ^ ^ 4 5 4 - * 1 * / + 2 *

Done –Only Item In Stack Contains Evaluated Expression

214

Stack <double> Eval(5);

What type of stack was to Evaluate The

Postfix?

215 216

Page 37: Objectives - cs.trinity.eduthicks/2320-2004-S/Lecture-PDF/InfixPrefix... · # 1 1 2 3 Objectives 1. Evaluate Mathematical Expressions 2. Infix Expression Operators, Operands, Scope

# 37

Extra Credit LabNot A Team - Individual

You will have lots of programming to do this semester, but you may program this and submit it by the first reading day to replace your

two lowest quizzes with 100%

Main Program is to continue to prompt the user for an Infix until

the user enters QUIT.

If the infix is Invalid, say so. If it is Valid, Evaluate it and display the evaluated value.

217

This Lab Can Be Easily Expanded To Process Multiple Digit Floating Point Numbers: 1.2 *

5.43

This Lab Can Be Expanded To Process Variables More Difficult!

Lots of string processing practice!

Right to left string processing will enable you to create algorithm to evaluate prefix! 218

Software Engineering I

Be able to create an algorithm that you can walk through manually prior to

sitting at the computer.

219

Software Engineering II

Create and test well ADT’s, such as the Stack, that can be used with multiple

datatypes in many programs.

220

Software Engineering III

Implement & Test one part of a program at a time. Major components for this lab might be to (1) verify the Infix, (2) create the Postfix, and (3) evaluate the Postfix.

221