Software Engineering Experiment one--programming randomly generates 30 arithmetic, arithmetic includes integer and true fraction within 100.

Source: Internet
Author: User

Software Engineering Experiment One:

--programming randomly generates 30 arithmetic, and the arithmetic includes integers and true fractions within 100.

Experimental ideas: The rand () function is used to randomly generate two arithmetic, including 100 within the integer and true fraction, to generate an integer within 100, with Rand ()%100, in order to avoid the division operation of the divisor cannot be zero, so the operator to the right of the integer directly with 1+rand ()% 99; When I randomly generate a true score, my husband becomes two integers to act as numerator and denominator, and the same denominator cannot be zero, with 1+rand ()%99. Subtraction arithmetic has an operation between integers and integers, an operation between fractions and fractions, an operation between integers and fractions, an operation between fractions and integers, a total of 16 cases, a random integer is generated, then divided by 16 to take the remainder, the remainder has 16 cases, as the event in the switch statement, To randomly generate a arithmetic, and then generate 30 arithmetic with a for loop.

#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace Std;
void Main ()
{
int num_1,num_2,i,j,a,b; Define two integer variable num_1,num_2, do 100 or less integer arithmetic at arithmetic, define i,a As Integer, do the numerator of true fraction, define j,b As Integer, and do the denominator of true fraction.
int x, y; Defines an integral type X,y,x as an event in a siwtch,case statement, and y generates a random number randomly.
Srand ((unsigned) time (NULL));//Use the Srand function to set up random seeds, so that the seed is related to the current period and avoids the same random number generated each time.
for (int k=0;k<30;k++)//apply for loop, total 30 cycles, generate 30 random operations.
{
Num_1=rand ()%100; Use the rand () function to randomly generate integers within 100.
Num_2=1+rand ()%99; Use the rand () function to generate integers from 1 to 100, as a second operand, to avoid the occurrence of 0 in a division operation.
I=rand ()%100; A molecule used to randomly generate the first true fraction.
J=1+rand ()%99; The denominator used to randomly generate the first true fraction.
A=rand ()%100; A molecule used to randomly generate a second true fraction.
B=1+rand ()%99; The denominator used to randomly generate a second true fraction.
Y=1+rand (); Randomly generates an integer divided by 15 to take the remainder
x=y%16; Assigns the obtained remainder to X, the arithmetic between integers and integers, the arithmetic between fractions, the arithmetic between integers and fractions, and the arithmetic between fractions and integers
switch (x)//Use the Siwtch,case statement for conditional judgment, randomly generated.
{
Case 0:cout<<num_1<< "+" <<Num_2<< "=" <<endl;break;//case0 to Case3 is a arithmetic between integers and integers
Case 1:cout<<num_1<< "-" <<Num_2<< "=" <<endl;break;
Case 2:cout<<num_1<< "*" <<Num_2<< "=" <<endl;break;
Case 3:cout<<num_1<< "/" <<Num_2<< "=" <<endl;break;
Case 4:if (i<j)//case4 to Case7 is the arithmetic between fractions and fractions
{
if (a<b)
{cout<< "(" <<i<< "/" <<j<< ")" << "+" << "(" <<a<< "/" <<b << ")" << "=" &LT;&LT;ENDL;}
Else
{cout<< "(" <<i<< "/" <<j<< ")" << "+" << "(" <<b<< "/" <<a << ")" << "=" &LT;&LT;ENDL;}
}
Else
{
cout<< "(" <<j<< "/" <<i<< ")" << "+" << "(" <<a<< "/" <<b< < ")" << "=" <<endl;
}
Break
Case 5:if (I&LT;J)
{
if (a<b)
{cout<< "(" <<i<< "/" <<j<< ")" << "-" << "(" <<a<< "/" <<b << ")" << "=" &LT;&LT;ENDL;}
Else
{cout<< "(" <<i<< "/" <<j<< ")" << "-" << "(" <<b<< "/" <<a << ")" << "=" &LT;&LT;ENDL;}
}
Else
{
cout<< "(" <<j<< "/" <<i<< ")" << "-" << "(" <<a<< "/" <<b< < ")" << "=" <<endl;
}
Break
Case 6:if (I&LT;J)
{
if (a<b)
{cout<< "(" <<i<< "/" <<j<< ")" << "*" << "(" <<a<< "/" <<b << ")" << "=" &LT;&LT;ENDL;}
Else
{cout<< "(" <<i<< "/" <<j<< ")" << "*" << "(" <<b<< "/" <<a << ")" << "=" &LT;&LT;ENDL;}

}
Else
{
cout<< "(" <<j<< "/" <<i<< ")" << "*" << "(" <<a<< "/" <<b< < ")" << "=" <<endl;
}
Break
Case 7:if (I&LT;J)
{
if (a<b)
{cout<< "(" <<i<< "/" <<j<< ")" << "/" << "(" <<a<< "/" <<b << ")" << "=" &LT;&LT;ENDL;}
Else
{cout<< "(" <<i<< "/" <<j<< ")" << "/" << "(" <<b<< "/" <<a << ")" << "=" &LT;&LT;ENDL;}

}
Else
{
cout<< "(" <<j<< "/" <<i<< ")" << "/" << "(" <<a<< "/" <<b< < ")" << "=" <<endl;
}
Break
Case 8:if (i<j)//case8 to Case11 is the arithmetic between fractions and integers
cout<<i<< "/" <<j<< "+" <<Num_2<< "=" <<endl;
Else
cout<<j<< "/" <<i<< "+" <<Num_2<< "=" <<endl;
Break
Case 9:if (I&LT;J)
cout<<i<< "/" <<j<< "-" <<Num_2<< "=" <<endl;
Else
cout<<j<< "/" <<i<< "-" <<Num_2<< "=" <<endl;
Break
Case 10:if (I&LT;J)
cout<<i<< "/" <<j<< "*" <<Num_2<< "=" <<endl;
Else
cout<<i<< "/" <<j<< "*" <<Num_2<< "=" <<endl;
Break
Case 11:if (I&LT;J)
cout<<i<< "/" <<j<< "/" <<Num_2<< "=" <<endl;
Else
cout<<i<< "/" <<j<< "/" <<Num_2<< "=" <<endl;
Break
Case 12:if (a<b)//case12 to Case15 is the arithmetic between fractions and integers
cout<<num_1<< "+" << "(" <<a<< "/" <<b<< ")" << "=" <<endl;
Else
cout<<num_1<< "+" << "(" <<b<< "/" <<a<< ")" << "=" <<endl;
Break
Case 13:if (A&LT;B)
cout<<num_1<< "-" << "(" <<a<< "/" <<b<< ")" << "=" <<endl;
Else
cout<<num_1<< "-" << "(" <<b<< "/" <<a<< ")" << "=" <<endl;
Break
Case 14:if (A&LT;B)
cout<<num_1<< "*" << "(" <<a<< "/" <<b<< ")" << "=" <<endl;
Else
cout<<num_1<< "*" << "(" <<b<< "/" <<a<< ")" << "=" <<endl;
Break
Case 15:if (A&LT;B)
cout<<num_1<< "/" << "(" <<a<< "/" <<b<< ")" << "=" <<endl;
Else
cout<<num_1<< "/" << "(" <<b<< "/" <<a<< ")" << "=" <<endl;
Break
}
}
}

Software Engineering Experiment one--programming randomly generates 30 arithmetic, arithmetic includes integer and true fraction within 100.

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.