自动生成小学四则运算题目——软件工程(改进)
源码已上传至Gitee,https://gitee.com/jiayouu/codes/ve3l7gf019shrwzxambni91
一、题目:
1) 能自动生成小学四则运算题目,并且不能出现负数;
2) 能支持真分数的四则运算;
二、功能设计:
实现四则运算题目和答案的生成。
三、实现思路:
1)生成随机数
2)生成随机算数运算符
3)生成整个表达式
4)确定年级
5)确定生成题目数量
6)用户输入答案
7)输出正确答案
四、改进
1)可以生成三个数字之间的四则运算
2)除不尽的数字保留小数点后两位
3)真分数化成最简简后以分数形式显示
4)可以选择年级
五、代码
1)两位数四则运算
import random
def FourFundamental_2():
lists = ['+', '-', '×', '÷'] #运算符
f=random.randint(0, 3)
f1=random.randint(1, 20)
f2=random.randint(1, 20)
if f== 0: #加法
result = f1 + f2
elif f == 1: #减法
f1, f2 = max(f1, f2), min(f1, f2) #比较大小,防止输出负数
result = f1 - f2
elif f== 2: #乘法
result = f1 * f2
elif f == 3: #除法
if f1>=f2:
if f1%f2 !=0:
result = round(f1/f2,2) #保留两位小数
else:
result = f1/f2
elif f1<f2:
h=hcf(f1,f2) #最大公约数
if h!=1:
result= str(int(f1/h))+'/'+str(int(f2/h))
else:
result = str(f1)+'/'+str(f2) #结果是最简分数
print(f1, lists[f], f2,end="")
return result
2)三位数四则运算
def FourFundamental_3():
lists = ['+', '-', '×', '÷']
x1=random.randint(0, 1)
x2=random.randint(2, 3)
f1=random.randint(1, 20)
f2=random.randint(1, 20)
f3=random.randint(1, 20)
if x1== 0: #加法
test ='('+ str(f1)+lists[0]+str(f2)+')' #插入括号
result = f1+f2
if x2==2 : #加法后叠加乘法
test ='('+ str(f1)+lists[0]+str(f2)+')'+lists[2]+str(f3)
result = (f1+f2)*f3
elif x2==3: #加法后叠加除法
test ='('+ str(f1)+lists[0]+str(f2)+')'+lists[3]+str(f3)
result = round((f1+f2)/f3,2) #保留两位小数
elif x1 == 1: #减法
f1, f2 = max(f1, f2), min(f1, f2) #比较大小,防止输出负数
test = '('+str(f1)+lists[1]+str(f2)+')'
result = f1 - f2
if x2==2 : #减法后叠加乘法
test ='('+ str(f1)+lists[1]+str(f2)+')'+lists[2]+str(f3)
result = (f1-f2)*f3
elif x2==3: #减法后叠加除法
test ='('+ str(f1)+lists[1]+str(f2)+')'+lists[3]+str(f3)
result = round((f1-f2)/f3,2) #保留两位小数
print(test,end="")
return result
3)生成最大公约数
def hcf(x, y):
"""该函数返回两个数的最大公约数"""
hcf=1
# 获取最小值
if x > y:
smaller = y
else:
smaller = x
for i in range(1,smaller + 1):
if((x % i == 0) and (y % i == 0)):
hcf = i
return hcf
4)运行
def init():
n=int(input("请输入年级:"))
n1=int(input("请输入需要生成的题目数量:"))
result=[0]*1000
answer=[0]*1000
if n==1 or n==2 or n==3:
for i in range(n1):
print("第",i+1,"题:")
result[i]=FourFundamental_2()
answer[i]=input("请输入你的答案:")
print("\033[1;35m正确答案是:\033[0m",result[i])
elif n==4 or n==5 or n==6:
for i in range(n1):
print("第",i+1,"题:")
result[i]=FourFundamental_3()
answer[i]=input("请输入你的答案:")
print("\033[1;35m正确答案是:\033[0m",result[i])
init()
六、运行结果


七、效能分析




七、PSP表格
| 预计耗时(分钟) | 实际耗时(分钟) | ||
| Planning | 计划 | 15 | 10 |
| Estimate | 估计这个任务需要多少时间 | 8 | 8 |
| Development | 开发 | 120 | 240 |
| Analysis | 需求分析 | 5 | 10 |
| Design Spec | 生成设计文档 | 10 | 10 |
| Design Review | 设计复审(和同事审核设计文档) | 10 | 10 |
| Coding Standerd | 代码规范(为目前的开发制定合适的规范) | 5 | 5 |
| Design | 具体设计 | 5 | 10 |
| Coding | 具体编码 | 30 | 60 |
| Code Review | 代码复审 | 5 | 10 |
| Text | 测试(自测,修改代码,提交修改) | 10 | 30 |
| Reporting | 报告 | 10 | 20 |
| Text Report | 测试报告 | 10 | 20 |
| Size Measurement | 计算工作量 | 5 | 5 |
| Postmortem & Process Improvement Plan | 事后总结,并提出过程改进计划 | 5 | 5 |
| Sum | 合计 | 245 | 432 |
浙公网安备 33010602011771号