Python Learning Path-python Basic Course (DAY6)

Source: Internet
Author: User

Homework:

Develop a simple Python calculator

    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

Calculator Source:

#!/usr/bin/env python#_*_ coding:utf-8 _*_#Author:moImportRedefCompute_multipy_divide (ARG): Value=Arg[0] MCH= Re.search ('\d+\.*\d*[\*\/]+[\+\-]?\d+\.*\d*', value)if  notMCH:returncontent= Re.search ('\d+\.*\d*[\*\/]+[\+\-]?\d+\.*\d*', value). Group ()ifLen (Content.split ('*')) > 1: V1,v2= Content.split ('*') Get_value= Float (v1) *Float (v2)Else: V1,v2= Content.split ('/') Get_value= Float (v1)/Float (v2) left,right= Re.split ('\d+\.*\d*[\*\/]+[\+\-]?\d+\.*\d*', value,1) New_value='%s%s%s'%(left,get_value,right) arg[0]=new_value compute_multipy_divide (ARG)defCompute_add_substract (ARG): whileTrue:if '--' inchARG[0]or '++' inchARG[0]or '-+' inchARG[0]or '+-' incharg[0]: arg[0]= Arg[0].replace ('--','+') arg[0]= Arg[0].replace ('++','+') arg[0]= Arg[0].replace ('-+','-') arg[0]= Arg[0].replace ('+-','-')        Else:             Break    ifArg[0].startswith ('-'): arg[1] + = 1Arg[0]= Arg[0].replace ('-','&') arg[0]= Arg[0].replace ('+','-') arg[0]= Arg[0].replace ('&','+') arg[0]= Arg[0][1:] Value=Arg[0] MCH= Re.search ('\d+\.*\d*[\+\-]{1}\d+\.*\d*', value)if  notMCH:returncontent= Re.search ('\d+\.*\d*[\+\-]{1}\d+\.*\d*', value). Group ()ifLen (Content.split ('+')) >1: V1,v2= Content.split ('+') Get_value= Float (v1) +Float (v2)Else: V1,v2= Content.split ('-') Get_value= Float (v1)-Float (v2) left,right= Re.split ('\d+\.*\d*[\+\-]{1}\d+\.*\d*', str (value), 1) New_value='%s%s%s'%(left,get_value,right) arg[0]=new_value compute_add_substract (ARG)defCompute (expression): InP=[expression,0] Compute_multipy_divide (INP) compute_add_substract (INP) Count= Divmod (inp[1],2) Result=float (inp[0])ifCOUNT[1] = = 1: Result= result * (-1)    returnresultdefexcute (expression):if  notRe.search ('\ (([\+\-\*\/]*\d+\.*\d*) {2,}\)', expression): Result=compute (expression)returnresult Content= Re.search ('\ (([\+\-\*\/]*\d+\.*\d*) {2,}\)', expression). Group () New_content= Content[1:len (content)-1] New_list= Re.split ('\ (([\+\-\*\/]*\d+\.*\d*) {2,}\)', expression,1)    Print('before calculation:', expression) left=New_list[0] right= New_list[2] Final=Compute (new_content)Print('calculation:%s=%s'%(new_content, Final)) New_expression='%s%s%s'%(left,final,right)Print('after calculation:', New_expression)Print('end of last calculation'. Center (50,'*'))    returnExcute (new_expression)defrun (): whileTrue:Print('Oldboy Calc'. Center (50,'*')) Str_input= Input ('(quit=q) >>:'). Strip ()ifStr_input = ='Q': Exit ()ifLen (str_input) = = 0:Continue        ifLen (str_input) >0:no_space_str= Re.sub ('\s*',"', Str_input) Final=Excute (NO_SPACE_STR)Print(Final)if __name__=='__main__': Run ()
View Code

Calculation Result:

Oldboy Calc *******************(Quit=Q) >>:1-2 * ((60-30 + ( -40/5) * (9-2*5/3 + 7/3*99/4*2998 +10 * 568/14))-( -4*3)/(16-3*2) ) before calculation:1-2* ((60-30+ ( -40/5) * (9-2*5/3+7/3*99/4*2998+10*568/14))-( -4*3)/(16-3*2) ) Calculation:-40/5=-8.0after calculation:1-2* ((60-30+-8.0* (9-2*5/3+7/3*99/4*2998+10*568/14))-( -4*3)/(16-3*2))Last calculated end **********************before calculation:1-2* ((60-30+-8.0* (9-2*5/3+7/3*99/4*2998+10*568/14))-( -4*3)/(16-3*2) ) Calculation:9-2*5/3+7/3*99/4*2998+10*568/14=173545.88095238098after calculation:1-2* (60-30+-8.0*173545.88095238098)-( -4*3)/(16-3*2))Last calculated end **********************before calculation:1-2* (60-30+-8.0*173545.88095238098)-( -4*3)/(16-3*2) ) Calculation:60-30+-8.0*173545.88095238098=-1388337.0476190478after calculation:1-2* ( -1388337.0476190478-( -4*3)/(16-3*2))Last calculated end **********************before calculation:1-2* ( -1388337.0476190478-( -4*3)/(16-3*2) ) Calculation:-4*3=-12.0after calculation:1-2* ( -1388337.0476190478--12.0/(16-3*2))Last calculated end **********************before calculation:1-2* ( -1388337.0476190478--12.0/(16-3*2) ) Calculation:16-3*2=10.0after calculation:1-2* ( -1388337.0476190478--12.0/10.0)Last calculated end **********************before calculation:1-2* ( -1388337.0476190478--12.0/10.0) Calculation:-1388337.0476190478--12.0/10.0=-1388335.8476190479after calculation:1-2*-1388335.8476190479********************** Last calculated end **********************2776672.6952380957****************** Oldboy Calc *******************(Quit=Q) >>:q
View Code

Python Learning Path-python Basic Course (DAY6)

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.