利用python写一个简易的计算器

import re


def caculate(s):

    str_res = re.findall("\([^()]*\)", s)
    if(len(str_res) != 0):
        for item in str_res:
            res_int = eval(item)
            res = s.replace(item, str(res_int))
        return caculate(res)
    else:
        return str(eval(s))


print(caculate("1 - 2 * ( (60-30 +(-9-2-5-2*3-5/3-40*4/2-3/5+6*3) * (-9-2-5-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )"))



#29938074.5936508
#29938074.5936508


你可能感兴趣的:(python)