0% found this document useful (0 votes)
7 views3 pages

CONVERSION

Uploaded by

pr414350
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

CONVERSION

Uploaded by

pr414350
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

def stkpush(stk,item):

stk.append(item)

def stkpop(stk):

v=stk.pop()

return v

def stkpeek(stk):

if len(stk)>0:

return stk[len(stk)-1]

else:

return '#'

def stkdisplay(stk):

for i in stk:

print(i,end=" ")

def stkprioty(i): # mo=5 e=6 d=4 m=3 a=2 s=1

if(i=='^'):

return 6

if (i=='%'):

return 5

elif(i=='/'):

return 4

elif(i=='//'):

return 3

elif(i=='*'):

return 2

elif(i=='+'):

return 1

elif(i=='-'):

return 0
elif(i=='('):

return -1

else:

return -2

def isop(op):

if op in "%^/*+-//":

return True

else:

return False

#main

opstk=[]

exp=[]

s=input("enter infix expression")

for i in s:

if i=='(':

stkpush(opstk,i)

elif isop(i):

while stkprioty(stkpeek(opstk)) > stkprioty(i):

stkpush(exp,stkpop(opstk))

stkpush(exp,i)

elif i==")":

while isop(stkpeek(opstk)):

stkpush(exp,stkpop(opstk))

stkpop(opstk)

else:

stkpush(exp,i)

for i in opstk:

stkpush(exp,i)
stkdisplay(exp)

opcount=0

operandcount=0

for i in s:

if isop(i):

opcount=opcount+1

else:

operandcount=operandcount+1

if(operandcount-opcount)==1:

print("it is correct")

else:

print("it is incorrect")

You might also like