下面是我的计算器作业:
首先呢,我们做计算器时先要考虑的是乘除加减,但是这里面还有优先级的存在,我们要考虑到,
还要用到正则表达式,主要就是考虑正则表达式的运用,思路:
source = "6 - 5*(25 - 21 +(2 - 9 + 5*2 - 8/4 +(2 - 1*5+8*4))*(6+5+1*6))"
对于这样一个式子来言,我们先对它进行格式化处理和检查是否有合法字符,再找到最里面的括号里面的内容,先进行乘除,在进行加减,然后把这一个式子的值重新赋给这个式子方便下一次运算,先进行括号里面的运算,再去除括号,一步一步运算。
先写一个主函数,再在主函数中一步步实现,格式化,检查,找到最里面的括号,进行乘除函数,加减函数,最后运算得出结果。
1 #-author:"sangshijia" 2 #date: 2016/9/12 3 import re 4 #source = "2*5-5+3" 5 #source = "25 - 21 +2 - 9 + 5*2 - 8" 6 def check(s): 7 flag = True #检查式子,看是否有不合法字符 8 if re.findall("[a-zA-Z]",s): 9 print("Invalid numbers") 10 flag = False 11 return flag 12 def format(s):#格式化式子,把多余的符号代替 13 s = s.replace(" ","") #把空格替换 14 s = s.replace("++","+") 15 s = s.replace("+-","-") 16 s = s.replace("-+","-") 17 s = s.replace("--","+") 18 s = s.replace("*+","*") 19 s = s.replace("/+", "/") 20 return s 21 def cal_mul_div(s):#(2-1*5+8/4) 22 while re.search("[*/]",s): #在式子中找到*或者/的时候。[*/]表示*或者/ 23 # print("assssssssssssssssss",s) 24 ret1 = re.search("[-]?d+.?d*[*/][-]?d+.?d*",s)#.group() #1*5 #找到带有*huozhe /的式子 25 #print(ret1) 26 if ret1: 27 ret1 = ret1.group()# ret1 1*5 进行乘法 28 if re.search('*',ret1): 29 a, b = re.split("[*]", ret1) 30 ret2= float(a)*float(b) 31 ret2 = str(ret2) 32 s = s.replace(ret1, ret2) 33 s = format(s) 34 print('ssss',s) 35 #print(s) 36 elif re.search('/',ret1): #进行除法 37 a, b = re.split("[/]", ret1) 38 ret2 = float(a) / float(b) 39 ret2 = str(ret2) 40 s = s.replace(ret1, ret2) 41 s = format(s) 42 print('ooooo',s) 43 else: 44 break 45 return s #(2-5+2+20) 46 def cal_add_sub(s):#(2-5.0+2.0) 47 while re.search("[+-]",s): 48 # print("ss+++++++++++",s) 49 ret1 = re.search("[-]?d+.?d*[+-][-]?d+.?d*", s) 50 if ret1: 51 ret1 = ret1.group()#2-5.0 52 # print(type(ret1), ret1,"+++++++++++") 53 if re.search("+",ret1): #进行加法运算 54 a, b = re.split("[+]", ret1) 55 ret2 = float(a) + float(b) 56 ret2 = str(ret2) 57 s = s.replace(ret1, ret2) 58 s = format(s) 59 elif re.search("-",ret1): #进行减法运算 60 a, b = re.split("[-]", ret1) 61 ret2 = float(a) - float(b) 62 ret2 = str(ret2) 63 s = s.replace(ret1, ret2) 64 s = format(s) 65 print('nnnnn',s) 66 else: 67 break 68 return s 69 70 source = "6 - 5*(25 - 21 +(2 - 9 + 5*2 - 8/4 +(2 - 1*5+8*4))*(6+5+1*6))" 71 #source = "6 - 5*(25 - 21 +(2 - 9 + 5*2 - 8/4 +(2 - 1*5+8/4))*(6+5+1*6))" 72 print(eval(source)) #我们先用eval算出式子的值 73 if check(source): 74 strs = format(source) 75 print("statement",strs) 76 while re.search("(",strs): 77 strs1 = re.search("([^()]+)",strs).group()#(2 - 1*5) #找到最里面的括号进行乘除加减 78 print('strs1',strs1) 79 result1 = cal_mul_div(strs1) #(2-1*5+8/4) 80 print(result1, "rest+++++++++++++++++") 81 result2 = cal_add_sub(result1)#(2-5.0+2.0) 82 print('mmmmmm',result2)# (-1.0) 83 result2 = result2[1:-1] # (12) #[1:-1] 表示去除括号() 84 strs = strs.replace(strs1, result2) #把计算出的结果赋给取出来的这一个式子 85 # print(strs,"-------------------") 86 else: 87 # print("else statement") 88 result1 = cal_mul_div(strs) 89 result2 = cal_add_sub(result1) 90 print('re2', result2) 91 # result2 = result2[1:-1] # (12) 92 strs = strs.replace(strs, result2) 93 94 print( "result is ", strs) 95 96 97 # 正则表达式使用有问题 98 # 替换有问题 99 # 条件判断有问题