小学四则运算
| 博客班级 | AHPU软件工程 |
|---|---|
| 作业要求 | 实现小学四则运算 |
| 作业目标 | |
| 学号 | 3180701124 |
一、题目要求
写一个能自动生成小学四则运算题目的程序,然后在此基础上扩展:
1)除了整数以外,还要支持真分数的四则运算,例如:1/6+1/8=7/24
2)程序要求能处理用户的输入,判断对错,累积分数
3)程序支持可以由用户自行选择加、减、乘、除运算
4)使用-n参数控制生成题目的个数,例如Myapp.exe -n 10,将生成10个题目
二、代码
真分数四则运算
void faction(int n)
{
int m1=0, z1=0, m2=0, z2=0,ansz,ansm;
char ans[10];
char ans1[12],ans2[12];
int sum=0;//总题数
int correct=0;//正确数
char temp;
system("cls");
while(1)
{
srand(time(NULL));
z1=1+rand()%9;
m1=1+rand()%9+z1;
z2=1+rand()%9;
m2=1+rand()%9+z2;
if(n==1)
{
printf("请回答:\n\t\t %d/%d + %d/%d = ",z1,m1,z2,m2);
scanf("%s",ans);
//加法结果计算
ansz=z1*m2+z2*m1;
ansm=m1*m2;
int i=gcd(ansz,ansm);
ansz/=i;
ansm/=i;
//结果转换为字符串
itoa(ansz,ans1,10);
itoa(ansm,ans2,10);
strcat(ans1,"/");
strcat(ans1,ans2);
if(strcmp(ans,ans1) == 0)
{
printf("你真棒!回答正确!\n");
correct++;
}
else{
printf("真遗憾,回答错误!");
printf("正确答案:%s\n",ans1);
}
}
if(n==2)
{
printf("请回答:\n\t\t %d/%d - %d/%d = ",z1,m1,z2,m2);
scanf("%s",ans);
//减法结果计算
ansz=z1*m2-z2*m1;
ansm=m1*m2;
int i=gcd(ansz,ansm);
ansz/=i;
ansm/=i;
//结果转换为字符串
itoa(ansz,ans1,10);
itoa(ansm,ans2,10);
strcat(ans1,"/");
strcat(ans1,ans2);
if(strcmp(ans,ans1) == 0)
{
printf("你真棒!回答正确!\n");
correct++;
}
else{
printf("真遗憾,回答错误!");
printf("正确答案:%s\n",ans1);
}
}
if(n==3)
{
printf("请回答:\n\t\t %d/%d * %d/%d = ",z1,m1,z2,m2);
scanf("%s",ans);
//乘法结果计算
ansz=z1*z2;
ansm=m1*m2;
int i=gcd(ansz,ansm);
ansz/=i;
ansm/=i;
//结果转换为字符串
itoa(ansz,ans1,10);
itoa(ansm,ans2,10);
strcat(ans1,"/");
strcat(ans1,ans2);
if(strcmp(ans,ans1) == 0)
{
printf("你真棒!回答正确!\n");
correct++;
}
else{
printf("真遗憾,回答错误!");
printf("正确答案:%s\n",ans1);
}
}
if(n==4)
{
printf("请回答:\n\t\t %d/%d / %d/%d = ",z1,m1,z2,m2);
scanf("%s",ans);
//除法结果计算
ansz=z1*m2;
ansm=z2*m1;
int i=gcd(ansz,ansm);
ansz/=i;
ansm/=i;
//结果转换为字符串
itoa(ansz,ans1,10);
itoa(ansm,ans2,10);
strcat(ans1,"/");
strcat(ans1,ans2);
if(strcmp(ans,ans1) == 0)
{
printf("你真棒!回答正确!\n");
correct++;
}
else{
printf("真遗憾,回答错误!");
printf("正确答案:%s\n",ans1);
}
}
sum++;
printf("是否继续?(Y/N):");
fflush(stdin);
scanf("%c",&temp);
if(temp=='n'||temp=='N')
{
break;
}
}
printf("一共回答了%d题\n",sum);
printf("回答对了%d题\n",correct);
printf("正确率为%.2f%%\n",correct*100.0/sum);
system("pause");
}
整数四则运算
void integer(int n)
{
int sum=0;//总题数
int correct=0;//正确数
int a,b;//两个操作数
int ans;//记录结果
char temp;
system("cls");
while(1)
{
srand(time(NULL));
a=rand()%50;
b=rand()%50;
while(n==4 && a%b!=0)
{
a=rand()%50;
b=rand()%50;
}
if(n==1)
{
printf("%d + %d = ",a,b);
scanf("%d",&ans);
if(ans == a+b)
{
correct++;
}
}
if(n==2)
{
printf("%d - %d = ",a,b);
scanf("%d",&ans);
if(ans == a-b)
{
correct++;
}
}
if(n==3)
{
printf("%d * %d = ",a,b);
scanf("%d",&ans);
if(ans == a*b)
{
correct++;
}
}
if(n==4)
{
printf("%d / %d = ",a,b);
scanf("%d",&ans);
if(ans == a/b)
{
correct++;
}
}
sum++;
printf("是否继续?(Y/N):");
fflush(stdin);
scanf("%c",&temp);
if(temp=='n'||temp=='N')
{
break;
}
}
printf("一共回答了%d题\n",sum);
printf("回答对了%d题\n",correct);
printf("正确率为%.2f%%\n",correct*100.0/sum);
system("pause");
}
int menu1(int ch1)
{
int ch2;
system("cls");
printf("1:加法\n");
printf("2:减法\n");
printf("3:乘法\n");
printf("4:除法\n");
printf("0:退出\n");
printf("请选择运算符号:");
scanf("%d",&ch2);
if(ch1==1)
{
integer(ch2);
}
if(ch1==2)
{
faction(ch2);
}
while(ch2<0||ch2>4)
{
printf("请重新选择:");
scanf("%d",&ch2);
}
return ch2;
}
菜单
int menu1(int ch1)
{
int ch2;
system("cls");
printf("1:加法\n");
printf("2:减法\n");
printf("3:乘法\n");
printf("4:除法\n");
printf("0:退出\n");
printf("请选择运算符号:");
scanf("%d",&ch2);
if(ch1==1)
{
integer(ch2);
}
if(ch1==2)
{
faction(ch2);
}
while(ch2<0||ch2>4)
{
printf("请重新选择:");
scanf("%d",&ch2);
}
return ch2;
}
int menu2()
{
start:
printf("1. 整数运算\n");
printf("2. 分数运算\n");
printf(" 请选择:");
int ch;
scanf("%d",&ch);
if(ch!=1&&ch!=2)
{
printf("请重新选择:");
scanf("%d",&ch);
}
menu1(ch);
system("pause");
system("cls");
goto start;
}
三、运行界面



四、个人小结
(1).psp表格
| psp2.1 | 任务内容 | 计划完成需要的时间(min) | 实际完成需要的时间(min) |
|---|---|---|---|
| Planning | 计划 | 120 | 130 |
| Development | 开发 | 100 | 180 |
| Analysis | 需求分析(包括学习新技术) | 10 | 30 |
| Design Spec | 生成设计文档 | 30 | 60 |
| Design Review | 设计复审 | 5 | 10 |
| Coding Standard | 代码规范 | 3 | 60 |
| Design | 具体设计 | 10 | 30 |
| Coding | 具体编码 | 36 | 45 |
| Code Review | 代码复审 | 5 | 7 |
| Test | 测试(自我测试,修改代码,提交修改) | 10 | 20 |
| Reporting | 报告 | 9 | 6 |
| Test Report | 测试报告 | 3 | 10 |
| Size Measurement | 计算工作量 | 3 | 1 |
| Postmortem & Process Improvement Plan | 事后总结,并提出过程改进计划 | 3 | 3 |
(2).心得与经验
通过本次作业,我练习了C语言的编写能力,学会了使用Markdown的编写规则,熟悉了在博客园发表博客的流程。

浙公网安备 33010602011771号