1 / 50

Stacks & Queues Infix Calculator

Stacks & Queues Infix Calculator. CSC 172 SPRING 2002 LECTURE 5. Workshop sign-up. Still time : Dave Feil-Seifer df001i@mail.rochester.edu Ross Carmara rc001i@mail.rochester.edu. Infix to postfix. 1 + 2 * 3 == 7 (because multiplication has higher precidence) 10 – 4 – 3

Télécharger la présentation

Stacks & Queues Infix Calculator

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Stacks & QueuesInfix 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.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 • Make an empty stack • Read tokens until EOF • If operand push onto stack • If operator • Pop two stack values • Perform binary operation • Push result • At EOF, pop final result

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  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: • Pop all stack symbols until a symbol of lower precedence (or a right-associative symbol of equal precedence) appears. • Push operator EOF: pop all remaining stack symbols

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

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

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

  34. 1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7 - 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 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