0

I was hoping you will be free and could help me solve my codes. I'm entirely new to python programming so I'm at the phase of learning on my own or self-study. So bare with me if my code seems really really really off.

Basically, my professor wants a python program where you'd input an infix expression. Once you press enter, it should output the converted infix expression to postfix and prefix.

Here are the sample run and instructions.

Create a Python program that will convert input Infix expression to the corresponding prefix and postfix expression using stack.

Sample Run: Input Infix Expression: (A + B * C) / D

Prefix Expression: / + A * B C D

Postfix Expression: A B C * + D /

Here's my code.

import output as output
from pip._internal.req.constructors import operators

def infixToPostfix(expression, output2=None):
    Operators = {'+', '-', '*', '/', '(', ')', '^'}
    Priority = {'+': 1, '-': 1, '*': 2, '/': 2, '^': 3}  

    stack = [] 

    output = ''
    outputs2=[]
    Operators2=[]
    priority2={ ')': 0, '+':1, '-':1, '/':2, '*':2, '^':3}

    for character in expression:
        if character not in Operators:
            output += character

        elif character == '(':
            stack.append('(')

        elif character == ')':
            while stack and stack[-1] != '(':
                output += stack.pop()
            stack.pop()

        else:
            while stack and stack[-1] != '(' and Priority[character] <= Priority[stack[-1]]:
                output += stack.pop()
            stack.append(character)

    while stack:
        output += stack.pop()

    return output

    for ch in expression[::-1]:
        if(ch==')'):
            operators.append(ch)
        elif(ch=='('):
            while(operators[-1]!=')'):
                ele=operators.pop()
                output.append(ele)
            operators.pop()
        elif(ch=='^' or ch=='/' or ch=='*' or ch=='+' or ch=='-'):
            if(len(operators)>0):
                while(priority2[operators[-1]]>priority2[ch]):
                    ele=operators.pop()
                    output2.append(ele)
                    if(len(operators)==0):
                        break
                    operators.append(ch)
                else:
                    output.append(ch)
if(len(operators)>0):
    while(len(operators)!=0):
        ele=operators.pop()
        output.append(ele)

expression = input('Enter infix expression: ')

print('Infix Notation: ', expression)

print('Postfix Notation: ', infixToPostfix(expression))

print("Prefix Notation: ",end=' ')
for ele in output[::-1]:
    print(ele,end='')
1
  • The part after return output will never execute. Did you copy/paste this code correctly? Commented Nov 15, 2022 at 5:58

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.