这个作业属于哪个课程 | https://edu.cnblogs.com/campus/zswxy/CST2019-4 |
---|---|
这个作业的要求在哪里 | https://edu.cnblogs.com/campus/zswxy/CST2019-4/homework/10277 |
作业正文 | 使用switch编写菜单程序,使用rand函数来产生随机数,并算出生成口算题的结果 |
其他参考文献 | c语言中%g是什么意思? |
2.2.2 设计思路和遇到的问题
在原来的基础上计算出答案;最大的问题是除法所得出的商的小数点的个数。
2.2.3 程序结果截图
2.2.4 程序代码
include
include
include
include
void menu(); void help();
void one(); void two();
void three(); void error();
int operation_1(int n);
int operation_2(int n);
int operation_3(int n);
int main()
{
int opt=1,n;
printf("==========口算生成器==========\n");
printf("欢迎使用口算生成器:\n");
printf("\n");
printf("帮助信息\n");
printf("您需要输入命令代号来进行操作,且\n");
printf("一年级题目为不超过十位的加减法\n");
printf("二年级题目为不超过百位的乘除法\n");
printf("三年级题目为不超过百位的加减乘除混合题目.\n");
while(opt!=0)
{
menu();
printf("请输入操作> ");
scanf("%d",&opt);
printf("<执行操作:)\n");
printf("\n");
switch(opt)
{
case 1:one();break;
case 2:two();break;
case 3:three();break;
case 4:help();break;
case 5:printf("程序结束, 欢迎下次使用\n");
printf("任意键结束……");
opt=0;
getch();break;
default:error();break;
}
}
}
void menu()
{
printf("\n");
printf("操作列表:\n");
printf("1)一年级 2)二年级 3)三年级\n");
printf("4)帮助 5)退出程序\n");
}
void help()
{
printf("\n");
printf("帮助信息\n");
printf("您需要输入命令代号来进行操作,且\n");
printf("一年级题目为不超过十位的加减法\n");
printf("二年级题目为不超过百位的乘除法\n");
printf("三年级题目为不超过百位的加减乘除混合题目.\n");
getch();
}
void one()
{
int n;
printf("现在是一年级题目:\n");
printf("请输入生成个数>");
scanf("%d",&n);
printf("<执行操作:)\n");
operation_1(n);
getch();
}
void two()
{
int n;
printf("现在是二年级题:\n");
printf("请输入生成个数>");
scanf("%d",&n);
printf("<执行操作:)\n");
operation_2(n);
getch();
}
void three()
{
int n;
printf("现在是三年级题目:\n");
printf("请输入生成个数>");
scanf("%d",&n);
printf("<执行操作:)\n");
operation_3(n);
getch();
}
void error()
{
printf("Error!!!\n");
printf("错误操作指令, 请重新输入\n");
getch();
}
operation_1(int n)
{
int i,j,num1,num2,num;
char signal[2]={'+','-'};
srand((unsigned)(time(NULL)));
for(i=1;i<=n;i++)
{
num1=rand()%10;num2=rand()%10;j=signal[rand()%2];
switch(j)
{
case '+':num=num1+num2;break;
case '-':num=num1-num2;break;
}
printf("%2d %2c %2d = %2d\n",num1,j,num2,num);
}
}
operation_2(int n)
{
int i,j,num1,num2,num3,num;
float Num;
char signal[2]={'','/'};
srand((unsigned)(time(NULL)));
for(i=1;i<=n;i++)
{
num1=rand()%10;num2=rand()%10;
num3=rand()%10+1;
j=signal[rand()%2];
switch(j)
{
case '':num=num1num2;break;
case '/':Num=num11.0/num3;break;
}
if(j=='/')
{
printf("%2d %2c %2d = %2g\n",num1,j,num3,Num);
}
else
{
printf("%2d %2c %2d = %2d\n",num1,j,num2,num);
}
}
}
operation_3(int n)
{
int j,k;
int i,num1,num2,num3,num4,num5,num6,num;
float Num,Num1,Num2;
char signal[4]={'+','-','','/'};
srand((unsigned)(time(NULL)));
for(i=1;i<=n;i++)
{
num1=rand()%100;num2=rand()%100;
num3=rand()%100;num4=rand()%100+1;
num5=rand()%100+1;
j=signal[rand()%4];k=signal[rand()%4];
if(k!=''&&k!='/')
{
switch(j)
{
case '+':num6=num1+num2;break;
case '-':num6=num1-num2;break;
case '':num6=num1num2;break;
case '/':Num=1.0num1/num4;break;
}
if(j=='/')
{
switch(k)
{
case '+':num=Num+1.0num3;break;
case '-':num=Num-1.0num3;break;
}
printf("%2d %2c %2d %2c %2d = %2g\n",num1,j,num2,k,num4,Num);
}
else
{
switch(k)
{
case '+':num=num6+num3;break;
case '-':num=num6-num3;break;
}
printf("%2d %2c %2d %2c %2d = %2d\n",num1,j,num2,k,num3,num);
}
}
else if(k=='')
{
switch(j)
{
case '+':num=num1+num2num3;break;
case '-':num=num1-num2num3;break;
case '':num=num1num2num3;break;
case '/':Num=1.0num1/num4*num3;break;
}
if(j=='/')
{
printf("%2d %2c %2d %2c %2d = %2g\n",num1,j,num2,k,num4,Num);
}
else
{
printf("%2d %2c %2d %2c %2d = %2d\n",num1,j,num2,k,num3,num);
}
}
else
{
Num1=num2*1.0/num4;Num2=num4*1.0/num5*1.0;
switch(j)
{
case '+':Num=num1+Num1;break;
case '-':Num=num1-Num1;break;
case '*':Num=num1*Num1;break;
case '/':Num=num1*1.0/Num2;break;
}
if(j=='/')
{
printf("%2d %2c %2d %2c %2d = %2g\n",num1,j,num4,k,num5,Num);
}
else
{
printf("%2d %2c %2d %2c %2d = %2g\n",num1,j,num2,k,num4,Num);
}
}
}
}
2.2.5 Gitee上传截图与链接
https://gitee.com/qq1224311807/zhao20199314