Github項目地址
【小學生的四則運算】--PSP表格
psp | 任務計划 | 計划完成需要的時間(min) | 實際完成需要的時間 |
Planning | 計划 | 120 | 60 |
Estimate | 估計這個任務需要多少時間,並 規划大致工作時間 |
70 | 60 |
Development | 開發 | 1000 | 500 |
Analysis | 需要分析(包括學習新技術) | 100 | 30 |
Design Spec | 生成設計文檔 | 10 | 5 |
Design Review | 設計復審 | 20 | 30 |
Coding Standard | 代碼規范 | 20 | 20 |
Design | 具體設計 | 60 | 30 |
Coding | 具體編碼 | 300 | 400 |
Code Review | 代碼復審 | 100 | 30 |
Test | 測試(自我測試,修改代碼,提交代碼) | 10 | 20 |
Reporting | 報告 | 60 | 40 |
Test Report | 測試報告 | 60 | 120 |
Size Measurement | 計算工作量 | 10 | 10 |
Postmortem & Process Improvement Plan |
事后總結,並提出改進計划 | 60 | 30 |
合計 | 2000 | 1385 |
項目要求
1、能自動生成小學四則運算題目
2、題目中不能出現負數或者結果為負數
3、除了整數的運算還包括真分數的運算
附加要求
1、自動產生括號
2、隨機生成多個表達式
思路描述
這個題目可以被划分為以下三個問題:
- 列出隨機的四則運算表達式。
- 計算所列出四則運算的結果。
- 接受用戶輸入並比較結果是否正確,並在命令行中表現出來。
如何解決
問題1
- 隨機操作數、隨機運算符、隨機括號、隨機長度等隨機變量可以利用python自帶隨機函數取得。
- 需要考慮除數及分母為0時的情況,此時表達式不成立。
問題2
- python中有一個函數eval能計算表達式的結果,但結果可能會為分數,要利用庫fractons中的一個函數Fraction,把輸出的結果全轉成分數,所以整數的不變,小數的變分數
- 再使用一個result的列表存儲每一個表達式結果,再用for提取其中的每個結果
問題三
- 利用input()函數輸入答案,再用eval和Fraction轉成數字。
- 使用一個answer 的列表存儲每一個輸入的答案,再用for 提取其中的每個答案並與result中的結果一一對應,判斷其對錯
具體程序設計
公共變量
變量 | 類型 | 作用 |
question | list | 存儲表達式 |
answer | list | 存儲回答的答案 |
result | list | 存儲表達式的結果答案 |
f | str | 把列表中的值合成表達式 |
operation | list | 存儲運算符 |
函數
函數名 | 作用 |
integer(n) |
隨機產生幾個表達式 |
func_integer(number) |
表達式的隨機生成與是否需要括號 |
result_integer(f, m) |
根據表達式計算結果 |
代碼說明
#integer 函數

1 def integer(n): 2 ch = [] #存儲表達式 3 number = random.randint(1, 4) #隨機產生表達式的數量 4 for i in range(number): 5 rand = random.randint(0, 1) #隨機產生0和1 判斷是否使用括號 6 a = func_integer(number) #調用表達式產生函數,產生表達式 7 if rand == 0: 8 op = operation[random.randint(2,3)] #產生*,/來連接有括號的表達式,避免產生+,— 9 rand = random.randint(0, 1) #隨機產生0和1 判斷是否使用內嵌括號或外嵌括號 10 if i != number - 1: #避免開始和結尾用無意義的括號 11 if rand == 0: 12 ch.append('(') 13 ch.append(a) 14 ch.append(op) 15 ch.append(random.randint(1,10)) 16 ch.append(')') 17 ch.append(operation[random.randint(0, 3)]) 18 else: 19 ch.append(a) 20 ch.append(operation[random.randint(0, 3)]) 21 else: 22 ch.append(a) 23 ch.append(operation[random.randint(0, 3)]) 24 else: 25 ch.append(a) 26 ch.append(operation[random.randint(0, 3)]) 27 28 f = '' 29 for k,i in enumerate(ch): #把列表中的所有值用f一個個連起來 30 if k != len(ch)-1: 31 f += str(i) 32 result_integer(f, n) #調用輸出函數
#func_integer() 函數

1 def func_integer(number): 2 f = '' 3 ch = [] 4 rand = random.randint(0, 1) #選擇內嵌或外嵌括號 5 if number != 1: #避免一個表達式也產生括號 6 if rand == 0: 7 ch.append('(') 8 op = operation[random.randint(0, 1)] 9 ch.append(random.randint(1, 10)) 10 ch.append(op) 11 ch.append(random.randint(1, 10)) 12 ch.append(')') 13 else: 14 op = operation[random.randint(0, 3)] 15 if op == '/': 16 a = random.randint(1, 10) 17 ch.append(a) 18 ch.append(op) 19 ch.append(random.randint(a, 10)) 20 else: 21 ch.append(random.randint(1, 10)) 22 ch.append(op) 23 ch.append(random.randint(1, 10)) 24 else: 25 op = operation[random.randint(0, 3)] 26 if op == '/': 27 a = random.randint(1, 10) 28 ch.append(a) 29 ch.append(op) 30 ch.append(random.randint(a, 10)) 31 else: 32 ch.append(random.randint(1, 10)) 33 ch.append(op) 34 ch.append(random.randint(1, 10)) 35 for i in ch: #把產生表達式當成一個整體 36 f += str(i) 37 return f
#result_integer()函數

1 def result_integer(f, m): 2 try: 3 n = eval(f) 4 n = Fraction('{}'.format(n)).limit_denominator() # 把表達式的結果轉成分數 5 if n > 0: # 判斷結果是否大於0,否則重新產生表達式 6 #print('題目:') 7 question.append(f) 8 result.append(n) 9 #print('請輸出答案:') 10 #x = Fraction('{}'.format(eval(input()))).limit_denominator() 11 else: 12 integer() 13 except: 14 integer(m)
代碼運行如下
總結及收獲
本次項目雖然核心算法要求並不難,但是包括測試優化自身調整這些我始終不能夠很好理解。
所以寫這次博客我只能勉強的運行一下代碼以及測試,並不能很好的做完每個函數的性能測試及優化
這個項目過程也可謂是坎坷不斷。
因為我個人現在能力問題和一點點地個人時間問題,這項目代碼還有很多需要改進的地方,例如無效化括號等等,我都沒能很好的解決。
還有很多需要改進的地方,以后再做補充。
未完待續...