Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- import math
- s = "0.00001*0.1/0.000001"
- def small_calc(tup):
- f1,sign,f2 = tup
- f1 = float(f1)
- f2 = float(f2)
- if sign == "*":
- result = f1*f2
- elif f2 != 0 and sign == "/":
- result = f1/f2
- elif sign == "+":
- result = f1+f2
- elif sign == "-":
- result = f1-f2
- else:
- result = "Some error"
- return(result)
- def calc(string):
- res = re.split('(\+|\-|\/|\*|\(|\))', string)
- res = list(filter(None, res))
- if "(" in res:
- s1,s2 = res.index("("), res.index(")")
- small = res[s1+1:s2]
- del(res[s1:s2+1])
- r1 = small_calc(small)
- res.insert(s1,r1)
- r2 = small_calc(res)
- return(r2)
- else:
- if "/" in res:
- s1 = res.index("/")
- small = res[s1-1:s1+2]
- del(res[s1-1:s1+2])
- r1 = small_calc(small)
- if round(s1/5) == 0:
- res.insert(0,r1)
- else:
- res.insert(len(res),r1)
- r2 = small_calc(res)
- return(r2)
- elif "*" in res:
- s1 = res.index("*")
- small = res[s1-1:s1+2]
- del(res[s1-1:s1+2])
- r1 = small_calc(small)
- if round(s1/5) == 0:
- res.insert(0,r1)
- else:
- res.insert(len(res),r1)
- r2 = small_calc(res)
- return(r2)
- print(calc(s))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement