infix to postfix python code

class stack:
def __init__(self):
    self.item = []

def push(self,it):
    self.item.append(it)
def peek(self):
    if self.isempty() == True:
        return 0
    return self.item[-1]

def pop(self):
    if self.isempty() == True:
        return 0
    return(self.item.pop())

def length(self):
    return (len(self.item))


def isempty(self):
    if self.item == []:
        return True
    else:
        return False

def display(self):
    if self.isempty()== True:
        return
    temps = stack()
    while(self.isempty() != True):
        x = self.peek()
        print("~",x)
        temps.push(x)
        self.pop()
    while(temps.isempty() != True):
        x = temps.peek()
        self.push(x)
        temps.pop()


def isOperand(self, ch): 
    return ch.isalpha()
def notGreater(self, i):
    precedence = {'+':1, '-':1, '*':2, '/':2, '%':2, '^':3}
    if self.peek() == '(':
        return False
    a = precedence[i]
    b = precedence[self.peek()] 
    if a  <= b:
        return True
    else:
        return False
        


def infixToPostfix(self, exp):
    output = ""
    
    for i in exp:
        
        if self.isOperand(i) == True: # check if operand add to output
            print(i,"~ Operand push to stack")
            output = output + i

        # If the character is an '(', push it to stack 
        elif i  == '(':
            self.push(i)
            print(i," ~ Found ( push into stack")

        elif i == ')':  # if ')' pop till '('
            while( self.isempty() != True and self.peek() != '('):
                n = self.pop() 
                output = output + n
                print(n, "~ Operator popped from stack")
            if (self.isempty() != True and self.peek() != '('):
                print("_________")
                return -1
            else:
                x = self.pop()
                print(x, "Popping and deleting (")
        else: 
            while(self.isempty() != True and self.notGreater(i)):
                c = self.pop()
                output = output + c
                print(c,"Operator popped after checking precedence from stack")
            self.push(i)
            print(i,"Operator pushed to stack")

    # pop all the operator from the stack 
    while self.isempty() != True:
        xx = self.pop()
        output = output + xx
        print(xx,"~ pop at last")
    print(output)
    self.display()
st = stack()
st.infixToPostfix("a+(b*c)")

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source