Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- os.system("clear")
- def position(x,y):
- pos=0
- ps=len(x)+1
- for i in x:
- if i==y:
- return(pos)
- break
- pos+=1
- return(ps)
- def posi(x):
- tr=[]
- c=0
- for i in x:
- if((i=='*') or (i=='+') or (i=='/') or (i=='-') or (i=='^')):
- tr.append(c)
- c+=1
- return(tr)
- def posio(x):
- tr=[]
- c=0
- for i in x:
- if((i=='(')):
- tr.append(c)
- c+=1
- return(tr)
- def posic(x):
- tr=[]
- c=0
- for i in x:
- if((i==')')):
- tr.append(c)
- c+=1
- return(tr)
- def calculate(x,y):
- p1=position(x,y)
- k1=int(x[p1-1])
- k2=int(x[p1+1])
- if y=='*':
- x.insert(p1-1,k1*k2)
- elif y=='/':
- x.insert(p1-1,k1/k2)
- elif y=='+':
- x.insert(p1-1,k1+k2)
- elif y=='-':
- x.insert(p1-1,k1-k2)
- elif y=='^':
- x.insert(p1-1,k1**k2)
- del x[p1]
- del x[p1]
- del x[p1]
- def calculator(b):
- if b[len(b)-1]=='=':
- kj=0
- a=[]
- while kj<(len(b)-1):
- a.append(b[kj])
- kj+=1
- else:
- a=b
- if len(a)!=1:
- kr=posi(a)
- damage=0
- c=1
- while c<len(kr):
- if kr[c]-kr[c-1]==1:
- damage=1
- c+=1
- if damage==0:
- bs=[]
- c=1
- bs.append(a[:kr[0]])
- bs.append(a[kr[0]])
- while c<len(kr):
- bs.append(a[(kr[c-1]+1):kr[c]])
- bs.append(a[kr[c]])
- c+=1
- bs.append(a[(kr[c-1]+1):])
- cr=[]
- c=0
- while (c<len(bs)):
- ln=''
- for i in bs[c]:
- ln=ln+i
- c+=1
- cr.append(ln)
- a=cr
- i=1
- while len(a)!=1:
- if('^' in a):
- calculate(a,'^')
- elif ('*' in a) and ('/' in a):
- p1=position(a,'*')
- p2=position(a,'/')
- if p1<p2:
- calculate(a,'*')
- else:
- calculate(a,'/')
- elif ('*' in a):
- calculate(a,'*')
- elif ('/'in a):
- calculate(a,'/')
- elif ('+' in a) and ('-' in a):
- p1=position(a,'+')
- p2=position(a,'-')
- if p1<p2:
- calculate(a,'+')
- else:
- calculate(a,'-')
- elif ('+' in a):
- calculate(a,'+')
- elif ('-' in a):
- calculate(a,'-')
- return(a[0])
- else:
- print("Invalid input")
- else:
- return a[0]
- def fpos(x,y):
- c=0
- pos=-1
- for i in x:
- if i==y:
- pos=c
- break
- c+=1
- return(pos)
- def lpos(x,y):
- c=0
- pos=-1
- for i in x:
- if i==y:
- pos=c
- c+=1
- return(pos)
- user_input=input()
- if user_input.count("=")>1:
- print("Invalid Input")
- exit()
- input()
- os.system("clear")
- kol=position(user_input,"=")
- if "=" in user_input:
- if kol!=len(user_input)-1:
- print("Invalid Input")
- exit()
- input()
- os.system("clear")
- yosh="!@#$%&_|}{\":?><QWERTYUIOPASDFGHJKLZXCVBNM[]\;'.,qwertyuiopasdfghjklzxcvbnm"
- for i in yosh:
- if i in user_input:
- print("Invalid Input")
- exit()
- input()
- os.system("clear")
- ocount=user_input.count("(")
- ccount=user_input.count(")")
- new_user_input=[]
- for i in user_input:
- new_user_input.append(i)
- danger=0
- copy_new_user_input=new_user_input.copy()
- if ocount==ccount:
- positions_of_openparanthesis=posio(copy_new_user_input)
- positions_of_closeparanthesis=posic(copy_new_user_input)
- abc=len(positions_of_openparanthesis)
- j=0
- while j<abc:
- i=1
- while i<len(copy_new_user_input) and len(positions_of_openparanthesis):
- if positions_of_openparanthesis[len(positions_of_openparanthesis)-1]+i in positions_of_closeparanthesis:
- del positions_of_openparanthesis[len(positions_of_openparanthesis)-1]
- i+=1
- j+=1
- if len(positions_of_openparanthesis)!=0:
- danger=1
- else:
- danger=1
- if danger==0:
- while copy_new_user_input.count("("):
- positions_of_openparanthesis=posio(copy_new_user_input)
- positions_of_closeparanthesis=posic(copy_new_user_input)
- j=0
- while j<abc:
- i=1
- kos=len(positions_of_openparanthesis)
- while i<len(copy_new_user_input) and kos:
- if positions_of_openparanthesis[len(positions_of_openparanthesis)-1]+i in positions_of_closeparanthesis:
- fi=positions_of_openparanthesis[len(positions_of_openparanthesis)-1]
- la=positions_of_openparanthesis[len(positions_of_openparanthesis)-1]+i
- pain=calculator(copy_new_user_input[fi+1:la])
- copy_new_user_input.insert(fi,str(pain))
- del copy_new_user_input[fi+1:la+2]
- positions_of_openparanthesis=posio(copy_new_user_input)
- positions_of_closeparanthesis=posic(copy_new_user_input)
- kos=0
- i+=1
- j+=1
- result=calculator(copy_new_user_input)
- print(result)
- else:
- print("invalid input")
- input()
- os.system("clear")
Add Comment
Please, Sign In to add comment