Home > Backend Development > PHP Tutorial > Software Engineering Pairing Assignment 02, Software Engineering Pairing 02_PHP Tutorial

Software Engineering Pairing Assignment 02, Software Engineering Pairing 02_PHP Tutorial

WBOY
Release: 2016-07-12 08:55:01
Original
941 people have browsed it

Software Engineering Pairing Assignment 02, Software Engineering Pairing 02

1. Design Idea:

I used php to implement the main functions of this task. By default, addition and subtraction must be included.

First, create the index.php file and create a form in the HTML statement. The content of the form includes the number of questions and the maximum and minimum values. , and some other options.

Then, create the rubric.php file, submit the form information to this file, and call the passed value when implementing the four arithmetic operations methods. The method of implementing the four arithmetic operations is similar to the method used when using Java several times before. The code has been modified and optimized. Put the questions and answers into the question.txt and answer.txt files respectively for later use.

Third, create submitAnswer.php, acceptAnswer.php, deleteAnswer.php and judgeAnswer.php files are used to submit answers online, receive answers, delete answers and judge answers respectively. The submitted answers are placed in the answer1.txt file. The answers are judged by comparing answer.txt and The content in the answer1.txt file is enough.

2. Source program code

Software Engineering Pairing Assignment 02, Software Engineering Pairing 02_PHP Tutorial 1 index.php 2 3 9 10 11 12 Four arithmetic calculation system 13 <script> <span> 14</span> <span>function</span><span> check() { </span><span> 15</span> <span>var</span> tt=/^(0|[1-9]d*)$/<span>; </span><span> 16</span> <span>if</span>(!tt.test(form1.<span>min</span>.<span>value)) </span><span> 17</span> <span> { </span><span> 18</span> alert('The minimum value input is illegal'<span>); </span><span> 19</span> form1.<span>min</span>.<span>focus(); </span><span> 20</span> <span>return</span> <span>false</span><span>; </span><span> 21</span> <span> } </span><span> 22</span> <span>if</span>(!tt.test(form1.<span>max</span>.value) || (form1.<span>max</span>.value< form1.<span>min</span>.<span>value)) </span><span> 23</span> <span> { </span><span> 24</span> alert('The maximum value input is illegal'<span>); </span><span> 25</span> form1.<span>min</span>.<span>focus(); </span><span> 26</span> <span>return</span> <span>false</span><span>; </span><span> 27</span> <span> } </span><span> 28</span> <span>if</span>(!tt.test(form1.num.value) || (form1.num.value==0<span>)) </span><span> 29</span> <span> { </span><span> 30</span> alert('The number of questions entered is illegal'<span>); </span><span> 31</span> form1.num.<span>focus(); </span><span> 32</span> <span>return</span> <span>false</span><span>; </span><span> 33</span> <span> } </span><span> 34</span> <span>return</span> <span>true</span><span>; </span><span> 35</span> <span> }</span><span> 36</span> </script> 37 38 39    40

41
42 Welcome to the primary school four arithmetic operation question system


43 44
45 By default, only two numbers participate in addition and subtraction

46 47 Please enter the minimum value (non-negative integer) to participate in the operation 48

49 50 Please enter the maximum value to participate in the operation (non-negative integer and not less than the minimum value) 51

52 53 Please enter the number of questions (positive integer) 54

55 56 Select the number of numbers to participate in the operation 57 2 58 3 59 4

60 61 Choose whether to add multiplication 62 Yes 63 No

64 65 Choose whether to add division 66 Yes 67 No

68 69 70 71
72
73 74 75 76 rubric.php 77 78 79 80 81 Question setting interface 82 83 84 85 php 86 if(is_numeric($_POST["max"])) 87 { 88 $max=$_POST["max"]; //maximum value 89 } 90 if(is_numeric($_POST["min"])) 91 { 92 $min=$_POST["min"]; //Minimum value 93 } 94 if(is_numeric($_POST["num"])) 95 { 96 $num=$_POST["num"]; //Number of questions 97 } 98 if(is_numeric($_POST["num1"])) 99 { 100 $num1=$_POST["num1"]; //Number of items participating in the operation 101 } 102 if(is_string($_POST["mul"])) 103 { 104 $mul=$_POST["mul"]; //Choose whether to add multiplication or not is t Nof 105 } 106 if(is_string($_POST["div"])) 107 { 108 $div=$_POST["div"]; //Choose whether to add division is t Nof 109 } 110 111 /* Define array */ 112 $a=array(); //Used in the method to store random numbers and operators 113 $amd=array(); // is used in the method to calculate the calculation after multiplication/division Store in this array 114 $b=array(); //Use when calling a method 115 $d=array(); //Storage question 116 $e=array(); //store answer 117 118 /* The Result class is used to encapsulate the result */ 119 class Result 120 { 121 public $r1; //$r1 stores the calculation result 122 } 123 124 /* Operators only have addition and subtraction methods */ 125 function addSub($min,$max,$num1,$result) 126 { 127 $str=mt_rand($min,$max); 128 $re=$str; 129 for($i=1;$i<$num1;$i ) 130 { 131 $a[$i]= mt_rand($min,$ max); 132 $c=mt_rand(0,1); 133 if($c==0) 134 { 135 $str=$str." ".$a[$i]; 136 $re=$re $a[$i]; 137 } 138 if($c==1) 139 { 140 $str=$str." - ".$a[$i]; 141 $re=$re-$a[$i]; 142 } 143 } 144 $result->r1=$re; 145 $str=$str." = "; 146 return $str; 147 }148 149 /* The operators are addition, subtraction and multiplication methods */ 150 function addSubMul($min,$max,$num1,$result) 151 { 152 $a[0]= mt_rand($min, $max ); 153 /* Store the numbers and operators in the calculation into the array $a */ 154 /*If spaces are added to the left and right sides of the operator in this loop, spaces should also be added to the operator in the if conditional statement in the subsequent while loop. Otherwise, an error will occur */ 155 for($i=1;$i<(2*$num1-1) ;$i=$i 2) //(2*$num1-1) is the number of numbers and operators involved in the calculation Sum of numbers 156 { 157 $c= mt_rand(0, 2); 158 if($c==0) 159 { 160 $a[$i]=' '; 161 $a[$i 1]= mt_rand($min, $max); 162 } 163 if($c==1) 164 { 165 $a[$i]=' - '; 166 $a[$i 1]= mt_rand($min, $max); 167 } 168 if($c==2) 169 { 170 $a[$i]=' x '; 171 $a[$i 1]= mt_rand($min, $max); 172 } 173 } 174 /* Store the multiplication in the calculation into the array $amd */ 175 $i=0; 176 $j=0; 177 while($i<(2*$num1-1)) //Whether there are spaces around the operator should be consistent with the for loop above 178 { 179 if($a[$i]==' x ') 180 { 181 $amd[$j-1]=$amd[$j-1 ]*$a[$i 1]; 182 $i=$i 2; 183 } 184 else 185 { 186 $amd[$j]=$a[$i] ; 187 $j ; 188 $i ; 189 } 190 } 191 /* Calculate the answer to the equation $re */ 192 $re=$amd[0]; 193 $k=1; 194 while($k<$j) // operator Whether there are spaces on the left and right should be consistent with the for loop above 195 { 196 if($amd[$k]==' ') 197 { 198 $re=$re $amd[$k 1]; 199 $k=$k 2; 200 continue; 201 }202 if($amd[$k]==' - ') 203 { 204 $re=$re-$amd[$k 1] ; 205 $k=$k 2; 206 } 207 } 208 /* Concatenate the values ​​in the array into a string calculation $str */ 209 $str=$a[0]; 210 for($i=1;$i<(2*$num1 -1);$i ) 211 { 212 $str.=$a[$i]; 213 } 214 $result->r1=$re; 215 $str=$str." = "; 216 return $str; 217 } 218 219 /* The operators are addition, subtraction and division methods */ 220 function addSubDiv($min,$max,$num1,$result) 221 { 222 $a[0]= mt_rand($min, $max ); 223 /* Store the numbers and operators in the calculation into the array $a */ 224 for($i=1;$i<(2*$num1-1) ;$i=$i 2) 225 { 226 $c= mt_rand(0, 2); 227 if($c==0) 228 { 229 $a[$i]=' '; 230 $a[$i 1]= mt_rand($min, $max); 231 } 232 if($c==1) 233 { 234 $a[$i]=' - '; 235 $a[$i 1]= mt_rand($min, $max); 236 } 237 if($c==2) 238 { 239 $a[$i]=' ÷ '; 240 $a[$i 1]= mt_rand($min, $max); 241 } 242 } 243 /* Store the calculation after division in the calculation into the array $amd */ 244 $i=0; 245 $j=0; 246 while($i<(2*$num1-1)) 247 { 248 if($a[$i]==' ÷ ') 249 { 250 while(($amd[$j-1] % $a[$i 1]!=0) || $a[$i 1]==0 ) //Avoid Division has remainder 251 { 252 $a[$i 1]= mt_rand($min, $max); 253 } 254 $amd[$j-1]=$amd[$j-1 ]/$a[$i 1]; 255 $i=$i 2; 256 }257 else 258 { 259 $amd[$j]=$a[$i] ; 260 $j ; 261 $i ; 262 } 263 } 264 /* Calculate the answer to the equation $re */ 265 $re=$amd[0]; 266 $k=1; 267 while($k<$j) 268 { 269 if($amd[$k]==' ') 270 { 271 $re=$re $amd[$k 1]; 272 $k=$k 2; 273 continue; 274 } 275 if($amd[$k]==' - ') 276 { 277 $re=$re-$amd[$k 1] ; 278 $k=$k 2; 279 } 280 } 281 282 /* Put the calculation into the string $str */ 283 $str=$a[0]; 284 for($i=1;$i<(2*$num1 -1);$i ) 285 { 286 $str.=$a[$i]; 287 } 288 $result->r1=$re; 289 $str=$str." = "; 290 return $str; 291 } 292 293 /* The operators are addition, subtraction, multiplication and division methods */ 294 function addSubMulDiv($min,$max,$num1,$result) 295 { 296 $a[0]= mt_rand($min, $max ); 297 /* Store the numbers and operators in the calculation into the array $a */ 298 for($i=1;$i<(2*$num1-1) ;$i=$i 2) 299 { 300 $c= mt_rand(0, 3); 301 if($c==0) 302 { 303 $a[$i]=' '; 304 $a[$i 1]= mt_rand($min, $max); 305 } 306 if($c==1) 307 { 308 $a[$i]=' - '; 309 $a[$i 1]= mt_rand($min, $max); 310 } 311 if($c==2) 312 { 313 $a[$i]=' x '; 314 $a[$i 1]= mt_rand($min, $max); 315 }316 if($c==3) 317 { 318 $a[$i]=' ÷ '; 319 $a[$i 1]= mt_rand($min, $max); 320 } 321 } 322 /* Store the calculation after division in the calculation into the array $amd */ 323 $i=0; 324 $j=0; 325 while($i<(2*$num1-1)) 326 { 327 if($a[$i]==' x ') 328 { 329 $amd[$j-1]=$amd[$j-1 ]*$a[$i 1]; 330 $i=$i 2; 331 } 332 else if($a[$i]==' ÷ ') 333 { 334 while(($amd[$j-1] % $a[$i 1]!=0) || $a[$i 1]==0 ) //Avoid Division has remainder 335 { 336 $a[$i 1]= mt_rand($min, $max); 337 } 338 $amd[$j-1]=$amd[$j-1 ]/$a[$i 1]; 339 $i=$i 2; 340 } 341 else 342 { 343 $amd[$j]=$a[$i] ; 344 $j ; 345 $i ; 346 } 347 } 348 /* Calculate the answer to the equation $re */ 349 $re=$amd[0]; 350 $k=1; 351 while($k<$j) 352 { 353 if($amd[$k]==' ') 354 { 355 $re=$re $amd[$k 1]; 356 $k=$k 2; 357 continue; 358 } 359 if($amd[$k]==' - ') 360 { 361 $re=$re-$amd[$k 1] ; 362 $k=$k 2; 363 } 364 } 365 /* Put the calculation into the string $str */ 366 $str=$a[0]; 367 for($i=1;$i<(2*$num1 -1);$i ) 368 { 369 $str.=$a[$i]; 370 }371 $result->r1=$re; 372 $str=$str." = "; 373 return $str; 374 } 375 376 $r= new Result(); 377 378 /* Addition and subtraction of two numbers */ 379 if(($num1==2) && ($mul=='f') && ($ div=='f')) 380 { 381 $question = fopen("question.txt", "w"); 382 $answer= fopen("answer.txt", "w"); 383 for($i=0;$i<$num;$i ) 384 { 385 $j=0; 386 $bool=true; 387 $b[$i]= addSub($min, $max, $num1, $r); 388 while(($r->r1)<0) //When the result is When negative, re-random 389 { 390 $b[$i]= addSub($min, $max, $num1, $r); 391 } 392 while(($bool) && ($i!=0))//Avoid duplication 393 { 394 while($b[$i]==$b[ $j]) 395 { 396 $b[$i]= addSub($min, $max, $num1, $r); 397 while(($r->r1)<0)//The result is negative , re-write the question 398 { 399 $b[$i]= addSub($min, $max, $num1, $r); 400 } 401 $j=0; 402 } 403 $j ; 404 if($j==$i) 405 { 406 $bool=false; 407 } 408 } 409 echo "( ".($i 1)." )  ".$b[ $i]."

"; 410 $d[$i]=$b[$i]."n "; 411 $e[$i]=$r->r1."n"; 412 fwrite($question,$d[$i]); 413 fwrite($answer,$e[$i]); 414 } 415 fclose($question); 416 fclose($answer); 417 }418 419 /* Addition, subtraction and multiplication of two numbers */ 420 if(($num1==2) && ($mul=='t') && ($ div=='f')) 421 { 422 $question = fopen("question.txt", "w"); 423 $answer= fopen("answer.txt", "w"); 424 for($i=0;$i<$num;$i ) 425 { 426 $j=0; 427 $bool=true; 428 $b[$i]= addSubMul($min, $max, $num1, $r); 429 while(($r->r1)<0) //When the result is When negative, re-random 430 { 431 $b[$i]= addSubMul($min, $max, $num1, $r); 432 } 433 while(($bool) && ($i!=0))//Avoid duplication 434 { 435 while($b[$i]==$b[ $j]) 436 { 437 $b[$i]= addSubMul($min, $max, $num1, $r); 438 while(($r->r1)<0)//The result is negative , re-write the question 439 { 440 $b[$i]= addSubMul($min, $max, $num1, $r); 441 } 442 $j=0; 443 } 444 $j ; 445 if($j==$i) 446 { 447 $bool=false; 448 } 449 } 450 echo "( ".($i 1)." )  ".$b[ $i]."

"; 451 $d[$i]=$b[$i]."n "; 452 $e[$i]=$r->r1."n"; 453 fwrite($question,$d[$i]); 454 fwrite($answer,$e[$i]); 455 } 456 fclose($question); 457 fclose($answer); 458 }459 460 /* Addition, subtraction and division of two numbers */ 461 if(($num1==2) && ($mul=='f') && ($ div=='t')) 462 { 463 $question = fopen("question.txt", "w"); 464 $answer= fopen("answer.txt", "w"); 465 for($i=0;$i<$num;$i ) 466 { 467 $j=0; 468 $bool=true; 469 $b[$i]= addSubDiv($min, $max, $num1, $r); 470 while(($r->r1)<0) //When the result is When negative, re-random 471 { 472 $b[$i]= addSubDiv($min, $max, $num1, $r); 473 } 474 while(($bool) && ($i!=0))//Avoid duplication 475 { 476 while($b[$i]==$b[ $j]) 477 { 478 $b[$i]= addSubDiv($min, $max, $num1, $r); 479 while(($r->r1)<0)//The result is negative , re-write the question 480 { 481 $b[$i]= addSubDiv($min, $max, $num1, $r); 482 } 483 $j=0; 484 } 485 $j ; 486 if($j==$i) 487 { 488 $bool=false; 489 } 490 } 491 echo "( ".($i 1)." )  ".$b[ $i]."

"; 492 $d[$i]=$b[$i]."n "; 493 $e[$i]=$r->r1."n"; 494 fwrite($question,$d[$i]); 495 fwrite($answer,$e[$i]); 496 } 497 fclose($question); 498 fclose($answer); 499 }500 501 /* Addition, subtraction, multiplication and division of two numbers */ 502 if(($num1==2) && ($mul=='t') && ($ div=='t')) 503 { 504 $question = fopen("question.txt", "w"); 505 $answer= fopen("answer.txt", "w"); 506 for($i=0;$i<$num;$i ) 507 { 508 $j=0; 509 $bool=true; 510 $b[$i]= addSubMulDiv($min, $max, $num1, $r); 511 while(($r->r1)<0)//The result is negative , re-write the question 512 { 513 $b[$i]= addSubMulDiv($min, $max, $num1, $r); 514 } 515 while(($bool) && ($i!=0))//Judgment of duplication 516 { 517 while($b[$i]==$b[ $j]) 518 { 519 $b[$i]= addSubMulDiv($min, $max, $num1, $r); 520 while(($r->r1)<0)//The result is negative , re-write the question 521 { 522 $b[$i]= addSubMulDiv($min, $max, $num1, $r); 523 } 524 $j=0; 525 } 526 $j ; 527 if($j==$i) 528 { 529 $bool=false; 530 } 531 } 532 echo "( ".($i 1)." )  ".$b[ $i]."

"; 533 $d[$i]=$b[$i]."n "; 534 $e[$i]=$r->r1."n"; 535 fwrite($question,$d[$i]); 536 fwrite($answer,$e[$i]); 537 } 538 fclose($question); 539 fclose($answer); 540 }541 542 /* Addition and subtraction of three numbers */ 543 if(($num1==3) && ($mul=='f') && ($ div=='f')) 544 { 545 $question = fopen("question.txt", "w"); 546 $answer= fopen("answer.txt", "w"); 547 for($i=0;$i<$num;$i ) 548 { 549 $j=0; 550 $bool=true; 551 $b[$i]= addSub($min, $max, $num1, $r); 552 while(($r->r1)<0)//The result is negative , re-write the question 553 { 554 $b[$i]= addSub($min, $max, $num1, $r); 555 } 556 while(($bool) && ($i!=0))//Judgment of duplication 557 { 558 while($b[$i]==$b[ $j]) 559 { 560 $b[$i]= addSub($min, $max, $num1, $r); 561 while(($r->r1)<0)//The result is negative , re-write the question 562 { 563 $b[$i]= addSub($min, $max, $num1, $r); 564 } 565 $j=0; 566 } 567 $j ; 568 if($j==$i) 569 { 570 $bool=false; 571 } 572 } 573 echo "( ".($i 1)." )  ".$b[ $i]."

"; 574 $d[$i]=$b[$i]."n "; 575 $e[$i]=$r->r1."n"; 576 fwrite($question,$d[$i]); 577 fwrite($answer,$e[$i]); 578 } 579 fclose($question); 580 fclose($answer); 581 }582 583 /* Addition, subtraction and multiplication of three numbers */ 584 if(($num1==3) && ($mul=='t') && ($ div=='f')) 585 { 586 $question = fopen("question.txt", "w"); 587 $answer= fopen("answer.txt", "w"); 588 for($i=0;$i<$num;$i ) 589 { 590 $j=0; 591 $bool=true; 592 $b[$i]= addSubMul($min, $max, $num1, $r); 593 while(($r->r1)<0)//The result is negative , re-write the question 594 { 595 $b[$i]= addSubMul($min, $max, $num1, $r); 596 } 597 while(($bool) && ($i!=0))//Judgment of duplication 598 { 599 while($b[$i]==$b[ $j]) 600 { 601 $b[$i]= addSubMul($min, $max, $num1, $r); 602 while(($r->r1)<0)//The result is negative , re-write the question 603 { 604 $b[$i]= addSubMul($min, $max, $num1, $r); 605 } 606 $j=0; 607 } 608 $j ; 609 if($j==$i) 610 { 611 $bool=false; 612 } 613 } 614 echo "( ".($i 1)." )  ".$b[ $i]."

"; 615 $d[$i]=$b[$i]."n "; 616 $e[$i]=$r->r1."n"; 617 fwrite($question,$d[$i]); 618 fwrite($answer,$e[$i]); 619 } 620 fclose($question); 621 fclose($answer); 622 }623 624 /* Addition, subtraction and division of three numbers */ 625 if(($num1==3) && ($mul=='f') && ($ div=='t')) 626 { 627 $question = fopen("question.txt", "w"); 628 $answer= fopen("answer.txt", "w"); 629 for($i=0;$i<$num;$i ) 630 { 631 $j=0; 632 $bool=true; 633 $b[$i]= addSubDiv($min, $max, $num1, $r); 634 while(($r->r1)<0)//The result is negative , re-write the question 635 { 636 $b[$i]= addSubDiv($min, $max, $num1, $r); 637 } 638 while(($bool) && ($i!=0))//Judgment of duplication 639 { 640 while($b[$i]==$b[ $j]) 641 { 642 $b[$i]= addSubDiv($min, $max, $num1, $r); 643 while(($r->r1)<0)//The result is negative , re-write the question 644 { 645 $b[$i]= addSubDiv($min, $max, $num1, $r); 646 } 647 $j=0; 648 } 649 $j ; 650 if($j==$i) 651 { 652 $bool=false; 653 } 654 } 655 echo "( ".($i 1)." )  ".$b[ $i]."

"; 656 $d[$i]=$b[$i]."n "; 657 $e[$i]=$r->r1."n"; 658 fwrite($question,$d[$i]); 659 fwrite($answer,$e[$i]); 660 } 661 fclose($question); 662 fclose($answer); 663 }664 665 /* Addition, subtraction, multiplication and division of three numbers */ 666 if(($num1==3) && ($mul=='t') && ($ div=='t')) 667 { 668 $question = fopen("question.txt", "w"); 669 $answer= fopen("answer.txt", "w"); 670 for($i=0;$i<$num;$i ) 671 { 672 $j=0; 673 $bool=true; 674 $b[$i]= addSubMulDiv($min, $max, $num1, $r); 675 while(($r->r1)<0)//The result is negative , re-write the question 676 { 677 $b[$i]= addSubMulDiv($min, $max, $num1, $r); 678 } 679 while(($bool) && ($i!=0))//Judgment of duplication 680 { 681 while($b[$i]==$b[ $j]) 682 { 683 $b[$i]= addSubMulDiv($min, $max, $num1, $r); 684 while(($r->r1)<0)//The result is negative , re-write the question 685 { 686 $b[$i]= addSubMulDiv($min, $max, $num1, $r); 687 } 688 $j=0; 689 } 690 $j ; 691 if($j==$i) 692 { 693 $bool=false; 694 } 695 } 696 echo "( ".($i 1)." )  ".$b[ $i]."

"; 697 $d[$i]=$b[$i]."n "; 698 $e[$i]=$r->r1."n"; 699 fwrite($question,$d[$i]); 700 fwrite($answer,$e[$i]); 701 } 702 fclose($question); 703 fclose($answer); 704 } 705 706 /* Addition and subtraction of four numbers */ 707 if(($num1==4) && ($mul=='f') && ($ div=='f')) 708 { 709 $question = fopen("question.txt", "w"); 710 $answer= fopen("answer.txt", "w"); 711 for($i=0;$i<$num;$i ) 712 { 713 $j=0; 714 $bool=true;
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template