PSP2流程下四则运算升级作业
1.题目要求:能自动生成小学四则运算题目,且每一道题目的运算结果不能为负。
2.软件功能:此次升级过程中主要对功能方面进行升级,例如增加可自由选择出题数量等。
3思路:
(1)四则运算加减乘除,采用两个随机数,由于不能出现负数,则对两个随机数进行比较大小再进行减法运算,除法一向特殊,所以在随机数的取值范围中设置不包括0。
(2)真分数运算在pycharm中导入fractions库,其余类似。
4实现过程:
设计三个函数:
def CreateInt() 生成整数四则运算
def CreateFra() 生成真分数四则运算
def CreateTest() 生成制定指定数量的四则运算题目
函数关系:
def CreateInt()与def CreateFra() 为独立的函数,负责生成随机四则运算,def CreateTest() 则随机调用上述两个函数生成题目。
PSP2表格:
5PSP表格:
预计耗时(分钟) | 是实际耗时(分钟) | ||
Planning | 计划 | 10 | 10 |
Estimate | 估计这个任务需要多少时间 | / | / |
Development | 开发 | 120 | 200 |
Analysis | 需求分析 | 5 | 10 |
Design Spec | 生成设计文档 | / | / |
Design Review | 设计复审(和同事审核设计文档) | / | / |
Coding Standerd | 代码规范(为目前的开发制定合适的规范) | / | / |
Design | 具体设计 | 5 | 10 |
Coding | 具体编码 | 30 | 60 |
Code Review | 代码复审 | 15 | 10 |
Text | 测试(自测,修改代码,提交修改) | 15 | 30 |
Reporting | 报告 | 10 | 20 |
Text Report | 测试报告 | 10 | 20 |
Size Measurement | 计算工作量 | / | / |
Postmortem & Process Improvement Plan | 事后总结,并提出过程改进计划 | / | / |
Sum | 合计 | 220 | 370 |
6代码说明:
import random; from fractions import Fraction; def CreateInt(): symbol = ['+', '-', '×', '÷'] fh = random.randint(0, 3) n1 = random.randint(1, 20) n2 = random.randint(1, 20) result = 0 if fh == 0: result = n1 + n2 elif fh == 1: n1, n2 = max(n1, n2), min(n1, n2) result = n1 - n2 elif fh == 2: result = n1 * n2 elif fh == 3: n1, n2 = max(n1, n2), min(n1, n2) while n1 % n2 != 0: n1 = random.randint(1, 10) n2 = random.randint(1, 10) n1, n2 = max(n1, n2), min(n1, n2) result = int(n1 / n2) print(n1, symbol[fh], n2, '= ', end='') return result def CreateFra(): symbol = ['+', '-', '×', '÷'] fh = random.randint(0, 3) t1 = random.randint(1, 10) t2 = random.randint(t1, 10) n1 = Fraction(t1, t2) t1 = random.randint(1, 10) t2 = random.randint(t1, 10) n2 = Fraction(t1, t2) result = 0 if fh == 0: result = n1 + n2 elif fh == 1: n1, n2 = max(n1, n2), min(n1, n2) result = n1 - n2 elif fh == 2: result = n1 * n2 elif fh == 3: n1, n2 = max(n1, n2), min(n1, n2) result = n1 / n2 print(n1, symbol[fh], n2, '= ', end='') return result def CreateTest(): symbol = ['+', '-', '×', '÷'] print('输入题库所需要的题目数量') n=int(input()) result=[] m=0 while m<=(n-1): fh = random.randint(0, 4) if fh==0: print(m+1,end='、') result.append(CreateFra()) print(' ') else: print(m+1,end='、') result.append(CreateInt()) print(' ') m=m+1 m=0 print('答案:') while m<=(n-1): print(m+1,'、',result[m]) m=m+1 print('1、提供四则运算式子') print('2、选择一定数量题目') n=int(input()) if n==1: print('input "01" to Quit') while True: fh = random.randint(0, 4) if fh == 0: result = CreateFra() jg = input() if jg == '01': break; sr = Fraction(jg) if sr == result: print('right') else: print('error. the Tight answer is', result) else: result = CreateInt() jg = input() if jg == '01': break; sr = int(jg) if sr == result: print('right') else: print('error. the Tight answer is', result) if n==2: CreateTest()
7结果说明