Randomly generate 30 arithmetic topics

Source: Internet
Author: User

1. Title: Randomly generate 30 to arithmetic questions, which cannot exceed 2-bit operations, including true fractions.

2. Design Ideas

(1) The first four numbers are randomly generated, the first two numbers are used as the arithmetic of integers, and then the first two numbers are then used as the second score, and the next two numbers as the number two.

(2) Each case is represented by the Switch\case function, and the remainder by dividing by 7 represents a different case in 8, the first four represents an integer subtraction operation, and the last four subtraction operations that represent fractions.

(3) Generate 30 expressions with a for loop.

3. Program code:

2016/3/3 Wang Zongze #include<iostream> #include <cstdlib> #include <ctime>using namespace Std;int main () { Srand ((unsigned) time (NULL)), int i;for (i=0;i<30;i++) {int Firstnum=rand ()%100;int Secondnum=rand ()%100;int Thirdnum=rand ()%100;int Forthnum=rand ()%100;switch (int (firstnum%7)) {case 0:cout<<firstnum<< "+" < <secondnum<< "=" <<endl; Break;case 1:cout<<firstnum<< "-" <<secondnum<< "=" <<endl; Break;case 2:cout<<firstnum<< "*" <<secondnum<< "=" <<endl; Break;case 3:cout<<firstnum<< "/" <<secondnum<< "=" <<endl; Break;case 4:if ((firstnum>secondnum) | | (Thirdnum>forthnum)) {i=i-1;} else cout<< "(" <<firstnum<< "/" <<secondnum<< ")" << "+" << "(" << thirdnum<< "/" <<forthnum<< ")" << "=" <<endl; Break;case 5:if ((firstnum>secondnum) | | (Thirdnum>forthnum)) {i=i-1;} else cout<< "(" <<firstnum<< "/"<<secondnum<<") "<<"-"<<" ("<<thirdnum<<"/"<<forthnum<<") "< < "=" <<endl; Break;case 6:if ((firstnum>secondnum) | | (Thirdnum>forthnum))   {i=i-1;} else cout<< "(" <<firstnum<< "/" <<secondnum<< ")" << "*" << "(" << thirdnum<< "/" <<forthnum<< ")" << "=" <<endl; Break;case 7:if ((firstnum>secondnum) | | (Thirdnum>forthnum))  {i=i-1; }else cout<< "(" <<firstnum<< "/" <<secondnum<< ")" << "/" << "(" << thirdnum<< "/" <<forthnum<< ")" << "=" <<endl;        Break }}}

4. Program Operation Result:

Randomly generate 30 arithmetic topics

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.