Let's say python can simply handle +-
/, parentheses cannot be processed. Practice processing. Practice the regular.
Some of the internet does not handle the negative sign well, as follows. Temporary unhandled parentheses missing between numbersand other conditions.
( -1+ (2-5
( -1))(2-5))
-1+ (2-5) * (2-5)
#/usr/bin/env python3#mail [email protected]import re,syssymbos_map={' + ': '-', ' + + ': ' + ', '-+ ': '-', '--': ' + '}#-1 -( -1) *-1 =-2 #过程 -1--1*-1 = 1---1 = -1+-1 = -1-1 = -2# Find the most inner brackets, numbers are unsigned, remove parentheses # Find the most inner brackets, have expressions, first multiplication, multiplication start with numbers, 1 *-1 1*1# multiplication Complete, from left to right, with symbol matching. -1+-1 Not equal to-(1-1), requires processing-1-------1 case (due to multiplication) def calc_element_3 (V1,v2,symbol): Print ("Calculation:%s,%s,%s"% (V1,v2,symbol)) ' Signed +-*/' v1,v2=float (v1), Float (v2) if symbol== ' + ': return v1+v2 elif symbol = = '-': Return v1-v2 Eli F symbol = = ' * ': return v1*v2 elif symbol = = '/': return v1/v2 else:print (symbol); Sys.exit () def multi_divi (s): "s An expression in parentheses that is used to handle multiplication. Locate 1*-2, processing as-2, processing 1 "print (" Processing multiplication:%s "%s) Re_seach_obj=re.search ([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_ob J.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 ("Processing plus minus:%s"%s) ' -1--1,1--1, -1+1,-1---1,-1---------1, for processing the addition and subtraction from left to right, processing 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, +-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 ') defHandler_expression (expression): print ("Enter expression processing%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 FLA G:re_obj=re.search (' \ ([+\-*/0-9.] *\) ', s) if Re_obj:s_match_str=re_obj.group (0) print ("Bracket matching:%s"%s_match_str) if re . Match (' \ ([+\-]? ( [0-9.]*] \) ', s_match_str): Print ("Left only single value:%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 ("Call processing%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_expressi On (no_braces_result) # print (result) if __name__ = = ' __main__ ': While true:exp=input ("Input expression:") exp=re.sub (' \s ', ', exp) no_braces_result=hadler_braces (STR (exp)) result=handler_expression (No_braces_result) PR Int (Result)
Brush question--Python calculator exercises