Regular Expression practice-calculator and regular expression practice

Source: Internet
Author: User
Tags mul

Regular Expression practice-calculator and regular expression practice

#-*-Coding: UTF-8 -*-
Import re
# This article draws on others' compile usage. I think the code is concise and clear.

Bk = re. compile (R' \ ([^ ()] + \) ') # Find the inmost bracket rule
Mul = re. compile (R' (\ d + \.? \ D * \ *-\ d + \.? \ D *) | (\ d + \.? \ D * \ d + \.? \ D *) ') # search for multiplication rules
Div = re. compile (R' (\ d + \.? \ D */-\ d + \.? \ D *) | (\ d + \.? \ D */\ d + \.? \ D *) ') # search for Division calculation rules
Add = re. compile (R '(-? \ D + \.? \ D * \ +-\ d + \.? \ D *) | (-? \ D + \.? \ D * \ + \ d + \.? \ D *) ') # search for addition calculation rules
Subt = re. compile (R '(-? \ D + \.? \ D * \-{2} \ d + \.? \ D *) | (-? \ D + \.? \ D * \-\ d + \.? \ D *) ') # search for subtraction rules
Remove = re. compile (R' [^ (]. * [^)] ') # parentheses

Def cal_mul (s ):
''' Calculate the multiplication in the expression '''
Sp = re. split (R' \ * ', mul. search (s). group ())
Result = str (float (sp [0]) * float (sp [1])
Return result

Def cal_div (s ):
'''Division in the calculation expression '''
Sp = re. split (R' \/', div. search (s). group ())
Result = str (float (sp [0])/float (sp [1])
Return result

Def cal_add (s ):
'''Addition in the calculation expression '''
Sp = re. split (R' \ + ', add. search (s). group ())
Result = re. sub (add, str (float (sp [0]) + float (sp [1]), s, count = 1)
Return result

Def cal_subt (s ):
'''Calculate the subtraction in the expression '''
Sp = subt. search (s). group ()
If sp. startswith ('-'): # if the expression starts with--1-1
S1 = re. sub (R' \-',' + ', sp) # Replace-with ++ 1 + 1
S2 = cal_add (s1) # Call addition + 2
S3 = re. sub (R' \ ++ ','-', s2) # Replace the result with--2.
Result = re. sub (subt, s3, s, count = 1)
Else:
S1 = re. split (R' \-', sp)
Result = re. sub (subt, str (float (s1 [0])-float (s1 [1]), s, count = 1)
Return result

Def main ():
While True:
S = input ("Enter the calculation formula (q exit) >>>> ")
If s = "q ":
Exit ()
Else:
S = "". join ([I for I in re. split ('\ s +', s)]) # Remove spaces in the input expression
If not s. startswith (') or not s. endswith ('): # judge whether the input is in parentheses.
S = "(% s)" % s #1 + 2 --> (1 + 2)
While bk. search (s): # judge whether there are inner brackets in s
S = re. sub (R' \-{2} ',' + ', s) # If the expression contains --> + example: -- 2 --> + 2
S1 = bk. search (s). group () # Find the most memory parentheses
If div. search (s1): # judge whether Division exists in s1.
S2 = div. search (s1). group () # obtain the Division expression
S3 = s1.replace (s2, cal_div (s2) # string replacement
If re. search (R' \ (\ +? \-? \ D + \.? \ D * \) ', s3): # determine whether s3 is a value in parentheses, for example: (3)
S3 = remove. search (s3). group () # remove the brackets (3) --> 3
S = s. replace (s1, s3) # replace s1 in s with s3
Elif mul. search (s1): # determine whether the expression contains multiplication.
S2 = mul. search (s1). group ()
S3 = s1.replace (s2, cal_mul (s2 ))
If re. search (R' \ (\ +? \-? \ D + \.? \ D * \) ', s3 ):
S3 = remove. search (s3). group ()
S = s. replace (s1, s3)
Elif subt. search (s1): # judge whether there is a subtraction in the expression
S2 = subt. search (s1). group ()
S3 = s1.replace (s2, cal_subt (s2 ))
If re. search (R' \ (\ +? \-? \ D + \.? \ D * \) ', s3 ):
S3 = remove. search (s3). group ()
S = s. replace (s1, s3)
Elif add. search (s1): # judge whether there is a method in the expression.
S2 = add. search (s1). group ()
S3 = s1.replace (s2, cal_add (s2 ))
If re. search (R' \ (\ +? \-? \ D + \.? \ D * \) ', s3 ):
S3 = remove. search (s3). group ()
S = s. replace (s1, s3)
Print ("Calculation Result: % s" % s)

If _ name _ = '_ main __':
Main ()

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.