python own calculator

Title: 
exp = '. 1 - 2 * ((+ 60-30 (- 40/5) * (. 5 * 9-2 /. 7. 3 + / 99. 3 * / * 2998 +10 * 568/14. 4)) - ( -4 * 3) / (16-3 * 2)) '
   If that write their own calculator to calculate the result of more than str, be careful not to use eval function, ha ha.
The general idea: definition of three functions, 
  functions 1: Calculation + - demerit * / final regular match, is calculated for this function.
  Function 2: calculating the similarity 1-2 + 3 * 4/5, n the first matching * /, the matching + -, and then calls the function 1 operator, get out a result replace lost regular match, then out of function 2 recursive
  function 3: engage Finally, large exp, Syngenta is matched, to calculate the transfer function 2 in the parentheses, the results calculated in replace, and then recursively calls the function 3.

Use the method: recursion, regular

Codes are as follows: the final result of the calculation and eval more consistent results, no problem description
import re
def jianfa(exp):
'''此函数:用于减法计算'''
if exp.count("-")>2:
exp=exp.replace("--","+")
a,b=exp.split("+")
a,b=float(a),float(b)
return a+b
elif exp.count("-")>1:
if "--" in exp:
exp=exp.replace("--","+")
a,b=exp.split("+")
a,b=float(a),float(b)
return a+b
else:
exp=exp.strip("-")
a,b=exp.split("-")
a,b=float(a),float(b)
return 0-(a+b)
else:
exp=exp.strip("-")
a,b=exp.split("-")
A, B = a float (A), a float (B)
return ab &

DEF jisuanqi (exp):
'' 'function: means for calculating addition, subtraction' ''
IF "*" in exp:
A, B = exp.split ( "*")
A, B = a float (A), a float (B)
return A * B
elif "/" in exp:
A, B = exp.split ( "/")
A, B = a float (A), a float (B)
return A / B
elif "+" in exp:
A, B = exp.split ( "+")
A, B = a float (A), a float (B)
return A + B
elif "-" in exp :
return jianfa (exp)
DEF compute_replace (exp, RET):
'' 'function: for calling jisuanqi, and then replace the result obtained demerit regular match' ''
new_ret = jisuanqi (right)
new_ret = str (new_ret)
= exp.replace new_exp (RET, new_ret)
return new_exp
DEF FMT (exp):
'' 'function: Used to remove special symbols, to avoid the regular matching error' ''
the while "++" or in exp "- "in exp or" + - "in exp or" - + "in exp:
exp = exp.replace (" ++ "," + ")
exp = exp.replace (" - "," + ")
exp = exp.replace ( "+ -", "-")
exp = exp.replace ( "- +", "-")
return exp
DEF func_compute (exp):
'' 'function: means for calculating as 1 + 2 * / 5 such continuous algorithm, first matching * / matching + -, then call jisuanqi calculated and recursively '' '
exp = FMT (exp)
IF "*" exp or in "/" in exp:
RET = the re.search . ( '\ d + (\ \ d +) [* /] -?? \ d + (\.\ + D)? ', exp) .group ()
new_exp compute_replace = (exp, RET)
t_n_exp = func_compute (new_exp) # calculated result, the recursive call this function again
t_n_exp return
elif "+" in exp or "-" in exp:
IF exp.startswith ( "-") and exp.count ( "-") == 1 and exp.count ( "+") == 0: # consider if the final result is negative, take this judgment
return exp
RET = re.search ( '-? \ d + (\ \ d +) [+ -].?.? \ d + (\ \ d +)', exp) .group ( )
new_exp = compute_replace (exp, RET)
t_n_exp = func_compute (new_exp) # calculated result, again recursively call this function
return t_n_exp
the else:
return exp
DEF match (exp):
'' 'function: regular matching first dispensing formulas in brackets, then to call the function calculation above, and recursively '' '
exp = exp.replace ( "", "")
IF "(" in exp:
match_exp the re.search = ( "\ ([^ ()] + \) ", exp).group()
n_match_exp=match_exp.strip("(")
n_match_exp = n_match_exp.strip(")")
ret=func_compute(n_match_exp)
new_exp=exp.replace(match_exp,ret)
c_new_exp=match(new_exp)
return c_new_exp
else:
return func_compute(exp)
exp = '1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )'
ret=match(exp)
print(ret)

 

Guess you like

Origin www.cnblogs.com/ops-song/p/11330708.html