1. 程式人生 > >Python 計算器開發

Python 計算器開發

沒有 earch 檢查 dal return exp regular 替換字符 def

計算器開發

  1. 實現加減乘除及拓號優先級解析
  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 import re
  2 #檢查表達式合法性
  3 def check_expression(string):
  4     check_result = True
  5     #括號是否匹配
  6     if not string.count("
(") == string.count(")"): 7 print("表達式錯誤,括號未閉合!") 8 check_result = False 9 if re.findall([a-z],string.lower()): 10 print("表達式錯誤,包含非法字符!") 11 check_result = False 12 13 return check_result 14 15 #格式化字符串 16 def format_string(string): 17 string = string.replace("
--","+") 18 string = string.replace("-+","-") 19 string = string.replace("++","+") 20 string = string.replace("+-","-") 21 string = string.replace("*+","*") 22 string = string.replace("/+","/") 23 string = string.replace( ,‘‘) 24 return string 25 26 #計算乘,除法#(30+6*2)
27 def calc_mul_div(string): 28 #從字符串中獲取一個乘法的表達式 29 # regular = "\d+\.{0,}\d{0,}[\*\/][\-]{0,}\d+\.{0,}\d{0,}" 30 regular = "\d+\.?\d*([*/]|\*\*)[\-]?\d+\.?\d*" 31 #如果還能找到乘或除法表達式 32 while re.findall(regular,string): 33 #獲取表達式 34 expression = re.search(regular,string).group() 35 36 #如果是乘法 37 if expression.count("*")==1: 38 #獲取要計算的兩個數 39 x,y = expression.split("*") 40 #計算結果 41 mul_result = str(float(x) * float(y)) 42 #將計算的表達式替換為計算結果值 43 string = string.replace(expression,mul_result) 44 #格式化一下 45 string = format_string(string) 46 47 #如果是除法 48 if expression.count("/"): 49 #獲取要計算的兩個數 50 x,y = expression.split("/") 51 #計算除法 52 div_result = str(float(x) / float(y)) 53 #用結果替換表達式 54 string = string.replace(expression,div_result) 55 string = format_string(string) 56 57 if expression.count(*)==2: 58 x,y = expression.split(**) 59 pow_result = 1 60 for i in range(int(y)): 61 pow_result*=int(x) 62 string = string.replace(expression,str(pow_result)) 63 string = format_string(string) 64 return string 65 66 #計算加,減法 67 def calc_add_sub(string):##(30+12-23+24-3) 68 #(30+12-23+24-3) 69 #定義正則表達式 70 #add_regular = "[\-]{0,}\d+\.{0,}\d{0,}\+[\-]{0,}\d+\.{0,}\d{0,}" 71 #sub_regular = "[\-]{0,}\d+\.{0,}\d{0,}\-[\-]{0,}\d+\.{0,}\d{0,}" 72 add_regular = [\-]?\d+\.?\d*\+[\-]?\d+\.?\d* 73 sub_regular = [\-]?\d+\.?\d*\-[\-]?\d+\.?\d* 74 75 #開始加法 76 while re.findall(add_regular,string): 77 #把所有的加法都算完,獲取所有加法表達式 78 add_list = re.findall(add_regular,string) 79 for add_str in add_list: 80 #獲取兩個加法的數 81 x,y = add_str.split("+") 82 add_result = "+" + str(float(x) + float(y)) 83 string = string.replace(add_str,add_result) 84 string = format_string(string) 85 86 #開始減法 87 while re.findall(sub_regular,string): 88 sub_list = re.findall(sub_regular,string) 89 for sub_str in sub_list: 90 numbers = sub_str.split("-") 91 #-3-5的情況split會返回3個值 92 if len(numbers) == 3: 93 result = 0 94 for v in numbers: 95 if v: 96 result -= float(v) 97 else: 98 x,y = numbers 99 result = float(x) - float(y) 100 #替換字符串 101 string = string.replace(sub_str,"+" + str(result)) 102 string = format_string(string) 103 return string 104 105 106 107 108 if __name__ == "__main__": 109 source = 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) ) 110 #source = "2-((-40/5)*(4-3))" 111 #source = "3**2" 112 #source = "3*6*8" 113 114 if check_expression(source): 115 print("source:",source) 116 print("eval result",eval(source)) 117 source = format_string(source) 118 print(source) 119 120 while source.count("(") > 0: 121 #格式化 122 #去括號,得到括號的字符串,結果如:(30+6/3) 123 strs = re.search(\([^()]*\),source).group() 124 #將括號的表達式進行乘,除運算 125 replace_str = calc_mul_div(strs) 126 #將運算的結果在進行加,減運算 127 replace_str = calc_add_sub(replace_str) 128 #將括號的字符串替換為計算結果,結果包含(),替換時去掉():[1:-1] 129 source = format_string(source.replace(strs,replace_str[1:-1])) 130 131 else: 132 #沒有括號就到最後單一表達式了 133 # 算乘除 134 replace_str = calc_mul_div(source) 135 #算加減 136 replace_str = calc_add_sub(replace_str) 137 source = source.replace(source,replace_str) 138 print("my result:",source.replace("+",""))
1 source: 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )
2 eval result 2776672.6952380957
3 1-2*((60-30+(-40/5)*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2))
4 my result: 2776672.6952380957
5 
6 Process finished with exit code 0

Python 計算器開發