The main thinking of the calculator is:
1, the input expression to remove its space
2. Determine if the parentheses in the expression entered by the user are valid
3. Each time the expression is found with only a pair of parentheses (that is, there is no parentheses inside the found expression)
4, to find only a pair of parentheses in the expression of the first multiplication and then add minus operation, the results of the calculation is replaced by the found expression
5 until there are no parentheses in the expression.
6. We have a function in the call above that computes the multiplication in the calculation of the addition and subtraction. Finally get the result.
The specific code is as follows:
#!/usr/bin/env python#-*-coding:utf-8-*-ImportRe#see if the parentheses of an expression are validdefcheck_bracket (expression): TMP= [] forIinchexpression:ifi = ='(': Tmp.append (i)elifi = =')': ifLen (TMP) >0:tmp.pop ()Else: returnFalseifLen (TMP) >0:returnFalseElse: returnTrue#remove extraneous symbols in an expressiondefclear_operator (expression): Expression= Expression.replace ('--','+') Expression= Expression.replace ('+-','-') returnexpression#Calculate multiplicationdefmultiply_divide (Exp_son):if '*' inchExp_son:left, right.= Exp_son.split ('*') returnSTR (float (left) *float (right))Else: Left, right= Exp_son.split ('/') returnSTR (float (left)/float (right))#4. An expression without parentheses computes the multiplication in a single element that splits only the formula in addition and subtraction into a list.defcal_no_bracket (expression):#first, the parentheses of the expression with only a pair of parentheses are removed #9-2*-5/3 + 7/3*99/4*2998 +10 * 568/14 #first multiplication In addition and subtraction whileTrue:ret= Re.search ('\d+\.? \d*[*/]-?\d+\.? \d*', expression)ifRet:exp_son=Ret.group () ret= Multiply_divide (Exp_son)#The multiplication that matches the formula into the function computes its value.expression =Expression.replace (Exp_son, ret) expression=clear_operator (expression)Else: #It's just a plus minus. We put all the values in this with a regular, except for the + all numbers to find out [ -3,34,3.2345] equivalent to + do the delimiter #We just need to add all the elements in it.ret = Re.finditer ('-?\d+\.? \d*', expression) sum=0 forIinchRet:sum+=Float (i.group ())returnstr (SUM)#3, first remove all the parentheses in the calculation of its resultsdefremove_bracket (expression): whileTrue:#Get rid of all the parentheses first.Express_a_bracket = Re.search ('\([^()]+\)', expression)ifExpress_a_bracket:#handle this expression with only a pair of parenthesesExpress_a_bracket =Express_a_bracket.group () ret= Cal_no_bracket (Express_a_bracket.strip ('()'))#To perform an operation on an expression in parenthesesexpression =Expression.replace (express_a_bracket, ret) expression=clear_operator (expression)Else: New_expression=cal_no_bracket (expression)returnnew_expressionif __name__=='__main__': #1, remove the user input expression to remove space #2, here we use the way of the stack to confirm that the user input bracket is legitimateExpression_input = input ('>>>:'). Replace (' ',"') ifCheck_bracket (expression_input):#if the parentheses of an expression are valid, then perform the followingRET =Remove_bracket (expression_input)Print(eval (expression_input))Print(ret)Else: Print('expression parentheses are not valid')
The Python learning path implements simple computer functions.