Advertisement
Guest User

Untitled

a guest
May 19th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.44 KB | None | 0 0
  1. import re
  2. import math
  3.  
  4. s = "0.00001*0.1/0.000001"
  5.  
  6. def small_calc(tup):
  7.     f1,sign,f2 = tup
  8.     f1 = float(f1)
  9.     f2 = float(f2)
  10.  
  11.     if sign == "*":
  12.         result = f1*f2
  13.     elif f2 != 0 and sign == "/":
  14.         result = f1/f2
  15.     elif sign == "+":
  16.         result = f1+f2
  17.     elif sign == "-":
  18.         result = f1-f2
  19.     else:
  20.         result = "Some error"
  21.     return(result)
  22.  
  23. def calc(string):
  24.     res = re.split('(\+|\-|\/|\*|\(|\))', string)
  25.  
  26.     res = list(filter(None, res))
  27.  
  28.     if "(" in res:
  29.         s1,s2 = res.index("("), res.index(")")
  30.         small = res[s1+1:s2]
  31.         del(res[s1:s2+1])
  32.         r1 = small_calc(small)
  33.         res.insert(s1,r1)
  34.         r2 = small_calc(res)
  35.         return(r2)
  36.     else:
  37.         if "/" in res:
  38.             s1 = res.index("/")
  39.             small = res[s1-1:s1+2]
  40.             del(res[s1-1:s1+2])
  41.             r1 = small_calc(small)
  42.             if round(s1/5) == 0:
  43.                 res.insert(0,r1)
  44.             else:
  45.                 res.insert(len(res),r1)
  46.             r2 = small_calc(res)
  47.             return(r2)
  48.         elif "*" in res:
  49.             s1 = res.index("*")
  50.             small = res[s1-1:s1+2]
  51.             del(res[s1-1:s1+2])
  52.             r1 = small_calc(small)
  53.             if round(s1/5) == 0:
  54.                 res.insert(0,r1)
  55.             else:
  56.                 res.insert(len(res),r1)
  57.             r2 = small_calc(res)
  58.             return(r2)
  59.  
  60. print(calc(s))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement