Nonsense EDITORIAL:
I am a novice white Python, some individuals feel that self-programming knowledge or grammar point very clear look at the time, and then after a few days the goose will not forget, so I plan to do something small projects in order to impress the knowledge points to remember the simplicity.
If only no brain shining on someone else's code knock, and slowly you will find that there is nothing with eggs, you just put the code up a knock but others do not understand why you wrote.
I think for yourself if not a bit that is not enough, when the needs of a specific show it to you to realize, you will find a little thought and no.
Self is best to find yourself a little needs to achieve, they would encounter many problems in the process of implementation, this time to first find out where the problem, then solve the problem step by step debugging, be sure to record the last final finishing.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Simple calculator development needs:
The simple arithmetic operations, and their priority is resolved in parentheses, and can output the correct calculation result.
Reference Alex Big Brother: Teaching Project - simple calculator by Python
analysis:
1. first need to detect the user's input is legal, such as matching parentheses
2. Since the input string, so the need for string parsing parses out the "+ - * / ()" symbols such
3. Priority resolution, first calculate the brackets content, in the calculation of multiplication and division, addition and subtraction last count
. 1 # ! - * - UTF-Coding. 8 - * - 2 # @time: $ [DATE] $ [the TIME]! . 3 # @author: GG! . 4 . 5 Import Re . 6 . 7 DEF comupute_mut_and_div (Formula): . 8 '' ' Operators multiplication and division '' ' . 9 Operators = the re.findall ( " [* /] " , Formula) 10 calc_list = re.split ( " [* /] " , Formula) . 11 RES = None # detail assignment for the first pass loop bye 12 for index, i inthe enumerate (calc_list): 13 is IF RES: # for the first time here is not performed, so the list index is not out of range 14 IF Operators [-index. 1] == " * " : 15 RES * = a float (I) 16 elif Operators [ . 1-index] == " / " : . 17 RES / = a float (I) 18 is the else : . 19 RES = a float (I) 20 is Print ( " \ 033 [31 is; 1M [% S] calculation result = \ 033 [0m " % Formula, RES) 21 return res 22 23 def handle_special_sign(plus_and_sub_operators, mul_and_div): 24 """ 25 有时会遇到这种情况:1-2*-2/1 26 plus_and_sub_operators:['-', '-'] 27 mul_and_div:['1', '2*', '2/1'] 28 :param plus_and_sub_operators: 29 :param mul_and_div: 30 :return: 31 """ 32 for index, i in enumerate(mul_and_div): 33 i = i.strip() 34 if i.endswith("*") or i.endswith("/"): 35 mul_and_div[index] = mul_and_div[index] + plus_and_sub_operators[index] + mul_and_div[index+1] 36 del mul_and_div[index+1] 37 del plus_and_sub_operators[index] 38 return plus_and_sub_operators, mul_and_div 39 40 def handle_minus_inlist(operators_list, calc_list): 41 """ 42 处理加减带负号的运算,1--4 = 1-(-4) = 1+4 43 operators_list: ['-'] 44 calc_list: ['1', '', '4.0'] 45 :param operators_list: 46 is : param calc_list: 47 : return: 48 "" " 49 for index, I in the enumerate (calc_list): 50 IF I == '' : 51 is calc_list [index +. 1] = " - " + calc_list [index +. 1 ] 52 is del calc_list [index] 53 is return operators_list, calc_list 54 is 55 DEF Compute (Formula): 56 is ' '' 57 is to parse the string 58 : param Formula: input string of 59 : return: the calculation result 60 ''' 61 formula = formula.strip("()") # 去括号,这里是(1+2-2*3+3/6) 62 plus_and_sub_operators = re.findall("[+-]", formula) 63 mul_and_div = re.split("[+-]", formula) # 取出乘除公式 64 65 plus_and_sub_operators, mul_and_div = handle_special_sign(plus_and_sub_operators, mul_and_div) 66 for i in mul_and_div: 67 if len(i) > 1: 68 print(i) 69 res = comupute_mut_and_div(i) 70 formula = formula.replace(i, str(res)) 71 else: 72 continue 73 calc_list = re.split("[+-]", formula) # 去除乘除公式 74 plus_and_sub_operators, calc_list = handle_minus_inlist(plus_and_sub_operators, calc_list) 75 total_res = None 76 for index, i in enumerate(calc_list): 77 if total_res: 78 if plus_and_sub_operators[index - 1] == "-": 79 total_res -= float(i) 80 elif plus_and_sub_operators[index - 1] == "+": 81 total_res += float(i) 82 else: 83 total_res = float(i) 84 print("\033[32;1m[%s]运算结果:\033[0m" % formula, total_res) 85 return total_res 86 87 def calc(formula): 88 '''去括号''' 89 brackets_flag = True 90 while brackets_flag: 91 m = re.search("\([^()]*\)", formula) 92 if m: 93 sub_res = compute(m.group()) # 替换 94 formula = formula.replace(m.group(), str(sub_res)) 95 else: 96 print("最终结果:", compute(formula)) 97 brackets_flag = False 98 99 if __name__ == '__main__': 100 # formula = "-1-2*((60-30+(-40/5)*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2))" 101 formula = input("请输入:") 102 calc(formula)
There is no tampering detection implement input, others are achieved, primarily with the regular expression extraction, segmentation and replacement, and enumerate function used in a for loop.
Subsequent re-update, improve operations, and other more advanced graphical interface display.