Python learning path-calculator

Source: Internet
Author: User

Python learning path-calculator
1. knowledge points applied 1. python Regular Expression application, re. search ('pattern', str ). group () 2. recursive use in functions 3. python functions, basic syntax, control statement if... else..., for Loop statement use 4, string formatting, String concatenation 5, list use 6. while True: Use of the statement endless loop 2. code

#! /Usr/bin/env python #-*-coding: UTF-8-*-import re # process multiplication and division def compute_mul_div (arg ): # Here we need to input 1 arg list value = arg [0] # print value # mch = re. search ('\ d + \. * \ d * [\ * \/] + [\ + \-]? \ D + \. * \ d *] ', value) # perform multiplication and division matching on the string. For example, if 1 + 2*3-3, the string matches: 2*3 mch = re. search ('\ d + \. * \ d * [\ * \/] + [\ + \-]? \ D + \. * \ d * ', value) # if not mch: return if not matched # Save the matched content in the content = re. search ('\ d + \. * \ d * [\ * \/] + [\ + \-]? \ D + \. * \ d * ', value ). group () # print content # perform * And/on the matched content, and then calculate the corresponding content, such as 2*3. Split the content first and then calculate if len (content. split ('*')> 1: n1, n2 = content. split ('*') get_value = float (n1) * float (n2) else: n1, n2 = content. split ('/') get_value = float (n1)/float (n2) # retrieve the content of the two sides of the matching content: before, after before, after = re. split ('\ d + \. * \ d * [\ * \/] + [\ + \-]? \ D + \. * \ d * ', value, 1) # concatenate the new string new_str = "% s" % (before, get_value, after) # assign new_str to arg [0] arg [0] = new_str # recursively calculate compute_mul_div (arg) # compute_mul_div (["1 + 2*3-4 ", 0]) # print compute_mul_div ([]) # process the addition and subtraction def compute_add_sub (arg): # arg = ["3 + 4-2--4 ++ 2 ", 0] # process the passed arg [0] Expression 1st times, convert the ++ \ -- in the expression to +, +-,-+ -, after the processing is complete, break while True: if arg [0]. _ contains _ ('--') or arg [0]. _ contains _ ('+-') or arg [0]. _ _ Contains _ ('--') or arg [0]. _ contains _ ('-+'): arg [0] = arg [0]. replace ('--', '+') arg [0] = arg [0]. replace ('+-', '-') arg [0] = arg [0]. replace ('-+', '-') arg [0] = arg [0]. replace ('--', '+') else: break # Then, it processes the passed arg [0] Expression 2nd times and extracts the first part as "-". and save the extracted times in arg [1] # And not extracted once: Replace "+" in the expression "-". "-" Replace with "+", and then assign the 1st to the last 1 bits in the arg [0] Expression string to arg [0] if arg [0]. startswith ('-'): arg [1] + = 1 arg [0] = arg [0]. replace ('-', '&') arg [0] = arg [0]. replace ('+', '-') arg [0] = arg [0]. replace ('&', '+') arg [0] = arg [0] [1:] value = arg [0] # Matches string values, match the content on both sides, such as 1 + 2-3, match 1 + 2 mch = re. search ('\ d + \. * \ d * [\ + \-] {1} \ d + \. * \ d * ', value) # if it does not match, it will be directly sent back if not mch: return # Save the matched content in the content, such as 1 + 2 content = re. search ('\ d + \. * \ d * [\ + \-] {1} \ d + \. * \ d * ', value ). group () # Calculate the Matching content: first, Judge +,-, and then split, and then calculate if len (content. split ('+')> 1: n1, n2 = content. split ('+ ') Get_value = float (n1) + float (n2) else: n1, n2 = content. split ('-') get_value = float (n1)-float (n2) # extract the content at both ends of the matching content and encapsulate it in before, after before, after = re. split ('\ d + \. * \ d * [\ + \-] {1} \ d + \. * \ d * ', str (value), 1) # before = re. split ('\ d + \. * \ d * [\ + \-] {1} \ d + \. * \ d * ', value, 1) [0] # after = re. split ('\ d + \. * \ d * [\ + \-] {1} \ d + \. * \ d * ', value, 1) [1] # splice the calculated: before + Result + after, new_str = "% s" % (before, get_value, after) # concatenate the string Assign a value to arg [0], and recursively calculate arg [0] = new_str compute_add_sub (arg) # compute_add_sub (["1 + 3-2", 0]) # Calculated expression # "def compute (expr): # first encapsulate the expression in the list. The first element of the list indicates the expression to be processed, the second element indicates that the first digit in the expression is the-number. Then, we extract the number of times) # perform the addition and subtraction compute_add_sub (indium) # determine whether the indium [1] is an odd or even number. If it is an odd number, the result is negative. Otherwise, the result is a positive count = divmod (indium [1], 2) result = float (indium [0]) if count [1] = 1: result = result * (-1) return result # Execute Extract the expression def excute (expr): # match the innermost brackets, such as: 1 + 2 * (3/(3-2) * 2 ), the match here is (3-2) # if no parentheses exist, the expression if not re is directly returned. search ('\ ([\ + \-\ * \/] * \ d + \. * \ d *) {2,} \) ', expr): return expr # use a regular expression to get the content in the brackets and remove the square brackets, get a new expression: content = re. search ('\ ([\ + \-\ * \/] * \ d + \. * \ d *) {2,} \) ', expr ). group () # only take the content between 1st and the second to the last in the string, that is, remove the brackets on both sides. For example, "(2 + 3)" only takes 2 + 3, and does not take the brackets new_content = content [1: len (content) on both sides) -1] # split expr by matching content: Get -- before, match content, after, get content on both sides of content, and assign value to before, after new_list = re. split ('\ ([\ + \-\ * \/] * \ d + \. * \ d *) {2,} \) ', expr) before = new_list [0] after = new_list [2] # before, nothing, after = re. split ('\ ([\ + \-\ * \/] * \ d + \. * \ d *) {2,} \) ', expr) # Calculate new_content and obtain result = compute (new_content) # splice before + result + after, new_expr = "% s" % (before, result, after) # returns the result. recursively execute excute (new_expr) return excute (new_expr) # main function if _ name _ = "_ main _": # Remove the spaces contained in the calculated expression, get the expression str1 = "4*3-10 * (3*2-1*9/3) + 10" no_space_str = re. sub ('\ s *', '', str1) # execute the brackets processing function to retrieve the inner expression ret = excute (no_space_str) for Priority Calculation or processing # indium = [ret, 0] # Calculate the obtained inner expression, including processing the multiplication, division, and addition and subtraction (final = compute (ret) # print the final calculated value print final #"""

 

Related Article

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.