Requirements:
- Implementation of subtraction and extension priority resolution
- User Input 1-2 * ((60-30 + ( -40/5) * (9-2*5/3 + 7/3*99/4*2998 +10 * 568/14))-( -4*3)/(16-3*2)) and other similar formulas, you must parse the inside (), +,-, *,/character Number and formula (cannot call Eval and other similar functions lazy implementation), the result of the operation, the results must be consistent with the results of the actual calculator
1 ImportRe2 3 defMain ():4 5A ="'. Join (Input ('Please enter a calculation to calculate the formula'). Split ())6 7 whileTrue:8 if '(' inchA:9ct = Re.search (r'\(([^()]+)\)', a)Ten ifCt is notNone: Oneb =ct.groups () [0] Ac =count (b) -A = Re.sub (r'\(([^()]+)\)', str (c), a, 1) - Else: thec =Count (a) - Print(c) - Break - + defAdd_min (a): - " " + calculate add and Subtract A :p Aram: at : return: - " " - - if '--' inchA: -A = A.replace ('--','+') - inc = Re.findall (r'-?\d+\.? \d*', a) -ls = [] to forIinchC: + Ls.append (float (i)) -Rest =sum (LS) the returnRest * $ Panax Notoginseng defMul (a): - " " the calculate the number of left + :p Aram CT: A : return: the " " + - $b = Re.search (r'\d+\.? \d* (\*-?\d+\.? \d*) +', a) $ ifB is notNone: -b =B.group () -Rest = 1 thec = Re.findall (r'-?\d+\.? \d*', B) -ls =[]Wuyi forIteminchC: the ls.append (float (item)) - forI1inchrange (len (LS)): WuRest = Rest *Ls[i1] -A = Re.sub (r'\d+\.? \d* (\*-?\d+\.? \d*) +', str (rest), a, 1) About returna $ - - - defDiv (a): A " " + Calculate Departure the :p Aram A: - : return: $ " " the theb = Re.search (r'\d+\.? \d* (\/-?\d+\.? \d*) +', a) the ifB is notNone: theb =B.group () -c = Re.findall (r'-?\d+\.? \d*', B) inls =[] the forIinchC: the Ls.append (float (i)) AboutRest =Ls[0] the forI1inchRange (1, Len (LS)): theRest = rest/Ls[i1] theA = Re.sub (r'\d+\.? \d* (\/-?\d+\.? \d*) +', str (rest), a, 1) + returna - the Bayi defcount (b): the " " the Calculation Results - : return: - " " the whileTrue: the if '*' inchB: thec = B.split ('*') the if '/' inchC[0]: -b =Div (b) the Else: theb =Mul (b) the elif '/' inchB:94b =Div (b) the the elif '+' or '-' inchB: theb =Add_min (b)98 returnb About Else: - returnb101 102 103 104 theMain ()
Develop a simple Python calculator