Automatic generation of 30 primary arithmetic topics, requiring: In addition to integers, to support the arithmetic of true fractions
Refinement steps:
1, to get the first operand, the system produces a random number (the range includes integers and true fractions).
2, randomly get an operator
(1) using 0,1,2,3 respectively to represent + 、-、 *,/arithmetic
(2) The system produces a random integer
(3) Divide this integer by 4 to take the remainder
(4) The remainder has four possible 0,1,2,3, thus obtaining the corresponding operator
3, get the second operand, the system produces a random number (the range includes integers and true fractions).
4, loop output 30 times arithmetic expression, set Srand ((unsigned) time (NULL)) temporal seed so that the program gets different results each time.
//Euna Lee 2015.3.4//arithmetic#include <iostream>#include<string>#include<stdlib.h>#include<time.h>using namespacestd;voidMain () {srand (unsigned) time (NULL));//set the time seed so that the program gets different results each time for(inti =0; I < -; i++) { intA,b,c; intfenmu_1, fenmu_2; //Generate first operandA = (rand ()% ( -))+1 ; if(A = = -) {a= (rand ()% About)+1; Fenmu_1= A + (rand ()% ( About-a+1)); cout<<"("<< a <<"/"<< fenmu_1 <<")"; } Elsecout <<A; //randomly generated operators Charo; C= (rand ()% -)%4; if(c = =0) O ='+'; Else if(c = =1) O ='-'; Else if(c = =2) O ='*'; Elseo ='/'; cout<<o; //generate the second operandb =1+rand ()% ( -); if(b = = -) {b= (rand ()% About)+1; Fenmu_2= B + (rand ()% ( About-b+1)); cout<<"("<< b <<"/"<< fenmu_2 <<")"; } Elsecout <<b; cout<<"="; cout<<Endl; }}
By writing this small program, I understand that the problem refinement has a very important significance, and then encounter complex situation, I will refine the problem, a little to solve.
Randomly generate 30-channel arithmetic