Prefix, Postfix, Infix Notation
Prefix, Postfix, Infix Notation
Infix Notation
To
add A, B, we write A+B To multiply A, B, we write A*B The operators ('+' and '*') go in between the operands ('A' and 'B') This is "Infix" notation.
Prefix Notation
Instead
of saying "A plus B", we could say "add A,B " and write +AB "Multiply A,B" would be written *AB This is Prefix notation.
Postfix Notation
Another
alternative is to put the operators after the operands as in AB+ and AB* This is Postfix notation.
Pre A In B Post
The
terms infix, prefix, and postfix tell us whether the operators go between, before, or after the operands.
Parentheses
Evaluate +
First:
2+3*5.
Postfix Notation
235*+= =235*+ = 2 15 + = 17 23 +5 * = =23+5* = 5 5 * = 25 No parentheses needed here either!
Conclusion:
Infix
is the only notation that requires parentheses in order to change the order in which the operations are done.
FPE has exactly one set of Parentheses enclosing each operator and its operands. Which is fully parenthesized? (A+B)*C ( ( A + B) * C ) ( ( A + B) * ( C ) )
Infix to Postfix
(((A+B)*C)-((D+E)/F)) A B+C* D E+F/Operand order does not change! Operators are in order of evaluation!
Assumptions: Space delimited list of tokens represents a FPE infix expression Operands are single characters. Operators +,-,*,/
output list Split the input into a list of tokens. for each token (left to right): if it is operand: append to output if it is '(': push onto Stack if it is ')': pop & append till '('
<empty> output: []
( output: []
(( output: []
((( output: []
(((+ output: [A B]
(( output: [A B + ]
((* output: [A B + ]
((*( output: [A B + ]
((*( output: [A B + C ]
((*(output: [A B + C ]
((*(output: [A B + C E ]
((* output: [A B + C E - ]
( output: [A B + C E - * ]
(/ output: [A B + C E - * ]
(/( output: [A B + C E - * ]
(/( output: [A B + C E - * F ]
(/(+ output: [A B + C E - * F ]
(/(+ output: [A B + C E - * F G ]
(/ output: [A B + C E - * F G + ]
stack:
<empty> output: [A B + C E - * F G + / ]
many parentheses. Establish precedence rules: My Dear Aunt Sally We can alter the previous program to use the precedence rules.
About it !!!