Arithmetic Expression –
INFIX TO POSTFIX
SARASWATHI RAMALINGAM
SRI AKILANDESWARI WOMENS
COLLEGE – WANDIWASH.
Arithmetic Expression
• Arithmetic Expression contains
– Operators (+,-,*,/,%,() )
– Operands (a..z)
– Ex:- (a*b),(a+b)-c
Classification of Expression
– Depending upon the position of the
operators with respect to the position of
the operands.
– Infix
– Prefix
– Postfix
Classification of Expression
INFIX PREFIX POSTFIX
Operator In between the
operands
Operator before the
operands
Operator after the
operands
Infix notation Prefix notation(Polish
notation)
Postfix notation(Reverse
Polish notation)
A+B +AB AB+
A+B*C +A*BC ABC*+
(A+B)*(C-D) *+AB-CD AB+CD-*
INFIX TO POSTFIX
• Conversion of Expression
• Evaluation of Postfix Expression
Conversion of Expression
• Steps
– Initialize an empty stack.
– Read the token from Infix string one at a
time from left to right.
Conversion of Expression
– If the scanned character is an operand
add it to the Postfix string.
– If the scanned character is an operator
• if the stack is empty push the character to
stack.
• If the stack is not empty, compare the
precedence of the character with element on
top of the stack.
Conversion of Expression
– If the character is a left parentheses,
push it on to the stack.
– If the character is a right parentheses,
• Repeatedly pop the stack and add popped out
element in to the string until a left
parentheses is encountered.
• Remove the left parentheses from the stack
and ignore it.
Conversion of Expression
• When top Stack has higher precedence over
the scanned character pop the stack else
push the scanned character to stack.
– (Repeat the step until the operator in the stack
has higher precedence than the scanned
character)
• After all the characters are read and the
stack is not empty then pop the stack and
add the string.
Example
A+B
1.
SYMBOL SCANNED STACK POSTFIX EXPRESSION
A A
2
SYMBOL SCANNED STACK POSTFIX EXPRESSION
A A
+ + A
3
SYMBOL SCANNED STACK POSTFIX EXPRESSION
A A
+ + A
B + AB
AB+
Example
A+B*C
1.
SYMBOL SCANNED STACK POSTFIX EXPRESSION
A A
2
SYMBOL SCANNED STACK POSTFIX EXPRESSION
A A
+ + A
3
SYMBOL SCANNED STACK POSTFIX EXPRESSION
A A
+ + A
B + AB
4
SYMBOL SCANNED STACK POSTFIX EXPRESSION
A A
+ + A
B + AB
* +* AB
5
SYMBOL SCANNED STACK POSTFIX EXPRESSION
A A
+ + A
B + AB
* +* AB
C +* ABC
ABC*+

Arithmetic expression INFIX TO POSTFIX CONVERTION saraswathi ramalingam

  • 1.
    Arithmetic Expression – INFIXTO POSTFIX SARASWATHI RAMALINGAM SRI AKILANDESWARI WOMENS COLLEGE – WANDIWASH.
  • 2.
    Arithmetic Expression • ArithmeticExpression contains – Operators (+,-,*,/,%,() ) – Operands (a..z) – Ex:- (a*b),(a+b)-c
  • 3.
    Classification of Expression –Depending upon the position of the operators with respect to the position of the operands. – Infix – Prefix – Postfix
  • 4.
    Classification of Expression INFIXPREFIX POSTFIX Operator In between the operands Operator before the operands Operator after the operands Infix notation Prefix notation(Polish notation) Postfix notation(Reverse Polish notation) A+B +AB AB+ A+B*C +A*BC ABC*+ (A+B)*(C-D) *+AB-CD AB+CD-*
  • 5.
    INFIX TO POSTFIX •Conversion of Expression • Evaluation of Postfix Expression
  • 6.
    Conversion of Expression •Steps – Initialize an empty stack. – Read the token from Infix string one at a time from left to right.
  • 7.
    Conversion of Expression –If the scanned character is an operand add it to the Postfix string. – If the scanned character is an operator • if the stack is empty push the character to stack. • If the stack is not empty, compare the precedence of the character with element on top of the stack.
  • 8.
    Conversion of Expression –If the character is a left parentheses, push it on to the stack. – If the character is a right parentheses, • Repeatedly pop the stack and add popped out element in to the string until a left parentheses is encountered. • Remove the left parentheses from the stack and ignore it.
  • 9.
    Conversion of Expression •When top Stack has higher precedence over the scanned character pop the stack else push the scanned character to stack. – (Repeat the step until the operator in the stack has higher precedence than the scanned character) • After all the characters are read and the stack is not empty then pop the stack and add the string.
  • 10.
  • 11.
    2 SYMBOL SCANNED STACKPOSTFIX EXPRESSION A A + + A
  • 12.
    3 SYMBOL SCANNED STACKPOSTFIX EXPRESSION A A + + A B + AB AB+
  • 13.
  • 14.
    2 SYMBOL SCANNED STACKPOSTFIX EXPRESSION A A + + A
  • 15.
    3 SYMBOL SCANNED STACKPOSTFIX EXPRESSION A A + + A B + AB
  • 16.
    4 SYMBOL SCANNED STACKPOSTFIX EXPRESSION A A + + A B + AB * +* AB
  • 17.
    5 SYMBOL SCANNED STACKPOSTFIX EXPRESSION A A + + A B + AB * +* AB C +* ABC ABC*+