这个作业属于哪个课程 | 班级链接 |
---|---|
这个作业要求在哪里 | 作业要求链接 |
这个作业的目标 | 要求实现一个自动生成小学四则运算题目的命令行程序 |
学号 | 姓名 | Github仓库地址 |
---|---|---|
3122004404 | 王钦鹏 | Github链接 |
- PSP表格
PSP2.1 | Personal Software Process Stages | 预估耗时(分钟) | 实际耗时(分钟) |
---|---|---|---|
Planning | 计划 | 30 | 40 |
Estimate | 估计这个任务需要多少时间 | 500 | 700 |
Development | 开发 | 400 | 450 |
Analysis | 需求分析 (包括学习新技术) | 200 | 300 |
Design Spec | 生成设计文档 | 30 | 20 |
Design Review | 设计复审 (和同事审核设计文档) | 40 | 40 |
Coding Standard | 代码规范 (为目前的开发制定合适的规范) | 20 | 30 |
Design | 具体设计 | 300 | 400 |
Coding | 具体编码 | 200 | 300 |
Code Review | 代码复审 | 20 | 40 |
Test | 测试(自我测试,修改代码,提交修改) | 40 | 50 |
Reporting | 报告 | 30 | 40 |
Test Report | 测试报告 | 20 | 25 |
Size Measurement | 计算工作量 | 40 | 50 |
Postmortem & Process Improvement Plan | 事后总结, 并提出过程改进计划 | 30 | 40 |
Total | 合计 | 1900 | 2525 |
- 效能分析
改进性能花了几十分钟,发现速度太慢生成不了10000个题目
3.设计实现过程
- getSignal()来随机生成4种运算符
- random()生成特定范围内随机数
- gcd()辗转相除法来获得两个数的最大公约数
- answer()通过计算得出结果写入文件中
- 主类函数供用户使用
- 代码说明
answer()
通过区分不同的运算符分为多种不同情况来逐一分析并运算得出结果
点击查看代码
int answers(int y,int num,int num1,int num2,int num3,char signal1,char signal2,int r)//生成答案
{
int ans,a,t,c=0;
FILE *fp;
FILE *fb;
fp=fopen("Answers.txt","a");
fb=fopen("Exercises.txt","a");
if(num==1)
{
int ans1,ans2;
switch(signal1)
{
case '+':
ans=num1+num2;
fprintf(fp,"答案%d:%d\n",y,ans);
fclose(fp);
fprintf(fb,"题目%d:%d + %d = \n",y,num1,num2);
fclose(fb);
break;
case '-':
if(num1<num2)
{
t=num1;
num1=num2;
num2=t;
}
ans=num1-num2;
fprintf(fp,"答案%d:%d\n",y,ans);
fclose(fp);
fprintf(fb,"题目%d:%d - %d = \n",y,num1,num2);
fclose(fb);
break;
case 'x':
ans=num1*num2;
fprintf(fp,"答案%d:%d\n",y,ans);
fclose(fp);
fprintf(fb,"题目%d:%d x %d = \n",y,num1,num2);
fclose(fb);
break;
case '/':
a=gcd(num1,num2);
ans1=num1/a;
ans2=num2/a;
c=0;
while(ans1>ans2)
{
ans1=ans1-ans2;
c++;
}
if(c>0)
{
fprintf(fp,"答案%d:%d'%d/%d\n",y,c,ans1,ans2);
fprintf(fb,"题目%d:%d / %d = \n",y,num1,num2);
}
if(c==0)
{
fprintf(fp,"答案%d: %d/%d\n",y,ans1,ans2);
fprintf(fb,"题目%d:%d / %d = \n",y,num1,num2);
}
fclose(fb);
fclose(fp);
break;
}
}
if(num==2)
{
if(signal2=='x'&&signal1!='/')
{
int ans1;
ans1=num2*num3;
switch(signal1)
{
case '+':
ans=num1+ans1;
fprintf(fp,"答案%d:%d\n",y,ans);
fclose(fp);
fprintf(fb,"题目%d:%d + %d x %d = \n",y,num1,num2,num3);
fclose(fb);
break;
case '-':
do{
num1=random(r);
num2=random(r);
num3=random(r);
}while(num1-num2*num3<0);
ans=num1-num2*num3;
fprintf(fp,"答案%d:%d\n",y,ans);
fclose(fp);
fprintf(fb,"题目%d:%d - %d x %d = \n",y,num1,num2,num3);
fclose(fb);
break;
case 'x':
ans=num1*ans1;
fprintf(fp,"答案%d:%d\n",y,ans);
fclose(fp);
fprintf(fb,"题目%d:%d x %d x %d = \n",y,num1,num2,num3);
fclose(fb);
break;
}
}
else
if(signal2=='/'&&signal1!='/')
{
int ans1,ans2;
c=0;
a=gcd(num2,num3);
ans1=num2/a;
ans2=num3/a;
while(ans1>ans2)
{
ans1=ans1-ans2;
c++;
}
switch(signal1)
{
case '+':
c=num1+c;
fprintf(fp,"答案%d:%d'%d/%d\n",y,c,ans1,ans2);
fprintf(fb,"题目%d:%d + %d / %d = \n",y,num1,num2,num3);
fclose(fb);
fclose(fp);
break;
case '-':
do{
num1=random(r);
num2=random(r);
num3=random(r);
}while((num1-num2/num3)<0||num3==0);
c=num1-c;
fprintf(fp,"答案%d:%d'%d/%d\n",y,c,ans1,ans2);
fclose(fp);
fprintf(fb,"题目%d:%d - %d / %d = \n",y,num1,num2,num3);
fclose(fb);
break;
case 'x':
int ans1;
c=0;
ans1=num1*num2;
a=gcd(ans1,num3);
ans1=ans1/a;
ans2=num3/a;
while(ans1>ans2)
{
ans1=ans1-ans2;
c++;
}
fprintf(fp,"答案%d:%d'%d/%d\n",y,c,ans1,ans2);
fclose(fp);
fprintf(fb,"题目%d:%d x %d / %d = \n",y,num1,num2,num3);
fclose(fb);
break;
}
}
if(signal1=='/')
{
int ans1,ans2;
c=0;
switch(signal2)
{
case '+':
a=gcd(num1,num2);
ans1=num1/a;
ans2=num2/a;
c=0;
while(ans1>ans2)
{
ans1=ans1-ans2;
c++;
}
c=c+num3;
fprintf(fp,"答案%d:%d'%d/%d\n",y,c,ans1,ans2);
fclose(fp);
fprintf(fb,"题目%d:%d %c %d %c %d = \n",y,num1,signal1,num2,signal2,num3);
fclose(fb);
break;
case '-':
a=gcd(num1,num2);
ans1=num1/a;
ans2=num2/a;
c=0;
while(ans1>ans2)
{
ans1=ans1-ans2;
c++;
}
do{
num3=random(r);
}while(c>=num3);
if(c==num3)
{
fprintf(fp,"答案%d:%d/%d\n",y,ans1,ans2);
fclose(fp);
fprintf(fb,"题目%d:%d %c %d %c %d = \n",y,num1,signal1,num2,signal2,num3);
fclose(fb);
}
else if(c>num3)
{
c=c-num3;
fprintf(fp,"答案%d:%d'%d/%d\n",y,c,ans1,ans2);
fclose(fp);
fprintf(fb,"题目%d:%d %c %d %c %d = \n",y,num1,signal1,num2,signal2,num3);
fclose(fb);
}break;
case 'x':
ans1=num1*num3;
a=gcd(ans1,num2);
ans1=ans1/a;
ans2=num2/a;
c=0;
while(ans1>ans2)
{
ans1=ans1-ans2;
c++;
}
fprintf(fp,"答案%d:%d'%d/%d\n",y,c,ans1,ans2);
fclose(fp);
fprintf(fb,"题目%d:%d %c %d %c %d = \n",y,num1,signal1,num2,signal2,num3);
fclose(fb);
break;
case '/':
ans2=num2*num3;
a=gcd(num1,ans2);
ans1=num1/a;
ans2=ans2/a;
c=0;
while(ans1>ans2)
{
ans1=ans1-ans2;
c++;
}
fprintf(fp,"答案%d:%d'%d/%d\n",y,c,ans1,ans2);
fclose(fp);
fprintf(fb,"题目%d:%d %c %d %c %d = \n",y,num1,signal1,num2,signal2,num3);
fclose(fb);
break;
}
}
else
if(signal1=='+')
{
int ans1,ans2;
c=0;
switch(signal2)
{
case '+':
ans=num1+num2+num3;
fprintf(fp,"答案%d:%d\n",y,ans);
fclose(fp);
fprintf(fb,"题目%d:%d %c %d %c %d = \n",y,num1,signal1,num2,signal2,num3);
fclose(fb);
break;
case '-':
do
{
num1=random(r);
num2=random(r);
num3=random(r);
}while(num1+num2<num3);
ans=num1+num2-num3;
fprintf(fp,"答案%d:%d\n",y,ans);
fclose(fp);
fprintf(fb,"题目%d:%d %c %d %c %d = \n",y,num1,signal1,num2,signal2,num3);
fclose(fb);
break;
}
}
else
if(signal1=='-')
{
switch(signal2)
{
case '+':
if(num1<num2)
{
t=num1;
num1=num2;
num2=t;
}
ans=num1-num2+num3;
fprintf(fp,"答案%d:%d\n",y,ans);
fclose(fp);
fprintf(fb,"题目%d:%d %c %d %c %d = \n",y,num1,signal1,num2,signal2,num3);
fclose(fb);
break;
case '-':
do
{
num1=random(r);
num2=random(r);
num3=random(r);
}while(num1<num2+num3);
ans=num1-num2-num3;
fprintf(fp,"答案%d:%d\n",y,ans);
fclose(fp);
fprintf(fb,"题目%d:%d - %d - %d = \n",y,num1,num2,num3);
fclose(fb);
break;
}
}
else
if(signal1=='x')
{
switch(signal2)
{
case '+':
ans=num1*num2+num3;
fprintf(fp,"答案%d:%d\n",y,ans);
fclose(fp);
fprintf(fb,"题目%d:%d x %d + %d = \n",y,num1,num2,num3);
fclose(fb);
break;
case '-':
do
{
num1=random(r);
num2=random(r);
num3=random(r);
}while(num1*num2<num3);
ans=num1*num2-num3;
fprintf(fp,"答案%d:%d\n",y,ans);
fclose(fp);
fprintf(fb,"题目%d:%d x %d - %d = \n",y,num1,num2,num3);
fclose(fb);
break;
}
}
}
return 0;
}
- 测试运行
6.项目小结
通过这次作业,第一次知道控制时间复杂度的重要性,要生成这么多题目,如果时间复杂度过高,则效率很低。更应该多去应用效率高的算法。