1. Random Handling
In the first design, the random is called every time. Next () is the new random random. Results in the process of running, each time the result has a duplicate expression, or even all the same, and in the course of their own debugging found that this is not the case. So consult the information and ask the classmate, only then know the actual operation process, because the time is very short, so the random number is not random situation.
Publicexpression carryout () {//random random = new random (); //Each number and symbol generation for each expression intI=0; Expression e=Newexpression (signnum); while(i<signnum+1) {i++; Iffraction=random. Next (0,2); Figure F=NewFigure (iffraction) ...} }
The random in the main function is then passed in every method used.
PublicCreate (intNintR, Random RA) { //random random = new random ();Quantity =N; Maxrange=R; Random=RA; Signnum= Random. Next (0,4); Sign. ADD ('+'); Sign. ADD ('-'); Sign. ADD ('*'); Sign. ADD ('/'); }
This solves the problem.
2. File output
New FileStream ("Answers.txt"new StreamWriter (answerfile);
Originally defined above, only need to use A1.write () or a1.writeline () can be the content you want to output to the file, but in the course of the operation found that the corresponding TXT file found inside is empty, Students say that the write function may be overwritten during the output process, resulting in no result at the end.
Instead, the change uses the Write series output in each section, but chooses to save it all in a string, with a a1.writeline (string) output.
Output = ("Correct:"+Right . Count (). ToString ()); inti; if(right. Count () >0) {Output+= (" ("); for(i =0; I < right. Count (); i++) { if(I! = right.) Count ()-1) {Output+ = (Right[i]. ToString () +", "); //C1. Write (i + ","); } Else{Output+ = (Right[i]. ToString () +") \ r \ n"); //C1. Write (i + ") \ n"); } } } ElseOutput+="\ r \ n";
3. Score Judgment
In the generation of fractional phase will always cause a dead loop, the true score must meet the numerator and denominator no convention number, the denominator is not 0 or 1, the numerator is not 0, so the numerator denominator is a random number after the generation of the two to determine whether there is greatest common divisor, but forget to consider the numerator is 0, the denominator is 0 or 1, So often in the loop does not come out.
if(iffraction==1){ //Fraction intD = Random. Next (1, maxrange+1); intn = random. Next (1, maxrange+1); while(true){ if((n%d!=0) && (d%n!=0)) Break; ElseN= Random. Next (1, Maxrange +1); } Basic B=NewBasic (); intm =B.GCD (n,d); D= d/m; N= n/m; F.denominator (d); F.numerator (n);
The random number generation range of numerator denominator is changed to 1-r.
4. Calculation part
The calculation section selects the infix prefix and computes the algorithm using the prefix expression.
But because this part of the code is more complex, there are a lot of unnoticed problems at the beginning of writing that affect the running results of the program, and finally find these errors when debugging.
(1) multiplication
else if (s = = " x " " *
Because the requirements are input and output to the file multiplication method with x and ÷ instead of * and/, so you have to read the characters to turn around, otherwise unable to process the calculation.
(2) Calculation of fractions
Because there is a calculation between integers and integers, the calculation between integers and fractions is calculated between fractions and fractions. When it comes to subtraction that requires a multiple of the numerator denominator, it's not very careful when you start writing, so forget to deal with it. Especially when fractions and fractions are added and subtracted.
if(c = ='+') { //subtraction of two fractionsFENMU = FENMU1 *FENMU2; Fenzi= FENMU1 * Fenzi2 + FENMU2 *Fenzi1; } Else if(c = ='-') {Fenmu= FENMU1 *FENMU2; Fenzi= FENMU2 * FENZI1-FENMU1 *Fenzi2; }
Correct it back when you are debugging.
(3) Change of stack
Infix prefix, the final result stored in the stack from the top of the stack pop output is the prefix expression.
However, the prefix expression is calculated from left to right, that is, the infix-to-prefix final stack and the required prefix expression corresponding to the Read method is exactly the opposite.
So in the two algorithmic links, add
while 0 ) { new Stackitem (0); St.setsign (signs. Peek ()); Items. Push (ST); Signs. Pop (); }
(4) ...
5 、......
The above are serious technical errors, there are relatively small because the parentheses or initialization of the problem is also found during the debugging process.
From the very beginning of the delivery of anything can not run, to the end of the normal operation, debug is the most necessary.
Arithmetic Modification and Perfection