500 likes | 516 Views
Learn about converting infix expressions to postfix, evaluate postfix expressions using stacks, and develop algorithms to simplify the evaluation process.
E N D
Stacks & QueuesInfix 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 == 3 (because subtraction proceeds left to right)
Infix to postfix 4 ^ 3 ^ 2 == 262144 != 4096 Generally, Rather than:
Precidence A few simple rules: () > ^ > * / > + - Subtraction associates left-to-right Exponentiation associates right to left
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?
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
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
1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - 3 1024 -1
1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - 6 3072 -1
1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - 18432 -1
1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - 7 18432 -1
1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - 2 7 18432 -1
1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - 2 2 7 18432 -1
1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - 4 7 18432 -1
1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - 2041 18432 -1
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?
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
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
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
1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7 ^ - 1 2
1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7 ^ - 1 2 3
1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7 ^ ^ - 1 2 3
1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7 ^ ^ - 1 2 3 3
1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7 - 1 2 3 3 ^ ^ -
1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7 ( - 1 2 3 3 ^ ^ -
1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7 ( - 1 2 3 3 ^ ^ - 4
1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7 + ( - 1 2 3 3 ^ ^ - 4
1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7 + ( - 1 2 3 3 ^ ^ - 4 5
1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7 * + ( - 1 2 3 3 ^ ^ - 4 5
1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7 * + ( - 1 2 3 3 ^ ^ - 4 5 6
1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7 - 1 2 3 3 ^ ^ - 4 5 6 * +
1 –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