Converting from infix to postfix in python | how to convert from infix to postfix in python |

Converting from infix to postfix in python | how to convert from infix to postfix in python |

 Converting From Infix to Postfix –

The Expression we ( human being ) write are called infix expression as the operatiors come in between in operands .

A + B this is infix expression because the  operator  ‘+’ comes between operands  A and B .

To evaluate expression manually infix notation is helpful as it is easily understandable by human brain.

But infix expression are hand to parse in a computer program hence it will be difficult to evaluate expression using infix notation.

To reduce the complexity of expression evaluation Prefix or Postfix expression are used in computer programs.

In Postfix expression , operators comes after the operands , below are an infix and respective postfix expression .

A+B -> AB+

 

Converting from infix to postfix in python | how to convert from infix to postfix in python |
Conversation  from infix to postfix

 


 

Steps for converting infix expression into postfix expression.

1.      Accept infix expression as a string exp

2.      For i is a alphabet or digit:

Append it to the postfix expression

            Else if I is ‘ ( ‘ :

                        Push ‘ ( ‘ is the stack

            Else if i is ‘ ) ‘ :

·         Pop the element from the stack and append it postfix expression until we get ‘ ) ‘ on top of the stack.

·         Pop ‘ ( ‘ from the stack and don’t add it in postfix expression .

           

Else if i is in ‘ */+- ‘ :

                        if stack is empty or top of the stack is ‘ ( ‘ :

                                    push the operator in the stack.

            Else :

Pop the element from the stack and continue  this until the operator on the top of the stack has same or less residence then the  i. i.e ( stack operator higher precedence than operator looking at then pop it and place at postfix expression .

            Else : push i in

stack .

 

3.      Once we scanned exp completely  start popping the element from the stack and appending them to postfix  expression .

Add if we encounter ‘ ( ‘ discard it .   



The Conversion from infix to postfix show in following table.

1)      A + B * C – D * E

2)      A *( B + C)

1]



2]

 








Post a Comment

Post a Comment (0)

Previous Post Next Post