C Implement arithmetic test

Source: Internet
Author: User

  

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.