四则运算升级版
#include <stdlib.h> #include <stdio.h> #include<time.h> int main(void) { int n=0,i,d=0,q,w,k,l,c=0,e,r,j; float cout=0; char *a[6]={"+","-","*","/","(",")"}; char b; printf("1.随机生成\n2.退出\n"); do{ do{ scanf("%d",&j);getchar(); if((j>2)||(j<1)) printf("输入错误,请重新输入:"); }while((j>2)||(j<1)); switch(j){ case 1: printf("开始生成\n"); printf("产生题数:"); do{ scanf("%d",&n);getchar(); if(n==0) printf("输入错误,请重新输入:"); }while(n==0); printf("输入控制数字范围:"); do{ scanf("%d",&c);getchar(); if(c==0) printf("输入错误,请重新输入:"); }while(c==0); c++; cout=0.0; srand(time(NULL)); for(d=0;d<n;d++){ i=rand() % 8; q=rand() % c; w=rand() % c; switch(i) { case 1: printf("%d+%d=",q,w); scanf("%d",&k); if(k==(q+w)){ printf("恭喜回答正确! 得%d分!\n",100/n); cout+=(100/n); } else printf("回答错误,继续加油!\n"); break; case 2: printf("%d-%d=\n",q,w); scanf("%d",&k); if(k==(q-w)){ printf("恭喜回答正确! 得%d分!\n",100/n); cout+=(100/n); } else printf("回答错误,继续加油!\n"); break; case 3: printf("%d*%d=\n",q,w); scanf("%d",&k); if(k==(q*w)){ printf("恭喜回答正确! 得%d分!\n",100/n); cout+=(100/n); } else printf("回答错误,继续加油!\n"); break; case 4: do{ if(w==0) w=rand() % c;}while(w==0); printf("%d/%d=\n",q,w); printf("1.答案为整数\n2.答案为分数\n"); scanf("%d",&k); switch(k){ case 1: scanf("%d",&k); if(k==(q/w)){ printf("恭喜回答正确! 得%d分!\n",100/n); cout+=(100/n); } else printf("回答错误,继续加油!\n"); break; case 2: scanf("%d",&k); scanf("%c",&b); getchar(); scanf("%d",&l); if((k/l)==(q/w)){ printf("恭喜回答正确! 得%d分!\n",100/n); cout+=(100/n); } else printf("回答错误,继续加油!\n"); break; default: printf("默认整数\n"); scanf("%d",&k); if(k==(q/w)){ printf("恭喜回答正确! 得%d分!\n",100/n); cout+=(100/n); } else printf("回答错误,继续加油!\n"); break; } break; case 5: e=rand() % c; r=rand() % c; do{ if(w==0||r==0) w=rand() % c; r=rand() % c; }while(w==0||r==0); printf("(%d/%d)+(%d/%d)=\n",q,w,e,r); printf("1.答案为整数\n2.答案为分数\n"); scanf("%d",&k); switch(k){ case 1: scanf("%d",&k); if(k==((q/w)+(e/r))){ printf("恭喜回答正确! 得%d分!\n",100/n); cout+=(100/n); } else printf("回答错误,继续加油!\n"); break; case 2: scanf("%d",&k); scanf("%c",&b); getchar(); scanf("%d",&l); if((k/l)==((q/w)+(e/r))){ printf("恭喜回答正确! 得%d分!\n",100/n); cout+=(100/n); } else printf("回答错误,继续加油!\n"); break; default: printf("默认整数\n"); scanf("%d",&k); if(k==((q/w)+(e/r))){ printf("恭喜回答正确! 得%d分!\n",100/n); cout+=(100/n); } else printf("回答错误,继续加油!\n"); break; } break; case 6: e=rand() % c; r=rand() % c; do{ if(w==0||r==0) w=rand() % c; r=rand() % c; }while(w==0||r==0); printf("(%d/%d)-(%d/%d)=\n",q,w,e,r); printf("1.答案为整数\n2.答案为分数\n"); scanf("%d",&k); switch(k){ case 1: scanf("%d",&k); if(k==((q/w)-(e/r))){ printf("恭喜回答正确! 得%d分!\n",100/n); cout+=(100/n); } else printf("回答错误,继续加油!\n"); break; case 2: scanf("%d",&k); scanf("%c",&b); getchar(); scanf("%d",&l); if((k/l)==((q/w)-(e/r))){ printf("恭喜回答正确! 得%d分!\n",100/n); cout+=(100/n); } else printf("回答错误,继续加油!\n"); break; default: printf("默认整数\n"); scanf("%d",&k); if(k==((q/w)-(e/r))){ printf("恭喜回答正确! 得%d分!\n",100/n); cout+=(100/n); } else printf("回答错误,继续加油!\n"); break; } break; case 7: e=rand() % c; r=rand() % c; do{ if(w==0||r==0) w=rand() % c; r=rand() % c; }while(w==0||r==0); printf("(%d/%d)*(%d/%d)=\n",q,w,e,r); printf("1.答案为整数\n2.答案为分数\n"); scanf("%d",&k); switch(k){ case 1: scanf("%d",&k); if(k==((q/w)*(e/r))){ printf("恭喜回答正确! 得%d分!\n",100/n); cout+=(100/n); } else printf("回答错误,继续加油!\n"); break; case 2: scanf("%d",&k); scanf("%c",&b); getchar(); scanf("%d",&l); if((k/l)==((q/w)*(e/r))){ printf("恭喜回答正确! 得%d分!\n",100/n); cout+=(100/n); } else printf("回答错误,继续加油!\n"); break; default: printf("默认整数\n"); scanf("%d",&k); if(k==((q/w)*(e/r))){ printf("恭喜回答正确! 得%d分!\n",100/n); cout+=(100/n); } else printf("回答错误,继续加油!\n"); break; } break; case 0: e=rand() % c; r=rand() % c; do{ if(w==0||r==0) w=rand() % c; r=rand() % c; }while(w==0||r==0); printf("(%d/%d)/(%d/%d)=\n",q,w,e,r); printf("1.答案为整数\n2.答案为分数\n"); scanf("%d",&k); switch(k){ case 1: scanf("%d",&k); if(k==((q/w)/(e/r))){ printf("恭喜回答正确! 得%d分!\n",100/n); cout+=(100/n); } else printf("回答错误,继续加油!\n"); break; case 2: scanf("%d",&k); scanf("%c",&b); getchar(); scanf("%d",&l); if((k/l)==((q/w)/(e/r))){ printf("恭喜回答正确! 得%d分!\n",100/n); cout+=(100/n); } else printf("回答错误,继续加油!\n"); break; default: printf("默认整数\n"); scanf("%d",&k); if(k==((q/w)/(e/r))){ printf("恭喜回答正确! 得%d分!\n",100/n); cout+=(100/n); } else printf("回答错误,继续加油!\n"); break; } break; } } printf("总成绩%0.1f!\n",cout); j=-1; printf("\n1.随机生成\n2.退出\n"); break; case 2: exit(0); break; default: printf("输入错误!请重新输入\n"); break; }}while(j==-1); return 0; }