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"