#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<0||n>4) { printf("输入错误,请重新输入:\n"); scanf("%d",&n); } switch(n) { case 1: { printf("***************************************************************\n"); printf("5.一位数加一位数\n"); printf("6.一位数加两位数\n"); printf("7.两位数加两位数\n"); printf("***************************************************************\n"); printf("请输入题型:"); scanf("%d",&i); while(i<5||i>7) { printf("输入错误,请重新输入:\n"); scanf("%d",&i); } if(i==5) { srand((unsigned)time(NULL)); a=rand()%10+1; b=rand()%10+1; 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==6) { srand((unsigned)time(NULL)); a=rand()%10+1; b=rand()%100+1; 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==7) { srand((unsigned)time(NULL)); a=rand()%100+1; b=rand()%100+1; 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 2: { printf("***************************************************************\n"); printf("5.一位数减一位数\n"); printf("6.两位数减一位数\n"); printf("7.两位数减两位数\n"); printf("***************************************************************\n"); printf("请输入题型:"); scanf("%d",&i); while(i<5||i>7) { printf("输入错误,请重新输入:\n"); scanf("%d",&i); } if(i==5) { srand((unsigned)time(NULL)); c=rand()%10+1; d=rand()%10+1; 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==6) { srand((unsigned)time(NULL)); c=rand()%100+1; d=rand()%10+1; 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==7) { srand((unsigned)time(NULL)); c=rand()%100+1; d=rand()%100+1; 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 3: { printf("***************************************************************\n"); printf("5.一位数乘一位数\n"); printf("6.一位数乘两位数\n"); printf("7.两位数乘两位数\n"); printf("***************************************************************\n"); printf("请输入题型:"); scanf("%d",&i); while(i<5||i>7) { printf("输入错误,请重新输入:\n"); scanf("%d",&i); } if(i==5) { srand((unsigned)time(NULL)); e=rand()%10+1; f=rand()%10+1; 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==6) { srand((unsigned)time(NULL)); e=rand()%10+1; f=rand()%100+1; 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==7) { srand((unsigned)time(NULL)); e=rand()%100+1; f=rand()%100+1; 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 4: { printf("***************************************************************\n"); printf("5.一位数除于一位数\n"); printf("6.两位数除于一位数\n"); printf("7.两位数除于两位数\n"); printf("***************************************************************\n"); printf("请输入题型:"); scanf("%d",&i); while(i<5||i>7) { printf("输入错误,请重新输入:\n"); scanf("%d",&i); } if(i==5) { srand((unsigned)time(NULL)); g=rand()%10+1; h=rand()%10+1; 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==6) { srand((unsigned)time(NULL)); g=rand()%100+1; h=rand()%10+1; 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==7) { srand((unsigned)time(NULL)); g=rand()%100+1; h=rand()%100+1; 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 0: { printf("谢谢使用!再见!\n"); break; } } }
这次每次生成的题目都不同,也可以进行判断正误,但感觉代码过多重复冗杂0.0,不知如何缩进又不改变原本的功能0.0。