作业三
| 这个作业属于哪个课程 | 软件工程 |
|---|---|
| 这个作业要求在哪里 | 结对项目 |
| 作业题目 | 实现一个自动生成小学四则运算题目的命令行程序 |
| 学生 | 3123004323施盈德 |
| GitHub | git |
PSP表格
| PSP2.1 | Personal Software Process Stages | 预估耗时(分钟) | 实际耗时 |
|---|---|---|---|
| planing | 计划 | 30 | 35 |
| Estimate | 估计这个任务需要多少时间 | 120 | 240 |
| Development | 开发 | 120 | 125 |
| Analysis | 需求分析 | 12 | 12 |
| Design Spec | 生成设计文档 | 20 | 26 |
| Design Review | 设计复审 | 10 | 5 |
| Design | 具体设计 | 12 | 12 |
| Coding | 具体代码 | 100 | 120 |
| Code Review | 代码复审 | 10 | 10 |
| Test | 测试 | 15 | 18 |
| Report | 报告 | 30 | 45 |
| Test Report | 测试报告 | 20 | 30 |
| Size Measurement | 计算工作量 | 20 | 26 |
| Postmortem & Process Improvement Plan | 事后总结, 并提出过程改进计划 | 15 | 16 |
| total | 合计 | 534 | 720 |
设计实现过程
只有一个主函数,用n来控制数量,r为数字范围,用rand来随机生成函数,生成的题目随机是用switch实现,type为题目种类,用rand生成,left和right为左右两数,同样用rand生成,在case中完成题目的输出,答案的写入。
代码
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
int main(){
int i=0;
int n=0;int r=0; //题目数量和数的范围
int x=0;
int type;
int left, right;
printf("请输入要出的题目数量\n");
scanf("%d",&n);
float answers[n];//存放答案
printf("请输入要出题目中数字的范围\n");
scanf("%d",&r);
srand(unsigned(time(0)));//使用rand必备,我试过放while循环里,但出错了
FILE* fp;//打开文件必备 以下操作是为了重置文件
fp = fopen("Exercises.txt", "w");
fprintf(fp,"题目如下\n");
fclose(fp);
while(x<n){
type=rand()%4;//随机生成数字
left=rand()%r;
right=rand()%r;
printf("type: :: %d %d %d\n",type,left,right);
switch(type){//根据随机生成的数字来选择题目类型
case 0:
answers[i]=(float)(left+right);//写入答案还有题目
printf("第%d题%d + %d = ?\n",i+1,left,right);
fp = fopen("Exercises.txt", "a+");
fprintf(fp,"第%d题%d + %d = ?\n",i+1,left,right);
fclose(fp);
break;
case 1:
answers[i]=(float)(left-right);
printf("第%d题%d - %d = ?\n",i+1, left, right);
fp = fopen("Exercises.txt", "a+");
fprintf(fp,"第%d题%d - %d = ?\n",i+1,left,right);
fclose(fp);
break;
case 2:
answers[i]=(float)(left*right);
printf("第%d题%d * %d = ?\n",i+1, left, right);
fp = fopen("Exercises.txt", "a+");
fprintf(fp,"第%d题%d * %d = ?\n",i+1,left,right);
fclose(fp);
break;
case 3:
float x0=left;
float y0=right;
if(y0==0) right=rand()%r;
answers[i]=(float)(x0/y0);
printf("第%d题%d / %d = ?\n",i+1, left, right);
fp = fopen("Exercises.txt", "a+");
fprintf(fp,"第%d题%d / %d = ?\n",i+1,left,right);
fclose(fp);
break;
}
x++;
i++;
if(i==n) printf("一共%d题",i);
}
printf("testttt\n");
fp=fopen("Answers.txt","w");//每次重置答案文件
fprintf(fp,"答案如下\n");
fclose(fp);
fp=fopen("Answers.txt","a+");
for(int m=0;m<i;m++){
fprintf(fp,"第%d题.%f\n",m+1,answers[m]);
}
fclose(fp);
return 0;
}
测试运行



项目小结
通过这次项目,我更好的提升了自己。

浙公网安备 33010602011771号