python鑷繁鍋氳绠楀櫒,,鏍囩锛?a hre


鏍囩锛?a href='http://www.byrx.net/so/1/replace' title='replace'>replace

棰樼洰锛?br />   exp = 鈥? - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )鈥?/pre>
   濡傞鑷繁鍐欒绠楀櫒锛岀畻鍑轰互涓妔tr涓殑缁撴灉锛屾敞鎰忎笉鑳界敤eval鍑芥暟锛屽搱鍝堛€?/pre>
澶ц嚧鎬濊矾锛氬畾涔?涓嚱鏁帮紝
銆€銆€鍑芥暟1锛氳绠?-*/锛屾渶缁堟鍒欏尮閰嶇殑璁拌繃锛岄兘瑕佷娇鐢ㄦ鍑芥暟璁$畻銆?br />銆€銆€鍑芥暟2锛氳绠楃被浼?-2+3*4/5锛屾鍒欏厛鍖归厤*/锛屽湪鍖归厤+-锛岀劧鍚庤皟鐢ㄥ嚱鏁?绠楋紝寰楀嚭鏉ョ粨鏋渞eplace鎺夋鍒欏尮閰嶏紝鐒跺悗鎺夊嚱鏁?閫掑綊锛?br />銆€銆€鍑芥暟3锛氭悶鏈€鍚庣殑澶х殑exp锛屽厛姝e垯鍖归厤鍑烘嫭鍙峰唴鐨勶紝鍘昏皟鍑芥暟2璁$畻锛岃绠楃殑缁撴灉鍦╮eplace锛岀劧鍚庤皟鐢ㄥ嚱鏁?閫掑綊銆?br />
浣跨敤鍒扮殑鏂规硶锛氶€掑綊銆佹鍒?/em>

瀹炵幇浠g爜濡備笅锛氭渶缁堣绠楃殑缁撴灉鍜宔val缁撴灉姣旇緝涓€鑷达紝璇存槑娌¢棶棰?br />
import re
def jianfa(exp):
鈥樷€樷€樻鍑芥暟锛氱敤浜庡噺娉曡绠椻€樷€樷€?br /> 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=float(a),float(b)
return a-b

def jisuanqi(exp):
鈥樷€樷€樻鍑芥暟锛氱敤浜庡姞鍑忎箻闄よ绠椻€樷€樷€?br /> if "*" in exp:
a,b=exp.split("*")
a,b=float(a),float(b)
return a*b
elif "/" in exp:
a,b=exp.split("/")
a,b=float(a),float(b)
return a/b
elif "+" in exp:
a,b=exp.split("+")
a,b=float(a),float(b)
return a+b
elif "-" in exp:
return jianfa(exp)
def compute_replace(exp,ret):
鈥樷€樷€樻鍑芥暟锛氱敤浜庤皟鐢╦isuanqi锛岀劧鍚庡緱鍑虹殑璁拌繃鐒跺悗鏇挎崲姝e垯鍖归厤鐨勭粨鏋溾€樷€樷€?br /> new_ret = jisuanqi(ret)
new_ret = str(new_ret)
new_exp = exp.replace(ret, new_ret)
return new_exp
def fmt(exp):
鈥樷€樷€樻鍑芥暟锛氱敤浜庡幓闄や互涓嬬壒娈婄殑绗﹀彿锛岄伩鍏嶆鍒欏尮閰嶅嚭閿欌€樷€樷€?br /> while "++" in exp or "--" 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):
鈥樷€樷€樻鍑芥暟锛氱敤浜庤绠楀1+2*/5杩欐牱鐨勮繛缁畻娉曪紝鍏堝尮閰?/鍦ㄥ尮閰?-,鐒跺悗璋冪敤jisuanqi璁$畻锛屽苟閫掑綊鈥樷€樷€?br /> exp=fmt(exp)
if "*" in exp or "/" in exp:
ret = re.search(鈥榎d+(\.\d+)?[*/]-?\d+(\.\d+)?鈥?exp).group()
new_exp=compute_replace(exp,ret)
t_n_exp = func_compute(new_exp) #璁$畻鍑虹殑缁撴灉锛屽啀鍘婚€掑綊璋冪敤鏈嚱鏁?br /> return t_n_exp
elif "+" in exp or "-" in exp:
if exp.startswith("-") and exp.count("-") ==1 and exp.count("+")==0: #鑰冭檻濡傛灉鏈€鍚庣粨鏋滀负璐熸暟锛岃蛋杩欎釜鍒ゆ柇
return exp
ret = re.search(鈥??\d+(\.\d+)?[+-]\d+(\.\d+)?鈥?exp).group()
new_exp = compute_replace(exp, ret)
t_n_exp = func_compute(new_exp) #璁$畻鍑虹殑缁撴灉锛屽啀鍘婚€掑綊璋冪敤鏈嚱鏁?br /> return t_n_exp
else:
return exp
def match(exp):
鈥樷€樷€樻鍑芥暟锛氭鍒欏尮鍏堥厤鍑烘嫭鍙峰唴鐨勫叕寮忥紝鐒跺悗鍘昏皟鐢ㄤ笂闈㈠嚱鏁拌绠楋紝骞堕€掑綊鈥樷€樷€?br /> exp=exp.replace(" ","")
if "(" in exp:
match_exp=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 = 鈥? - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )鈥?br />ret=match(exp)
print(ret)

python鑷繁鍋氳绠楀櫒

鏍囩锛?a href='http://www.byrx.net/so/1/replace' title='replace'>replace

鍘熸枃鍦板潃锛歨ttps://www.cnblogs.com/ops-song/p/11330708.html

评论关闭