1 / 10

Lab Session 5

COP 3502 Spring 2005 Christopher Elwell. Lab Session 5. Administrivia. Recitation notes duplicated online: http://www.cs.ucf.edu/~celwell Office Hours MTWF 2-4 PM Quizzes and Tests Quizzes may be given at any time (lab or class) Assignment 1 Should have been submitted

saniya
Download Presentation

Lab Session 5

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. COP 3502 Spring 2005 Christopher Elwell Lab Session 5

  2. Administrivia • Recitation notes duplicated online: • http://www.cs.ucf.edu/~celwell • Office Hours • MTWF 2-4 PM • Quizzes and Tests • Quizzes may be given at any time (lab or class) • Assignment 1 • Should have been submitted • Cutoff date (with penalty) 2/10/05

  3. Postfix Expressions • Solve using a stack • When you encounter a number, push it onto the stack • When you encounter an operator (*, /, +, -) • Pop two numbers from the stack • Perform the operation • Push the result onto the stack

  4. Postfix Expressions 1.) 6 7 9 + - 2.) 6 7 9 + - 2 / 11 2 2 / + * 3.) 2 8 – 12 * 2 5 6 + - / 3 3 / * 4.) 4 4 4 4 + - * 2 4 + + 5 / 3 *

  5. Postfix Expressions 1.) 6 7 9 + - = -10 2.) 6 7 9 + - 2 / 11 2 2 / + * = -60 3.) 2 8 – 12 * 2 5 6 + - / 3 3 / * = 8 4.) 4 4 4 4 + - * 2 4 + + 5 / 3 * = -6

  6. Converting Infix to Postfix • General Algorithm: • Process from left to right • Copy numbers directly to the output • Use stack for operators • Always push ( onto the stack • When you encounter ), pop symbols off the stack until you reach the preceding ( (discard both parentheses) • Operators: • If the operator being “scanned” has higher precedence than the symbol at the stack top, push that symbol onto the stack • Otherwise, pop • Pop all operators off stack when you reach end of expression.

  7. Converting Infix to Postfix 1.) X*Y-(P+Q)*(R/S) 2.) E-F*G/(P+Q)+T 3.) M+L/K*U

  8. Converting Infix to Postfix 1.) X*Y-(P+Q)*(R/S) = XY*PQ+RS/*- 2.) E-F*G/(P+Q)+T = EFG*PQ+/-T+ 3.) M+L/K*U = MLK/U*+

  9. Valid? 1.) 3 5 7 - + 3 2 9 * / - 2.) 4 3 – 2 3 * 5 8 - + 4 - * 2 + 3.) 7 5 3 - + 3 / * 4 2 +

  10. Valid? 1.) 3 5 7 - + 3 2 9 * / - Valid 2.) 4 3 – 2 3 * 5 8 - + 4 - * 2 + Valid 3.) 7 5 3 - + 3 / * 4 2 + Invalid

More Related