Import Re from functools Import the reduce DEF mul_div (exp): # of multiplication or division of two numbers IF ' * ' in exp: A, B = exp.split ( ' * ' ) return a float (A) * a float (B) IF ' / ' in exp: A, B = exp.split ( ' / ' ) return a float (A) / a float (B) DEF exp_fmt (exp): the while the re.search ( '[+-]{2,}',exp): exp = exp.replace('--','+') exp = exp.replace('+-','-') exp = exp.replace('-+','-') exp = exp.replace('++','+') return exp def remove_addsub(exp): ret = re.findall(' ? - (?.: \ \ D +)? [+] \ D + ' , exp) RES = the reduce ( the lambda A, B: a float (A) + a float (B), RET) return RES DEF remove_muldiv (exp): # calculate all multiplication and division expression the while True: RET = the re.search ( ' .??. \ D + (\ \ D +) [* /] - \ D + (\ \ D +)? ' , exp) IF RET : son_exp = ret.group () # . 3. 4 *. 5 * 12 is RES = mul_div (son_exp) exp = exp.replace (son_exp, STR (RES)) the else : returnexp DEF CAL (exp): RES = remove_muldiv (exp) # computation is in RES = exp_fmt (RES) # sign finishing RET = remove_addsub (RES) # calculating subtraction return RET DEF main (exp): exp = exp.replace ( ' ' , ' ' ) the while True: RET = the re.search ( ' \ ([^ ()] + \) ' , exp) IF RET: RES = CAL (ret.group ()) exp = exp.replace(ret.group(), str(res)) else: return cal(exp) exp = '1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )' ret = main(exp) print(ret)