github项目地址 : https://github.com/kongkalong/python
PSP表格 :
PSP2.1 |
预估耗时(小时) |
实际耗时(小时) |
Planning |
|
|
.Estimate |
72 |
48 |
Development |
|
|
.Analysis |
0.5 |
0.1 |
.Design Spec |
0 |
0 |
.Coding Standard |
0 |
0 |
.Design Review |
0 |
0 |
.Design |
24 |
8 |
.Coding |
48 |
36 |
.Code Review |
0 |
4 |
.Test |
0 |
6 |
Reporting |
|
|
.Test Report |
0 |
0 |
.Size Measurement |
0 |
0 |
.Postmortem&Process Inprovement Plan |
0 |
0 |
合计 |
|
|
设计过程 :
首先,定义生成随机整数的函数printInteger()和生成随机真分数的函数printProperFraction(),然后分别定义输出加法运算的函数addition()、输出减法运算的函数subtraction()、输出乘法运算的函数multiplication()和输出除法运算的函数division(),最后定义一个数值在0-3的随机数,0-3按顺序分别代表addition() subtraction() multiplication() division(),在while循环内用if语句随机输出四则运算。
代码 :
import random
#生成0到100的随机整数
def printInteger():
#返回包括整数的字符串和值的数组
a=[]
m=random.randint(0,100)
n=str(m)
a.append(m)
a.append(n)
return a
#输出真分数,真分数表示为m/n
def printProperFraction():
while True:
m=random.randint(3,100)
n=random.randint(2,100)
if m>n and m%n!=0:
break
#返回包括真分数的字符串和值的数组
a=[]
a.append(m/n)
a.append(str(m)+"/"+str(n))
return a
#输出加法运算
def addition():
m=random.randint(0,1)
n=random.randint(0,1)
#运算符左右均为真分数
if m==0 and n==0:
a=printProperFraction()
b=printProperFraction()
print(a[1]+"+"+b[1])
#运算符左边为真分数,右边为整数
if m==0 and n==1:
a=printProperFraction()
b=printInteger()
print(a[1]+"+"+b[1])
#运算符左边为整数,右边为真分数
if m==1 and n==0:
a=printInteger()
b=printProperFraction()
print(a[1]+"+"+b[1])
#运算符左右均为整数
if m==1 and n==1:
a=printInteger()
b=printInteger()
print(a[1]+"+"+b[1])
#输出减法运算
def subtraction():
m=random.randint(0,1)
n=random.randint(0,1)
if m==0 and n==0:
a=printProperFraction()
b=printProperFraction()
#如果运算符左边的数小于右边的数则交换位置
if a[0]
a,b=b,a
print(a[1]+"-"+b[1])
if m==0 and n==1:
a=printProperFraction()
b=printInteger()
if a[0]
a,b=b,a
print(a[1]+"-"+b[1])
if m==1 and n==0:
a=printInteger()
b=printProperFraction()
if a[0]
a,b=b,a
print(a[1]+"-"+b[1])
if m==1 and n==1:
a=printInteger()
b=printInteger()
if a[0]
a,b=b,a
print(a[1]+"-"+b[1])
#输出乘法运算
def multiplication():
m=random.randint(0,1)
n=random.randint(0,1)
if m==0 and n==0:
a=printProperFraction()
b=printProperFraction()
print(a[1]+"×"+b[1])
if m==0 and n==1:
a=printProperFraction()
b=printInteger()
print(a[1]+"×"+b[1])
if m==1 and n==0:
a=printInteger()
b=printProperFraction()
print(a[1]+"×"+b[1])
if m==1 and n==1:
#避免出现0×0
while True:
a=printInteger()
b=printInteger()
if a[0]!=0 or b[0]!=0:
break
print(a[1]+"×"+b[1])
#输出除法运算
def division():
m=random.randint(0,1)
n=random.randint(0,1)
if m==0 and n==0:
a=printProperFraction()
b=printProperFraction()
print(a[1]+"÷"+b[1])
if m==0 and n==1:
a=printProperFraction()
#避免除数为0
while True:
b=printInteger()
if b[0]!=0:
break
print(a[1]+"÷"+b[1])
if m==1 and n==0:
a=printInteger()
b=printProperFraction()
print(a[1]+"÷"+b[1])
if m==1 and n==1:
a=printInteger()
while True:
b=printInteger()
if b[0]!=0:
break
print(a[1]+"÷"+b[1])
count=0
while count<300:
type=random.randint(0,3)
if type==0:
addition()
if type==1:
subtraction()
if type==2:
multiplication()
if type==3:
division()
count+=1
测试运行 :
PSP表格 :
PSP2.1 |
预估耗时(小时) |
实际耗时(小时) |
Planning |
|
|
.Estimate |
|
48 |
Development |
|
|
.Analysis |
|
0.1 |
.Design Spec |
|
0 |
.Coding Standard |
|
0 |
.Design Review |
|
0 |
.Design |
|
8 |
.Coding |
|
36 |
.Code Review |
|
4 |
.Test |
|
6 |
Reporting |
|
|
.Test Report |
|
0 |
.Size Measurement |
|
0 |
.Postmortem&Process Inprovement Plan |
|
0 |
合计 |
|
|