源程序:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void main()
{
int a,b,c,d;
int total1=0,total2=0;
while(1)
{
srand(time(NULL));
d=a=rand()%10;
b=(rand()%a+a)%10;
if(a%4==1&&a<b)c=a,a=b,b=c;
if(b==0&&a%4==3)b=rand()%10+1;
if(a%4==3&&a%b!=0)
{
srand(time(0));
a=b*(rand()%a);
}
switch(d%4)
{
case 0:printf("%d + %d = ",a,b);break;
case 1:printf("%d - %d = ",a,b);break;
case 2:printf("%d × %d = ",a,b);break;
case 3:printf("%d ÷ %d = ",a,b);break;
}
scanf("%d",&c);
switch(d%4)
{
case 0:if(c==a+b)
{
printf("正確\n");total1++;
}
else
{
printf("錯誤\n");total2++;
}
break;
case 1:if(c==a-b)
{
printf("正確\n");total1++;
}
else
{
printf("錯誤\n");total2++;
}
break;
case 2:if(c==a*b)
{
printf("正確\n");total1++;
}
else
{
printf("錯誤\n");total2++;
}
break;
case 3:if(c==a/b)
{
printf("正確\n");total1++;
}
else
{
printf("錯誤\n");total2++;
}
break;
}
printf("按任意鍵繼續,Q退出!\n");
fflush(stdin);
if(getchar()=='Q')break;
}
printf("你答對了%d道題,答錯%d道題\n",total1,total2);
}
感想:本程序只實現了加、減、乘、除四個基本運算,像括號啊,分數啊,小數點這些都沒搞定,雖然程序完成的不是很好,但起碼基本的功能是有的!
文章列表