Develop a simple Python calculator

Source: Internet
Author: User
Tags mul

Requirements:

    1. Implementation of subtraction and extension priority resolution
    2. User Input 1-2 * ((60-30 + ( -40/5) * (9-2*5/3 + 7/3*99/4*2998 +10 * 568/14))-( -4*3)/(16-3*2)) and other similar formulas, you must parse the inside (), +,-, *,/character Number and formula (cannot call Eval and other similar functions lazy implementation), the result of the operation, the results must be consistent with the results of the actual calculator

1 ImportRe2 3 defMain ():4 5A ="'. Join (Input ('Please enter a calculation to calculate the formula'). Split ())6 7      whileTrue:8         if '(' inchA:9ct = Re.search (r'\(([^()]+)\)', a)Ten             ifCt is  notNone: Oneb =ct.groups () [0] Ac =count (b) -A = Re.sub (r'\(([^()]+)\)', str (c), a, 1) -         Else: thec =Count (a) -             Print(c) -              Break -  + defAdd_min (a): -     " " + calculate add and Subtract A :p Aram: at : return: -     " " -  -     if '--' inchA: -A = A.replace ('--','+') -  inc = Re.findall (r'-?\d+\.? \d*', a) -ls = [] to      forIinchC: + Ls.append (float (i)) -Rest =sum (LS) the     returnRest *  $ Panax Notoginseng defMul (a): -     " " the calculate the number of left + :p Aram CT: A : return: the     " " +  -  $b = Re.search (r'\d+\.? \d* (\*-?\d+\.? \d*) +', a) $     ifB is  notNone: -b =B.group () -Rest = 1 thec = Re.findall (r'-?\d+\.? \d*', B) -ls =[]Wuyi          forIteminchC: the ls.append (float (item)) -          forI1inchrange (len (LS)): WuRest = Rest *Ls[i1] -A = Re.sub (r'\d+\.? \d* (\*-?\d+\.? \d*) +', str (rest), a, 1) About         returna $  -  -  - defDiv (a): A     " " + Calculate Departure the :p Aram A: - : return: $     " " the  theb = Re.search (r'\d+\.? \d* (\/-?\d+\.? \d*) +', a) the     ifB is  notNone: theb =B.group () -c = Re.findall (r'-?\d+\.? \d*', B) inls =[] the          forIinchC: the Ls.append (float (i)) AboutRest =Ls[0] the          forI1inchRange (1, Len (LS)): theRest = rest/Ls[i1] theA = Re.sub (r'\d+\.? \d* (\/-?\d+\.? \d*) +', str (rest), a, 1) +         returna -  the Bayi defcount (b): the     " " the Calculation Results - : return: -     " " the      whileTrue: the         if '*' inchB: thec = B.split ('*') the             if '/' inchC[0]: -b =Div (b) the             Else: theb =Mul (b) the         elif '/' inchB:94b =Div (b) the  the         elif '+' or '-' inchB: theb =Add_min (b)98             returnb About         Else: -             returnb101 102 103 104  theMain ()

Develop a simple Python calculator

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.