102
1

carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

1

Page 2: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

22

Page 3: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

33

Page 4: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

4

Page 5: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

Must Get Same Answer As Manual Solution! Preserve The Mathematical Order Of Operations

5 + 2 ^ 3 ^ 2 = _______69 ?517 ?Other ?

5

Page 6: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

Maybe A Spreadsheet Will Help?

69 ?517 ?

Maybe Not!

5 + 2 ^ 3 ^ 2 = _______

6

Page 7: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

Create A 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

7

Page 8: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

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

8

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 9: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

9

Page 10: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

5 2 3 2

10

5 + 2 ^ 3 ^ 2

1 – Write Down All Of The Operands

Infix

Prefix

Postfix

5 2 3 2

Page 11: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

5 2 3 2^

11

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

Page 12: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

5 + 2 ^ 3 ^ 2

5 2 3 2^^

12

2 – Solve As If Doing Mathematically – Order Of Operations

Infix

Prefix

Postfix

5 ^2 ^3 2

5 2 3 2^

5 2 ^3 2

Page 13: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

5 2 3 2^^+

13

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

Page 14: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

14

Page 15: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

( 2 + 3 ) * ( 5 – 2 )

15

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

Page 16: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

( 2 + 3 ) * ( 5 – 2 )

16

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

Page 17: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

( 2 + 3 ) * ( 5 – 2 )

17

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

Page 18: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

18

Page 19: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

19

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

Page 20: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

* 3 2 3 4 2 2 3

20

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 21: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

21

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

Page 22: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

22

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

Page 23: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

23

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

Page 24: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

24

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

Page 25: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

25

Page 26: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

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

26

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 27: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

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

27

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 28: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

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

28

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 29: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

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

29

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 30: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

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

30

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 31: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

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

31

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 32: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

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

32

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 33: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

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

33

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 34: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

34

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

Page 35: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

35

Page 36: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

36

Page 37: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

37

Page 38: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

38

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 39: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

39

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

Page 40: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

40

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

Page 41: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

41

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

Page 42: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

42

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

Page 43: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

43

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

Page 44: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

44

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 45: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

45

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

Page 46: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

46

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

Page 47: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

47

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

Page 48: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

48

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

Page 49: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

49

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

Page 50: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

50

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 51: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

51

Page 52: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

52

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

Page 53: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

53

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

Page 54: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

54

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

Page 55: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

55

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

Page 56: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

56

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 57: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

57

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

Page 58: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

58

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

Page 59: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

59

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

Page 60: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

60

Page 61: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

61

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

Page 62: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

62

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 63: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

63

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 -

Page 64: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

64

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 -

Page 65: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

65

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 -

Page 66: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

66

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 -

Page 67: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

67

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 -

Page 68: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

68

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 69: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

69

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

Page 70: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

70

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 /

Page 71: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

71

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

Page 72: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

72

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 -

-

Page 73: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

73

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

Page 74: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

74

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 75: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

75

Page 76: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

76

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)

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

Page 77: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

77

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

Page 78: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

78

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 +

Page 79: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

79

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

Page 80: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

80

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 81: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

81

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

Page 82: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

82

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 ^

Page 83: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

83

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

^

Page 84: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

84

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 /

^

Page 85: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

85

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

Page 86: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

86

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 87: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

87

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 *

Page 88: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

88

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

Page 89: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

89

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

Page 90: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

90

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

Page 91: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

91

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

Page 92: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

92

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 4

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 93: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

93

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 4 -

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 -

*

Page 94: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

94

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 4 - *

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

Page 95: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

95

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 4 - * 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

Page 96: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

96

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 4 - * 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

+

Page 97: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

97

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 4 - * 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 *

Page 98: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

98

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 4 - * 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 99: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

99

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 4 - * 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

Page 100: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

Scala:var st = new charStack(50)

Stack <char> Postfix(5);

What type of stack was to

create the Postfix from the Infix?

100

Page 101: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

You Must Have Function

def createPostfix (infix : String) : String =

{// You Do"2 3 +"

}

Test With Multiple Digit DoublesYou May Assume A Blank Between Each Token In The Infix ExpressionYou May Assume That The Infix Is Valid (Other Than ({[)

101

Page 102: carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/Lectures/PCS1-Create-Postfix-Slides… · Examine one Infix character at a time – left to right • If Operand, Write it to

if ((diagnosticLevel == 20) || (diagnosticLevel == 99)){

var postfix = "?" var infix = "?" do{

println("\n\n\n====================================================")println("===== Interactive Testing createPostfix =====")println("===== diagnosticLevel = 20 =====")println("====================================================\n") printlnprint("\nEnter Infix <QUIT to Exit> : ")infix = readLine

if (postfix.toUpperCase != "QUIT"){

println ("\nInfix Expression = " + infix + "\n")var postfix = createPostfix(infix)println("\n===> Postfix Solution = " + postfix)

}

} while (infix.toUpperCase != "QUIT")102

Add Your Own Diagnostic TestingYou Must Include The Following Make Any Corrections Necessary!

Set Your Diagnostic