Python Regular Expressions implement the calculator function, python Regular Expressions

Source: Internet
Author: User
Tags mul

Python Regular Expressions implement the calculator function, python Regular Expressions

Requirements:

The user enters an operation expression and the terminal displays the calculation result.

Code:

#! /Usr/bin/env/python3 #-*-coding: UTF-8-*-"the user inputs the computation expression, show the calculation result "" _ author _ = 'jack' import rebracket = re. compile (R' \ ([^ ()] + \) ') # Find the inmost bracket rule mul = re. compile (R' (\ d + \.? \ D * \ *-\ d + \.? \ D *) | (\ d + \.? \ D * \ d + \.? \ D *) ') # Find the multiplication rule div = re. compile (R' (\ d ++ \.? \ D */-\ d + \.? \ D *) | (\ d + \.? \ D */\ d + \.? \ D *) ') # search for the Division rule add = re. compile (R '(-? \ D + \.? \ D * \ +-\ d + \.? \ D *) | (-? \ D + \.? \ D * \ + \ d + \.? \ D *) ') # search for addition operation rules sub = re. compile (R' (\ d ++ \.? \ D * -- \ d + \.? \ D *) | (\ d + \.? \ D *-\ d + \.? \ D *) ') # search for the subtraction rule c_f = re. compile (R '\(? \ +? -? \ D ++ \)? ') # Check whether the rule strip = re. compile (R' [^ (]. * [^)] ') # detachment rule def Mul (s): "Multiplication operation in calculation expression" exp = re. split (R' \ * ', mul. search (s ). group () return s. replace (mul. search (s ). group (), str (float (exp [0]) * float (exp [1]) def Div (s ): "division operation in calculation expression" "exp = re. split (R'/', div. search (s ). group () return s. replace (div. search (s ). group (), str (float (exp [0])/float (exp [1]) def Add (s ): "addition operation in calculation expression" "exp = re. split (R' \ + ', add. search (s ). group () return s. replace (add. search (s ). group (), str (float (exp [0]) + float (exp [1]) def Sub (s ): "subtraction operation in calculation expression" "exp = re. split (R'-', sub. search (s ). group () return s. replace (sub. search (s ). group (), str (float (exp [0])-float (exp [1]) def calc (): while True: 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) 'if s = 'q': break else: s = ''. join ([x for x in re. split ('\ s +', s)]) # split the expression by space and restructure 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) s = str (' (% s) '% s) while bracket. search (s): # If expression s contains brackets s = s. replace ('--', '+') # Check the expression and replace the -- operation with the + operation s_search = bracket. search (s ). group () # assign the inner brackets and content to the variable s_search if div. search (s_search): # If the division operation exists (must be placed before multiplication) s = s. replace (s_search, Div (s_search) # execute the division operation and replace the result with the original expression elif mul. search (s_search): # If multiplication exists, s = s. replace (s_search, Mul (s_search) # execute the multiplication operation and replace the result with the original expression elif sub. search (s_search): # If the subtraction operation exists (must be placed before addition) s = s. replace (s_search, Sub (s_search) # execute the subtraction operation and replace the result with the original expression elif add. search (s_search): # If the addition operation has s = s. replace (s_search, Add (s_search) # execute the addition operation and replace the result with the original expression elif c_f.search (s_search): # If no operation is performed in the brackets (similar to (-2.32) s = s. replace (s_search, strip. search (s_search ). group () # Remove The parentheses, for example: (-2.32) --->-2.32 print ('The answer is: %. 2f '% (float (s) if _ name _ =' _ main _ ': calc ()

Running effect:

The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.