#/usr/bin/env python3 #mail infaaf@126.com import re,sys symbos_map={'+-':'-','++':'+','-+':'-','--':'+'} # -1-(-1)*-1 =-2 #过程 -1--1*-1 => -1-- -1 => -1+-1 => -1-1 => -2 # 找最内括号,数字后面无符号,去除括号 # 找最内括号,有表示式,先乘除,乘除 从数字开始匹配, 1*-1 1*1 # 乘除完成,从左到右,带符号匹配。 -1+-1 不等于 -(1-1) ,需要处理-1-------1 情况(由于乘除时未处理符号) def calc_element_3(v1,v2,symbol): print("计算: %s,%s,%s"%(v1,v2,symbol)) '''带符号 + - * / ''' v1,v2=float(v1),float(v2) if symbol=='+':return v1+v2 elif symbol =='-':return v1-v2 elif symbol == '*':return v1*v2 elif symbol =='/':return v1/v2 else:print(symbol);sys.exit() def multi_divi(s): ''' s括号内表达式,用于处理乘除。找到1*-2,处理为-2 ,处理1次 ''' print("处理乘除: %s"%s) re_seach_obj=re.search(r'([0-9.]+)([*/])([+-])?([0-9.]+)',s) if re_seach_obj is not None: s_match_str = re_seach_obj.group(0) # 1*-1 value1=re_seach_obj.group(1) value2=re_seach_obj.group(4) simblos=re_seach_obj.group(2) simblo_ext=re_seach_obj.group(3) ret=calc_element_3(value1,value2,simblos) ret=simblo_ext+str(ret) print(s_match_str,ret) s=s.replace(s_match_str,ret) return s # res=multi_divi('-1-2*-2') # print(res) def add_minu(s): print("处理加减: %s"%s) ''' -1--1,1--1,-1+1,-1---1,-1---------1,用于从左往右处理加减,处理1次''' if re.search(r'[*/]', s): print("should do * / before + -: %s"%s) sys.exit() while re.search(r'[+-*\]{2,}',s): #-1-1 ,1+++++1 => -1-1 , 1+1 for symbos_key in symbos_map: s=s.replace(symbos_key,symbos_map[symbos_key]) # print(s) re_seach_obj = re.search(r'([+-]?[0-9.]+)([+-])([0-9.]+)', s) if re_seach_obj: s_match_str = re_seach_obj.group(0) # 1*-1 value1=re_seach_obj.group(1) value2=re_seach_obj.group(3) simblos=re_seach_obj.group(2) ret=calc_element_3(value1,value2,simblos) # print(s_match_str,ret) s=s.replace(s_match_str,str(ret)) # print(s) return s # res=add_minu('1.0+1.5++++1') def handler_expression(expression): print("进入表达式处理%s"%expression) while re.search('[*/]',expression): expression=multi_divi(expression) while re.search('[0-9.]+[+-]+[0-9.]+',expression): expression=add_minu(expression) return expression # res=handler_expression('-1+---5*-2/-1++2+2+2') # print(res) # a=handler_expression('1+2--5.0*-3.0') # print(a) def hadler_braces(s): print(s) flag=True while flag: re_obj=re.search('([+-*/0-9.]*)',s) if re_obj: s_match_str=re_obj.group(0) print("括号匹配: %s"%s_match_str) if re.match('([+-]?([0-9.]*))',s_match_str): print("仅剩余单个值: %s"%s_match_str) s_match_str_match=re.match('(([+-]?[0-9.]*))',s_match_str).group(1) s = s.replace(s_match_str, s_match_str_match) print(s) else: print("调用处理%s"%s_match_str) s_match_str_str=re.search('(([+-*/0-9.]*))',s).group(1) ret=handler_expression(s_match_str_str) s = s.replace(s_match_str, str(ret)) print(s) else: flag=False return s # no_braces_result=hadler_braces('(-1+(2-5*(-1))*(2-5))') # result=handler_expression(no_braces_result) # print(result) if __name__ == '__main__': while True: exp=input("输入表达式: ") exp=re.sub('s','',exp) no_braces_result=hadler_braces(str(exp)) result=handler_expression(no_braces_result) print(result)
假设python只能简单处理+-/,不能处理括号。练习处理。练习正则。
网上有些无法很好处理负号,如下情况。
暂未加入括号间无*号情况
(-1+(2-5(-1))*(2-5))
-1+(2-5)*(2-5)