A design idea:
1. The first thing you can think of is the composition of a arithmetic formula: two operands and an operator;
2. The random function of two operands is generated by invoking random functions, in which the range of operands can be set;
3. The random generation of an operator can be divided into subtraction four cases, respectively, by generating four random numbers to determine which operator;
4. Finally, the two combine to complete the generation of stochastic formula;
Two program code:
#include"stdafx.h"#include"stdlib.h" //call one of the random functions#include"iostream.h"#include"time.h"intMainintargcChar*argv[]) {srand (unsigned) time (NULL));//make a randomly generated formula non-repetitive intK1,k2,n;//K1,k2 is a two random number, producing n formulas intCh//CH has four different casescout<<"Please enter the number of question:"; CIN>>N; for(intI=1; i<=n;i++) {K1=rand ()% -;//randomly generates a number within 100K2=rand ()% -; CH=rand ()%4;//randomly generates an operation symbol Switch(CH) { Case 0: cout<<k1<<"+"<<k2<<"="<<Endl; Break; Case 1: cout<<k1<<"-"<<k2<<"="<<Endl; Break; Case 2: cout<<k1<<"*"<<k2<<"="<<Endl; Break; Case 3: cout<<k1<<"/"<<k2<<"="<<Endl; Break; } } return 0;}
Three running results:
The number of formulas can be entered by the user themselves, here choose to produce 20:
Four classes not completed in time reasons:
1. First is the most basic random function of the header file if #include "stdlib.h", What I thought in class is "MATH.H".
2. Another is that the random generation of operational symbols has not been achieved, not just to randomly produce special symbols, you can convert it to the generation of random situations, that is, four of cases of production.
Writing a simple program with C + + to randomly produce multiple two-digit arithmetic formulas