Calculator example
First, the idea of text analysis
Get an expression, for example, ' 3+ (-80/2) + 24 * 5 - (18/3 + 4 * (90 / 15-100) -3) ', by this expression, we need to look into mathematics outside algorithms, should also take into account the input format of the input expression problems existing in the course
1, the format problem: there is a space expression, it is necessary to remove the spaces
2, algorithm: go parentheses brackets, the first calculation expression without parentheses inside the brackets; first multiplication and division and then count subtraction operator; sign problems have negative negative negative makes
3, the use of regular expressions to match the corresponding entry expression is then calculated
4, each of which had to be calculated is fed back to the place where that item
5, repeat things, the need to use cycle
Second, the calculation sub-modules (code)
- 1, to a space module
exp = '3+ (-80/2)+ 24*5-(18/3+ 4*(90/15-100) -3)' new_exp = exp.replace(' ','') print(new_exp)
operation result:
3+(-80/2)+24*5-(18/3+4*(90/15-100)-3)
- 2, and calculates the matching items without parentheses brackets
First use regular expressions to identify items without parentheses brackets
= the re.search RET ( ' \ ([^ ()] + \) ' , new_exp) IF RET: exp_no_bracket = ret.group () # screened brackets expression without parentheses Print (exp_no_bracket)
operation result:
(-80/2)
The brackets without parentheses items go parentheses, then multiplication and division to find simple items, call the function using the method of calculation of multiplication and division results
DEF cal_exp_no_bracket (EX): # EX parentheses are items without parentheses EX = ex.strip ( ' () ' ) # The bracketed term inside the parenthesis is not removed # after first calculating subtraction, multiplication and division RET = the re.search ( ' .??.? \ d + \ \ d * [* /] - \ d + \ \ d * ' , EX) # use regular expressions to find the first occurrence of multiplication and division of items IF RET: ex_son = ret.group () # sub-expression, only the most simple multiplication and division Print (ex_son) ....
....
RET = cal_exp_no_bracket (exp_no_bracket) function call # calculated brackets without parentheses
= new_exp.replace new_exp (exp_no_bracket, RET) # brackets not the calculated values in parenthesis in place of the last entry corresponding
Print (new_exp)
operation result:
80/2
Calculation of atomic expression, i.e., expression of the most simple form of atoms
DEF cal_ex_son (ex_son): IF ' / ' in ex_son: A, B, = ex_son.split ( ' / ' ) return STR (a float (A) / a float (B)) elif ' * ' in ex_son: A, B, ex_son.split = ( ' * ' ) return STR (a float (a) * a float (B)) ....
.... RET = cal_ex_son (ex_son) # call function calculated atomic expressions Print (RET) new_ex = ex.replace (ex_son, ret) # The final calculated value to replace the original brackets without brackets that an
Operating results should be:
-40.0
3+-40.0+24*5-(18/3+4*(90/15-100)-3)
- 3, matching and addition and subtraction calculation
ret = re.findall('-?\d+\.?\d*',ex) sum = 0 for i in ret: sum += float(i) return str(sum)
- 4, the sign of the processing
The sign was negative, negative makes
# Processing of the sign, positive or negative in negative, negative makes DEF dealwith (exp): exp = exp.replace ( ' + - ' , ' - ' ) exp = exp.replace ( ' - ' , ' + ' ) return exp ....
.... new_exp = dealwith (new_exp) Print (new_exp)
Operating results should be:
3-40.0+24*5-(18/3+4*(90/15-100)-3)
- 5, main program
And by calling the nested function, step by step calculation
# To calculate the expression exp = ' 3+ (-80/2) + 24 * 5- (18 is / +. 4. 3 * (90 / 15-100) -3) ' # to space new_exp = exp.replace ( ' ' , ' ' ) Print (new_exp) RES = remove_bracket (new_exp) Print (RES)
Third, the total module code calculator
2 and 3 and the above four points are calculated only the first matching entry brackets without parentheses, and the first term appears multiplication and division, and subtraction of a term appears, to count all such items , need to use cycle, to achieve, to find and calculate the main type brackets without parentheses items, as well as find and calculate simple addition and subtraction and multiplication and division equations need to repeatedly look for the calculation, the final finishing of the general framework of the calculator:
Import re # introduced re module, need a regular expression # processing of the sign, positive or negative in negative, negative makes DEF dealwith (exp): exp = exp.replace ( ' + - ' , ' - ' ) exp = exp.replace ( ' - ' , ' + ' ) return exp # calculated atoms in the form of an expression, multiplication and division of two numbers DEF cal_ex_son (ex_son): IF ' / ' in ex_son: a, B, = ex_son.split ( ' / ' ) return STR (a float (A) / a float (B)) elif ' * ' in ex_son: A, B, = ex_son.split ( ' * ' ) return STR (a float (A) * a float (B)) the else : Print ( ' All of multiplication and division calculation formulas are good ' ) # calculate the most simple brackets there is no formula in parentheses, first calculate multiplication and division, then addition and subtraction to calculate DEF cal_exp_no_bracket (ex): # ex is no parentheses parentheses term ex ex.strip = ( ' () ' ) # rear inside parentheses items without parentheses removal subtraction, multiplication and division to calculate # the while True: # The cyclic brackets no longer find items brackets RET = the re.search ( ' .? \ + D \ \ D * [* /] -?.? \ + D \ \ D * ' , EX) # regular expressions a first multiplication and division to find items appearing IF RET: # Description multiply and divide as well as expression ex_son ret.group = () # subexpression, only simple multiplication and division, atomically Print ( ' matches to only multiplication and division the term: ' , ex_son) RET = cal_ex_son (ex_son) # function call atoms calculation expression EX = ex.replace (ex_son, RET) # final calculated values without parentheses brackets replace the original one that EX = dealwith (EX) the else : # Description expressions no multiplication and division, and addition and subtraction calculation = the re.findall RET ( ' -?.? \ + D \ \ D * ' , EX) SUM = 0 for I in RET: SUM + = a float (I) return STR (SUM) # Expression screened brackets without parentheses type, and counted DEF no_bracket_in_bracket (new_exp): the while True: # the cyclic find all one level inside the items without parentheses RET = the re.search ( ' \ ([^ ()] + \) ' , new_exp) IF RET: exp_no_bracket = ret.group () # selected expressions without parentheses brackets Print ( ' matched to the inside there are no entries in parentheses: ' , exp_no_bracket) RET = cal_exp_no_bracket (exp_no_bracket) # call brackets calculated items without parentheses Print (RET) new_exp = new_exp.replace (exp_no_bracket, RET) # calculated brackets the final values without parentheses in place inside the entry corresponding new_exp = dealwith (new_exp) Print (new_exp) the else : Print ( ' expression without the parentheses: ' , new_exp) RET = cal_exp_no_bracket (new_exp) Print (RET) BREAK # Main program, to calculate the expression exp = ' 3+ (-80/2) + 24 * 5- (18 is / +. 4. 3 * (90 / 15-100) -3) ' # to space new_exp = exp. Replace ( ' ' , '' ) Print (new_exp) RES = no_bracket_in_bracket (new_exp) Print (RES)
operation result:
3 + (- 80/2) * 24 + 5 - (18/3 + 4 * (90 / 15-100) -3 ) matched to the inside there are no entries in parentheses: ( -80/2 ) matched to only multiplication and division item method: 80/2 -40.0 3-40.0 5- + 24 * (18/3 + 4 * (90 / 15-100) -3 ) matched to the inside there are no entries in parentheses: ( 90 / 15-100 ) matched to the item only multiplication and division: 90/15 -94.0 3-40.0 5- + 24 * (18/4 + 3 * -94.0-3 ) matched to the inside there are no entries in parentheses: ( 18/3 + 4 * -94.0-3 ) matched to the item only multiplication and division: 18/3 matching entry only to multiplication and division: 4 * -94.0 -373.0 3-40.0 * 24 + 5 + 373.0 expression without the parentheses: 3-40.0 * 5 + 24 + 373.0 entries that match only to multiplication and division: 24 * 5 456.0 None process Finished with Exit code 0