Guest User

Untitled

a guest
Oct 19th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. import os
  2. os.system("clear")
  3. def position(x,y):
  4. pos=0
  5. ps=len(x)+1
  6. for i in x:
  7. if i==y:
  8. return(pos)
  9. break
  10. pos+=1
  11. return(ps)
  12. def posi(x):
  13. tr=[]
  14. c=0
  15. for i in x:
  16. if((i=='*') or (i=='+') or (i=='/') or (i=='-') or (i=='^')):
  17. tr.append(c)
  18. c+=1
  19. return(tr)
  20. def posio(x):
  21. tr=[]
  22. c=0
  23. for i in x:
  24. if((i=='(')):
  25. tr.append(c)
  26. c+=1
  27. return(tr)
  28. def posic(x):
  29. tr=[]
  30. c=0
  31. for i in x:
  32. if((i==')')):
  33. tr.append(c)
  34. c+=1
  35. return(tr)
  36. def calculate(x,y):
  37. p1=position(x,y)
  38. k1=int(x[p1-1])
  39. k2=int(x[p1+1])
  40. if y=='*':
  41. x.insert(p1-1,k1*k2)
  42. elif y=='/':
  43. x.insert(p1-1,k1/k2)
  44. elif y=='+':
  45. x.insert(p1-1,k1+k2)
  46. elif y=='-':
  47. x.insert(p1-1,k1-k2)
  48. elif y=='^':
  49. x.insert(p1-1,k1**k2)
  50. del x[p1]
  51. del x[p1]
  52. del x[p1]
  53. def calculator(b):
  54. if b[len(b)-1]=='=':
  55. kj=0
  56. a=[]
  57. while kj<(len(b)-1):
  58. a.append(b[kj])
  59. kj+=1
  60. else:
  61. a=b
  62.  
  63. if len(a)!=1:
  64. kr=posi(a)
  65. damage=0
  66. c=1
  67. while c<len(kr):
  68. if kr[c]-kr[c-1]==1:
  69. damage=1
  70. c+=1
  71. if damage==0:
  72. bs=[]
  73. c=1
  74. bs.append(a[:kr[0]])
  75. bs.append(a[kr[0]])
  76. while c<len(kr):
  77. bs.append(a[(kr[c-1]+1):kr[c]])
  78. bs.append(a[kr[c]])
  79. c+=1
  80. bs.append(a[(kr[c-1]+1):])
  81. cr=[]
  82. c=0
  83. while (c<len(bs)):
  84. ln=''
  85. for i in bs[c]:
  86. ln=ln+i
  87. c+=1
  88. cr.append(ln)
  89. a=cr
  90. i=1
  91. while len(a)!=1:
  92. if('^' in a):
  93. calculate(a,'^')
  94. elif ('*' in a) and ('/' in a):
  95. p1=position(a,'*')
  96. p2=position(a,'/')
  97. if p1<p2:
  98. calculate(a,'*')
  99. else:
  100. calculate(a,'/')
  101. elif ('*' in a):
  102. calculate(a,'*')
  103. elif ('/'in a):
  104. calculate(a,'/')
  105. elif ('+' in a) and ('-' in a):
  106. p1=position(a,'+')
  107. p2=position(a,'-')
  108. if p1<p2:
  109. calculate(a,'+')
  110. else:
  111. calculate(a,'-')
  112. elif ('+' in a):
  113. calculate(a,'+')
  114. elif ('-' in a):
  115. calculate(a,'-')
  116.  
  117. return(a[0])
  118. else:
  119. print("Invalid input")
  120. else:
  121. return a[0]
  122. def fpos(x,y):
  123. c=0
  124. pos=-1
  125. for i in x:
  126. if i==y:
  127. pos=c
  128. break
  129. c+=1
  130. return(pos)
  131. def lpos(x,y):
  132. c=0
  133. pos=-1
  134. for i in x:
  135. if i==y:
  136. pos=c
  137. c+=1
  138. return(pos)
  139. user_input=input()
  140. if user_input.count("=")>1:
  141. print("Invalid Input")
  142. exit()
  143. input()
  144. os.system("clear")
  145. kol=position(user_input,"=")
  146. if "=" in user_input:
  147. if kol!=len(user_input)-1:
  148. print("Invalid Input")
  149. exit()
  150. input()
  151. os.system("clear")
  152. yosh="!@#$%&_|}{\":?><QWERTYUIOPASDFGHJKLZXCVBNM[]\;'.,qwertyuiopasdfghjklzxcvbnm"
  153. for i in yosh:
  154. if i in user_input:
  155. print("Invalid Input")
  156. exit()
  157. input()
  158. os.system("clear")
  159. ocount=user_input.count("(")
  160. ccount=user_input.count(")")
  161. new_user_input=[]
  162. for i in user_input:
  163. new_user_input.append(i)
  164. danger=0
  165. copy_new_user_input=new_user_input.copy()
  166. if ocount==ccount:
  167. positions_of_openparanthesis=posio(copy_new_user_input)
  168. positions_of_closeparanthesis=posic(copy_new_user_input)
  169. abc=len(positions_of_openparanthesis)
  170. j=0
  171. while j<abc:
  172. i=1
  173. while i<len(copy_new_user_input) and len(positions_of_openparanthesis):
  174. if positions_of_openparanthesis[len(positions_of_openparanthesis)-1]+i in positions_of_closeparanthesis:
  175. del positions_of_openparanthesis[len(positions_of_openparanthesis)-1]
  176. i+=1
  177. j+=1
  178. if len(positions_of_openparanthesis)!=0:
  179. danger=1
  180.  
  181. else:
  182. danger=1
  183. if danger==0:
  184. while copy_new_user_input.count("("):
  185. positions_of_openparanthesis=posio(copy_new_user_input)
  186. positions_of_closeparanthesis=posic(copy_new_user_input)
  187. j=0
  188. while j<abc:
  189. i=1
  190. kos=len(positions_of_openparanthesis)
  191. while i<len(copy_new_user_input) and kos:
  192. if positions_of_openparanthesis[len(positions_of_openparanthesis)-1]+i in positions_of_closeparanthesis:
  193. fi=positions_of_openparanthesis[len(positions_of_openparanthesis)-1]
  194. la=positions_of_openparanthesis[len(positions_of_openparanthesis)-1]+i
  195. pain=calculator(copy_new_user_input[fi+1:la])
  196. copy_new_user_input.insert(fi,str(pain))
  197. del copy_new_user_input[fi+1:la+2]
  198. positions_of_openparanthesis=posio(copy_new_user_input)
  199. positions_of_closeparanthesis=posic(copy_new_user_input)
  200. kos=0
  201. i+=1
  202. j+=1
  203. result=calculator(copy_new_user_input)
  204. print(result)
  205. else:
  206. print("invalid input")
  207.  
  208. input()
  209. os.system("clear")
Add Comment
Please, Sign In to add comment