carme.cs.trinity.educarme.cs.trinity.edu/thicks/1320/lectures/pcs1-create-postfix-slides… ·...

Post on 01-May-2020

5 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

1

22

33

4

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

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

5

Maybe A Spreadsheet Will Help?

69 ?517 ?

Maybe Not!

5 + 2 ^ 3 ^ 2 = _______

6

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

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

9

5 2 3 2

10

5 + 2 ^ 3 ^ 2

1 – Write Down All Of The Operands

Infix

Prefix

Postfix

5 2 3 2

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

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

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

14

( 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

( 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

( 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

18

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

* 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

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

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

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

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

25

( 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

( 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

( 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

( 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

( 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

( 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

( 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

( 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

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

35

36

37

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

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

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

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

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

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

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

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

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

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

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

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

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

51

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

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

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

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

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

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

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

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

60

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

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 -

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 -

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 -

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 -

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 -

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 -

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 -

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

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 /

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

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 -

-

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

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

75

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 *

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

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 +

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

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 ^

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

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 ^

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

^

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 /

^

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

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

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 *

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

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

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

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

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

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 -

*

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

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

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

+

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 *

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

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

Scala:var st = new charStack(50)

Stack <char> Postfix(5);

What type of stack was to

create the Postfix from the Infix?

100

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

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

top related