Four operational procedures

Source: Internet
Author: User

For the generation of 30 random arithmetic, my first thought was this:

1, generate a random number.

2, control the generated random number within the appropriate range

3. Randomly select an operator

4. Randomly generate a second random number

5, control the generated random number within the appropriate range

6. Print output

This wrote a preliminary procedure:

Wang Wenchi Arithmetic

#include <iostream>

#include <string>

#include <iomanip>

#include <time.h>

using namespace Std;

void Main ()

{

int first, second,c,i;

String op;

Srand ((unsigned) time (NULL)); Make a Time seed

for (i = 0; i <; i++)

{

First = rand ()% 99; Generates a random integer within the first 0~99

c = 1 + rand ()% 4; Generates a random integer within 1~4

Switch (c)

{

Case 1:op = "+"; Break

Case 2:op = "-"; Break

Case 3:op = "x"; Break

Case 4:op = "÷"; Break

Default:break;

}

Second = rand ()% 99; Generates a random integer within the second 0~99

cout << SETW (3) << first << SETW (3) << op << setw (3) << second << SETW (3) <&L T "=" << Endl;

}

}

Picture 1 is the result of the run. It took about 30 minutes.

In order to achieve a true fractional operation, I divide it into 4 situations:

1, two numbers are integers;

2, the first is a true fraction, the second is an integer;

3, the first is an integer, the second is a true fraction;

4, two numbers are true scores.

True Fractional realization: Generate a random number of 0~99, generate a random number of 1~99, compare size, put small in front, large in the back, shape like: small/large.

In this way, the perfect program is written:

Wang Wenchi Arithmetic

#include <iostream>

#include <string>

#include <iomanip>

#include <time.h>

using namespace Std;

String oper (String op)

{

int C;

c = 1 + rand ()% 4; Generates a random integer within 1~4

Switch (c)

{

Case 1:op = "+"; Break

Case 2:op = "-"; Break

Case 3:op = "x"; Break

Case 4:op = "÷"; Break

Default:break;

}

return op;

}

void Number1 ()//two numbers are integers

{

String op;

int first, second;

First = rand ()% 99; Generates a random integer within the first 0~99

Second = rand ()% 99; Generates a random integer within the second 0~99

cout << SETW (3) << first << SETW (3) << oper (OP) << SETW (3) << second << SETW (3) << "=" << Endl;

}

void Number2 ()//The first is a true fraction, the second is an integer

{

String op;

int First1, first2, second,temp;

First1 = rand ()% 99; Generates a random integer within a 0~99

First2 = 1+rand ()% 99; Generates a random integer within a 1~99

if (FIRST1>FIRST2)

{

temp = First1;

First1 = First2;

First2 = temp;//Exchange, guaranteed First1<first2, and first2 not equal to 0

}

Second = rand ()% 99; Generates a random integer within the second 0~99

cout << SETW (3) << first1<< "/" <<first2 << SETW (3) << oper (OP) << SETW (3) <&L T Second << SETW (3) << "=" << Endl;

}

void Number3 ()//First is an integer, the second is a true fraction

{

String op;

int first, SECOND1, Second2, temp;

First = rand ()% 99; Generates a random integer within the first 0~99

Second1 = rand ()% 99; Generates a random integer within a 0~99

Second2 = 1 + rand ()% 99; Generates a random integer within a 1~99

if (SECOND1>SECOND2)

{

temp = Second1;

Second1 = Second2;

Second2 = temp;//Exchange, guaranteed Second1<second2, and second2 not equal to 0

}

cout << SETW (3) << first << SETW (3) << oper (OP) << SETW (3) << second1 << "/" < ;< second2 << SETW (3) << "=" << Endl;

}

void Number4 ()//two numbers are true fractions

{

String op;

int First1, First2, Second1, Second2, temp;

First1 = rand ()% 99; Generates a random integer within a 0~99

FIRST2 = 1 + rand ()% 99; Generates a random integer within a 1~99

Second1 = rand ()% 99; Generates a random integer within a 0~99

Second2 = 1 + rand ()% 99; Generates a random integer within a 1~99

if (FIRST1>FIRST2)

{

temp = First1;

First1 = First2;

First2 = temp;//Exchange, guaranteed First1<first2, and first2 not equal to 0

}

if (SECOND1>SECOND2)

{

temp = Second1;

Second1 = Second2;

Second2 = temp;//Exchange, guaranteed Second1<second2, and second2 not equal to 0

}

cout << SETW (3) << first1 << "/" << first2 << setw (3) << oper (OP) << SETW (3) < ;< second1 << "/" << Second2 << setw (3) << "=" << Endl;

}

void Main ()

{

int z,i;

Srand ((unsigned) time (NULL)); Make a Time seed

for (i = 0; i <; i++)

{

z = 1 + rand ()% 4; Generates a random integer within 1~4

cout << "section" << SETW (2) <<i + 1 << "title:";

Switch (z)

{

Case 1:number1 (); Break

Case 2:number2 (); Break

Case 3:number3 (); Break

Case 4:number4 (); Break

Default:break;

}

}

}

After the picture is complete, the result is running.

This process took about 40 minutes.

Four operational procedures

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.