#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void srand(unsigned);//随机生成不同的数
void main()
{
int n,i,a,b,c,d,e,f,g,h,j;
printf("****************************欢迎使用!************************\n");
printf("\n");
printf("1.加法运算\n");
printf("2.减法运算\n");
printf("3.乘法运算\n");
printf("4.除法运算\n");
printf("0.退出程序\n");
printf("***************************************************************\n");
printf("请选择运算类型:");
scanf("%d", &n);
while(n<||n>)
{
printf("输入错误,请重新输入:\n");
scanf("%d",&n);
}
switch(n)
{
case :
{
printf("***************************************************************\n");
printf("5.一位数加一位数\n");
printf("6.一位数加两位数\n");
printf("7.两位数加两位数\n");
printf("***************************************************************\n");
printf("请输入题型:");
scanf("%d",&i);
while(i<||i>)
{
printf("输入错误,请重新输入:\n");
scanf("%d",&i);
}
if(i==)
{
srand((unsigned)time(NULL));
a=rand()%+;
b=rand()%+;
c=a+b;
printf("%d+%d=\n",a,b);
printf("请输入答案:\n");
scanf("%d",&d);
while(d!=c)
{
printf("回答错误,请重做:\n");
printf("%d+%d=\n",a,b);
scanf("%d",&d);
}
printf("回答正确!\n");
}
if(i==)
{
srand((unsigned)time(NULL));
a=rand()%+;
b=rand()%+;
c=a+b;
printf("%d+%d=\n",a,b);
printf("请输入答案:\n");
scanf("%d",&d);
while(d!=c)
{
printf("回答错误,请重做:\n");
printf("%d+%d=\n",a,b);
scanf("%d",&d);
}
printf("回答正确!\n"); }
if(i==)
{
srand((unsigned)time(NULL));
a=rand()%+;
b=rand()%+;
c=a+b;
printf("%d+%d=\n",a,b);
printf("请输入答案:\n");
scanf("%d",&d);
while(d!=c)
{
printf("回答错误,请重做:\n");
printf("%d+%d=\n",a,b);
scanf("%d",&d);
}
printf("回答正确!\n");
} break;
} case :
{
printf("***************************************************************\n");
printf("5.一位数减一位数\n");
printf("6.两位数减一位数\n");
printf("7.两位数减两位数\n");
printf("***************************************************************\n");
printf("请输入题型:");
scanf("%d",&i);
while(i<||i>)
{
printf("输入错误,请重新输入:\n");
scanf("%d",&i);
}
if(i==)
{
srand((unsigned)time(NULL));
c=rand()%+;
d=rand()%+;
e=c-d;
printf("%d-%d=\n",c,d);
printf("请输入答案:\n");
scanf("%d",&g);
while(g!=e)
{
printf("回答错误,请重做:\n");
printf("%d-%d=\n",c,d);
scanf("%d",&g);
}
printf("回答正确!\n");
} if(i==)
{
srand((unsigned)time(NULL));
c=rand()%+;
d=rand()%+; e=c-d;
printf("%d-%d=\n",c,d);
printf("请输入答案:\n");
scanf("%d",&g);
while(g!=e)
{
printf("回答错误,请重做:\n");
printf("%d-%d=\n",c,d);
scanf("%d",&g);
}
printf("回答正确!\n");
}
if(i==)
{
srand((unsigned)time(NULL));
c=rand()%+;
d=rand()%+;
e=c-d;
printf("%d-%d=\n",c,d);
printf("请输入答案:\n");
scanf("%d",&g);
while(g!=e)
{
printf("回答错误,请重做:\n");
printf("%d-%d=\n",c,d);
scanf("%d",&g);
}
printf("回答正确!\n");
}
break;
} case :
{
printf("***************************************************************\n");
printf("5.一位数乘一位数\n");
printf("6.一位数乘两位数\n");
printf("7.两位数乘两位数\n");
printf("***************************************************************\n");
printf("请输入题型:");
scanf("%d",&i);
while(i<||i>)
{
printf("输入错误,请重新输入:\n");
scanf("%d",&i);
}
if(i==)
{
srand((unsigned)time(NULL));
e=rand()%+;
f=rand()%+;
g=e*f;
printf("%d*%d=\n",e,f);
printf("请输入答案:\n");
scanf("%d",&h);
while(h!=g)
{
printf("回答错误,请重做:\n");
printf("%d*%d=\n",e,f);
scanf("%d",&h);
}
printf("回答正确!\n");
}
if(i==)
{
srand((unsigned)time(NULL));
e=rand()%+;
f=rand()%+; g=e*f;
printf("%d*%d=\n",e,f);
printf("请输入答案:\n");
scanf("%d",&h);
while(h!=g)
{
printf("回答错误,请重做:\n");
printf("%d*%d=\n",e,f);
scanf("%d",&h);
}
printf("回答正确!\n");
}
if(i==)
{
srand((unsigned)time(NULL));
e=rand()%+;
f=rand()%+; g=e*f;
printf("%d*%d=\n",e,f);
printf("请输入答案:\n");
scanf("%d",&h);
while(h!=g)
{
printf("回答错误,请重做:\n");
printf("%d*%d=\n",e,f);
scanf("%d",&h);
}
printf("回答正确!\n");
}
break;
}
case :
{
printf("***************************************************************\n");
printf("5.一位数除于一位数\n");
printf("6.两位数除于一位数\n");
printf("7.两位数除于两位数\n");
printf("***************************************************************\n");
printf("请输入题型:");
scanf("%d",&i);
while(i<||i>)
{
printf("输入错误,请重新输入:\n");
scanf("%d",&i);
}
if(i==)
{
srand((unsigned)time(NULL));
g=rand()%+;
h=rand()%+;
i=g/h;
printf("%d/%d=\n",g,h); printf("请输入答案:\n");
scanf("%d",&j);
while(j!=i)
{
printf("回答错误,请重做:\n");
printf("%d/%d=\n",g,h);
scanf("%d",&j);
}
printf("回答正确!\n"); }
if(i==)
{
srand((unsigned)time(NULL));
g=rand()%+;
h=rand()%+; i=g/h;
printf("%d/%d=\n",g,h); printf("请输入答案:\n");
scanf("%d",&j);
while(j!=i)
{
printf("回答错误,请重做:\n");
printf("%d/%d=\n",g,h);
scanf("%d",&j);
}
printf("回答正确!\n");
}
if(i==)
{
srand((unsigned)time(NULL));
g=rand()%+;
h=rand()%+; i=g/h;
printf("%d/%d=\n",g,h); printf("请输入答案:\n");
scanf("%d",&j);
while(j!=i)
{
printf("回答错误,请重做:\n");
printf("%d/%d=\n",g,h);
scanf("%d",&j);
}
printf("回答正确!\n"); }
break;
}
case :
{
printf("谢谢使用!再见!\n");
break;
}
} }
这次每次生成的题目都不同,也可以进行判断正误,但感觉代码过多重复冗杂0.0,不知如何缩进又不改变原本的功能0.0。