Randomly generated arithmetic problems

Source: Internet
Author: User

/*
information: 20133075 Jang-hoon "randomly generated 30 arithmetic topics"

Requirements: To support the arithmetic of true fractions in addition to integers

Design ideas:
1. Generate a natural number through the Radom function and assign a value to the variable
2. Another variable is the true fraction, the molecular score mother Small
3. Randomly generated arithmetic characters
4. Note that the divisor is 0 cases
Note:
This is a two function! One is the Srand function! This is used before calling the rand () function!
Rand () is a function that produces a random number! And Srand is a function that sets the seed of a random number! Usually these two
A function is used together! To complete the function of generating random numbers! and time (NULL) The return value of this function is
For the parameters of the Srand function! This means that the current system time as a random number of seeds to generate random numbers! To
To NULL for this parameter. Only set to NULL to get the system time!
*/

#include <iostream>
#include <time.h>
#include <string>
using namespace Std;

int main ()
{
Generate random numbers with current system time as a seed of random numbers
Srand (Time (NULL));
Char X;

cout << "Please choose the difficulty of Arithmetic question: (1 or 2) \ n";
cout << "1. Simple (Pure integer operation) \ n" << "2. Difficulty (arithmetic with true score) \ n";
Cin >> X;

1. Simple (Pure integer operation)
if (X = = ' 1 ')
{

for (int count = 0; count < 30;count++)
{
int a = 0, b = 0;

A = rand ()% 100;
b = rand ()% 100;

Randomly generated arithmetic characters
int sign = 0;
Sign = (rand ()% 100)% 4;
String Opera_sign[4] = {"+", "-", "*", "/"};

Regenerate when B=0 and operation is division
while (b = = 0 & sign = = 3)
{
b = rand ()% 100;
}

Switch (sign)
{
Case 0:cout << a << opera_sign[sign] << b << "=" << Endl; Break
Case 1:cout << a << opera_sign[sign] << b << "=" << Endl; Break
Case 2:cout << a << opera_sign[sign] << b << "=" << Endl; Break
Case 3:cout << a << opera_sign[sign] << b << "=" << Endl; Break
}
}
}


2. Difficulty (arithmetic with true score)
if (X = = ' 2 ')
{
for (int count = 0; count <; count++)
{
int a1 = 0, B1 = 0, a2 = 0, b2 = 0;

A1 = rand ()% 100;
B1 = rand ()% 100;
A2 = rand ()% 100;
B2 = rand ()% 100;

Determine if the score is true
while (a1>b1| | b1==0)
{
A1 = rand ()% 100;
B1 = rand ()% 100;
}

while (a2>b2| | b2==0)
{
A2 = rand ()% 100;
B2 = rand ()% 100;
}

Randomly generated arithmetic characters
int sign = 0;
Sign = (rand ()% 100)% 4;
String Opera_sign[4] = {"+", "-", "*", "/"};

Switch (sign)
{
Case 0:cout << "(" << A1 << "/" << B1 << ")" << Opera_sign[sign] << "(" << A2 << "/" << B2 << ")" << "=" << Endl; Break
Case 1:cout << "(" << A1 << "/" << B1 << ")" << Opera_sign[sign] << "(" << A2 << "/" << B2 << ")" << "=" << Endl; Break
Case 2:cout << "(" << A1 << "/" << B1 << ")" << Opera_sign[sign] << "(" << A2 << "/" << B2 << ")" << "=" << Endl; Break
Case 3:cout << "(" << A1 << "/" << B1 << ")" << Opera_sign[sign] << "(" << A2 << "/" << B2 << ")" << "=" << Endl; Break
}
}
}

}

After you run the program, you will get the following prompt:

Simple selection, can produce:

Select a difficult level to produce a true fractional operation:

In this question, it is important to note that the use of the Srand (Time (NULL)) function ensures that the system produces a different result per run!

This is a two function! One is the Srand function! This is used before calling the rand () function!
Rand () is a function that produces a random number! And Srand is a function that sets the seed of a random number! Usually these two
A function is used together! To complete the function of generating random numbers! and time (NULL) The return value of this function is
For the parameters of the Srand function! This means that the current system time as a random number of seeds to generate random numbers! To
To NULL for this parameter. Only set to NULL to get the system time!
*/

Randomly generated arithmetic problems

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.