Download - Expression evaluation
Infix NotationInfix Notation
To add A, B, we writeA+B
To multiply A, B, we writeA*B
The operators ('+' and '*') go in between the operands ('A' and 'B')
This is "Infix" notation.
To add A, B, we writeA+B
To multiply A, B, we writeA*B
The operators ('+' and '*') go in between the operands ('A' and 'B')
This is "Infix" notation.
Prefix NotationPrefix Notation
Instead of saying "A plus B", we could say "add A,B " and write
+ A B "Multiply A,B" would be written
* A B This is Prefix notation.
Instead of saying "A plus B", we could say "add A,B " and write
+ A B "Multiply A,B" would be written
* A B This is Prefix notation.
Postfix NotationPostfix Notation
Another alternative is to put the operators after the operands as in
A B +and
A B * This is Postfix notation.
Another alternative is to put the operators after the operands as in
A B +and
A B * This is Postfix notation.
The terms infix, prefix, and postfix tell us whether the operators go between, before, or after the operands.
The terms infix, prefix, and postfix tell us whether the operators go between, before, or after the operands.
ParenthesesParentheses
Evaluate 2+3*5. + First:
(2+3)*5 = 5*5 = 25 * First:
2+(3*5) = 2+15 = 17 Infix notation requires
Parentheses.
Evaluate 2+3*5. + First:
(2+3)*5 = 5*5 = 25 * First:
2+(3*5) = 2+15 = 17 Infix notation requires
Parentheses.
What about Prefix Notation?What about Prefix Notation?
+ 2 * 3 5 = = + 2 * 3 5 = + 2 15 = 17
* + 2 3 5 = = * + 2 3 5 = * 5 5 = 25
No parentheses needed!
+ 2 * 3 5 = = + 2 * 3 5 = + 2 15 = 17
* + 2 3 5 = = * + 2 3 5 = * 5 5 = 25
No parentheses needed!
Postfix NotationPostfix Notation
2 3 5 * + = = 2 3 5 * + = 2 15 + = 17
2 3 + 5 * = = 2 3 + 5 * = 5 5 * = 25
No parentheses needed here either!
2 3 5 * + = = 2 3 5 * + = 2 15 + = 17
2 3 + 5 * = = 2 3 + 5 * = 5 5 * = 25
No parentheses needed here either!
Conclusion:Conclusion:
Infix is the only notation that requires parentheses.
Infix is the only notation that requires parentheses.
Why all thisWhy all this
At our level, we cannot evaluate an infix expression, but we can turn an infix evaluation into a prefix or postfix expression and then evaluate them.
At our level, we cannot evaluate an infix expression, but we can turn an infix evaluation into a prefix or postfix expression and then evaluate them.
What we didWhat we did
We took infix expression and turned them into postfix expression.
We took infix expression and turned them into postfix expression.
Now we want show how we can convert Infix to Postfix using stack.
Now we want show how we can convert Infix to Postfix using stack.
Now we will see a little bit complex Example which contain parentheses.Now we will see a little bit complex Example which contain parentheses.
Infix to PostfixInfix to Postfix
( ( A + B ) * ( C - E ) ) / ( F + G ) )
stack: ( output: []
( ( A + B ) * ( C - E ) ) / ( F + G ) )
stack: ( output: []
Infix to PostfixInfix to Postfix
( A + B ) * ( C - E ) ) / ( F + G ) )
stack: ( ( output: []
( A + B ) * ( C - E ) ) / ( F + G ) )
stack: ( ( output: []
Infix to PostfixInfix to Postfix
A + B ) * ( C - E ) ) / ( F + G ) )
stack: ( ( ( output: []
A + B ) * ( C - E ) ) / ( F + G ) )
stack: ( ( ( output: []
Infix to PostfixInfix to Postfix
+ B ) * ( C - E ) ) / ( F + G ) )
stack: ( ( ( output: [A]
+ B ) * ( C - E ) ) / ( F + G ) )
stack: ( ( ( output: [A]
Infix to PostfixInfix to Postfix
B ) * ( C - E ) ) / ( F + G ) )
stack: ( ( ( + output: [A]
B ) * ( C - E ) ) / ( F + G ) )
stack: ( ( ( + output: [A]
Infix to PostfixInfix to Postfix
) * ( C - E ) ) / ( F + G ) )
stack: ( ( ( + output: [A B]
) * ( C - E ) ) / ( F + G ) )
stack: ( ( ( + output: [A B]
Infix to PostfixInfix to Postfix
* ( C - E ) ) / ( F + G ) )
stack: ( ( output: [A B + ]
* ( C - E ) ) / ( F + G ) )
stack: ( ( output: [A B + ]
Infix to PostfixInfix to Postfix
( C - E ) ) / ( F + G ) )
stack: ( ( * output: [A B + ]
( C - E ) ) / ( F + G ) )
stack: ( ( * output: [A B + ]
Infix to PostfixInfix to Postfix
C - E ) ) / ( F + G ) )
stack: ( ( * ( output: [A B + ]
C - E ) ) / ( F + G ) )
stack: ( ( * ( output: [A B + ]
Infix to PostfixInfix to Postfix
- E ) ) / ( F + G ) )
stack: ( ( * ( output: [A B + C ]
- E ) ) / ( F + G ) )
stack: ( ( * ( output: [A B + C ]
Infix to PostfixInfix to Postfix
E ) ) / ( F + G ) )
stack: ( ( * ( - output: [A B + C ]
E ) ) / ( F + G ) )
stack: ( ( * ( - output: [A B + C ]
Infix to PostfixInfix to Postfix
) ) / ( F + G ) )
stack: ( ( * ( - output: [A B + C E ]
) ) / ( F + G ) )
stack: ( ( * ( - output: [A B + C E ]
Infix to PostfixInfix to Postfix
) / ( F + G ) )
stack: ( ( * output: [A B + C E - ]
) / ( F + G ) )
stack: ( ( * output: [A B + C E - ]
Infix to PostfixInfix to Postfix
/ ( F + G ) )
stack: ( output: [A B + C E - * ]
/ ( F + G ) )
stack: ( output: [A B + C E - * ]
Infix to PostfixInfix to Postfix
( F + G ) )
stack: ( / output: [A B + C E - * ]
( F + G ) )
stack: ( / output: [A B + C E - * ]
Infix to PostfixInfix to Postfix
F + G ) )
stack: ( / ( output: [A B + C E - * ]
F + G ) )
stack: ( / ( output: [A B + C E - * ]
Infix to PostfixInfix to Postfix
+ G ) )
stack: ( / ( output: [A B + C E - * F ]
+ G ) )
stack: ( / ( output: [A B + C E - * F ]
Infix to PostfixInfix to Postfix
G ) )
stack: ( / ( + output: [A B + C E - * F ]
G ) )
stack: ( / ( + output: [A B + C E - * F ]
Infix to PostfixInfix to Postfix
) )
stack: ( / ( + output: [A B + C E - * F G ]
) )
stack: ( / ( + output: [A B + C E - * F G ]
Infix to PostfixInfix to Postfix
)
stack: ( / output: [A B + C E - * F G + ]
)
stack: ( / output: [A B + C E - * F G + ]
Infix to PostfixInfix to Postfix
stack: <empty> output: [A B + C E - * F G + / ]
Done!!!
stack: <empty> output: [A B + C E - * F G + / ]
Done!!!