Help me with this. Thank you! Create a python code with this problem, without using 'def'
Create a program that will convert an infix expression entered by the user to its equivalent prefix and postfix expression.
Sample Output:
Enter an Infix Expression: a+b^(c^d- e/(f+g)+(h^i)^j)
Postfix Expression: abcd^efg+/- hi^j^+^+
Prefix Expression: +a^b+- ^cd/e+fg^^hij
top = -1
capacity = 0
array = []
output = []
precedence = {'+':1, '-':1, '*':2, '/':2, '^':3}
print(10 + 80)
exp = "a+b*(c^d-e)^(f+g*h)-i"
print("a".isalpha())
for row in exp:
if row.isalpha():
output.append(row)
elif row == '(':
top += 1
array.append(row)
elif row == ')':
while(( top != -1) and array[-1] != '('):
if top != -1:
top -= 1 #Popping top
a = top
output.append(a)
if(( top != -1) and array[-1] != '('):
print('i')
else:
top -= 1
elif row == '+' or '-' or '*' or '/' or '^':
if top== -1:
array.append(row)
else:
b = top - 1
a = array[0]
precedence[a] <= precedence[row]
output.append(row)
array.append(row)
for i in output:
print(i)
#Converting the infix to postfix
final_list = output[::-1]
print("The Prefix Form")
for i in final_list:
print(i)
Comments
Leave a comment