Automatic Question Program (four arithmetic operations of numbers less than 10) and four arithmetic operations

Source: Internet
Author: User

Automatic Question Program (four arithmetic operations of numbers less than 10) and four arithmetic operations

The Code is as follows:

# Include <iostream> # include <cstdlib> # include <ctime> using namespace std; int main () {int num1, num2, op, result1, result2; // num1, num2: operand; op: operator; result1, result2: result do {srand (time (NULL); num1 = rand () * 10/(RAND_MAX + 1 ); num2 = rand () * 10/(RAND_MAX + 1); // generate the number of operations op = rand () * 4/(RAND_MAX + 1 ); // generate the operator 0 -- +, 1 ---, 2 -- *, 3 --/switch (op) {case 0: cout <num1 <"+" <num2 <"=? "; Cin> result1; if (num1 + num2 = result1) cout <" you are right "<endl; else cout <"you are wrong" <endl; break; case 1: cout <num1 <"-" <num2 <"=? "; Cin> result1; if (num1-num2 = result1) cout <" you are right "<endl; else cout <" you are wrong "<endl; break; case 2: cout <num1 <"*" <num2 <"=? "; Cin> result1; if (num1 * num2 = result1) cout <" you are right "<endl; else cout <"you are wrong" <endl; break; case 3: cout <num1 <"/" <num2 <"=? "; Cin> result1; cout <" remainder =? "; Cin> result2; if (num1/num2 = result1) & (num1% num2 = result2) cout <" you are right "<endl; else cout <"you are wrong" <endl; break ;}while (1); return 0 ;}

The library cstdlib contains a random number generation function. The first statement srand (time (NULL) in the loop of the Program sets the seed of the random number. The seed is required for the generation of the random number. Different seed can generate different random number sequences. If you do not set the seed of the random number, the system will specify one. The seed of the random number specified by the system for each program and each execution is the same, that is, in the program, each execution of the same question. At the same time, if the programmer sets the seed to a fixed value, the random number sequence each time the program executes will be the same. What if the seeds selected for each execution of the program are different? In a computer system, time is always changing. Therefore, setting the system time as a seed is a good idea. Time (NULL) is the current system time. To use the clock, the header file ctime must be included. ---- Weng huiyu's "C ++ program design thoughts and methods"

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.