对上篇“小学生四则运算”的改进

本篇博客是基于上一篇博客并对其进行改进。

Github源码地址:https://github.com/SG-Zhang-X/Dokey/blob/mySE/xiaoxueyunsuan

优化

  1.对产生真分数重新做了处理,导入fractions库,使用其中的函数来对该部分进行优化

  2.对题目形式进行优化。上篇blog中所出现的题目存在除号与分数线分布清楚的情况,本次改进只用“+-*”来进行运算,题目更加清晰明了

  3.对题目结果进行优化。上篇blog中题目结果为小数,且在对题目判断正误时存在精确度问题。本片blog先对整数进行处理,然后根据调用fractions库中的Fraction函数对结果进行处理,使最终结果更加精确,并且改进了题目判断的精确问题。

  4.改进了上一篇博客中出现的bug

代码呈现

import random
import fractions

def LowCalculate():#三年级以下题目,只涉及两个数字的加减运算
while(True):
integer1 =random.randint(1, 10)
integer2 =random.randint(1, 10)
if (integer1 > integer2):
break
lowsymbol = random.choice("+-")
if eval(str(integer1)+str(lowsymbol)+str(integer2))>0:
return str(integer1)+str(lowsymbol)+str(integer2)

def ProperFraction():#产生真分数
while (True):
molecular = random.randint(1, 20) # 分子
denominator = random.randint(1, 20) # 分母
if (molecular > denominator):
Fract = fractions.Fraction(molecular, denominator)
molecular = Fract.numerator # 提取分子
denominator = int(Fract.denominator) # 提取分母
return molecular, denominator

def blacket():#产生三年级以上题目,含真分数的四则运算,以及返回结果
left = "("
right = ")"
integer1 = str(random.randint(1, 10))
integer2 = str(random.randint(1, 10))
symbol = random.sample("+-*", 2)
fraction = ProperFraction()
fraction = fractions.Fraction(fraction[0],fraction[1])#输出分数
molecular =fraction.numerator#提取分子
denominator =int(fraction.denominator)#提取分母
value = [integer1, integer2, fraction]
equation = random.sample(value, 3)
for i in range(3):
if isinstance(equation[i],int)==False:
Fract = equation[i]
equation[i] = float(Fract)
for i in range(3):
if equation[i] != Fract:
temp = int(equation[i]*denominator)
equation[i] = fractions.Fraction(temp,denominator)
return symbol , equation

def Result():#计算运算结果
symbol,equation = blacket()
ans=0
if symbol[0] == "*"and symbol[1] == "+":
ans = equation[0]*equation[1]+equation[2]
elif symbol[0] == "*"and symbol[1] == "-":
ans = equation[0]*equation[1]-equation[2]
elif symbol[0] == "+"and symbol[1] == "*":
ans = equation[1]*equation[2]+equation[0]
elif symbol[0] == "-" and symbol[1] == "*":
ans = equation[0]-equation[1]*equation[2]
elif symbol[0] == "-" and symbol[1] == "+":
ans = equation[0] - equation[1] + equation[2]
elif symbol[0] == "+" and symbol[1] == "-":
ans = equation[0] + equation[1] - equation[2]
return str(equation[0]) + str(symbol[0]) + str(equation[1]) + str(symbol[1]) + str(equation[2]), ans

def Number():#刷题数目
try:
print("请输入刷题数目")
num = input("")
num = int(num)

except(ValueError,ArithmeticError):
print("请重新输入数字")
num = Number()
finally:
print("继续运行")
return num

def Select():#选择难度
Num = Number()
if choice == '1':
count=0
for i in range (int(Num)):
Que = LowCalculate()
Que = str(Que)
Res = eval(Que)
print( Que ,"=","请输入答案")
answer = fractions.Fraction('{}'.format(eval(input()))).limit_denominator()#将输入的结果转换为分数
#answer = int(answer)
if (abs(int(answer) -int( Res))) == 0:
count = count+1
print("你的回答正确")
else:
print("答案错误", "正确答案是:", Res)
print("你一共答对了", count, "题", "准确率为:", count / 5)
elif choice == '2':
count = 0
for i in range(int(Num)):
Calculate=Result()
if Calculate[1]>0:
print(Calculate[0]+ "="," 请输入答案:")
else:
Calculate = Result()
answer = fractions.Fraction('{}'.format(eval(input()))).limit_denominator()

if (Calculate[1]==answer):
count=count+1
print("你的回答正确","准确答案是:",Calculate[1])
else:
print("答案错误","正确答案是:",Calculate[1])
print("你一共答对了",count,"题","准确率为:",count/5)

if __name__ == '__main__':
print("请选择难度,"
"三年级以下请输入1",
"三年级以上请输入2")
choice = input()
X=Select()

结果显示

 

 

 

 

 

 

 

 

 

 性能分析

 

 

 

 

 

 

 

 

 

 

 

 

PSP时间表

PSP2.1Personal Software Process Stages预估耗时(分钟)实际耗时(分钟)
Planning 计划  7  10
· Estimate · 估计这个任务需要多少时间  200  400
Development 开发  205  370
· Analysis · 需求分析 (包括学习新技术)  20  20
· Design Spec · 生成设计文档 10 15
· Design Review · 设计复审 (和同事审核设计文档)  10 15
· Coding Standard · 代码规范 (为目前的开发制定合适的规范)  15 20
· Design · 具体设计  20 20
· Coding · 具体编码  80 170
· Code Review · 代码复审  20 10
· Test · 测试(自我测试,修改代码,提交修改)  30 100
Reporting 报告  40 60 
· Test Report · 测试报告  20 30 
· Size Measurement · 计算工作量  10 10 
· Postmortem & Process Improvement Plan · 事后总结, 并提出过程改进计划  10 20 
合计    257  
440
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
下次优化方向
  1.题目长度由使用者自己设定
  2.对题目形式作进一步优化
  3.对本篇博客中的可能出现的问题做进一步优化
 
posted @ 2020-09-28 10:15  .SG  阅读(132)  评论(0编辑  收藏  举报