简易菜单代码的学习 | https://edu.cnblogs.com/campus/zswxy/CST2019-4/homework/10277 |
---|---|
作业链接 | https://edu.cnblogs.com/campus/zswxy/CST2019-4/homework/10277 |
这个作业的目标 | 学习完成使用%g的形式输出答案 |
参考文献 | https://zhidao.baidu.com/question/271899014.html |
1.设计思路和遇到的问题
设计思路:以%g的方式输出答案,分类讨论三年级的题目。
遇到的问题:无。
2.程序截图
3.程序代码
#include <stdio.h>
#include<stdlib.h>
#include<time.h>
void menu();
void help();
void error();
void operation_1();void one_1();
void operation_2();void two_1();
void operation_3();void three_1();
int main()
{
int n;
printf("========== 口算生成器 ==========
欢迎使用口算生成器 :
希望小学期中考试
");
printf("
");
help();
while(1)
{
menu();
scanf("%d",&n);
switch(n)
{
case 1:operation_1();break;
case 2:operation_2();break;
case 3:operation_3();break;
case 4:help();break;
}
printf("
");
if(n==5) break;
if(n>5||n<1) error();
}
return 0;
}
void help()
{
printf("帮助信息
您需要输入命令代号来进行操作, 且
");
printf("一年级题目为不超过十位的加减法;
二年级题目为不超过百位的乘除法;
");
printf("三年级题目为不超过百位的加减乘除混合题目.
");
printf("
");
}
void menu()
{
printf("操作列表:
1)一年级 2)二年级 3)三年级
4)帮助 5)退出程序
请输入代号:");
}
void error()
{
printf("憨憨,请重新输入正确数值。");
printf("
");
printf("
");
}
void operation_1()
{
printf("请输入题目数量>");
one_1();
}
void operation_2()
{
printf("请输入题目数量>");
two_1();
}
void operation_3()
{
printf("请输入题目数量>");
three_1();
}
void one_1()
{
int n,a,b,c;
char op[2]={'+','-'};
scanf("%d",&n);
printf("一年级题目如下:
");
srand((unsigned)time(NULL));
for(int i=1;i<=n;i++)
{
a=rand()%10+1;
b=rand()%10+1;
c=rand()%2;
if(op[c]=='+')
printf("%2d + %2d = %2d",a,b,a+b);
else
printf("%2d - %2d = %2d",a,b,a-b);
printf("
");
}
}
void two_1()
{
int n,a,b,c;
char op[2]={'*','/'};
scanf("%d",&n);
printf("二年级题目如下:
");
srand((unsigned)time(NULL));
for(int i=1;i<=n;i++)
{
a=rand()%100+1;
b=rand()%100+1;
c=rand()%2;
if(op[c]=='*')
printf("%3d * %3d = %3d",a,b,a*b);
else
printf("%3d / %3d = %3g",a,b,(double)(a/(b*1.0)));
printf("
");
}
}
void three_1()
{
int n,a,b,c,d,e;
char op[4]={'+','-','*','/'} ;
scanf("%d",&n);
printf("三年级题目如下:
");
srand((unsigned)time(NULL));
for(int i=1;i<=n;i++)
{
a=rand()%100+1;
b=rand()%100+1;
c=rand()%100+1;
d=rand()%4;
e=rand()%4;
switch(d)
{
case 0:
printf("%3d + %3d ",a,b);
if(op[e]=='*')
printf("* %3d = %3d ",e,a+b*c);
else if(op[e]=='/')
printf("/ %3d = %3g",c,a+b/(c*1.0));
else if(op[e]=='+')
printf("+ %3d = %3d ",c,a+b+c);
else
printf("- %3d = %3d",c,a+b-c);
break;
case 1:
printf("%3d - %3d ",a,b);
if(op[e]=='*')
printf("* %3d = %3d ",e,a-b*c);
else if(op[e]=='/')
printf("/ %3d = %3g",c,a-b/(c*1.0));
else if(op[e]=='+')
printf("+ %3d = %3d ",c,a-b+c);
else
printf("- %3d = %3d",c,a-b-c);
break;
case 2:
printf("%3d * %3d ",a,b);
if(op[e]=='*')
printf("* %3d = %3d ",e,a*b*c);
else if(op[e]=='/')
printf("/ %3d = %3g",c,a*b/(c*1.0));
else if(op[e]=='+')
printf("+ %3d = %3d ",c,a*b+c);
else
printf("- %3d = %3d",c,a*b-c);
break;
case 3:
printf("%3d / %3d ",a,b);
if(op[e]=='*')
printf("* %3d = %3g ",e,a/(c*b*1.0));
else if(op[e]=='/')
printf("/ %3d = %3g",c,a/(b*1.0)/c);
else if(op[e]=='+')
printf("+ %3d = %3g ",c,a/(b*1.0)+c);
else
printf("- %3d = %3g",c,a/(b*1.0)-c);
break;
}
printf("
");
}
}
Gitee上传截图与链接