Download presentation
Presentation is loading. Please wait.
1
Postfix notation
2
About postfix notation Postfix, or Reverse Polish Notation (RPN) is an alternative to the way we usually write arithmetic expressions (which is called infix, or algebraic notation –“Postfix” refers to the fact that the operator is at the end –“Infix” refers to the fact that the operator is in between –For example, 2 2 + postfix is the same as 2 + 2 infix –There is also a seldom-used prefix notation, similar to postfix Advantages of postfix: –You don’t need rules of precedence –You don’t need rules for right and left associativity –You don’t need parentheses to override the above rules Advantages of infix: –You grew up with it –It’s easier to see visually what is done first
3
How to evaluate postfix Going from left to right, if you see an operator, apply it to the previous two operands (numbers) Example: Equivalent infix: 2 + 10 * 4 / 5 – (9 – 3) 2 10 4 * 5 / + 9 3 - - 40 810 6 4
4
Computer evaluation of postfix Going left to right, –If you see a number, put it on the stack –If you see an operator, pop two numbers from the stack, do the operation, and put the result back on the stack (The top number on the stack is the operand on the right) The result is the only thing on the stack when done –If there’s more than one thing, there was an error in the expression 2 10 4 * 5 / + 9 3 - - 2 10 2 4 10 2 40 2 5 40 2 8 210 9 10 3 9 10 6 10 4
5
A helpful observation When you convert between infix and postfix, the operands (numbers) are always in the same order The operators are probably in a different order We’re going to talk about how to convert manually between the two systems Your textbook has more information on how to do the conversion by computer 2 10 4 * 5 / + 9 3 - - 2 + 10 * 4 / 5 – ( 9 – 3 )
6
From infix to postfix Figure out, using the infix rules, which operation to perform next Write the new operand or operands in their correct places Write the operator at the end Postfix does not use parentheses, but we’ll put them in temporarily to help show the way things are grouped Example: 2 + 10 * 4 / 5 – (9 – 3) –The multiply is done first: 10 4 * –Next, the divide: (10 4 *) 5 / –The addition: 2 (10 4 * 5 /) + –The rightmost subtraction: (2 10 4 * 5 / +) 9 3 - –The leftmost subtraction: (2 10 4 * 5 / +) (9 3 -) - –The final result: 2 10 4 * 5 / + 9 3 - -
7
From postfix to infix Why would you want to? OK--working from left to right, for each operator, move it between the two preceding operands, and put parenthesis around the whole group Example: 2 10 4 * 5 / + 9 3 - - 2 (10 * 4) 5 / + 9 3 - - 2 ((10 * 4) / 5) + 9 3 - - (2 + ((10 * 4) / 5) 9 3 - - (2 + ((10 * 4) / 5) (9 - 3) - ((2 + ((10 * 4) / 5) - (9 - 3)) –Now you can remove unnecessary parentheses (if you want to)
8
Unary operators In infix, - means both “negation” (as a unary operator) and “subtraction” (as a binary operator) Likewise, + means both “addition” and “identity” In infix notation, an operator is unary if: –It is the very first thing in the expression, or –It immediately follows a left (opening) parenthesis We have no such clues in postfix notation –The usual solution is simply to use a different symbol, such as ~, for unary negation
9
Hewlett-Packard calculators Some of the finest calculators, such as expensive ones made by HP, use RPN (Reverse Polish Notation) –It’s actually quite easy to get used to using RPN –RPN more closely mirrors the way you think about doing a calculation –With an algebraic calculator, it’s easier to just copy an equation from a textbook into a calculator With just a little practice, entering an algebraic formula into an RPN calculator becomes very easy (and takes fewer keystrokes) Despite the advantages, RPN has a difficult time in the marketplace –People in general don’t like to learn new things if they don’t have to...and this isn’t necessarily a bad thing
10
The End
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.