軟體工程第三周作業 —— 四則運算
1. 專案要求
1.1 要求闡述
- 生成小學四則運算題題目,結果不能為負數
- 支持真分數的四則運算
1.2 詳細要求 【易知大學】
1.3 詳細代碼 【GitHub】
2. PSP表格
|
PSP2.1 |
Personal Software Process Stages |
預估耗時(分鐘) |
實際耗時(分鐘) |
|
Planning |
計劃 |
15 |
25 |
|
Estimate |
估計這個任務需要多少時間 |
15 |
25 |
|
Development |
開發 |
247 |
395 |
|
Analysis |
需求分析 (包括學習新技術) |
30 |
45 |
|
Design Spec |
生成設計檔案 |
30 |
30 |
|
Design Review |
設計復審 (和同事審核設計檔案) |
15 |
10 |
|
Coding Standard |
代碼規范 (為目前的開發制定合適的規范) |
12 |
10 |
|
Design |
具體設計 |
30 |
35 |
|
Coding |
具體編碼 |
60 |
180 |
|
Code Review |
代碼復審 |
30 |
20 |
|
Test |
測驗(自我測驗,修改代碼,提交修改) |
40 |
65 |
|
Reporting |
報告 |
85 |
60 |
|
Test Report |
測驗報告 |
30 |
25 |
|
Size Measurement |
計算作業量 |
30 |
10 |
|
Postmortem & Process Improvement Plan |
事后總結, 并提出程序改進計劃 |
25 |
25 |
|
合計 |
|
347 |
480 |
3. 解題思路描述
將問題分解為兩部分:第一部分生成算式,第二部分計算算式的值,
3.1 第一部分
生成算式分為三個小步驟,
首先,使用亂數生成運算元與運算子,通過引數設定運算元與運算子的數量,再將其拼接為算式,如3×7+9÷3,
其次,在算式上插入括號,括號插在有乘除附近的加減子算式中,如3×(7+9)÷3,
最后,在算式的基礎上將其中的數字替換為分數,如果不想使用分數則跳過此步驟,
3.2 第二部分
計算算式的值,將算式轉為逆波蘭式,之后使用堆疊計算算式結果,結果保留分數形式,如7/2,
4. 設計實作程序
4.1 引數說明
創建OPT方法存盤相關引數,如運算元數值上限、運算元個數、使用的運算子種類、是否包含分數,
4.2 生成算式
使用GeneralFormular類生成算式的中綴運算式,其中包含8個方法,
|
|
方法 |
說明 |
|
1 |
def catFormula(self, operand1, operator, operand2) |
連接算式 |
|
2 |
def getRandomIntOperand(self) |
回傳隨機整數運算元 |
|
3 |
def getRandomFractionOperand(self) |
回傳隨機分數運算元 |
|
4 |
def getRandomOperator(self) |
回傳隨機運算子 |
|
5 |
def getOriginFormular(self) |
生成整數源算式 |
|
6 |
def insertBracket(self, formular) |
插入括號 |
|
7 |
def replaceFraction(self, formular) |
帶入分數 |
|
8 |
def solve(self) |
整合生成算式的后綴運算式,帶括號 |


4.3 計算算式
使用ComputeFormular類計算算式,通過將中綴運算式轉為后綴運算式,使用堆疊計算結果,
|
|
方法 |
說明 |
|
1 |
def getPostFormular(self, formular) |
中綴運算式轉為后綴運算式 |
|
2 |
def calcFormular(self, formular) |
計算算式的值 |
|
3 |
def solve(self, formular) |
整合計算中綴運算式的值 |
5. 代碼示例
5.1 GeneralFormular.getOriginFormular()方法
def getOriginFormular(self): ''' * 生成整數源算式 * @return {str} ''' # 通過self.opt.oper_num控制運算元個數,回圈呼叫catFormula()方法構造算式 tmp = self.getRandomIntOperand() for i in range(self.opt.oper_num-1): tmp = self.catFormula(tmp, self.getRandomOperator(), self.getRandomIntOperand()) # 去掉'÷0' while(True): if '÷0' in tmp: tmp = tmp.replace('÷0', '÷'+str(self.getRandomIntOperand())) else: break return tmp
5.2 GeneralFormular.insertBracket()方法
def insertBracket(self, formular): ''' * 插入括號 * @param formular {str} 源算式 * @return {str} ''' # print(formular) # 若只包含+號 或 只有兩個運算元 則不用加括號 if self.opt.oper_variety <= 2 or self.opt.oper_num == 2: return formular # 若不包含×÷ 則不用加括號 if '×' not in formular and '÷' not in formular: return formular # 運算元串列 operand_list = re.split("[-|+|×|÷]", formular) # 運算子串列 operator_list = re.split("[!0-9]", formular) # 去掉空字符 while '' in operator_list: operator_list.remove('') # print(operand_list, operator_list) # 存盤添加括號的算式 new_formular = "" # flag表示是否已存在左括號,作用在于構造出一對括號 flag = 0 # 添加括號 for i in range(len(operator_list)): oper = operator_list.pop(0) # 若下一個符號為 + or - , 則插入括號 if oper == '-' or oper == '+': if flag == 0: new_formular += "(" flag = 1 new_formular += (str(operand_list.pop(0)) + str(oper)) else: new_formular += str(operand_list.pop(0)) if flag == 1: new_formular += ")" flag = 0 new_formular += str(oper) # print(operand_list, operator_list, new_formular) new_formular += str(operand_list.pop(0)) if flag == 1: new_formular += ")" return new_formular
6. 測驗運行
6.1 不包含分數

6.2 包含分數

7. 性能分析
7.1 GeneralFormular.getOriginFraction()方法

7.2 GeneralFormular.replaceFraction()方法

7.3 GeneralFormular.solve()方法

7.4 ComputeFormular.solve()方法

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/92611.html
標籤:Python
