1 ImportRe2 ImportFunctools3 4 5 defMinus_operator_handler (Formula):6 " "handle some special minus-sign operations" "7Minus_operators = Re.split ("-", Formula)8Calc_list= Re.findall ("[0-9]", Formula)9 ifMinus_operators[0] = ="':#the first value must be the minus sign .TenCalc_list[0] ='-%s'%Calc_list[0] Oneres = Functools.reduce (LambdaX,y:float (x)-float (y), calc_list) A Print("\033[33;1m minus sign [%s] processing result: \033[0m"%formula, RES) - returnRes - the defremove_duplicates (Formula): -Formula = Formula.replace ("++","+") -Formula = Formula.replace ("+-","-") -Formula = Formula.replace ("-+","-") +Formula = Formula.replace ("--","+") -Formula = Formula.replace ("- -","+") + returnFormula A defCompute_mutiply_and_dividend (Formula): at " "multiplication, it's a string." " -Operators = Re.findall ("[*/]", Formula) -Calc_list = Re.split ("[*/]", Formula) -res =None - forIndex,iinchEnumerate (calc_list): - ifRes: in ifOPERATORS[INDEX-1] = ="*": -Res *=float (i) to elifOPERATORS[INDEX-1] = ="/": +Res/=float (i) - Else: theres =float (i) * $ Print("\033[31;1m[%s] Operation result =\033[0m"%formula, RES)Panax Notoginseng returnRes - defhandle_minus_in_list (operator_list,calc_list): the " "Sometimes this happens when you separate the arithmetic characters from the values ['-', '-', '-'] [', ' 14969037.996825399 ', ', ' 12.0/10.0 '] + This requires that the spaces in the 2nd list be turned into minus signs and the values that follow them, and it's disgusting. A " " the forIndex,iinchEnumerate (calc_list): + ifi = ="':#it actually represents the minus sign, and it's changed to minus. -Calc_list[index+1] = i + calc_list[index+1].strip () $ defhandle_special_occactions (plus_and_minus_operators,multiply_and_dividend): $ " "Sometimes this happens, ['-', '-'] [' 1 ', ' 2 * ', ' 14969036.7968254 '],2* ... This is actually 2*-14969036.7968254, it needs special treatment, it's disgusting." " - forIndex,iinchEnumerate (multiply_and_dividend): -i =I.strip () the ifI.endswith ("*")orI.endswith ("/"): -Multiply_and_dividend[index] = Multiply_and_dividend[index] + Plus_and_minus_operators[index] + multiply_and_ Dividend[index+1]Wuyi delMultiply_and_dividend[index+1] the delPlus_and_minus_operators[index] - returnPlus_and_minus_operators,multiply_and_dividend Wu defCompute (Formula): - " "here is the formula without parentheses" " About $Formula = Formula.strip ("()")#Remove the extension of the outer package -Formula = Remove_duplicates (Formula)#remove out-of-duplicate +--number -Plus_and_minus_operators = Re.findall ("[+-]", Formula) -Multiply_and_dividend = Re.split ("[+-]", formula)#Remove Multiplication Formula A ifLen (Multiply_and_dividend[0].strip ()) = = 0:#that means it's definitely a minus sign . +MULTIPLY_AND_DIVIDEND[1] = plus_and_minus_operators[0] + multiply_and_dividend[1] the delMultiply_and_dividend[0] - delPlus_and_minus_operators[0] $ thePlus_and_minus_operators,multiply_and_dividend=handle_special_occactions (plus_and_minus_operators,multiply_and_dividend) the forIndex,iinchEnumerate (multiply_and_dividend): the ifRe.search ("[*/]", i): theSub_res =compute_mutiply_and_dividend (i) -Multiply_and_dividend[index] =Sub_res in the #Start Operation +,- the Print(Multiply_and_dividend, plus_and_minus_operators) AboutTotal_res =None the forIndex,iteminchEnumerate (multiply_and_dividend): the ifTotal_res:#represents not the first cycle the ifPLUS_AND_MINUS_OPERATORS[INDEX-1] = ='+': +Total_res + =Float (item) - elifPLUS_AND_MINUS_OPERATORS[INDEX-1] = ='-': theTotal_res-=Float (item)Bayi Else: theTotal_res =Float (item) the Print("\033[32;1m[%s] Operation result: \033[0m"%formula,total_res) - returnTotal_res - the defCalc (Formula): the " "calculate the main entrance of the program, the main logic is to calculate the value in the extension number, calculate and then calculate the multiplication, and then calculate plus minus" " theParenthesise_flag =True theCalc_res = None#initialization operation result is none, not yet started operation, of course, none. - whileParenthesise_flag: them = Re.search ("\([^()]*\)", formula)#find the innermost extension number. the ifm: the #print ("value in the first extension number:", M.group ())94Sub_res =Compute (M.group ()) theFormula =Formula.replace (M.group (), str (sub_res)) the Else: the Print('\033[41;1m----No extension ...---\033[0m')98 About Print('\n\n\033[42;1m Final Result: \033[0m', Compute (formula)) -Parenthesise_flag = False#the extension number in the formula has been stripped.101 102 if __name__=='__main__':103 104 #res = Calc ("1-2 * ((60-30 + ( -40/5) * (9-2*5/3 + 7/3*99/4*2998 +10 * 568/14)-( -4*3)/(16-3*2))") theres = Calc ("1-2 * ((60-30 + ( -9-2-5-2*3-5/3-40*4/2-3/5+6*3) * ( -9-2-5-2*5/3 + 7/3*99/4*2998 +10 * 568/14))-( -4*3)/(16-3*2) )")
Python Implementation Calculator