Presentation is loading. Please wait.

Presentation is loading. Please wait.

Stacks & Queues Infix Calculator CSC 172 SPRING 2004 LECTURE 13.

Similar presentations


Presentation on theme: "Stacks & Queues Infix Calculator CSC 172 SPRING 2004 LECTURE 13."— Presentation transcript:

1 Stacks & Queues Infix Calculator CSC 172 SPRING 2004 LECTURE 13

2 Announcements  CIF – cool living space  Social Engineering Quiz  Project – issued today, due Friday 3/26  Read Weiss Chapter 11  Read about Trees  Weiss Chap 18

3 WORKSHOP LEADERSHIP  Consider being a CSC 171 workshop leader?  Good grade in CSC 171?  Belief in the workshop program?  Seeking to develop your leadership skills?  Pay and credit?

4 Interest ?  “SW Eng & Advanced Java Programming”  The “game course”  Multithreading, Graphics, UIs, SWeng  CSC 290  4 credits – normal grading – ok for CSC UL  Fall term 2004  CSC 172 pre-req

5 Interest?  “Recreational Graphics”  The “Maya” course – animation & ray tracing  CSC 390  1 or 2 credits – pass/fail – CSC credit, but not UL  Thursdays 4:50-6:05?  No pre-reqs

6 A useful stack algorithm  Postfix evaluation  We can rewrite the infix expression 1+2  As the postfix expression 1 2 +  “Think” like a computer  “load value ‘1’ into accumulator  “load value ‘2’ into register A  Add value in register A to value in accumulator  How about 1+2+3+4 ?  How about 2*3+4?  How about 2+3*4?

7 How to implement?  Can you write method that evaluates postfix expressions?  double postfixeval(Object[] items)  Where objects in items[] are either  Double  Character

8 Postfix evaluation using a stack 1. Make an empty stack 2. Read tokens until EOF a. If operand push onto stack b. If operator i. Pop two stack values ii. Perform binary operation iii. Push result 3. At EOF, pop final result

9 Trace by hand  1 2 3 4 + + +  2 3 * 4 +  2 3 4 * +

10 Infix to postfix 1 + 2 * 3 == 7 (because multiplication has higher precedence) 10 – 4 – 3 == 3 (because subtraction proceeds left to right)

11 Infix to postfix 4 ^ 3 ^ 2 == 262144 != 4096 Generally, Rather than:

12 Precidence A few simple rules: ( ) > ^ > * / > + - Subtraction associates left-to-right Exponentiation associates right to left

13 Infix Evaluation 1 – 2 – 4 ^ 5 * 3 * 6 / 7 ^ 2 ^ 2 == -8 (1 – 2) – ( ( ( ( 4 ^ 5) * 3) * 6) / (7 ^ ( 2 ^ 2 ) ) ) Could you write a program to evaluate stuff like this?

14 Postfix  If we expressed (1 – 2) – ( ( ( ( 4 ^ 5) * 3) * 6) / (7 ^ ( 2 ^ 2 ) ) ) As 1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - Then, we could use the postfix stack evaluator

15 Postfix evaluation using a stack 1. Make an empty stack 2. Read tokens until EOF a. If operand push onto stack b. If operator i. Pop two stack values ii. Perform binary operation iii. Push result 3. At EOF, pop final result

16 1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / -

17 1

18 2121

19

20 1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - 4

21 1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - 5 4

22 1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - 1024

23 1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - 3 1024

24 1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - 3072

25 1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - 6 3072

26 1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - 18432

27 1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - 7 18432

28 1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - 2 7 18432

29 1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - 2 7 18432

30 1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - 4 7 18432

31 1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - 2041 18432

32 1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - 7

33 1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - -8

34 But how to go from infix to postfix?  Could you write a program to do it?  What data structures would you use  Stack  Queue  How about a simple case just using “+”  1+ 2 + 7 + 4  1 2 7 4 + + +  Operands send on to output?  Operator push on stack?  Pop ‘em all at the end?

35 More complex 2 ^ 5 – 1 == 2 5 ^ 1 – Modify the simple rule? If you are an operator, pop first, then push yourself? 1 + 2 + 7 + 4 1 2 + 7 + 4 + ok

36 Even more complex 3 * 2 ^ 5 - 1 3 2 5 ^ * 1 – If you are an operator: Pop if the top of the stack is higher precedence than

37 Infix to postfix Stack Algorithm Operands : Immediately output Close parenthesis: Pop stack until open parenthesis Operators: 1. Pop all stack symbols until a symbol of lower precedence (or a right-associative symbol of equal precedence) appears. 2. Push operator EOF: pop all remaining stack symbols

38 1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7

39 1

40 - 1

41 - 1 2

42 1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7 ^- ^- 1 2

43 1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7 ^- ^- 1 2 3

44 1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7 ^^- ^^- 1 2 3

45 1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7 ^^- ^^- 1 2 3 3

46 1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7 - 1 2 3 3 ^ ^ -

47 1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7 (- (- 1 2 3 3 ^ ^ -

48 1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7 (- (- 1 2 3 3 ^ ^ - 4

49 1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7 +(- +(- 1 2 3 3 ^ ^ - 4

50 1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7 +(- +(- 1 2 3 3 ^ ^ - 4 5

51 1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7 *+(- *+(- 1 2 3 3 ^ ^ - 4 5

52 1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7 *+(- *+(- 1 2 3 3 ^ ^ - 4 5 6

53 1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7 - 1 2 3 3 ^ ^ - 4 5 6 * +

54 1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7 *- *- 1 2 3 3 ^ ^ - 4 5 6 * +

55 1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7 *- *- 1 2 3 3 ^ ^ - 4 5 6 * + 7

56 1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7 1 2 3 3 ^ ^ - 4 5 6 * + 7 * -

57 1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7 1 2 3 3 ^ ^ - 4 5 6 * + 7 * - ((1 – (2 ^ (3 ^ 3))) – (( 4 + (5 * 6)) * 7)) Input To evaluation stack


Download ppt "Stacks & Queues Infix Calculator CSC 172 SPRING 2004 LECTURE 13."

Similar presentations


Ads by Google