Four arithmetic question generator (C ++) and four arithmetic question Generator

Source: Internet
Author: User

Four arithmetic question generator (C ++) and four arithmetic question Generator

Question: Four arithmetic question generator.

Idea: Use the rand () % 100 function to generate a random positive integer of less than 100. Likewise, use the random function rand () % 4 to generate random 0, 1, 2 and 3 are random integers corresponding to the four conditions of addition, subtraction, multiplication, and division. This program numbers the question and can customize the number of questions to automatically remove the illegal situation where the divisor is 0.

Code:

# Include <iostream. h>
# Include <stdlib. h>
# Include <time. h>
Int num (int x)
{
Return rand () % 100;
}
Int fh ()
{
Return rand () % 4;
}
Int main ()
{
Int a, I, j, n;
Cout <"~~~~~~~~~~~~~~~~~~ "<" Please leave your homework rationally! "<"~~~~~~~~~~~~~~~~~ "<" \ N ";
Cout <"Enter the number of questions :";
Cin>;
Srand (time (NULL ));
While (1)
{
If (a <1)
{
Cout <"incorrect input. Please enter again :";
Cin>;
}
Else
{
For (j = 0; j <a; j ++)
{
I = fh ();
Switch (I)
{
Case 0:
Cout <j + 1 <":" <"" <num (1) <"+" <num (2) <"=" <"\ n ";
Break;
Case 1:
Cout <j + 1 <":" <"" <num (1) <"-" <num (2) <"=" <"\ n ";
Break;
Case 2:
Cout <j + 1 <":" <"" <num (1) <"*" <num (2) <"=" <"\ n ";
Break;
Case 3:
N = num (2 );
If (n! = 0)
{
Cout <j + 1 <":" <"" <num (1) <"/" <n <"=" <"\ n ";
}
Else
{
J --;
}
Break;
}
}
Break;
}
}
Return 0;
}

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.