四則運算


Github地址:https://github.com/sjzsjzsjz/jisuan

PSP表格:

 

 

預測時間(分鍾)

nning

計划

2

Estimate

估計這個任務需要多少時間

120

Development

開發

10

Analysis

需求分析

10

Design Spec

生成設計文檔

3

Design Review

設計復審(和同事審核設計文檔)

2

Coding Standerd

代碼規范(為目前的開發制定合適的規范)

1

Design

具體設計

5

Coding

具體編碼

60

Code Review

代碼復審

10

Text

測試(自測,修改代碼,提交修改)

5

Reporting

報告

10

Text Report

測試報告

10

Size Measurement

計算工作量

5

Postmortem & Process Improvement Plan

事后總結,並提出過程改進計划

10

 

解題思路描述:

(1) 思考如何隨機生成四則運算表達式

(2) 查閱相關博客,了解效能分析所用工具

(3) 學習所用到的類庫,如random庫

 

設計實現過程:

這個四則運算表達式由三個函數實現,分別為create1()、create2()、calcul(),前兩個函數負責生成表達式,最后一個表達式負責計算結果。

 

代碼說明:

def create1(t):
    s1=random.randint(1,10)
    s2=random.randint(1,10)
    s3=random.choice(['+','-','*','/'])
    calcul(s1,s2,s3,t)

def create2(t):
    s1=random.randint(1,10)
    s2=random.randint(s1,11)
    s3=random.randint(1,10)
    s4=random.randint(s3,11)
    s5=random.choice(['+','-'])
    sr="第"+str(t)+"題:"+str(s1)+'÷'+str(s2)+s5+str(s3)+'÷'+str(s4)+'='
    l1.append(sr)
    if s5=='+':
        l2.append(s1/s2+s3/s4)
    else:
        if s1/s2-s3/s4>0:
            l2.append(s1/s2-s3/s4)
        else:
            sr="第"+str(t)+"題:"+str(s3)+'÷'+str(s4)+s5+str(s1)+'÷'+str(s2)+'='
            l2.append(s3/s4-s1/s2)
def calcul(s1,s2,s3,t):
    sr="第"+str(t)+"題:"+str(s1)+s3+str(s2)+'='
    if s3=='+':
        l2.append(s1+s2)
    elif s3=='-':
        if s1>=s2:
            l2.append(s1-s2)
        else:
            sr="第"+str(t)+"題:"+str(s2)+s3+str(s1)+'='
            l2.append(s2-s1)
    elif s3=='*':
        sr="第"+str(t)+"題:"+str(s1)+'x'+str(s2)+'='
        l2.append(s1*s2)
    elif s3=='/':
        sr="第"+str(t)+"題:"+str(s1)+'÷'+str(s2)+'='
        l2.append(s1/s2)
    l1.append(sr)

 

測試運行:

 

性能分析:

這個程序通過使用cProfile和time模塊來測試程序的性能

 

 

PSP表格:

 

 

實際時間(分鍾)

nning

計划

10

Estimate

估計這個任務需要多少時間

180

Development

開發

10

Analysis

需求分析

10

Design Spec

生成設計文檔

3

Design Review

設計復審(和同事審核設計文檔)

2

Coding Standerd

代碼規范(為目前的開發制定合適的規范)

1

Design

具體設計

5

Coding

具體編碼

90

Code Review

代碼復審

20

Text

測試(自測,修改代碼,提交修改)

5

Reporting

報告

30

Text Report

測試報告

10

Size Measurement

計算工作量

5

Postmortem & Process Improvement Plan

事后總結,並提出過程改進計划

10


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM