A design idea
1. In order to set a number of controllable parameters, these parameters can be formed into an array, that is, a parameter set, and then each parameter is set by the situation;
2. The array includes the number of topics, multiplication control, numerical range, fractional control, positive and negative control, print a total of 6 elements;
3. For control variables, can be set to 1 and 0 control;
Two program code
#include"stdafx.h"#include"stdlib.h" //call one of the random functions#include"iostream.h"#include"time.h"intMainintargcChar*argv[]) { inta[6],ch;//define an array to hold the controllable parameters inttemp; Srand ((unsigned) time (NULL)); //so that the randomly generated formula does not repeat every time intK1,K2;//K1,k2 is a two random number, producing n formulascout<<"Please enter the number of question:"<<Endl; CIN>>a[0]; cout<<"Please enter whether there is a multiplication (->1; No->0):"<<Endl; CIN>>a[1]; cout<<"Please enter a range of values:"; CIN>>a[2]; cout<<"Please enter whether to select a score (with->1; No->0):"<<Endl; CIN>>a[3]; cout<<"Please enter the result if there is a negative number (->1; No->0):"<<Endl; CIN>>a[4]; cout<<"Please enter a few columns:"<<Endl; CIN>>a[5]; for(intI=1; i<=a[0];i++)//the user decides how many questions to print a[0] { if(a[3]==0) {K1=rand ()% (a[2]);//randomly generated a[2], a[2] is a range of values entered by the userK2=rand ()% (a[2]); if(a[4]==0)//control result has no negative number, if subtract negative, then exchange two numbers { if(k1<K2) {Temp=K1; K1=K2; K2=temp; } } ElseK1=rand ()% (a[2]); K2=rand ()% (a[2]); if(a[1]==1){//a[1] to control multiplication parametersCh=rand ()%4;//CH has four kinds of cases, subtraction } ElseCH=rand ()%2;//CH has two cases, plus minus, no multiplication if((i+1)%a[5]==0)//A[5] is the number of printed columns, and the remainder of the number divided by the column count determines its position.{cout<<Endl; } Switch(CH) { Case 0: cout<<k1<<"+"<<k2<<"="<<"\t\t"; Break; Case 1: cout<<k1<<"-"<<k2<<"="<<"\t\t"; Break; Case 2: cout<<k1<<"*"<<k2<<"="<<"\t\t"; Break; Case 3: cout<<k1<<"/"<<k2<<"="<<"\t\t"; Break; } } Else{ intN1,n2,n3,n4; N1=rand ()% -;//randomly generates a number within 20N2=rand ()% -; N3=rand ()% -; N4=rand ()% -; if(a[1]==1) {ch=rand ()%4; } ElseCH=rand ()%2; //randomly generates an operation symbol if((i+1)%a[5]==0) {cout<<Endl; } Switch(CH)//Output Score { Case 0: cout<<"("<<n1<<"/"<<n2<<")+("<<n3<<"/"<<n4<<")="<<"\t\t"; Break; Case 1: cout<<"("<<n1<<"/"<<n2<<")-("<<n3<<"/"<<n4<<")="<<"\t\t"; Break; Case 2: cout<<"("<<n1<<"/"<<n2<<")*("<<n3<<"/"<<n4<<")="<<"\t\t"; Break; Case 3: cout<<"("<<n1<<"/"<<n2<<")/("<<n3<<"/"<<n4<<")="<<"\t\t"; Break; } }} cout<<Endl; return 0;}
Three running results
Four programming experience
1. Before programming first see this one after another requirements, do not know how to do, and later in accordance with the class of teachers to help us analyze the idea, this one can control the parameters, can be put into an array, the same parameters can be considered to do so, but also to avoid the programming confusion or missing function.
2. When programming for the function of whether or not to choose, originally wanted to generate a function, return a fractional value, but the score of the display should be a string, did not do it, in the main function with a selection of statements, re-write a output, more stupid method.
3. In the printing method of the number of columns function, but also a struggle.
New arithmetic User Selection function applet