#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <math.h>
void math()
{
double dim = 0;
int num = 0;
int i = 0;
int right = 0 ;//
int wrong = 0 ;//
time_t t;
int choose = 0;
int op_1 = 0 ,op_2 = 0;
double result = 0;
double trueresult = 0;
srand((unsigned)time(&t));
printf("Enter the dimension:");
scanf("%lf" ,&dim);
//printf("%f\n",dim);
printf("Enter how many question you want to do:");
scanf("%d" ,&num);
//printf("%d\n",num);
for(i = 0 ; i < num ; i++)
{
choose = rand()%4;
switch(choose)
{
case 0://add
op_1 = rand()%(num+1);
op_2 = rand()%(num+1);
printf("%d + %d = " , op_1 , op_2);
scanf("%lf" , &result);
trueresult = (double)op_1 + op_2;
if( fabs(result-trueresult) < 0.01 )
{
right++;
printf("Congratulations ,you give the right answer!\n");
}
else
{
wrong++;
printf("Oh, it's so pity,your answer is wrong,but donot lose heart!\n");
}
break;
case 1://substract
op_1 = rand()%num;
op_2 = rand()%num;
if(op_1 > op_2)
{
printf("%d - %d = " , op_1 , op_2);
scanf("%lf" , &result);
trueresult =(double) op_1 - op_2;
if( fabs(result-trueresult) < 0.01 )
{
right++;
printf("Congratulations ,you give the right answer!\n");
}
else
{
wrong++;
printf("Oh, it's so pity,your answer is wrong,but donot lose heart!\n");
}
}
else
{
printf("%d - %d = " , op_2 , op_1);
scanf("%lf" , &result);
trueresult =(double)
op_2 - op_1;
if( fabs(result-trueresult) < 0.01 )
{
right++;
printf("Congratulations ,you give the right answer!\n");
}
else
{
wrong++;
printf("Oh, it's so pity,your answer is wrong,but donot lose heart!\n");
}
}
break;
case 2://mutiply
op_1 = rand()%num;
op_2 = rand()%num;
printf("%d * %d = " , op_1 , op_2);
scanf("%lf" , &result);
trueresult = (double) op_1 * op_2;
if( fabs(result-trueresult) < 0.01 )
{
right++;
printf("Congratulations ,you give the right answer!\n");
}
else
{
wrong++;
printf("Oh, it's so pity,your answer is wrong,but donot lose heart!\n");
}
break;
case 3:
op_1 = rand()%num;
op_2 = rand()%num;
while(op_2 == 0)
op_2 = rand()%num;
printf("%d / %d = " , op_1 , op_2);
scanf("%lf" , &result);
trueresult = (double)op_1 / op_2;
if( fabs(result-trueresult) < 0.01 )
{
right++;
printf("Congratulations ,you give the right answer!\n");
}
else
{
wrong++;
printf("Oh, it's so pity,your answer is wrong,but donot lose heart!\n");
}
break;
}
}
printf("Total question is:%d\n" , num);
printf("Given right answer is:%d\n" , right);
printf("Given worng answer is:%d\n" , wrong);
printf("Correct answer rate is:%f\n" , (double)right/num);
}
int main()
{
math();
return 0;
}
仅供参考!!!呵呵