发布时间:2023-02-26 09:30
题目要求:
(1)更新已有程序
解题思路描述:
(1)更新主界面的说明
设计实现过程:
追加说明
代码说明:
首先说明整数的四则运算,生成两个随机数并随机运算,在减法中比较大小防止出现负数,在除法中比较大小并循环取整除的随机数组合。最后输出算式并返回正确答案。
1 defnewint():
2 opr = [\'+\', \'-\', \'×\', \'÷\']
3 fh = random.randint(0, 3)
4 n1 = random.randint(1, 20)
5 n2 = random.randint(1, 20) 6 rjg =0 7 if fh ==0: 8 rjg = n1 +n2 9 elif fh == 1: 10 n1, n2 =max(n1, n2), min(n1, n2) 11 rjg = n1 -n2 12 elif fh == 2: 13 rjg = n1 *n2 14 elif fh == 3: 15 n1, n2 =max(n1, n2), min(n1, n2) 16 while n1 % n2 !=0: 17 n1 = random.randint(1, 10) 18 n2 = random.randint(1, 10) 19 n1, n2 =max(n1, n2), min(n1, n2) 20 rjg = int(n1 /n2) 21 print(n1, opr[fh], n2, \'= \', end=\'\') 22 returnrjg 23 \'\'\'jg = input() 24 sr = int(jg) 25 if int(sr) == rjg: 26 print(\'right\') 27 else: 28 print(\'error. the Tight answer is\', rjg)\'\'\'
真分数四则运算类似。
1 defnewfra():
2 opr = [\'+\', \'-\', \'×\', \'÷\']
3 fh = random.randint(0, 3)
4 t1 = random.randint(1, 10)
5 t2 = random.randint(t1, 10) 6 n1 =Fraction(t1, t2) 7 t1 = random.randint(1, 10) 8 t2 = random.randint(t1, 10) 9 n2 =Fraction(t1, t2) 10 rjg =0 11 if fh ==0: 12 rjg = n1 +n2 13 elif fh == 1: 14 n1, n2 =max(n1, n2), min(n1, n2) 15 rjg = n1 -n2 16 elif fh == 2: 17 rjg = n1 *n2 18 elif fh == 3: 19 n1, n2 =max(n1, n2), min(n1, n2) 20 rjg = n1 /n2 21 print(n1, opr[fh], n2, \'= \', end=\'\') 22 returnrjg 23 \'\'\'jg = input() 24 sr = Fraction(jg) 25 if sr == rjg: 26 print(\'right\') 27 else: 28 print(\'error. the Tight answer is\', rjg)\'\'\'
newtest()函数是要求用户输入一个整数来输出算式的数量,采用while循环随机生成整数或者真分数运算,将答案保存在rjg列表的同时输出算式直到算式数量达到要求。最后输出rjg列表即输出答案。
1 defnewtest():
2 opr = [\'+\', \'-\', \'×\', \'÷\']
3 print(\'输入题库所需要的题目数量\')
4 n=int(input())
5 rjg=[] 6 m=0 7 while m<=(n-1): 8 fh = random.randint(0, 4) 9 if fh==0: 10 print(m+1,end=\'、\') 11 rjg.append(newfra()) 12 print(\' \') 13 else: 14 print(m+1,end=\'、\') 15 rjg.append(newint()) 16 print(\' \') 17 m=m+1 18 m=0 19 print(\'答案:\') 20 while m<=(n-1): 21 print(m+1,\'、\',rjg[m]) 22 m=m+1
下列为主函数,第一个模式负责调用上述newint()、new函数,并获得函数返回值即算式答案,与用户输入值进行比较。第二个模式则是生成算式题目
print(\'欢迎使用四则运算生成器\')print(\'1、四则运算\')print(\'2、制作题库\')
n=int(input())if n==1:print(\'input \"0000\" to Quit\')whileTrue:
fh= random.randint(0, 4)if fh ==0:
rjg=newfra()
jg=input()if jg == \'0000\':break;
sr=Fraction(jg)if sr ==rjg:print(\'right\')else:print(\'error. the Tight answer is\', rjg)else:
rjg=newint()
jg=input()if jg == \'0000\':break;
sr=int(jg)if sr ==rjg:print(\'right\')else:print(\'error. the Tight answer is\', rjg)if n==2:
newtest()
测试运行:
查看主界面的变化,如下图:
改进程序性能过程:
(1)追加标题
PSP表格:
预计耗时(分钟)
是实际耗时(分钟)
Planning
计划
5
15
Estimate
估计这个任务需要多少时间
5
10
Development
开发
100
150
Analysis
需求分析
2
15
Design Spec
生成设计文档
20
30
Design Review
设计复审(和同事审核设计文档)
10
20
Coding Standerd
代码规范(为目前的开发制定合适的规范)
30
50
Design
具体设计
10
30
Coding
具体编码
40
100
Code Review
代码复审
3
15
Text
测试(自测,修改代码,提交修改)
15
40
Reporting
报告
20
40
Text Report
测试报告
5
15
Size Measurement
计算工作量
10
15
Postmortem & Process Improvement Plan
事后总结,并提出过程改进计划
10
20
Sum
合计
280
565