模拟加减乘除,用正则和递归实现,效率有点低,主要目的是练习正则。
执行效果如下:
1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998+10 * 568/14 )) - (-4*3)/ (16-3*2) ) #原始计算公式 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998+10 * 568/14 )) - (-4*3)/ (16-3*2) ) #原始计算公式 1-2*((60-30+-8.0*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2)) #提取(-40/5)进行计算,去除了所有的空白字符 1-2*((60-30+-8.0*173545.88095238098)-(-4*3)/(16-3*2)) #提取(9-2*5/3+7/3*99/4*2998+10*568/14)进行计算 1-2*(-1388337.0476190478-(-4*3)/(16-3*2)) #提取(60-30+-8.0*173545.88095238098)进行计算 1-2*(-1388337.0476190478--12.0/(16-3*2)) #提取(-4*3)进行处理 1-2*(-1388337.0476190478--12.0/10.0) #提取(16-3*2)进行处理 1-2*-1388335.8476190479 #提取(-1388337.0476190478--12.0/10.0)进行处理 2776672.6952380957 #最终结果,与计算器结果执行一致
1 #!/usr/bin/env python 2 #-*- coding:utf-8 -*- 3 # @Time : 2017/11/6 15:37 4 # @Author : lichuan 5 # @File : calcu.py 6 7 8 import re 9 import time 10 # s='3 * 10+9/3+2 * 6' 11 # re.compile('[-+]') 12 13 #做乘法除法 14 def mul(strings): 15 ''' 16 乘法加法 17 :param s: 18 :return: 19 ''' 20 # print('this is add_del function') 21 pattern=re.compile('\d+\.?\d*[\*\/]+[-+]?\d+\.?\d*') 22 match=pattern.search(strings) 23 if not match: 24 return strings 25 s=match.group() 26 if '*' in s: 27 value1=s.split('*')[0] 28 value2=s.split('*')[1] 29 value=float(value1)*float(value2) 30 elif '/' in s: 31 value1 = s.split('/')[0] 32 value2 = s.split('/')[1] 33 value = float(value1) / float(value2) 34 new_s=pattern.sub(str(value),strings,count=1) 35 # print(new_s) 36 return mul(new_s) 37 38 #做加法减法 39 def add_del(strings): 40 # print('this is add_del function') 41 strings=clear(strings) 42 pattern = re.compile('[-]?\d+\.?\d*\s*[-+]+\s*\d+\.?\d*') 43 match = pattern.search(strings) 44 if not match: 45 return strings 46 s=match.group() 47 if '+' in s: 48 value1 = s.split('+')[0] 49 value2 = s.split('+')[1] 50 value = float(value1) + float(value2) 51 new_s = pattern.sub(str(value), strings, count=1) 52 return add_del(new_s) 53 elif '-' in s : 54 if re.match('^-',s): 55 value1 = s.split('-')[1] 56 value2 = s.split('-')[2] 57 value = -float(value1) - float(value2) 58 else: 59 value1 = s.split('-')[0] 60 value2 = s.split('-')[1] 61 value = float(value1) - float(value2) 62 new_s = pattern.sub(str(value), strings, count=1) 63 # print(new_s) 64 return add_del(new_s) 65 66 #提取括号内容进行操作 67 def brackets(strings): 68 # print('this is brackets function') 69 print(strings) 70 strings = re.sub('\s', '', strings) 71 # strings=clear(strings) 72 pattern = re.compile('\([-]?\d+\.?\d*\s*([-+*/]*\s*\d+\.?\d*\s*){1,}\)') 73 match = pattern.search(strings) 74 if not match: 75 s=mul(strings) 76 r=add_del(s) 77 return r 78 else: 79 s=match.group() 80 new_s=s.replace('(','') 81 new_s=new_s.replace(')','') 82 m_s=mul(new_s) 83 a_s=add_del(m_s) 84 # print(a_s) 85 # print(strings) 86 r=pattern.sub(a_s,strings,count=1) 87 return brackets(r) 88 89 #合并--、-+、+-、++等重复的运算符 90 def clear(strings): 91 # print('this is clear function') 92 s=strings 93 pattern_1 = re.compile('[-+]+[-+]+') 94 m = pattern_1.search(strings) 95 if not m: 96 return strings 97 else: 98 strings=re.sub('--','+',s) 99 strings=re.sub('-+','-',strings) 100 strings=re.sub('\+\+','+',strings) 101 strings=re.sub('\+\-','-',strings) 102 return clear(strings) 103 104 105 if __name__ == '__main__': 106 start=time.time() 107 s = '1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998+10 * 568/14 )) - (-4*3)/ (16-3*2) )' 108 print(s) 109 b=brackets(s) 110 print(b) 111 end=time.time()