The function home Plus implements the simple calculator

Source: Internet
Author: User

This implementation mainly uses the knowledge of the regular matching knowledge and functions to complete a calculator to achieve a simple subtraction calculation

Import re# multiplication Operation def numl_mod (args): Resurt = Re.compile (' (\d+\.? \d*) ([*/]) (\-?\d+\.? \d*) ') #正则匹配, assigns the matched result to Resurt while Resurt.search (args): #while循环判断直到算出最终结果 new_num = Resurt.search (args) n        Um = New_num.group () #取出原值赋给num num1 = float (new_num.group (1)) num2 = New_num.group (2) #取出来的是 */number, judged at if num3 = Float (New_num.group (3)) if num2 = = ' * ': result = NUM1 * num3# return * results elif num2 = '/': result = num1/num3# Returns/results of args = Args.replace (num, str (result), 1) #将计算的结果替换返回 return args# p Rint (Numl_mod ("10*2/5")) # #加减运算def Poor_add (args): Resurt = Re.compile (' (\-?\d+\.? \d*) ([-+]) (\-?\d+\.? \d*) ' while Resurt.search (args): New_num = resurt.search (args) num = New_num.group () Num1 = float            (New_num.group (1)) num2 = New_num.group (2) num3 = Float (New_num.group (3)) if num2 = = '-': result = num1-num3 elif num2 = = ' + ': result = Num1 + num3        args = Args.replace (num, str (result), 1) Return args# print (Poor_add ("3-5+6")) #检查判断去括号 # origin = "1-2* ((60-30+ ( -9-2-5-2*3-5/3-40*4/2-3/5+6*3) * ( -9-2-2*5/3+7/3*99/4*2998+10*568/14))-( -4*3)/(16-3*25)) "# origin = input (" Enter Number >>>>> ") #检测式子的合法性def check (args): flag = true# The default identity bit is true if Re.findall (" [A-za-z] ", args): #检查式子的合法性, Cannot contain uppercase and lowercase letters print ("error") flag = False return flag# detection Operation def format (args): #检测式子, replace with space/++/+-/--, etc., to return a new formula Sub NEW_ARGS1 = Args.replace ("+ +", "+") New_args2 = New_args1.replace ("+-", "-") New_args3 = New_args2.replace ("--") , "+") NEW_ARGS4 = New_args3.replace ("-+", "-") NEW_ARGS5 = New_args4.replace ("", "") NEW_ARGS6 = New_args5.repl Ace ("*+", "*") New_args = New_args6.replace ("/+", "/") return New_argsprint ("--------------") print ("Welcome to Computer") print ("-        -------------") #主代码块 # Executes the statement block while True:choice = input (" Select q/q "to Exit" y/y ") if choice = =" Q "or choice =" Q ": Print ("Thank you for your use") Break ORigin = input ("Enter number >>>>>") if check (origin): New_origin = Format (origin) while re . Search ("\ (", New_origin): #匹配如果有括号, execute the following code New_new_origin = Re.search ("\ ([^ ()]+\)", New_origin). Group () #匹配出最里面的括号里 The expression # Print (New_new_origin) s = Numl_mod (new_new_origin) # print (s) S1 = Poor_ Add (s) s2 = s1[1:-1] New_origin = New_origin.replace (New_new_origin, S2, 1) # Print (new_o Rigin) # Print (S2, type (s2)) # break Else: #括号执行完后执行的代码块 Resurt = Numl_mod (new_orig IN) # Print (Resurt, "Resurt") # print (resurt) New_resurt = Poor_add (resurt) # A2 = a2[1:-1] Print (New_resurt) #返回的最终结果, which is the final calculation we're going to get

  

The function home Plus implements the simple 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.