Presentation is loading. Please wait.

Presentation is loading. Please wait.

Stacks & Queues Infix Calculator CSC 172 SPRING 2002 LECTURE 5.

Similar presentations


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

1 Stacks & Queues Infix Calculator CSC 172 SPRING 2002 LECTURE 5

2 Workshop sign-up Still time : Dave Feil-Seifer df001i@mail.rochester.edu Ross Carmara rc001i@mail.rochester.edurc001i@mail.rochester.edu

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

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

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

6 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?

7 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

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 1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / -

10 1

11 2121

12

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

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

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

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

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

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

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

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

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

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

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

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

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

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

27 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?

28 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

29 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

30 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

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

32 1

33 - 1

34 - 1 2

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

50 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 2002 LECTURE 5."

Similar presentations


Ads by Google