首页 > 代码库 > 计算器<代码>

计算器<代码>

  1 import re  2 l_no = "-4.0*-4+((-1-8.0*2*-1)-(-9.456/1.57))/8+-8*7"  3 true_tr = "-4.0*-4+((-1-8.0*2*-1)-(-9.456/1.57))/8+-8*7"  4 trunswer = eval(true_tr)  5 print(trunswer)  6   7 l = re.sub(" +","",l_no)  8 l_list = list(l)  9 def check_w(formula):                     #  check函数 10     formula=re.sub("--","+",formula) 11     formula=re.sub("-\+|\+-","+",formula) 12     a = re.findall("[a-z]|[A-Z]",formula) 13     b = re.findall("\(\)",formula) 14     if a or b: 15         print("存在字母空括号") 16         exit() 17     return formula 18 l_no = check_w(l_no) 19 s = l_no 20 def left_parenthesis(formula):         # find the left parenthesis  -- 21     a = re.search("\(", formula) 22     if a == None: 23         return None 24     b = a.span() 25     return b[1] 26 def right_parenthesis(formula):          # find the right parenthesis -- 27     a = re.search("\)", formula) 28     if a == None: 29         return None 30     b = a.span() 31     return b[1] 32 def code_muldiv(numcc):                # function for multiplication and division 33     result = []                        # 这是一种算法 所有factor制成列表 所有mathematical symbol 34     for i in numcc:                    # (-+或者*/)制成一个列表 之后利用列表有顺序来进行计算 35         jishufuhao = 0                 # 正好适合8*-8这种形式 36         jishushu = 1 37         fuhao2 = [] 38         num = [] 39         fuhao2 = re.findall("\*|/",i) 40         num = re.split("[*/]",i)             # 将所有因数(除数)整理成一个列表 41         flag2 =True 42         while flag2: 43             if fuhao2[jishufuhao] == "*": 44                 ga = float(num[jishushu-1]) * float(num[jishushu]) 45                 num[jishushu] = ga 46                 jishushu+=1 47                 jishufuhao+=1 48             elif fuhao2[jishufuhao] == "/": 49                 ha = float(num[jishushu - 1]) / float(num[jishushu]) 50                 num[jishushu] = ha 51                 jishushu += 1 52                 jishufuhao += 1 53             if jishufuhao+1 > len(fuhao2): 54                 flag2 = False 55         result.append(num[jishushu-1]) 56     return result 57 def code_addsub(numcc):       # function for addition and subtraction 58     result = [] 59     for i in numcc: 60         jishufuhao = 0 61         jishushu = 1 62         fuhao2 = [] 63         num = [] 64         i = re.sub("--","+",i) 65         i = re.sub("\+-|-\+","-",i) 66         i = re.sub("^\+","",i) 67         a = re.findall("^-\d+\.?\d*",i)    ### 如果formula为 -9+8 ,这个函数的功能 68         if a == []:                        ### 就是将其转换为  0+8-9 这样便将minus 69             pass                           ### 转化为无符号式子 70         else:                              ### 71             i = re.sub(a[0],"0",i,1)       ### 72             i = "".join([i,a[0]])          ### 73         fuhao2 = re.findall("\+|-",i) 74         num = re.split("[+-]",i) 75         if fuhao2 ==[]: 76             return numcc 77         flag2 = True 78         while flag2: 79             if fuhao2[jishufuhao] == "+": 80                 ga = float(num[jishushu-1]) + float(num[jishushu]) 81                 num[jishushu] = ga 82                 jishushu+=1 83                 jishufuhao+=1 84             elif fuhao2[jishufuhao] == "-": 85                 ha = float(num[jishushu - 1]) - float(num[jishushu]) 86                 num[jishushu] = ha 87                 jishushu += 1 88                 jishufuhao += 1 89             if jishufuhao+1 > len(fuhao2): 90                 flag2 = False 91         result.append(num[jishushu-1]) 92     return result 93 def exchange_l(formula):           # for calculating mul div 94     flag4 = True 95     while flag4: 96         need_md = re.findall("\d+\.?\d*[*/]-?\d+\.?\d*",formula) 97         if need_md == []: 98             break 99         e = need_md100         k = code_muldiv(e)101         out_s = formula.replace(str(need_md[0]),str(k[0]))102         formula =out_s103     return formula104 flag2 = True105 while flag2:106     flag = True107     a = 0108     y_y=right_parenthesis(s)109     if y_y == None:                       # 判断 没有括号时,直接计算110         formula_jj = exchange_l(l)111         final_result = code_addsub([formula_jj])112         l = l.replace(l, str(final_result[0]), 1)113         s = l114         print("result:",s)115         break116     else:117         y = right_parenthesis(s) - 1118     while flag:                              # 多次寻找左括号 并且切片后再寻找119         position_psis = left_parenthesis(s)120         if position_psis == None:121             position_psis = 0122             break123         s= s[position_psis:]             # 将找到的左括号后面的字符串切片124         a += position_psis               # 记录下序列号125         if a > y:                        # 若是左括号序列号比右括号大 则停止寻找126             flag = False127     a = a - position_psis128     need_l = l[a:y]                      # 需要处理的括号内容129     ex_input = l[a - 1:y + 1]130     need_l =str(need_l)   # 3+2-3131     formula_jj = exchange_l(need_l)      # 进行乘除处理132     final_result = code_addsub([formula_jj]) # 进行加减处理133     l = l.replace(ex_input, str(final_result[0]), 1) # 用结果替换掉字符串134     s = l                                            # 赋值再次循环用

 

计算器<代码>