代碼github位址:https://github.com/yiduobaozhi/-1
PSP表格:
預測時間(分鐘) | ||
planning | 計劃 | 15 |
Estimate | 估計這個任務需要多少時間 | 100 |
Development | 開發 | |
Analysis | 需求分析 | 10 |
Design Spec | 生成設計文檔 | |
Design Review | 設計複審(和同僚稽核設計文檔) | |
Coding Standerd | 代碼規範(為目前的開發制定合适的規範) | 5 |
Design | 具體設計 | 3 |
Coding | 具體編碼 | 120 |
Code Review | 代碼複審 | |
Text | 測試(自測,修改代碼,送出修改) | |
Reporting | 報告 | 7 |
Text Report | 測試報告 | |
Size Measurement | 計算工作量 | |
Postmortem & Process Improvement Plan | 事後總結,并提出過程改進計劃 | 12 |
解題思路:
(1)尋找一種可以用于真分數的函數
(2)随機生成幾個數或者幾個分子分母
(3)設計代碼,實作加減乘除基本功能
(4)查詢效能分析相關資料,嘗試優化代碼
設計過程:
分開為整數部分計算和分數部分計算,分數調用Fraction函數進行計算,使用random随機生成數字,用于計算四則計算
具體代碼:
from fractions import Fraction #導入分數函數
import numpy as np
import cProfile
def size():
print("你需要算的是分數還是整數?")
a=input()
if a=="分數":
x = np.random.randint(1,100)
y = np.random.randint(1,100)
e = input("加減乘除?")
b = np.random.randint(1,100)
c = np.random.randint(1,100)
print(x,y,b,c)
if e=="+":
print("答案為:%s+%s=%s"%(Fraction(x,y),Fraction(b,c),Fraction(x, y) + Fraction(b, c)))
elif e=="-":
print("答案為:%s-%s=%s" % (Fraction(x, y), Fraction(b, c), Fraction(x, y) - Fraction(b, c)))
elif e=="*":
print("答案為:%s*%s=%s" % (Fraction(x, y), Fraction(b, c), Fraction(x, y) * Fraction(b, c)))
elif e=="/":
print("答案為:%s/%s=%s" % (Fraction(x, y), Fraction(b, c), Fraction(x, y) / Fraction(b, c)))
elif a=="整數":
x1=np.random.randint(1,100)
y1=np.random.randint(1,100)
print(x1,y1)
e1=input("加減乘除?")
if e1 == "+":
print("答案為:%s+%s=%s" %(x1, y1, x1+y1))
elif e1== "-":
print("答案為:%s-%s=%s" %(x1, y1, x1-y1))
elif e1 == "*":
print("答案為:%s*%s=%s" %(x1, y1, x1 * y1))
elif e1=="/":
print("答案為:%s/%s=%s" %(x1, y1, x1 / y1))
cProfile.run(size)
測試運作:
效能分析:
實際時間(分鐘) | ||
nning | 40 | |
9 | ||
1 | ||
21 | ||
60 | ||
20 | ||
2 | ||