import re def atom_cal(exp): # 计算乘除法 if '*' in exp: a,b = exp.split('*') return str(float(a) * float(b)) elif '/' in exp: a, b = exp.split('/') return str(float(a) / float(b)) def format_exp(exp): # 格式化符号 exp = exp.replace('--','+') exp = exp.replace('+-','-') exp = exp.replace('-+','-') exp = exp.replace('++','+') return exp def mul_div(exp): # 查找传入字符串的乘除法 while True: ret = re.search('d+(.d+)?[*/]-?d+(.d+)?',exp) if ret: atom_exp = ret.group() res = atom_cal(atom_exp) exp = exp.replace(atom_exp,res) else:return exp def add_sub(exp): # 计算加减法 ret = re.findall('[+-]?d+(?:.d+)?', exp) exp_sum = 0 for i in ret: exp_sum += float(i) return exp_sum def cal(exp): exp = mul_div(exp) exp = format_exp(exp) exp_sum = add_sub(exp) return exp_sum # float def main(exp): exp = exp.replace(' ','') # 去空格 while True: ret = re.search('([^()]+)',exp) # 找到最内层括号里的内容 if ret : inner_bracket = ret.group() res = str(cal(inner_bracket)) exp = exp.replace(inner_bracket,res) exp = format_exp(exp) else:break return cal(exp) s = '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(s) print(ret,type(ret))