After-school experiment arithmetic

Source: Internet
Author: User

First, Function introduction

1. Can achieve any two integers within 100 and the true fraction of the mixed arithmetic, random number with time changes do not repeat, and take into account the meiosis is greater than the meiosis and the divisor can not be zero, you can automatically output 20 different formulas.

Second, the idea of programming

1. First write the random number function
2. Generate two random numbers, call a random number function, a true fraction and an integer, and use a random number to take the remainder to combine the two random numbers.
3. Using the random number of the remainder to implement the selection of operators, where division to ensure that the divisor is not zero, subtraction to ensure that the minuend greater than the meiosis, using the corresponding operator and two random numbers of the formula to calculate the results.
4. Use the loop to achieve the output of 20 arithmetic formulas.

Third, the experimental code

Haoying 2016.3.3 Letter 1301-2 class 20132919
Arithmetic Small Program
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace Std;
int main ()
{
Double n[4];
int edg,choose,i,next;
Char Mark;
Srand ((int) time (NULL));//Transform random number
for (next=0;next<20;next++)//Set 20 lanes per day arithmetic
{
for (i=0;i<2;i++)
{
Edg= (rand ()%100)%2;
if (edg==0)
N[i]=rand ()%100;//random number range "0,100"
Else
N[i]=rand ()%100/100.0;//random true fraction
}
Choose= (rand ()%100)%4;
if (choose==0)
{
N[2]=N[0]+N[1];
mark= ' + ';
}
else if (choose==1)
{
if (N[0]>n[1])
{
N[2]=N[0]-N[1];
mark= '-';
}
Else
{
N[3]=N[0];
N[0]=N[1];
n[1]=n[3];//make the meiosis greater than the meiosis
N[2]=N[0]-N[1];
mark= '-';
}
}
else if (choose==2)
{
N[2]=N[0]*N[1];
mark= ' * ';
}
else if (choose==3)
{
while (n[1]==0)//guaranteed divisor greater than 0
{
Edg= (rand ()%100)%2;
if (edg==0)
N[1]=rand ()%100;//random number range "0,100"
Else
N[1]=rand ()%100/100.0;//random true fraction
}
N[2]=N[0]/N[1];
mark= '/';
}
cout<<n[0]<<mark<<n[1]<< "=" <<n[2]<<endl;//Display the formula
}
return 0;
}

Iv. Results of operation

After-school experiment arithmetic

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.