Python job calculator, python job

Source: Internet
Author: User

Python job calculator, python job

Ideas:

Extract the innermost brackets in a loop before performing operations

Use regular expressions to find corresponding operators during operation

Multiplication and division before addition and subtraction

(Refer to the code of Wu sir and Jin Jiao Wang)

Flowchart:

1 #! /Usr/bin/env python 2 #-*-coding: UTF-8-*-3 import re 4 def cheng_and_chu (arg ): # Calculate and multiply 5 val = arg [0] # assign the 0th elements in the input list to val 6 mch = re. search ('\ d + \. * \ d * [\ * \/] + [\ + \-]? \ D + \. * \ d * ', val) # use the regular expression's string search function to match the arithmetic character in val and assign it to mch 7 if not mch: # If mch does not multiply by 8 return # The ending function returns the value 9 content = re in mch. search ('\ d + \. * \ d * [\ * \/] + [\ + \-]? \ D + \. * \ d * ', val ). group () # use the regular expression to search for strings to match the Arithmetic Operators in val, and use the group to get the group and assign the value to content 10 if len (content. split ('*')> 1: # divide the group in the content by *. the string length after the split is greater than 1 11 n1, n2 = content. split ('*') # divide the group in the content by *. The split two parts are assigned to n1, n2 12 value = float (n1) * float (n2) respectively) # convert n1 and n2 to floating point numbers and multiply them. The result is assigned to value 13 else: 14 n1, n2 = content. split ('/') 15 value = float (n1)/float (n2) 16 qianzhui, houzhui = re. split ('\ d + \. * \ d * [\ * \/] + [\ + \-]? \ D + \. * \ d * ', val, 1) # Use the division method in the regular expression. The value in val is \ d ++ \. * \ d * [\ * \/] + [\ + \-]? \ D + \. * \ d *, and assign the split values to qianzhi with the suffix 17 new_str = "% s" % (qianzhui, value, houzhui) # reformat and splice the string. 18 arg [0] = new_str # copy the new string to 19 cheng_and_chu (arg), The 0th element in arg) # continue to run the multiplication and division function 20 21 def jia_and_jian (arg): # Calculate addition and subtraction 22 while True: 23 if arg [0]. _ contains _ ('+-') or arg [0]. _ contains _ ("++") or arg [0]. _ contains _ ('-+') or arg [0]. _ contains _ ("--"): # If the first element of arg is '+-', '--', '+ + ', '-+' 24 arg [0] = arg [0]. replace ('+-', '-') # replace-with +-25 arg [0] = arg [0]. replace ('++', '+') # replace ++ 26 arg [0] = arg [0]. replace ('-+', '-') # replace-+ 27 arg [0] = arg [0]. replace ('--', '+') # replace + with -- 28 else: 29 break 30 if arg [0]. startswith ('-'): # If the first element of arg starts with-31 arg [1] + = 1 # The first element of arg is automatically added with 1 32 arg [0] = arg [0]. replace ('-', '&') 33 arg [0] = arg [0]. replace ('+', '-') 34 arg [0] = arg [0]. replace ('&', '+') # change-to +, + to-35 arg [0] = arg [0] [1:] # Remove the symbol 36 val = arg [0] 37 mch = re from the first 0th elements in arg. search ('\ d + \. * \ d * [\ + \-] {1} \ d + \. * \ d * ', val) 38 if not mch: 39 return 40 content = re. search ('\ d + \. * \ d * [\ + \-] {1} \ d + \. * \ d * ', val ). group () 41 if len (content. split ('+')> 1: 42 n1, n2 = content. split ('+') 43 value = float (n1) + float (n2) 44 else: 45 n1, n2 = content. split ('-') 46 value = float (n1)-float (n2) 47 before, after = re. split ('\ d + \. * \ d * [\ + \-] {1} \ d + \. * \ d * ', val, 1) 48 new_str = "% s" % (before, value, after) 49 arg [0] = new_str 50 jia_and_jian (arg) 51 52 def jisuan (sr): # Calculate function 53 new_sr = [sr. strip ('()'), 0] # remove the brackets on both sides of the passed value and assign a list value to new_sr 54 cheng_and_chu (new_sr) # Call the multiplication function 55 jia_and_jian (new_sr) # Call the addition/subtraction function 56 if divmod (new_sr [1], 2) [1] = 1: # Place the 1st elements in the returned list in 2, if the 1st elements of the obtained tuples are equal to 1, 57 jieguo = float (new_sr [0]) # convert the 0th elements in the list returned after the operation to the floating point type, assign jieguo 58 jieguo = jieguo *-1 # And multiply jieguo by-1 59 else: # if it is not equal to 1 60 jieguo = float (new_sr [0]) # convert the 0th elements in the list returned after the operation to the floating point type and assign the value to jieguo 61 return jieguo # The end function returns jieguo 62 63 def kuohao (sr ): # Remove the bracket function 64 flag = True # Set a flag 65 while flag: 66 m = re. search ("\ ([^ ()] + \)", sr) # locate the brackets in the sr and assign them to m 67 if m: 68 sub_sr = m. group () # intercept the first brace in m. 69 sub_res = jisuan (sub_sr) # Call the calculation function and assign the return value to sub_res 70 sr = sr. replace (m. group (), str (sub_res) # Replace the first bracket content intercepted by m with the sub_res 71 else: 72 print ('result :', jisuan (sr) # If no parentheses exist, print the result 73 flag = False # Set the flag to False, used to jump out of the loop 74 75 if _ name _ = "_ main _": 76 print ("---------- welcome to the calculator ----------") 77 while True: 78 flag = True # Set a flag 79 sr = input ("Enter the function to be calculated (q: Exit):") # Let the user enter the computing function, assigned to sr 80 sr = re. sub ("\ s *", "", sr) # locate all spaces in the sr and delete 81 error = re. search ('[0-9q \ * \/\-\ +]', sr) # use the regular search method to find the characters 0-9q \ * \/\-\ + in sr, assign error 82 while flag: 83 if not error: # if error is null 84 print ("incorrect input ") # print input error 85 sr = input ("Please re-enter the function to be calculated (q: Exit):") # re-enter 86 else: 87 if error. group () = 'q': # If the user inputs q 88 exit ("Thank you for using, goodbye") # The whole program 89 kuohao (sr) will be launched) # If the user inputs a computing function, call the 90 flag removal bracket function = False # And set the flag to False.Calculator

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.