Python-Regular Expressions implement the calculator function and python-Regular Expressions
Requirements:
The user enters an operation expression and the terminal displays the calculation result.
Code:
1 #! /Usr/bin/env/python3 2 #-*-coding: UTF-8-*-3 4 "the user inputs the computation expression, show the calculation result "5 6 _ author _ = 'jack' 7 8 import re 9 10 bracket = re. compile (R' \ ([^ ()] + \) ') # Find the inmost bracket Rule 11 mul = re. compile (R' (\ d + \.? \ D * \ *-\ d + \.? \ D *) | (\ d + \.? \ D * \ d + \.? \ D *) ') # search for the multiplication rule 12 div = re. compile (R' (\ d ++ \.? \ D */-\ d + \.? \ D *) | (\ d + \.? \ D */\ d + \.? \ D *) ') # search for Division Rule 13 add = re. compile (R '(-? \ D + \.? \ D * \ +-\ d + \.? \ D *) | (-? \ D + \.? \ D * \ + \ d + \.? \ D *) ') # search for addition calculation rules 14 sub = re. compile (R' (\ d ++ \.? \ D * -- \ d + \.? \ D *) | (\ d + \.? \ D *-\ d + \.? \ D *) ') # search for the subtraction Rule 15 c_f = re. compile (R '\(? \ +? -? \ D ++ \)? ') # Check whether the algorithm is completed in the brackets. Rule 16 strip = re. compile (R' [^ (]. * [^)] ') # parentheses Rule 17 18 19 def Mul (s): 20 "Multiplication operation in calculation expression" 21 exp = re. split (R' \ * ', mul. search (s ). group () 22 return s. replace (mul. search (s ). group (), str (float (exp [0]) * float (exp [1]) 23 24 25 def Div (s ): 26 "division operation in calculation expression" "27 exp = re. split (R'/', div. search (s ). group () 28 return s. replace (div. search (s ). group (), str (float (exp [0])/float (exp [1]) 29 30 31 def Add (s ): 32 "" addition operation in calculation expression "" 33 exp = re. split (R' \ + ', add. search (s ). group () 34 return s. replace (add. search (s ). group (), str (float (exp [0]) + float (exp [1]) 35 36 37 def Sub (s ): 38 "" subtraction operation in calculation expression "" 39 exp = re. split (R'-', sub. search (s ). group () 40 return s. replace (sub. search (s ). group (), str (float (exp [0])-float (exp [1]) 41 42 43 def calc (): 44 while True: 45 s = input ('Please input the expression (q for quit): ') # example: '1 + 2-(3*4-3/2 + (3-2*(3 + 5-3 *-0.2-3.3*2.2-8.5/2.4) + 10) + 10) '46 if s = 'q': 47 break48 else: 49 s = ''. join ([x for x in re. split ('\ s +', s)]) # split the expression by space and restructure 50 if not s. startswith ('): # if there are no parentheses at the beginning and end of the expression entered by the user, the format is: (expression) 51 s = str (' (% s) '% s) 52 while bracket. search (s): # If expression s contains brackets 53 s = s. replace ('--', '+') # Check the expression and replace the -- operation with the + operation 54 s_search = bracket. search (s ). group () # assign the inmost brackets and content to the variable s_search55 if div. search (s_search): # If the division operation exists (must be placed before multiplication) 56 s = s. replace (s_search, Div (s_search) # execute the division operation and replace the result with the original expression 57 elif mul. search (s_search): #58 s = s if the multiplication operation exists. replace (s_search, Mul (s_search) # execute the multiplication operation and replace the result with the original expression 59 elif sub. search (s_search): # If the subtraction operation exists (must be placed before addition) 60 s = s. replace (s_search, Sub (s_search) # execute the subtraction operation and replace the result with the original expression 61 elif add. search (s_search): # If the addition operation has 62 s = s. replace (s_search, Add (s_search) # execute the addition operation and replace the result with the original expression 63 elif c_f.search (s_search): # If no operation is performed in the brackets (similar to (-2.32) except) 64 s = s. replace (s_search, strip. search (s_search ). group () # Remove The parentheses, for example: (-2.32) --->-2.3265 66 print ('The answer is: %. 2f '% (float (s) 67 68 if _ name _ =' _ main _ ': 69 calc ()
Use regular expressions to implement the calculator function
Running effect: