# Include <iostream>
# Include <time. h>
# Include <stdlib. h>
# Include <stdio. h>
# Include <string>
# Include <fstream>
# Include <iomanip>
# Include <cmath>
# Define random (x) (RAND () % x)
Using namespace STD;
Void output (int sf) // open the TXT record question
{
If (Sf = 1)
{
Ofstream OUTFILE ("formula.txt", IOS: Out );
If (! OUTFILE)
{
Cout <"Open Error !! "<Endl;
Exit (1 );
}
OUTFILE <"---------------- job set ----------------" <Endl;
OUTFILE. Close ();
}
}
Int istrue_input (INT num01) // determines whether the input 0, 1 complies with the standard.
{
While (1)
{
If (num01! = 0 & num01! = 1)
{
Cout <"incorrect input. Please enter it again (enter 0 or 1):" <Endl;
Cin> num01;
}
Else
Break;
}
Return num01;
}
Int creat_random (INT R1, int R2) // generates a random number within a specified range.
{
Int R, Shu;
R = R2-R1;
Shu = R1 + rand () % R;
Return shu;
}
String create_lai (int lai) // generate a Random Operator
{
Srand (INT) time (0 ));
String sign;
Int num_sym, sign_num;
If (Lai = 0) // add or subtract
Num_sym = 2;
Else if (Lai = 1) // add, subtract, multiply, and divide
Num_sym = 4;
Sign_num = rand () % num_sym; // generate random numbers to represent Operators
Switch (sign_num)
{
Case (0): Sign = '+'; break;
Case (1): Sign = '-'; break;
Case (2): Sign = '*'; break;
Case (3): Sign = '/'; break;
}
Return sign;
}
/* Judge positive and negative integers and take the opposite number */
Int opp_zf (int)
{
If (A <0)
A =-;
Return;
}
/* Convert int type to string type */
String int_str (INT Shu)
{
Char Zhuan [100];
String STR, Zuo = "(", you = ")";
STR = ITOA (Shu, Zhuan, 10 );
If (Shu <0)
STR = Zuo + STR + you;
Return STR;
}
/* Generate a (in a certain range) real score */
Void creat_fen (INT R1, int R2, string & fenshu, double & zhi)
{
Int I, r = R2-R1, up, down, S;
Bool flag;
String Xian = "/", Fu = "-", Zuo = "(", you = ")";
String zhuan1, zhuan2;
Loop: Do
{
Flag = false; // global variables cannot be defined !!
Up = R1 + rand () % R;
Up = opp_zf (up );
Down = R1 + rand () % R;
Down = opp_zf (down );
} While (up> = down) | (down = 0) | (up = 0 )); // numerator ≥ denominator or Denominator = 0 or Numerator = 0 regenerate
For (I = 2; I <= up; I ++)
{
If (up % I = 0 & down % I = 0)
{
Flag = true; // There are common approx.
Break;
}
}
If (flag = true)
{
Goto loop;
}
Else
{
Zhi = (double) up/(double) down;
Zhuan1 = int_str (up); // up/down are positive numbers and no parentheses are added.
Zhuan2 = int_str (down );
S = rand () % 2;
Switch (s)
{
// Positive score
Case (0 ):
Fenshu = Zuo + zhuan1 + Xian + zhuan2 + you;
Break;
// Negative score
Case (1 ):
Fenshu = Zuo + Fu + zhuan1 + Xian + zhuan2 + you;
Zhi =-zhi;
Break;
}
}
}
Void have_kuohao (INT R1, int R2, string & formula, double & zhi)
{
Srand (INT) time (0 ));
Int op, OP1, OP2;
Int YN, qH;
String symbol1, symbol2;
String Zhuan, zhuan1, zhuan2;
String Jia = "+", Jian = "-", Cheng = "*", Chu = "/", Zuo = "(", you = ")";
Zhi = (double) creat_random (R1, R2 );
OP1 = creat_random (R1, R2 );
OP2 = creat_random (R1, R2 );
OP = creat_random (R1, R2); // assume that the denominator of OP2 cannot be 0.
While (1)
{
If (OP = 0)
OP = creat_random (R1, R2 );
Else
Break;
}
Zhuan1 = int_str (zhi );
Zhuan2 = int_str (OP );
Symbol1 = create_prop (1 );
If (symbol1 = "+ ")
{
Zhi = (double) OP1 + (double) OP2;
Formula = zhuan1 + Jia + zhuan2;
YN = rand () % 2;
Switch (yn)
{
// No parentheses are added.
Case (0): break;
// Brackets
Case (1 ):
{
Formula = Zuo + formula + you;
Qh = rand () % 2;
Symbol2 = create_prop (1 );
Switch (QH)
{
// Add it to the front
Case (0 ):
{
Formula = symbol2 + formula;
OP = creat_random (R1, R2 );
Zhuan = int_str (OP );
If (symbol2 = "+ ")
{
Zhi = (double) OP + Zhi;
Formula = Zhuan + Jia + formula;
}
Else if (symbol2 = "-")
{
Zhi = (double) OP-zhi;
Formula = Zhuan + Jian + formula;
}
Else if (symbol2 = "*")
{
Zhi = (double) OP * Zhi;
Formula = Zhuan + cheng + formula;
}
Else if (symbol2 = "/")
{
If (Zhi = 0) // prevent the value after the randomly generated division sign from being 0;
Break;
Else
{
Zhi = (double) OP/Zhi;
Formula = Zhuan + Chu + formula;
}
}
Break;
}
// Add it to the end
Case (1 ):
{
Formula = formula + symbol2;
OP = creat_random (R1, R2); // prevents random division numbers from being generated. The denominator is 0.
While (1)
{
If (OP = 0)
OP = creat_random (R1, R2 );
Else
Break;
}
Zhuan = int_str (OP );
If (symbol2 = "+ ")
{
Zhi = Zhi + (double) OP;
Formula = formula + Jia + Zhuan;
}
Else if (symbol2 = "-")
{
Zhi = Zhi-(double) OP;
Formula = formula + Jian + Zhuan;
}
Else if (symbol2 = "*")
{
Zhi = Zhi * (double) OP;
Formula = formula + cheng + Zhuan;
}
Else if (symbol2 = "/")
{
Zhi = Zhi/(double) OP;
Formula = formula + Chu + Zhuan;
}
Break;
}
}
Break;
}
}
}
Else if (symbol1 = "-")
{
Zhi = (double) OP1-(double) OP2;
Formula = zhuan1 + Jian + zhuan2;
}
Else if (symbol1 = "*")
{
Zhi = (double) OP1 * (double) OP2;
Formula = zhuan1 + cheng + zhuan2;
}
Else if (symbol1 = "/")
{
Zhi = (double) OP1/(double) OP2;
Formula = zhuan1 + Chu + zhuan2;
}
}
/* Contains real scores */
Void have_fenshu (int ll, int R1, int R2, string & formula, double & Zhi, int JJ)
{
Srand (INT) time (0 ));
String F1, F2, symbol, Zhuan;
Double Z1, Z2; // Value
Int Inter, type;
Inter = creat_random (R1, R2); // integer
Zhuan = int_str (inter );
Creat_fen (R1, R2, F1, Z1); // Score 1
Creat_fen (R1, R2, F2, Z2); // score 2
Symbol = create_percent (LL); // symbol
Type = rand () % 3; // type
If (JJ = 0) // add or subtract no negative number
{
Inter = opp_zf (inter );
Zhuan = int_str (inter );
While (1)
{
If (Z1 <0)
Creat_fen (R1, R2, F1, Z1 );
Else
Break;
}
While (1)
{
If (Z2 <0)
Creat_fen (R1, R2, F2, Z2 );
Else
Break;
}
}
Switch (type)
{
// OP1 score
Case (0 ):
Formula = F1 + symbol + Zhuan;
If (symbol = "+ ")
Zhi = Z1 + (double) Inter;
Else if (symbol = "-")
{
If (JJ = 0)
Break;
Else
Zhi = Z1-(double) Inter;
}
Else if (symbol = "*")
Zhi = z1 * (double) Inter;
Else if (symbol = "/")
{
If (Inter! = 0)
Break;
Else
Zhi = Z1/(double) Inter;
}
Break;
// OP2 score
Case (1 ):
Formula = Zhuan + symbol + F1;
If (symbol = "+ ")
Zhi = (double) Inter + Z1;
Else if (symbol = "-")
Zhi = (double) Inter-Z1;
Else if (symbol = "*")
Zhi = (double) inter * Z1;
Else if (symbol = "/")
{
If (Z1! = 0)
Zhi = (double) inter/Z1;
Else
Break;
}
Break;
// Full score
Case (2 ):
Formula = F1 + symbol + F2;
If (symbol = "+ ")
Zhi = Z1 + Z2;
Else if (symbol = "-")
{
If (JJ = 0) & (Z1 <Z2 ))
Break;
Else
Zhi = Z1-Z2;
}
Else if (symbol = "*")
Zhi = z1 * Z2;
Else if (symbol = "/")
{
If (Z1! = 0)
Zhi = Z1/Z2;
Else
Break;
}
Break;
}
}
/* Excluding real scores */
Void only_integer (int ll, int R1, int R2, string & formula, double & Zhi, int JJ, int CC)
{
Srand (INT) time (0 ));
Int OP1, OP2;
String symbol, zhuan1, zhuan2;
OP1 = creat_random (R1, R2); // integer 1
OP2 = creat_random (R1, R2); // integer 2
Symbol = create_percent (LL); // symbol
If (JJ = 0) // add or subtract without a negative number
{
OP1 = opp_zf (OP1 );
OP2 = opp_zf (OP2 );
If (symbol = "-") & (OP1 <OP2 ))
{
Int temp;
Temp = OP1;
OP1 = OP2;
OP2 = temp;
}
}
If (Cc = 0) & (symbol = "/") // Division does not include the remainder (when negative number calculation is removed)
{
While (1)
{
OP1 = opp_zf (OP1 );
OP2 = opp_zf (OP2 );
If (OP1% OP2! = 0) // returns the remainder.
{
OP1 = creat_random (R1, R2 );
OP2 = creat_random (R1, R2 );
}
Else
Break;
}
}
Zhuan1 = int_str (OP1 );
Zhuan2 = int_str (OP2 );
Formula = zhuan1 + symbol + zhuan2;
If (symbol = "+ ")
Zhi = OP1 + OP2;
Else if (symbol = "-")
Zhi = OP1-OP2;
Else if (symbol = "*")
Zhi = OP1 * OP2;
Else if (symbol = "/")
Zhi = OP1/OP2;
}
Void main ()
{
Ofstream OUTFILE ("formula.txt", IOS: Out );
If (! OUTFILE)
{
Cout <"Open error! "<Endl;
Exit (1 );
}
OUTFILE <"---------------- welcome! Pupils:-D ---------------- "<Endl;
Int I, j, N, R1, R2;
Int SF, KH, FS, Lei, JJ, CC, Count = 0;
String question = ""; // generated question
Double Zhi, answer;
Int fenzi, fenmu;
Bool repeat = false;
Cout <"---------------- welcome! Pupils:-D ---------------- "<Endl;
/* Select the question range and quantity */
Cout <"What range of questions do you Want to customize for your children? (Can contain negative numbers & upper limit ≥ lower limit): "<Endl;
Cout <"lower limit :";
Cin> R1;
Cout <"Upper Limit :";
Cin> R2;
While (1) // whether the input is valid
{
If (R2 <R1)
{
Cout <"the input range is unreasonable. Please enter it again (upper limit ≥ lower limit):" <Endl;
Cout <"lower limit :";
Cin> R1;
Cout <"Upper Limit :";
Cin> R2;
}
Else
Break;
}
Cout <"how many questions are customized? Enter the number of instances: "<Endl;
Cout <"(Tip: do not issue too many questions for your child (up to 1000 questions )! Thank you for your cooperation !) "<Endl;
Cin> N;
While (1) // whether the input is valid
{
If (n <= 0 || n> 1000)
{
Cout <"the number of input questions is unreasonable. Please enter the following again:" <Endl;
Cin> N;
}
Else
Break;
}
/* Select the print mode */
Cout <"do you need to save the document? (0, No 1, required) "<Endl;
Cin> SF;
Sf = istrue_input (SF); // whether the input is valid
Output (SF );
Under cout <"**********, select the difficulty of the question based on your children's learning level !! * ******** "<Endl;
/* Contains parentheses */
Cout <"do you want to exercise the difficulty calculation with parentheses? (0, No 1, yes )";
Cin> KH;
Kh = istrue_input (kH); // whether the input is valid
If (kh = 1) // contains parentheses
{
For (I = 1; I <= N; I ++)
{
String formula = "";
Double Zhi;
Have_kuohao (R1, R2, formula, zhi );
}
}
// Contains no parentheses
Else if (kh = 0)
{
/* Addition and subtraction or four Arithmetic Operations */
Cout <"select: 0. Do you only perform addition and subtraction? 1. Perform four arithmetic operations? ";
Cin> lei;
Lei = istrue_input (LEI); // whether the input is valid
Cout <"Is there a negative number in addition and subtraction? (0, No 1, yes )";
Cin> JJ;
JJ = istrue_input (jj );
If (Lei = 1) // multiplication and division
{
Cout <"Is there any remainder in Division? (0, No 1, yes )";
Cin> cc;
Cc = istrue_input (CC );
}
/* Whether it contains the real score */
Cout <"does the question contain real scores? (0, No 1, yes )";
Cin> FS;
Istrue_input (FS); // whether the input is valid
Cout <n <":" <Endl;
If (Sf = 1) // file output
{
OUTFILE <n <":" <Endl;
}
String Ku [10000];
For (I = 1; I <= N; I ++)
{
If (FS = 0) // full integer
Only_integer (Lei, R1, R2, question, Zhi, JJ, CC );
Else if (FS = 1) // contains the real score
Have_fenshu (Lei, R1, R2, question, Zhi, JJ );
Ku [I] = question;
For (j = 0; j <I; j ++)
{
If (KU [J] = Ku [I])
{
I = I-1;
Repeat = true; // repeated
Break;
}
}
If (! Repeat) // do not repeat
{
/* Is_rept (I, STR );*/
Cout <I <"," <question <"=" <Endl;
If (Sf = 1) // file output
{
OUTFILE <I <"," <question <"=" <Zhi <Endl;
}
If (Zhi-(INT) zhi) <1e-7 & (Zhi-(INT) zhi)>-(1e-7) // it can be considered as an integer
{
Cin> answer;
}
Else
{
Cout <"molecule :";
Cin> fenzi;
Cout <"denominator :";
Cin> fenmu;
Answer = (double) fenzi/(double) fenmu;
}
If (answer-zhi) <1e-7 & (answer-zhi)>-(1e-7 ))
{
Cout <"√" <Endl;
Count ++;
}
Else
{
Cout <"×" <Endl;
Cout <"Answer:" <Zhi <Endl;
}
}
}
Cout <"Your child answered a total of questions" <count <!! Congratulations !! "<Endl;
If (Sf = 1) // file output
{
OUTFILE <"Your child answered a total of questions" <count <!! Congratulations !! "<Endl;
}
Cout <"Incorrect" <n-count <"Please analyze the cause and correct the problem ~~ Come on !! "<Endl;
If (Sf = 1) // file output
{
OUTFILE <"Incorrect" <n-count <"Please analyze the cause and correct the problem ~~ Come on !! "<Endl;
}
}
OUTFILE. Close ();
}
On the basis of the previous operation, the function of directly determining whether a positive error is returned and storing the result is added. You can select the correct one based on your requirements.
Online Q & A is implemented, right and wrong can be judged, and results can be saved