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

  • Post author:
  • Post category: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




版权声明:本文为lidongkui123原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。