Computer Science, asked by Preethisanga9, 1 year ago

While us humans can understand how to calculate a simple mathematical expression like 2 + 3 * 7 - 9 / 5, computers use a special technique to make it easier for them. They convert an infix expression to a postfix expression. An infix expression is one in which all the operators (+,-,*,/) appear between operands, just like our usual expressions. A postifx expression is one in which all the operands appear first, and the operators appear after the operands. Write a class Fixes that has 2 strings infix and postfix. It should have a parameterized constructor to accept the value of infix. Also, it should have a method called convert that converts the infix to a postfix expression and stores it in the postfix. Write only the Fixes class. Main class has already been written. Note: all operands will be of single digit only Example Input: 2+3*7-9/5 Output: 237*+95/- Example Input: (2+3)*7-9/2^1 Output: 23+7*921^/-

Answers

Answered by powersteel3344
0

Answer:

what are you asking in this question???

Answered by dshvarma05
8

Answer:

class Fixes :

   postfix=str()

   def __init__(self,infix):

       self.infix=infix

   def convert(self) :

       stack=[]

       ops=['+','-','*','/','^','%','(',')']

       prec={'^':3,'*':2,'/':2,'%':2,'+':1,'-':1}

       for i in self.infix :

           if (i not in ops):

               self.postfix=self.postfix+i

           elif i=='(':

               stack.append(i)

           elif i==')' :

               while(stack and stack[-1]!='('):

                   self.postfix=self.postfix+stack.pop()

               stack.pop()

           else:

               while stack and stack[-1]!='(' and prec[i]<=prec[stack[-1]]:

                   self.postfix=self.postfix+stack.pop()

               stack.append(i)

       while stack:

           self.postfix=self.postfix+stack.pop()nation:

Similar questions