python3 正则表达式 re模块之辣眼睛 计算器
额。。。学到几个常用模块了,也要其中考试了,每天晚上敲一点,敲得脑壳疼,不过又想到好一点的办法了,有时间再改吧。
此非吾所欲也,实属无奈也。。。。复习之路漫漫,吾将到书上求索,在此不多逗留,我挥一挥衣袖,留下一段惨不忍睹的代码,且不带走一片云彩。
1 #!usr/bin/env/ python 2 # -*- coding:utf-8 -*- 3 # Author: XiaoFeng 4 import re 5 6 7 a = '1 - 2 * ( ( 6 0 -3 0 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )' 8 a_new = re.sub(" ", "", a) 9 print(a_new) 10 ret = re.findall(r"\([^()]*\)", a_new) 11 while ret: 12 ret = re.findall(r"\([^()]*\)", a_new) 13 print(ret) 14 for element in ret: 15 formula = element 16 formula = formula.replace("(", "") 17 formula = formula.replace(")", "") 18 # print(formula) 19 for i in formula: 20 if "--" in formula: 21 formula = formula.replace("--", "+") 22 elif "*" == i: 23 handle = re.search(r"[-+]?\d+\.?\d*\*[-+]?\d+\.?\d*", formula) 24 if handle: 25 handle_list = handle.group() 26 else: 27 continue 28 handle_a, handle_b = re.findall(r"[-+]?\d+\.?\d*", handle_list) 29 if "." in handle_a or "." in handle_b: 30 result = float(handle_a) * float(handle_b) 31 if result >= 0: 32 result = "+" + str(result) 33 result = str(result) 34 else: 35 result = int(handle_a) * int(handle_b) 36 if result >= 0: 37 result = "+" + str(result) 38 result = str(result) 39 formula = formula.replace(handle_list, result) 40 # print(formula) 41 elif "/" == i: 42 handle = re.search(r"[-+]?\d+\.?\d*/[-+]?\d+\.?\d*", formula) 43 if handle: 44 handle_list = handle.group() 45 else: 46 continue 47 handle_a, handle_b = re.findall(r"[-+]?\d+\.?\d*", handle_list) 48 if "." in handle_a or "." in handle_b: 49 result = float(handle_a) / float(handle_b) 50 if result >= 0: 51 result = "+" + str(result) 52 result = str(result) 53 else: 54 result = int(handle_a) / int(handle_b) 55 if result >= 0: 56 result = "+" + str(result) 57 result = str(result) 58 formula = formula.replace(handle_list, result) 59 # print(formula) 60 for i in formula: 61 if "--" in formula: 62 formula = formula.replace("--", "+") 63 elif "+" == i: 64 handle = re.search(r"[-+]?\d+\.?\d*\+[-+]?\d+\.?\d*", formula) 65 if handle: 66 handle_list = handle.group() 67 else: 68 continue 69 handle_a, handle_b = re.findall(r"[-+]?\d+\.?\d*", handle_list) 70 if "." in handle_a or "." in handle_b: 71 result = float(handle_a) + float(handle_b) 72 if result >= 0: 73 result = "+" + str(result) 74 result = str(result) 75 else: 76 result = int(handle_a) + int(handle_b) 77 if result >= 0: 78 result = "+" + str(result) 79 result = str(result) 80 formula = formula.replace(handle_list, result) 81 # print(formula) 82 elif "-" == i: 83 handle = re.search(r"[-+]?\d+\.?\d*-[-+]?\d+\.?\d*", formula) 84 if handle: 85 handle_list = handle.group() 86 else: 87 continue 88 handle_a, handle_b = re.findall(r"[-+]?\d+\.?\d*", handle_list) 89 if "." in handle_a or "." in handle_b: 90 result = float(handle_a) + float(handle_b) 91 if result >= 0: 92 result = "+" + str(result) 93 result = str(result) 94 else: 95 result = int(handle_a) + int(handle_b) 96 if result >= 0: 97 result = "+" + str(result) 98 result = str(result) 99 formula = formula.replace(handle_list, result) 100 # print(formula) 101 # print(formula) 102 # print(element) 103 a_new = a_new.replace(element, formula) 104 # print(a_new) 105 print("------------") 106 print(a_new) 107 formula = a_new 108 for i in formula: 109 if "--" in formula: 110 formula = formula.replace("--", "+") 111 elif "*" == i: 112 handle = re.search(r"[-+]?\d+\.?\d*\*[-+]?\d+\.?\d*", formula) 113 if handle: 114 handle_list = handle.group() 115 else: 116 continue 117 handle_a, handle_b = re.findall(r"[-+]?\d+\.?\d*", handle_list) 118 if "." in handle_a or "." in handle_b: 119 result = float(handle_a) * float(handle_b) 120 if result >= 0: 121 result = "+" + str(result) 122 result = str(result) 123 else: 124 result = int(handle_a) * int(handle_b) 125 if result >= 0: 126 result = "+" + str(result) 127 result = str(result) 128 formula = formula.replace(handle_list, result) 129 print(formula) 130 elif "/" == i: 131 handle = re.search(r"[-+]?\d+\.?\d*/[-+]?\d+\.?\d*", formula) 132 if handle: 133 handle_list = handle.group() 134 else: 135 continue 136 handle_a, handle_b = re.findall(r"[-+]?\d+\.?\d*", handle_list) 137 if "." in handle_a or "." in handle_b: 138 result = float(handle_a) / float(handle_b) 139 if result >= 0: 140 result = "+" + str(result) 141 result = str(result) 142 else: 143 result = int(handle_a) / int(handle_b) 144 if result >= 0: 145 result = "+" + str(result) 146 result = str(result) 147 formula = formula.replace(handle_list, result) 148 print(formula) 149 print("=========") 150 for i in formula: 151 if "--" in formula: 152 formula = formula.replace("--", "+") 153 elif "+" == i: 154 handle = re.search(r"[-+]?\d+\.?\d*\+[-+]?\d+\.?\d*", formula) 155 if handle: 156 handle_list = handle.group() 157 else: 158 continue 159 handle_a, handle_b = re.findall(r"[-+]?\d+\.?\d*", handle_list) 160 if "." in handle_a or "." in handle_b: 161 result = float(handle_a) + float(handle_b) 162 if result >= 0: 163 result = "+" + str(result) 164 result = str(result) 165 else: 166 result = int(handle_a) + int(handle_b) 167 if result >= 0: 168 result = "+" + str(result) 169 result = str(result) 170 formula = formula.replace(handle_list, result) 171 print(formula) 172 elif "-" == i: 173 handle = re.search(r"[-+]?\d+\.?\d*-[-+]?\d+\.?\d*", formula) 174 if handle: 175 handle_list = handle.group() 176 else: 177 continue 178 handle_a, handle_b = re.findall(r"[-+]?\d+\.?\d*", handle_list) 179 if "." in handle_a or "." in handle_b: 180 result = float(handle_a) + float(handle_b) 181 if result >= 0: 182 result = "+" + str(result) 183 result = str(result) 184 else: 185 result = int(handle_a) + int(handle_b) 186 if result >= 0: 187 result = "+" + str(result) 188 result = str(result) 189 formula = formula.replace(handle_list, result) 190 print(formula)