11#! /usr/bin/env/python322#-*-coding:utf-8-*-3344"""user input calculation expression, display calculation result"""5566__author__='Jack'7788ImportRe99TenTen bracket = re.compile (r'\([^()]+\)')#find the most inner parentheses rule OneOne mul = Re.compile (r'(\d+\.? \d*\*-\d+\.? \d*) | (\d+\.? \d*\*\d+\.? \d*)')#searching for multiplication rules Adiv = Re.compile (r'(\d+\.? \d*/-\d+\.? \d*) | (\d+\.? \d*/\d+\.? \d*)')#Look for the division operation rules -Add = Re.compile (r'(-?\d+\.? \d*\+-\d+\.? \d*) | (-?\d+\.? \d*\+\d+\.? \d*)')#Search for addition operation rules -A sub = Re.compile (r'(\d+\.? \d*--\d+\.? \d*) | (\d+\.? \d*-\d+\.? \d*)')#Search for subtraction operations rules theC_f = Re.compile (r'\ (? \+?-? \d+\)?')#Check if the rule is completed in parentheses -Strip = Re.compile (r'[^(].*[^)]')#parentheses Rule -17 -18 +19defMul (s): -20"""to calculate the multiplication in an expression""" +Exp = Re.split (r'\*', Mul.search (s). Group ()) A22returnS.replace (Mul.search (s). Group (), str (float (exp[0)) * Float (exp[1]))) at23 -24 -25defDiv (s): -26"""Calculating Division operations in an expression""" -+ exp = Re.split (r'/', Div.search (s). Group ()) -28returnS.replace (Div.search (s). Group (), str (float (exp[0))/float (exp[1]))) in29 -30 to31defAdd (s): +32"""Calculating the addition operation in an expression""" -+ exp = Re.split (r'\+', Add.search (s). Group ()) the34returnS.replace (Add.search (s). Group (), str (float (exp[0)) + float (exp[1]))) *35 $36Panax Notoginseng37defSub (s): -38"""to calculate the subtraction in an expression""" theRe.split exp = (r'-', Sub.search (s). Group ()) +40returnS.replace (Sub.search (s). Group (), str (float (exp[0))-Float (exp[1]))) A41 the42 +43defCalc (): -44 whileTrue: $s = input ('Please input the expression (Q for Quit):')#Example: ' 1+2-(4-3/2+ (3-2* (5-3* -0.2-3.3*2.2-8.5/2.4) +10) +10) ' $46ifs = ='Q': -47 Break -48Else: thes ="'. join ([x forXinchRe.split ('\s+', s)])#split expression by space and reorganize -50if notS.startswith ('('):#If the user enters an expression with no parentheses, it is uniformly formatted as: (expression)Wuyis = str ('(%s)'%s) the52 whileBracket.search (s):#If the expression s has parentheses -+ S = s.replace ('--','+')#Check the expression and replace the--operation with the + operation WuS_search = Bracket.search (s). Group ()#assigns the inner parentheses and their contents to the variable S_search -55ifDiv.search (S_search):#If the division operation exists (must be placed before multiplication) AboutS.replace s = (S_search, Div (S_search))#performs a division operation and replaces the result with the original expression $57elifMul.search (S_search):#if the multiplication operation exists -+-S = S.replace (S_search, Mul (S_search))#performs a multiplication and replaces the result with the original expression -59elifSub.search (S_search):#if the subtraction operation exists (must be placed before addition) -s = S.replace (S_search, Sub (S_search))#performs a subtraction operation and replaces the result with the original expression A61elifAdd.search (S_search):#If the addition operation exists ++ S = s.replace (S_search, Add (S_search))#performs an addition operation and replaces the result with the original expression the63elifC_f.search (S_search):#If there is no operation in parentheses (except (-2.32)) -+ S = s.replace (S_search, Strip.search (s_search). Group ())#take off the brackets, for example: ( -2.32)---> -2.32 $65 the66Print('The answer is:%.2f'%(float (s))) the67 the68if __name__=='__main__': theCalc ()
Python Regular Implementation Calculator