Evaluation of postfix expression using stack in Python

Evaluation of postfix expression using stack in Python

Unlike infix expression postfix expression don’t have any parenthesis it has only two characters that are Operator And operand Using stack, we can easily evaluate postfix expression there will be only two scenarios. We have to scan string from left to right. If we encounter operator while we are scanning the string then we will pop two elements from the stack and perform the operation of current operator and we will push back the result into the stack. And if we encounter operand just push that operand into the stack.

Steps for evaluating postfix expression.

  • 1. Accept postfix expression string in post variable.
  • 2.
    For i in post:
        If i is an operand:
            Push i in stack.
        Else:
            Pop two elements from stack e.g. X and Y
            Perform operation with current operator on both the parents i.e X i Y
            Push the result back into the stack.
    End for loop.
    
  • 3. Finally pop out the result from the stack.

Evaluate below postfix expression 234+*6-

Accept above string in variable exp.

for i in exp:

evaluate postfix expression
Evaluate postfix expression using stack in python

CODE: Program to show use of stack to evaluate postfix expression using python.

"""
Author : ITVoyagers (itvoyagers.in)

Date :4th November 2019

Description : Program to show use of stack to evaluate postfix expression using python.
"""
class evaluate_postfix:
    def __init__(self):
        self.items=[]
        self.size=-1
    def isEmpty(self):
        return self.items==[]
    def push(self,item):
        self.items.append(item)
        self.size+=1
    def pop(self):
        if self.isEmpty():
            return 0
        else:
            self.size-=1
            return self.items.pop()
    def seek(self):
        if self.isEmpty():
            return False
        else:
            return self.items[self.size]
    def evalute(self,expr):
        for i in expr:
            if i in '0123456789':
                self.push(i)
            else:
                op1=self.pop()
                op2=self.pop()
                result=self.cal(op2,op1,i)
                self.push(result)
        return self.pop()
    def cal(self,op2,op1,i):
        if i is '*':
            return int(op2)*int(op1)
        elif i is '/':
            return int(op2)/int(op1)
        elif i is '+':
            return int(op2)+int(op1)
        elif i is '-':
            return int(op2)-int(op1)
s=evaluate_postfix()
expr=input('enter the postfix expression')
value=s.evalute(expr)
print('the result of postfix expression',expr,'is',value)


OUTPUT
>>> ================================ RESTART ================================
>>> 
enter the prefix expression++596^-64
the result of postfix expression ++596^-64 is 20
>>> ================================ RESTART ================================
>>> 
enter the prefix expression+-927
the result of postfix expression +-927 is 14
>>> 
>>> 

You can also check following content on conversion, evaluation and other applications of stack

Leave a Comment