python制作一个简易计算器_python小项目——实现简易的计算器

1 #!usr/bin/evn python2 #coding:cp9363 #Author:Siwen4 5 6 print ("Hello world!")7 8 9 importre10 11 defhandle_special_occactions(plus_minus,multiply_and_dividend):12  #有时会出现这种情况 , ['-', '-'] ['1 ', ' 2 * ', '14969036.7968254'],2*...后面这段实际是 2*-14969036.7968254,需要特别处理下,太恶心了'''13     for index,i inenumerate(multiply_and_dividend):14         i =i.strip()15         if i.endswith("*") or i.endswith("/"):16             multiply_and_dividend[index] = multiply_and_dividend[index] + plus_minus[index] + multiply_and_dividend[index+1]17             del multiply_and_dividend[index+1]18             delplus_minus[index]19     returnplus_minus,multiply_and_dividend20 21 defremove_jia(formula):22     formula = formula.replace("++","+")23     formula = formula.replace("+-","-")24     formula = formula.replace("-+","-")25     formula = formula.replace("--","+")26     formula = formula.replace("- -","+")27     returnformula28 29 30 defcompute_mutiply_and_dividend(formula):31     operators = re.findall("[*/]", formula )32     calc_list = re.split("[*/]", formula )33     t_res =None34     for index,item inenumerate(calc_list):35         item=item.strip()36         if t_res: #代表不是第一次循环37             if operators[index-1] == '/':38                 t_res /=float(item)39             if operators[index-1] == '*':40                 t_res *=float(item)41         else:42             t_res =float(item)43     print("乘除结果为:",t_res)44     return(t_res)45 46 defcompute(formula):47     #计算不带括号的算式48     formula=formula.strip('()')49     formula=remove_jia(formula)50     plus_minus=re.findall(r'[+-]',formula)51     multiply_and_dividend = re.split("[+-]", formula)#去除乘除的公式52     if len(multiply_and_dividend[0].strip()) == 0:#代表这肯定是个负号53         multiply_and_dividend[1] = plus_minus[0] + multiply_and_dividend[1]54         delmultiply_and_dividend[0]55         delplus_minus[0]56 57     plus_minus,multiply_and_dividend=handle_special_occactions(plus_minus,multiply_and_dividend)58 59     for index,i inenumerate(multiply_and_dividend):60         if re.search("[*/]",i):61             sub_res =compute_mutiply_and_dividend(i)62             multiply_and_dividend[index] =sub_res63 64     print(multiply_and_dividend, plus_minus)65     total_res =None66     for index,item inenumerate(multiply_and_dividend):67         68         if total_res: #代表不是第一次循环69             70             if plus_minus[index-1] == '+':71                 total_res +=float(item)72             if plus_minus[index-1] == '-':73                 total_res -=float(item)74 75         else:76             total_res =float(item)77     print('算式%s计算结果为:'%formula,total_res)78     return(total_res)79     80     81 82 defcale(formula):83     #主要逻辑,先算括号里的值,再算乘除,再算加减84     parenthesise_flag=True85     sub=None86     sub_res=087     whileparenthesise_flag:88         m=re.search('([^()]*)',formula)89         90         ifm:91             m=m.group()92             print(m)93             #print('先算括号里',m)94             sub_res=compute(m)95             print(sub_res)96             formula=formula.replace(m,str(sub_res))97         else:98             print('没有括号了')99             #formula=formula.replace(m.group(),str(sub_res))100             print(formula)101             sub=compute(formula)102            #print '计算结果为:',sub103             parenthesise_flag=False#算式中的括号都已经被剥除了104 res="1-2*((60-30+(-40/5)*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2))"105 #res='1+2*(3+4)'106 res=cale(res)107             108

你可能感兴趣的:(python制作一个简易计算器)