二柱子高階隨機運算(帶真分數)
#include<stdio.h>#include<stdlib.h>#include<time.h>int main(){ int x,y,z,c,t,f,m,n,q,result; srand((unsigned)time( NULL ) ); //初始化隨機數發生器,使得每次執行生成的隨機數不同 printf("*****二柱子的計算題*****\n"); printf("請輸入輸出的題的道數:"); scanf("%d",&q); printf("********************\n1.整數四則運算!\n2.真分數的四則運算!\n********************\n"); scanf("%d",&f); for(int i=0;i<q;i++) switch(f) {case 1: { x=rand()%100; y=rand()%100; z=rand()%4; switch(z) { case 0: printf("%d+%d=\n",x,y); result=x+y; break; case 1: if(x<y) { t=x; x=y; y=t; } printf("%d-%d=\n",x,y); result=x-y; break; case 2: printf("%d×%d=\n",x,y); result=x*y; break; case 3: if(y!=0) { printf("%d÷%d=\n",x,y); result=x/y; } else { i=i-1; } break; default: printf("超出測試範圍\n"); break; }scanf("%d", &c); //輸入答案if(c == result) //與正確答案一致{ printf("Right\n\n"); } else { printf("Wrong\n\n"); //錯} };break; case 2: { x=rand()%100; y=rand()%100; m=rand()%100; n=rand()%100; z=rand()%4; if(y!=0&&n!=0) {if(x<y) { t=x; x=y; y=t; } if(m<n) { t=m; m=n; n=t; } switch(z) { case 0: printf("%d/%d+%d/%d=\n",x,y,m,n); result=x/y+m/n; break; case 1: printf("%d/%d-%d/%d=\n",x,y,m,n); result=x/y-m/n; break; case 2: printf("%d/%d×%d/%d=\n",x,y,m,n); result=(x/y)*(m/n); break; case 3: printf("%d/%d÷%d/%d=\n",x,y,m,n); result=(x/y)/(m/n); break; default: printf("超出測試範圍\n");}} else printf("分母為0不是分數!"); scanf("%d", &c); //輸入答案if(c == result) //與正確答案一致{ printf("Right\n\n"); } else { printf("Wrong\n\n"); //錯} };break; printf("endl.\n"); return 0;} }