###<h1 id=_link_1st>一.作业信息</h1>
| 博客班级 | [软件工程](https://edu.cnblogs.com/campus/ahgc/AHPU-se-JSJ18?filter=homework)|
|----------------------- |------------------------------|
| 作业要求 | [作业要求](https://edu.cnblogs.com/campus/ahgc/AHPU-se-JSJ18/homework/11377) |
| 作业目标 | 能够精准高效完成四则运算并输出结果;迅速理解需求制定功能|
| 学号 | 3180701222 |
#**目录**
#####* [一.作业信息](#_link_1st)
#####* [二.作业要求](#_link_2st)
#####* [三.代码提交与截图](#_link_3st)
#####* [四.个人小结](#_link_4st)
##<h2 id=_link_2st>**二.作业要求**</h2>
写一个能自动生成小学四则运算题目的程序,然后在此基础上扩展:
1. 除了整数以外,还要支持真分数的四则运算,例如:1/6+1/8=7/24
2. 程序要求能处理用户的输入,判断对错,累积分数
3. 程序支持可以由用户自行选择加、减、乘、除运算
4. 使用-n参数控制生成题目的个数,例如Myapp.exe -n 10,将生成10个题目
##<h3 id=_link_3st>**三.代码提交与运行截图**</h3>
###1.主要函数
```
char getsignal(); //获取随机运算符函数
int random(double, double); //获取随机数函数
int getResult(int, int, char); //结果计算函数
int takeTest(); //题目生成函数
```
###2.主函数
```void main()
{
int n, a, right = 0;
double percent;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
a = takeTest();
right = right + a;
}
printf("Powerful!\n");
printf("The number of right:%d\n", right);
percent = ((double)right * 100.00) / (double)n;
printf("%0.2f %%\n", percent);
}
```
###3.获取随机运算符函数
```
char getsignal()
{
char signal[4]={'+','-','*','/'};
srand((unsigned)time(NULL));
return signal[rand()%4];
}
```
###4.获取随机数函数
```
int random(double start, double end)
{
return (int)(start+(end-start)*rand()/(RAND_MAX+ 1.0));
}
```
###5.计算结果函数
```
int getResult(int num1,int num2,char signal)//整数四则运算
{
int res;
switch(signal)
{
case '+':
res=num1+num2;break;
case '-':
res=num1-num2;break;
case '*':
res=num1*num2;break;
case '/':
res=num1/num2;break;
default:
printf("运算符错误!\n");
}
return res;
}
```
###6.真分数运算函数
```
void fs() //分数
{
int x1,y1,x2,y2,p,fz,fm,temp;
char answer[30],result[20],s[9];
int d;
d=rand()%4;
srand((int)time(0));
x1=rand()%30+2;
y1=rand()%30+2;
if(x1>y1||x1==y1)
{
x1=rand()%30+2;
y1=rand()%30+2;
}
temp=gcd(x1,y1);
if(temp>0) {
x1=x1/temp;
y1=y1/temp;
}
else if (temp<0)
{
x1=-x1/temp;
y1=-y1/temp;
}
x2=rand()%30+2;
y2=rand()%30+2;
if(x2>y2||x2==y2)
{
x2=rand()%30+2;
y2=rand()%30+2;
}
temp=gcd(x2,y2);
if(temp>0) {
x2=x2/temp;
y2=y2/temp;
}
else if (temp<0)
{
x2=-x2/temp;
y2=-y2/temp;
}
switch(d)
{
case 0: printf("%d/%d+%d/%d=",x1,y1,x2,y2);
fz=x1*y2+x2*y1;
fm=y1*y2; break;
case 1: printf("%d/%d-%d/%d=",x1,y1,x2,y2);
fz=x1*y2-x2*y1;
fm=y1*y2; break;
case 2: printf("%d/%d×%d/%d=",x1,y1,x2,y2);
fz=x1*x2;
fm=y1*y2; break;
case 3: printf("%d/%d÷%d/%d=",x1,y1,x2,y2);
fz=x1*y2;
fm=y1*x2; break;
}
p=gcd(fz,fm);
if(p>0) {
fz=fz/p;
fm=fm/p;
}
else if (p<0)
{
fz=-fz/p;
fm=-fm/p;
}
if(fz%fm==0)
{
temp=fz/fm;
itoa(fz,result,10);
}
else
{
itoa(fz,result,10);
itoa(fm,s,10);
strcat(result,"/");
strcat(result,s);
}
scanf("%s:",&answer);
if(strcmp(result,answer)==0)
{
printf("正确\n");
ttrue++;
}
else
printf("错误 \t答案是%s\n",result);
}
```
###7.题目生成函数
```
int takeTest()
{
int get;
int num1,num2,a;
char signal;
srand((unsigned)time(NULL));
signal=getSignal();
num1=random(0,1000);
num2=random(1,1000);
if(signal=='-')
{
if(num1<num2)
{
int temp;
temp=num1;
num1=num2;
num2=temp;
}
}
if(signal=='/')
{
if(num2==0)
{
int temp;
temp=num1;
num1=num2;
num2=temp;
}
}
printf("%d%c%d=",num1,signal,num2);
scanf("%d",&get);
fflush(stdin); //清空输入缓冲区
if(getResult(num1,num2,signal)==get)
{
printf("You're right!\n");
a=1;
}
else
{
printf("It's wrong!\n");
printf("The right answer is: %d\n",getResult(num1,num2,signal));
a=0;
}
return a;
}
```
###运行结果
![](https://img2020.cnblogs.com/blog/2180843/202011/2180843-20201107191702960-557493213.png)
##<h4 id=_link_4st>**四.个人小结**</h4>
| psp2.1 | 任务内容 | 计划完成需要的时间(min) | 实际完成需要的时间(min) |
| ------ | ------ | ------ | ------ |
| Planning | 计划 | 300 | 250 |
| Estimate | 估计这个任务需要多少时间,并规划大致工作步骤 | 30 | 45 |
| Development | 开发 | 40 | 40 |
| Analysis | 需求分析(包括学习新技术) | 30 | 20 |
| Design Spec | 生成设计文档 | 30 | 20 |
| Design Review | 设计复审 | 20 | 15 |
| Coding Standard | 代码规范 | 10 | 10 |
| Design | 具体设计 | 30 | 60 |
| Coding | 具体编码 | 90 | 120 |
| Code Review | 代码复审 | 30 | 60 |
| Test | 测试(自我测试,修改代码,提交修改) | 40 | 60 |
| Reporting | 报告 | 20 | 20 |
| Test Report | 测试报告 | 10 | 25 |
| Size Measurement | 计算工作量 | 30 | 20 |
| Postmortem & Process Improvement Plan | 事后总结,并提出过程改进计划| 20 | 30 |