四则运算作业升级

Github项目地址

https://github.com/978244046/aa/tree/master

PSP

PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际耗时(分钟)
Planning 计划 10 8
· Estimate · 估计这个任务需要多少时间 10 8
Development 开发 600 800+
· Analysis · 需求分析 (包括学习新技术) 30 35
· Design Review · 设计复审 (和同事审核设计文档) 10 15
· Coding Standard · 代码规范 (为目前的开发制定合适的规范) 5 5
· Design · 具体设计 40 60
· Coding · 具体编码 5h*60 7h*60
· Code Review · 代码复审 1h*60 1.5h*60
· Test · 测试(自我测试,修改代码,提交修改) 3h*60 2h*60
· Test Report · 测试报告+博客 4h*60 4.5h*60

 


解题思路

这个题目非常复杂性。这个题目可以被划分为以下问题:

列出随机的四则运算表达式。

计算所列出四则运算的结果。

接受用户输入并比较结果是否正确,并在命令行中表现出来。

设计中需要设计加法,减法,乘法,除法,分数运算

 

设计过程

依次设计加法,减法,乘法,除法和真分数运算函数


 

代码说明

#-*- coding: utf-8 -*-

'''
Created on 2019年3月24日

@author: LKR
'''

# -*- coding: utf-8 -*-
import random
from fractions import Fraction
def add(a,b):
return a+b
def minus(a,b):
return a-b
def multiply(a,b):
return a*b
def divide(a,b):
return a/b
def fraction(a,b):
return Fraction(a,b)

ans = 0
print ("小学四则运算测试:(结果保留整数)")
count = 1
right = 0
while(1):
add1 = random.randint(1, 10) # 锟斤拷1
add2 = random.randint(1, 10) # 锟斤拷2
t = random.random()
if 0<=t<=0.2:
ans = add(add1,add2)
eq = str(add1)+'+'+str(add2)
print("Q%d: %s=" %(count,eq))

elif 0.2<t<=0.4:
ans = minus(add1,add2)
eq = str(add1)+'-'+str(add2)
print("Q%d: %s=" %(count,eq))

elif 0.4<t<=0.6:
ans = multiply(add1,add2)
eq = str(add1)+'*'+str(add2)
print("Q%d: %s=" %(count,eq))

elif 0.6<t<=0.8:
ans = fraction(add1,add2)
eq = str(add1)+'/'+str(add2)
print("Q%d: %s=" %(count,eq))

else :
add3 = random.randint(1, 10) # 锟斤拷1
add4 = random.randint(1, 10)
ans = fraction(add1,add2)+fraction(add3,add4)
eq = str(add1)+'/'+str(add2)+'+'+str(add3)+'/'+str(add4)
print("Q%d: %s=" %(count,eq))

sss = input("A%d: "%count)
try:
if round(eval(sss),5) == round(float(ans),5):
print('回答正确!')
print('正确答案:',end = '')
print(ans)

right = right + 1
elif round(eval(sss),5) != round(float(ans),5):
print('回答错误!')
print('正确答案:',end = '')
print(ans)
print(type(ans))
count += 1
except:
if sss in ['q','Q']:
break
else:
print("输入错误")
continue
print("正确个数 %d"%right)
print("总个数%d"%(count-1))
print("正确率:%.2f" %(right/(count-1)))

猜你喜欢

转载自www.cnblogs.com/lkr007/p/10618587.html