Design ideas: Using the rand () function to generate random numbers, according to the modulo operation to control the integer range, subtraction also through the random number modulo operation to achieve the random appearance function.
The following features are implemented:
An integer that is within 100 of the operand;
The subtraction operation appears randomly;
The subtraction operation does not show negative numbers;
Can judge the correctness in time;
Record answers to the correct number of questions.
The source code is as follows:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main ()
{
int tiliang,rightnumber=0;
int X,y,z,temp,result;
printf ("Number of input tests:");
scanf ("%d", &tiliang);
Srand ((unsigned) time (NULL)); Set seed
for (int i=0;i<tiliang;i++)//number of questions cycles
{
X=rand ()%100;
Y=rand ()%100;
Z=rand ()%4; Randomly appearing according to the value of Z arithmetic
if (z==0)
{
printf ("%d+%d=", X, y);
scanf ("%d", &result);
if (result==x+y)
{
printf ("Bad,!\n");
rightnumber++;
}
Else
printf ("Stupid, Wrong!\n");
}
if (z==1)
{
printf ("%d÷%d=", X, y);
scanf ("%d", &result);
if (result==x/y)
{
printf ("Bad,!\n");
rightnumber++;
}
Else
printf ("Stupid, Wrong!\n");
}
if (z==2)
{
if (x<y)
{
Temp=x;
X=y;
Y=temp;
}
printf ("%d-%d=", X, y);
scanf ("%d", &result);
if (result==x-y)
{
printf ("Bad,!\n");
rightnumber++;
}
Else
printf ("Stupid, Wrong!\n");
}
if (z==3)
{
printf ("%d*%d=", X, y);
scanf ("%d", &result);
if (result==x*y)
{
printf ("Bad,!\n");
rightnumber++;
}
Else
printf ("Stupid, Wrong!\n");
}
}
printf ("\ n This test did the right%d \ n", rightnumber);
return 0;
}
The results of the operation are as follows:
C Implement arithmetic test