设计思路:
在尝试没学好的Java Web失败后,投向了半学会的C#的怀抱,虽然说不是网页或安卓程序会扣分……管不了那么多了。
功能的话和四则运算3相同,只是加了界面。其他功能基本不需更改即满足要求,但运算过程不能相同的要求没能完成。
Form2设计
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 11 namespace 二四_软件工程20160406 12 { 13 14 public partial class Form2 : Form 15 { 16 int num; 17 bool chengchu; 18 bool kuohao; 19 bool yushu; 20 int shengxian; 21 int xiaxian; 22 public Form2() 23 { 24 InitializeComponent(); 25 } 26 27 private void label5_Click(object sender, EventArgs e) 28 { 29 30 } 31 32 private void button1_Click(object sender, EventArgs e) 33 { 34 num= int.Parse(textBox1.Text); 35 36 if (textBox2.Text == "Y") chengchu = true; 37 else if(textBox2.Text == "N") chengchu = false; 38 39 if (textBox3.Text == "Y") kuohao = true; 40 else if (textBox3.Text == "N") kuohao = false; 41 42 if (textBox4.Text == "Y") yushu = true; 43 else if (textBox4.Text == "N") yushu = false; 44 shengxian = int.Parse(textBox5.Text); ; 45 xiaxian = int.Parse(textBox6.Text); 46 47 new Form1(num, chengchu, kuohao, yushu, shengxian, xiaxian).Show(); 48 49 //Form1 a=new Form1(num,chengchu,kuohao,yushu,shengxian,xiaxian); 50 } 51 } 52 }
Form1设计
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 public struct equation 11 { //定义结构体“算式”equation 12 public double value; //值value,用于存算式的值 13 public int num; //当前算式为num元运算 14 public bool isFraction; //标记是否为分数 15 public string shape; //形式shape,用于存算式形式 16 public string line1t; 17 public string line2t; 18 public string line3t; 19 } 20 21 namespace 二四_软件工程20160406 22 { 23 public partial class Form1 : Form 24 { 25 Equation A = new Equation(); 26 int numbertest = 0; 27 int totalR=0; 28 public Form1(int a,bool b,bool c,bool d,int e,int f) 29 { 30 InitializeComponent(); 31 A.CreateEquation(a,b,c,d,e,f); 32 A.Printshape(); 33 textBox4.Text = "1"; 34 textBox1.Text = A.array[numbertest].line1t + System.Environment.NewLine + A.array[numbertest].line2t + System.Environment.NewLine + A.array[numbertest].line3t; 35 textBox5.Text = A.NUM_equation.ToString(); 36 37 } 38 39 private void label1_Click(object sender, EventArgs e) 40 { 41 42 } 43 44 private void textBox2_TextChanged(object sender, EventArgs e) 45 { 46 47 } 48 49 private void button2_Click(object sender, EventArgs e) 50 { 51 string buffer=textBox2.Text; 52 bool Result_Right = true; 53 int NumofDiv = 0; 54 int Numofdot = 0; 55 int SpaceofDiv=0; 56 if (buffer.Length!=0) 57 { 58 for (int m = 0; m < buffer.Length; m++) 59 { 60 if ((buffer[m] < 48 || buffer[m] > 57) && buffer[m] != '/' && buffer[m] != '.' && buffer[m] == '-') 61 { 62 Result_Right = false; 63 break; 64 } 65 if (buffer[m] == '/') 66 { 67 NumofDiv += 1; 68 if (NumofDiv > 1) 69 { 70 Result_Right = false; 71 break; 72 } 73 SpaceofDiv = m; 74 } 75 if (buffer[m] == '.') 76 { 77 Numofdot += 1; 78 if (Numofdot > 1) 79 { 80 Result_Right = false; 81 break; 82 } 83 } 84 } 85 if (Result_Right) 86 { 87 double result; 88 if (NumofDiv == 1) 89 { 90 string strfor = buffer.Substring(0, SpaceofDiv); 91 string strlat = buffer.Substring(SpaceofDiv + 1, buffer.Length - SpaceofDiv - 1); 92 double former = Convert.ToDouble(strfor); 93 double latter = Convert.ToDouble(strlat); 94 if ((latter - 0) < 1E-7 && (latter - 0) > (-1E-7)) 95 { 96 Result_Right = false; 97 textBox3.Text = "错误!"; 98 return; 99 } 100 result = former / latter; 101 } 102 else 103 { 104 result = Convert.ToDouble(buffer); 105 } 106 if ((result - A.array[numbertest].value) < 1E-2 && (result - A.array[numbertest].value) > -(1E-2)) 107 { 108 Result_Right = true; 109 } 110 else 111 { 112 Result_Right = false; 113 } 114 } 115 if (Result_Right) 116 { 117 //cout << "正确!" << endl << endl; 118 totalR += 1; 119 textBox3.Text = "正确!"; 120 textBox6.Text = totalR.ToString(); 121 } 122 else 123 { 124 //cout << "错误!" << endl << endl; 125 textBox3.Text = "错误!"; 126 } 127 } 128 else 129 { 130 //cout << "错误!" << endl << endl; 131 textBox3.Text = "错误!"; 132 } 133 } 134 135 private void textBox3_TextChanged(object sender, EventArgs e) 136 { 137 138 } 139 140 private void button1_Click(object sender, EventArgs e) 141 { 142 numbertest += 1; 143 textBox4.Text = ((numbertest + 1).ToString()); 144 textBox2.Text = ""; 145 textBox3.Text = ""; 146 textBox1.Text = A.array[numbertest].line1t + System.Environment.NewLine + A.array[numbertest].line2t + System.Environment.NewLine + A.array[numbertest].line3t; 147 } 148 149 private void textBox4_TextChanged(object sender, EventArgs e) 150 { 151 152 } 153 154 private void label3_Click(object sender, EventArgs e) 155 { 156 157 } 158 159 private void textBox5_TextChanged(object sender, EventArgs e) 160 { 161 162 } 163 164 private void textBox6_TextChanged(object sender, EventArgs e) 165 { 166 167 } 168 } 169 public class absoluteValue 170 { 171 public void Do(int[] a) 172 { 173 int numfirst = a[0]; 174 int numsecond = a[1]; 175 int tonumf; //第一个数的绝对值 176 int tonums; 177 int flag_1; 178 int flag_2; 179 int temp; 180 if (numfirst < 0) 181 { 182 tonumf = 0 - numfirst; 183 flag_1 = -1; 184 } 185 else 186 { 187 tonumf = numfirst; 188 flag_1 = 1; 189 } 190 if (numsecond < 0) 191 { 192 tonums = 0 - numsecond; 193 flag_2 = -1; 194 } 195 else 196 { 197 tonums = numsecond; 198 flag_2 = 1; 199 } 200 if (tonums < tonumf) 201 { 202 temp = tonumf; 203 tonumf = tonums; 204 tonums = temp; 205 } 206 int i; 207 for (i = tonumf; i > 0; i--) 208 { 209 if (tonumf % i == 0 && tonums % i == 0) 210 { 211 break; 212 } 213 } 214 if (i != 0) 215 { 216 numfirst = tonumf / i; 217 numsecond = tonums / i; 218 } 219 else 220 { 221 numfirst = tonumf; 222 numsecond = tonums; 223 } 224 if (flag_1 * flag_2 < 0) 225 { 226 numfirst = 0 - numfirst; 227 } 228 } 229 } 230 public class Equation 231 { 232 Random ran = new Random(); 233 public equation[] array; 234 public int NUM_equation = 10; 235 public bool HasMultAndDivi = true; //是否有乘除法 236 public bool HasBrackets = true; //是否有括号 237 public bool HasNegative = false; //加减是否有负数 238 public bool HasRemainder = true; //除法是否有余数 239 public int MaxOfRange = 50; //数值范围上限 240 public int MinOfRange = 0; //数值范围下限 241 public Equation() 242 { 243 array = new equation[9999]; 244 } 245 public void CreateEquation(int a,bool b,bool c,bool d,int e,int f) 246 { 247 NUM_equation = a; 248 HasMultAndDivi = b; 249 HasBrackets = c; 250 HasRemainder = d; 251 MaxOfRange = e; 252 MinOfRange = f; 253 // default_random_engine generator; 254 // uniform_int_distribution<int> distribution(MinOfRange, MaxOfRange); 255 for (int i = 0; i < NUM_equation; i++) 256 { 257 if (ran.Next() % 2 == 0) 258 { 259 array[i].isFraction = false; 260 } 261 else 262 { 263 array[i].isFraction = true; 264 } 265 string middle = null; 266 char sign = '0'; //生成初始运算符 267 do 268 { 269 switch (ran.Next() % 4) 270 { 271 case 0: 272 sign = '+'; 273 break; 274 case 1: 275 sign = '-'; 276 break; 277 case 2: 278 sign = '*'; 279 break; 280 case 3: 281 sign = '/'; 282 break; 283 } 284 } while ((sign == '/' || sign == '*') && !HasMultAndDivi); 285 bool wrong = false; 286 double Ivalue = 0; 287 do 288 { 289 wrong = false; 290 if (!array[i].isFraction) //生成运算数-整数 291 { 292 int numFormer; 293 int numLatter; 294 numFormer = (ran.Next() % (MaxOfRange - MinOfRange + 1)) + MinOfRange;// distribution(generator); 295 do 296 { 297 numLatter = (ran.Next() % (MaxOfRange - MinOfRange + 1)) + MinOfRange;//distribution(generator); 298 } while (numLatter == 0 && sign == '/'); 299 string strnumFormer = numFormer.ToString(); 300 string strnumLatter = numLatter.ToString(); 301 if (sign == '+' || sign == '-') //加减 302 { 303 if (HasNegative) 304 { 305 if (sign == '+') 306 { 307 Ivalue = (double)numFormer + (double)numLatter; 308 } 309 else 310 { 311 Ivalue = (double)numFormer - (double)numLatter; 312 } 313 if (Ivalue < MinOfRange || Ivalue > MaxOfRange) 314 { 315 wrong = true; 316 } 317 else 318 { 319 middle = strnumFormer; 320 middle = middle + sign; 321 if (numLatter < 0) 322 { 323 middle = middle + '(' + strnumLatter + ')'; 324 } 325 else 326 { 327 middle = middle + strnumLatter; 328 } 329 330 //Equation[i].shape = strnumFormer + sign; 331 //Equation[i].shape = Equation[i].shape + strnumLatter; 332 } 333 } 334 else 335 { 336 if (numFormer < 0 || numLatter < 0) 337 { 338 wrong = true; 339 } 340 else 341 { 342 if (sign == '+') 343 { 344 Ivalue = (double)numFormer + (double)numLatter; 345 } 346 else 347 { 348 Ivalue = (double)numFormer - (double)numLatter; 349 } 350 if (Ivalue < MinOfRange || Ivalue > MaxOfRange || Ivalue < 0) 351 { 352 wrong = true; 353 } 354 else 355 { 356 middle = strnumFormer; 357 middle = middle + sign; 358 middle = middle + strnumLatter; 359 //Equation[i].shape = strnumFormer + sign; 360 //Equation[i].shape = Equation[i].shape + strnumLatter; 361 } 362 } 363 } 364 } 365 else if (sign == '*') //乘 366 { 367 Ivalue = (double)numFormer * (double)numLatter; 368 if (Ivalue < MinOfRange || Ivalue > MaxOfRange) 369 { 370 wrong = true; 371 } 372 else 373 { 374 middle = strnumFormer; 375 middle = middle + sign; 376 if (numLatter < 0) 377 { 378 middle = middle + '(' + strnumLatter + ')'; 379 } 380 else 381 { 382 middle = middle + strnumLatter; 383 } 384 //Equation[i].shape = strnumFormer + sign; 385 //Equation[i].shape = Equation[i].shape + strnumLatter; 386 } 387 } 388 else //除 389 { 390 if (HasRemainder) 391 { 392 Ivalue = (double)numFormer / (double)numLatter; 393 if (Ivalue < MinOfRange || Ivalue > MaxOfRange) 394 { 395 wrong = true; 396 } 397 else 398 { 399 middle = strnumFormer; 400 middle = middle + sign; 401 if (numLatter < 0) 402 { 403 middle = middle + '(' + strnumLatter + ')'; 404 } 405 else 406 { 407 middle = middle + strnumLatter; 408 } 409 //Equation[i].shape = strnumFormer + sign; 410 //Equation[i].shape = Equation[i].shape + strnumLatter; 411 } 412 } 413 else 414 { 415 if (numFormer % numLatter != 0) 416 { 417 wrong = true; 418 } 419 else 420 { 421 Ivalue = (double)numFormer / (double)numLatter; 422 if (Ivalue < MinOfRange || Ivalue > MaxOfRange) 423 { 424 wrong = true; 425 } 426 else 427 { 428 middle = strnumFormer; 429 middle = middle + sign; 430 if (numLatter < 0) 431 { 432 middle = middle + '(' + strnumLatter + ')'; 433 } 434 else 435 { 436 middle = middle + strnumLatter; 437 } 438 //Equation[i].shape = strnumFormer + sign; 439 //Equation[i].shape = Equation[i].shape + strnumLatter; 440 } 441 } 442 } 443 } 444 } 445 else //生成运算数-分数 446 { 447 int NumeratorFormer; 448 int NumeratorLatter; 449 int DenominatorFormer; 450 int DenominatorLatter; 451 do 452 { 453 NumeratorFormer = (ran.Next() % (MaxOfRange - MinOfRange + 1)) + MinOfRange;//distribution(generator); 454 } while (NumeratorFormer == 0); 455 do 456 { 457 DenominatorFormer = (ran.Next() % (MaxOfRange - MinOfRange + 1)) + MinOfRange; //distribution(generator); 458 } while (DenominatorFormer == 0 || DenominatorFormer == NumeratorFormer); 459 int[] a1 = new int[] { NumeratorFormer, DenominatorFormer }; 460 absoluteValue One = new absoluteValue(); 461 One.Do(a1); 462 do 463 { 464 NumeratorLatter = (ran.Next() % (MaxOfRange - MinOfRange + 1)) + MinOfRange; //distribution(generator); 465 } while (NumeratorLatter == 0); 466 do 467 { 468 DenominatorLatter = (ran.Next() % (MaxOfRange - MinOfRange + 1)) + MinOfRange; //distribution(generator); 469 } while (DenominatorLatter == 0 || DenominatorLatter == NumeratorLatter); 470 int[] a2 = new int[] { NumeratorLatter, DenominatorLatter }; 471 One.Do(a2); 472 string strNF = NumeratorFormer.ToString(); 473 string strDF = DenominatorFormer.ToString(); 474 string strNL = NumeratorLatter.ToString(); 475 string strDL = DenominatorLatter.ToString(); 476 if (sign == '+' || sign == '-') //加减 477 { 478 if (HasNegative) 479 { 480 if (sign == '+') 481 { 482 Ivalue = (double)NumeratorFormer / (double)DenominatorFormer + (double)NumeratorLatter / (double)DenominatorLatter; 483 } 484 else 485 { 486 Ivalue = (double)NumeratorFormer / (double)DenominatorFormer - (double)NumeratorLatter / (double)DenominatorLatter; 487 } 488 if (Ivalue < MinOfRange || Ivalue > MaxOfRange) 489 { 490 wrong = true; 491 } 492 else 493 { 494 middle = strNF; 495 middle = middle + 'd'; 496 middle = middle + strDF; 497 middle = middle + sign; 498 middle = middle + strNL; 499 middle = middle + 'd'; 500 middle = middle + strDL; 501 //Equation[i].shape = strNF + 'd'; 502 //Equation[i].shape = Equation[i].shape + strDF + sign + strNL + 'd' + strDL; 503 } 504 } 505 else 506 { 507 if (NumeratorFormer < 0 || NumeratorLatter < 0) 508 { 509 wrong = true; 510 } 511 else 512 { 513 if (sign == '+') 514 { 515 Ivalue = (double)NumeratorFormer / (double)DenominatorFormer + (double)NumeratorLatter / (double)DenominatorLatter; 516 } 517 else 518 { 519 Ivalue = (double)NumeratorFormer / (double)DenominatorFormer - (double)NumeratorLatter / (double)DenominatorLatter; 520 } 521 if (Ivalue < MinOfRange || Ivalue > MaxOfRange || Ivalue < 0) 522 { 523 wrong = true; 524 } 525 else 526 { 527 middle = strNF; 528 middle = middle + 'd'; 529 middle = middle + strDF; 530 middle = middle + sign; 531 middle = middle + strNL; 532 middle = middle + 'd'; 533 middle = middle + strDL; 534 //Equation[i].shape = strNF + 'd'; 535 //Equation[i].shape = Equation[i].shape + strDF + sign + strNL + 'd' + strDL; 536 } 537 } 538 } 539 } 540 else //乘除 541 { 542 if (sign == '*') 543 { 544 Ivalue = ((double)NumeratorFormer / (double)DenominatorFormer) * ((double)NumeratorLatter / (double)DenominatorLatter); 545 } 546 else 547 { 548 Ivalue = ((double)NumeratorFormer / (double)DenominatorFormer) / ((double)NumeratorLatter / (double)DenominatorLatter); 549 } 550 if (Ivalue < MinOfRange || Ivalue > MaxOfRange) 551 { 552 wrong = true; 553 } 554 else 555 { 556 middle = strNF; 557 middle = middle + 'd'; 558 middle = middle + strDF; 559 middle = middle + sign; 560 middle = middle + strNL; 561 middle = middle + 'd'; 562 middle = middle + strDL; 563 //Equation[i].shape = strNF + 'd'; 564 //Equation[i].shape = Equation[i].shape + strDF + sign + strNL + 'd' + strDL; 565 } 566 } 567 } 568 } while (wrong); 569 array[i].shape = middle; 570 array[i].value = Ivalue; 571 array[i].num = 2; 572 if (HasBrackets) 573 { 574 if (ran.Next() % 5 != 0) 575 { 576 array[i].shape = '(' + array[i].shape + ')'; 577 MakeBrackets TheM = new MakeBrackets(); 578 equation[] TheE = new equation[] { array[i] }; 579 array[i] = TheM.Add(TheE, MaxOfRange, MinOfRange, HasNegative, HasMultAndDivi, HasRemainder); 580 } 581 } 582 for (int v = 0; v < i; v++) 583 { 584 if (array[i].shape == array[v].shape) 585 { 586 array[i].shape = ""; 587 array[i].value = 0; 588 array[i].num = 0; 589 i = i - 1; 590 break; 591 } 592 593 } 594 //cout << Equation[i].shape << endl;/// 595 } 596 } 597 598 public void Printshape() 599 { 600 for (int i = 0; i < NUM_equation; i++) 601 { 602 if (array[i].isFraction) 603 { 604 string line1 = ""; 605 string line2 = ""; 606 string line3 = ""; 607 bool Fin_Negative = false; 608 bool Fin_Numerator = false; 609 bool Fin_Denominator = false; 610 int NumNumerator = 0; 611 int NumDenominator = 0; 612 for (int j = 0; j < array[i].shape.Length; j++) 613 { 614 if ((array[i].shape[j] < 48 || array[i].shape[j] > 57) && array[i].shape[j] != 'd' && array[i].shape[j] != '-') 615 { 616 Fin_Denominator = true; 617 Fin_Numerator = false; 618 line1 = line1 + " "; 619 line2 = line2 + ' ' + array[i].shape[j] + ' '; 620 line3 = line3 + " "; 621 } 622 else if (array[i].shape[j] == '-') 623 { 624 if (j != 0 && (array[i].shape[j - 1] == '-' || array[i].shape[j - 1] == '+' || array[i].shape[j - 1] == '*' || array[i].shape[j - 1] == '/')) 625 { 626 line1 = line1 + '-'; 627 line2 = line2 + '-'; 628 line3 = line3 + ' '; 629 } 630 else 631 { 632 633 Fin_Denominator = true; 634 Fin_Numerator = false; 635 636 line1 = line1 + " "; 637 line2 = line2 + " - "; 638 line3 = line3 + " "; 639 } 640 } 641 else if (array[i].shape[j] == 'd') 642 { 643 Fin_Numerator = true; 644 Fin_Denominator = false; 645 for (int k = 1; ; k++) 646 { 647 //if ((array[i].shape[j + k]==NULL)||(array[i].shape[j + k] < 48 || array[i].shape[j + k] > 57)) 648 if ((j + k) >= array[i].shape.Length || (array[i].shape[j + k] < 48 || array[i].shape[j + k] > 57)) 649 { 650 NumDenominator = k - 1; 651 break; 652 } 653 } 654 for (int h = 1; ; h++) 655 { 656 if ((j - h < 0) || (array[i].shape[j - h] < 48 || array[i].shape[j - h] > 57)) 657 { 658 NumNumerator = h - 1; 659 break; 660 } 661 } 662 if (NumNumerator > NumDenominator) 663 { 664 for (int n = 0; n < NumNumerator - NumDenominator; n++) 665 { 666 line3 = line3 + ' '; 667 } 668 for (int n = 0; n < NumNumerator; n++) 669 { 670 line2 = line2 + '-'; 671 } 672 } 673 if (NumDenominator > NumNumerator) 674 { 675 for (int n = 0; n < NumDenominator - NumNumerator; n++) 676 { 677 line1 = line1 + ' '; 678 } 679 for (int n = 0; n < NumDenominator; n++) 680 { 681 line2 = line2 + '-'; 682 } 683 } 684 else 685 { 686 for (int n = 0; n < NumDenominator; n++) 687 { 688 line2 = line2 + '-'; 689 } 690 } 691 } 692 else 693 { 694 if (!Fin_Numerator) 695 { 696 line1 = line1 + array[i].shape[j]; 697 } 698 else if (!Fin_Denominator) 699 { 700 line3 = line3 + array[i].shape[j]; 701 } 702 } 703 } 704 /*if (PrintToFile) 705 { 706 ofile << line1 << endl << line2 << endl << line3 << endl << endl; 707 }*/ 708 //else 709 //{ 710 //cout << line1 << endl << line2 << endl << line3 << endl << endl; 711 array[i].line1t = line1; 712 array[i].line2t = line2; 713 array[i].line3t = line3; 714 //} 715 716 } 717 else 718 { 719 //if (PrintToFile) 720 //{ 721 // ofile << array[i].shape << endl << endl; 722 //} 723 //else 724 //{ 725 //cout << array[i].shape << endl << endl; 726 //} 727 array[i].line1t = ""; 728 array[i].line2t = array[i].shape; 729 array[i].line3t = ""; 730 } 731 } 732 } 733 public class MakeBrackets 734 { 735 public equation Add(equation[] thisEquation, int MaxOfRange, int MinOfRange, bool HasNegative, bool HasMultAndDivi, bool HasRemainder) 736 { 737 Random ran = new Random(); 738 bool wrong = false; 739 double value = 0; 740 string interForShape = ""; 741 do 742 { 743 interForShape = (thisEquation[0]).shape; 744 wrong = false; 745 if ((thisEquation[0]).isFraction) //若为分数运算 746 { 747 int numerator; 748 int denominator; 749 do 750 { 751 numerator = (ran.Next() % (MaxOfRange - MinOfRange + 1)) + MinOfRange; //distribution(generator); 752 } while (numerator == 0); 753 do 754 { 755 denominator = (ran.Next() % (MaxOfRange - MinOfRange + 1)) + MinOfRange; //distribution(generator); 756 757 } while (denominator == 0 || denominator == numerator); 758 int[] a3 = new int[] { numerator, denominator }; 759 absoluteValue Two = new absoluteValue(); 760 Two.Do(a3); 761 /*if (numerator > denominator) 762 { 763 int tem; 764 tem = numerator; 765 numerator = denominator; 766 denominator = tem; 767 }*/ 768 string strNumerator = numerator.ToString(); 769 string strDenominator = denominator.ToString(); 770 switch (ran.Next() % 8) 771 { 772 case 0: 773 if (HasNegative) 774 { 775 value = (thisEquation[0]).value + (double)numerator / (double)denominator; 776 if (value > MaxOfRange || value < MinOfRange) 777 { 778 wrong = true; 779 break; 780 } 781 interForShape = '+' + interForShape; 782 interForShape = strDenominator + interForShape; 783 interForShape = 'd' + interForShape; 784 interForShape = strNumerator + interForShape; 785 } 786 else 787 { 788 if (numerator > 0 && denominator > 0) 789 { 790 value = (thisEquation[0]).value + (double)numerator / (double)denominator; 791 if (value > MaxOfRange || value < MinOfRange || value < 0) 792 { 793 wrong = true; 794 break; 795 } 796 interForShape = '+' + interForShape; 797 interForShape = strDenominator + interForShape; 798 interForShape = 'd' + interForShape; 799 interForShape = strNumerator + interForShape; 800 } 801 else 802 { 803 wrong = true; 804 break; 805 } 806 } 807 break; 808 case 1: 809 if (HasNegative) 810 { 811 value = (thisEquation[0]).value + (double)numerator / (double)denominator; 812 if (value > MaxOfRange || value < MinOfRange) 813 { 814 wrong = true; 815 break; 816 } 817 interForShape = interForShape + '+'; 818 interForShape = interForShape + strNumerator; 819 interForShape = interForShape + 'd'; 820 interForShape = interForShape + strDenominator; 821 } 822 else 823 { 824 if (numerator > 0 && denominator > 0) 825 { 826 value = (thisEquation[0]).value + (double)numerator / (double)denominator; 827 if (value > MaxOfRange || value < MinOfRange || value < 0) 828 { 829 wrong = true; 830 break; 831 } 832 interForShape = interForShape + '+'; 833 interForShape = interForShape + strNumerator; 834 interForShape = interForShape + 'd'; 835 interForShape = interForShape + strDenominator; 836 } 837 else 838 { 839 wrong = true; 840 break; 841 } 842 } 843 break; 844 case 2: 845 if (HasNegative) 846 { 847 value = (double)numerator / (double)denominator - (thisEquation[0]).value; 848 if (value > MaxOfRange || value < MinOfRange) 849 { 850 wrong = true; 851 break; 852 } 853 interForShape = '-' + interForShape; 854 interForShape = strDenominator + interForShape; 855 interForShape = 'd' + interForShape; 856 interForShape = strNumerator + interForShape; 857 } 858 else 859 { 860 if (numerator > 0 && denominator > 0) 861 { 862 value = (double)numerator / (double)denominator - (thisEquation[0]).value; 863 if (value > MaxOfRange || value < MinOfRange || value < 0) 864 { 865 wrong = true; 866 break; 867 } 868 interForShape = '-' + interForShape; 869 interForShape = strDenominator + interForShape; 870 interForShape = 'd' + interForShape; 871 interForShape = strNumerator + interForShape; 872 } 873 else 874 { 875 wrong = true; 876 break; 877 } 878 } 879 break; 880 case 3: 881 if (HasNegative) 882 { 883 value = (thisEquation[0]).value - (double)numerator / (double)denominator; 884 if (value > MaxOfRange || value < MinOfRange) 885 { 886 wrong = true; 887 break; 888 } 889 interForShape = interForShape + '-'; 890 interForShape = interForShape + strNumerator; 891 interForShape = interForShape + 'd'; 892 interForShape = interForShape + strDenominator; 893 } 894 else 895 { 896 if (numerator > 0 && denominator > 0) 897 { 898 value = (double)numerator / (double)denominator - (thisEquation[0]).value; 899 if (value > MaxOfRange || value < MinOfRange || value < 0) 900 { 901 wrong = true; 902 break; 903 } 904 interForShape = interForShape + '-'; 905 interForShape = interForShape + strNumerator; 906 interForShape = interForShape + 'd'; 907 interForShape = interForShape + strDenominator; 908 } 909 else 910 { 911 wrong = true; 912 break; 913 } 914 } 915 break; 916 case 4: 917 if (HasMultAndDivi) 918 { 919 value = (double)numerator / (double)denominator * (thisEquation[0]).value; 920 if (value > MaxOfRange || value < MinOfRange) 921 { 922 wrong = true; 923 break; 924 } 925 interForShape = '*' + interForShape; 926 interForShape = strDenominator + interForShape; 927 interForShape = 'd' + interForShape; 928 interForShape = strNumerator + interForShape; 929 } 930 else 931 { 932 wrong = true; 933 } 934 break; 935 case 5: 936 if (HasMultAndDivi) 937 { 938 value = (thisEquation[0]).value * (double)numerator / (double)denominator; 939 if (value > MaxOfRange || value < MinOfRange) 940 { 941 wrong = true; 942 break; 943 } 944 interForShape = interForShape + '*'; 945 interForShape = interForShape + strNumerator; 946 interForShape = interForShape + 'd'; 947 interForShape = interForShape + strDenominator; 948 } 949 else 950 { 951 wrong = true; 952 } 953 break; 954 case 6: 955 if (HasMultAndDivi) 956 { 957 value = (double)numerator / (double)denominator / (thisEquation[0]).value; 958 if (value > MaxOfRange || value < MinOfRange) 959 { 960 wrong = true; 961 break; 962 } 963 interForShape = '/' + interForShape; 964 interForShape = strDenominator + interForShape; 965 interForShape = 'd' + interForShape; 966 interForShape = strNumerator + interForShape; 967 } 968 else 969 { 970 wrong = true; 971 } 972 break; 973 case 7: 974 if (HasMultAndDivi) 975 { 976 value = (thisEquation[0]).value / ((double)numerator / (double)denominator); 977 if (value > MaxOfRange || value < MinOfRange) 978 { 979 wrong = true; 980 break; 981 } 982 interForShape = interForShape + '/'; 983 interForShape = interForShape + strNumerator; 984 interForShape = interForShape + 'd'; 985 interForShape = interForShape + strDenominator; 986 } 987 else 988 { 989 wrong = true; 990 } 991 break; 992 } 993 } 994 else //若为整数运算 995 { 996 int integer; 997 integer = (ran.Next() % (MaxOfRange - MinOfRange + 1)) + MinOfRange; //distribution(generator); 998 string strInteger = integer.ToString(); 999 switch (ran.Next() % 8) 1000 { 1001 case 0: 1002 if (HasNegative) 1003 { 1004 value = (thisEquation[0]).value + (double)integer; 1005 if (value > MaxOfRange || value < MinOfRange) 1006 { 1007 wrong = true; 1008 break; 1009 } 1010 interForShape = '+' + interForShape; 1011 if (integer < 0) 1012 { 1013 //interForShape = '(' + strInteger + ')' + interForShape; 1014 interForShape = ')' + interForShape; 1015 interForShape = strInteger + interForShape; 1016 interForShape = '(' + interForShape; 1017 } 1018 else 1019 { 1020 interForShape = strInteger + interForShape; 1021 } 1022 } 1023 else 1024 { 1025 if (integer > 0) 1026 { 1027 value = (thisEquation[0]).value + (double)integer; 1028 if (value > MaxOfRange || value < MinOfRange || value < 0) 1029 { 1030 wrong = true; 1031 break; 1032 } 1033 interForShape = '+' + interForShape; 1034 interForShape = strInteger + interForShape; 1035 } 1036 else 1037 { 1038 wrong = true; 1039 break; 1040 } 1041 } 1042 break; 1043 case 1: 1044 if (HasNegative) 1045 { 1046 value = (thisEquation[0]).value + (double)integer; 1047 if (value > MaxOfRange || value < MinOfRange) 1048 { 1049 wrong = true; 1050 break; 1051 } 1052 interForShape = interForShape + '+'; 1053 if (integer < 0) 1054 { 1055 //interForShape = interForShape + '(' + strInteger + ')'; 1056 interForShape = interForShape + '('; 1057 interForShape = interForShape + strInteger; 1058 interForShape = interForShape + ')'; 1059 } 1060 else 1061 { 1062 interForShape = interForShape + strInteger; 1063 } 1064 } 1065 else 1066 { 1067 if (integer > 0) 1068 { 1069 value = (thisEquation[0]).value + (double)integer; 1070 if (value > MaxOfRange || value < MinOfRange || value < 0) 1071 { 1072 wrong = true; 1073 break; 1074 } 1075 interForShape = interForShape + '+'; 1076 interForShape = interForShape + strInteger; 1077 } 1078 else 1079 { 1080 wrong = true; 1081 break; 1082 } 1083 } 1084 break; 1085 case 2: 1086 if (HasNegative) 1087 { 1088 value = (double)integer - (thisEquation[0]).value; 1089 if (value > MaxOfRange || value < MinOfRange) 1090 { 1091 wrong = true; 1092 break; 1093 } 1094 interForShape = '-' + interForShape; 1095 if (integer < 0) 1096 { 1097 //interForShape = '(' + strInteger + ')' + interForShape; 1098 interForShape = ')' + interForShape; 1099 interForShape = strInteger + interForShape; 1100 interForShape = '(' + interForShape; 1101 } 1102 else 1103 { 1104 interForShape = strInteger + interForShape; 1105 } 1106 } 1107 else 1108 { 1109 if (integer > 0) 1110 { 1111 value = (double)integer - (thisEquation[0]).value; 1112 if (value > MaxOfRange || value < MinOfRange || value < 0) 1113 { 1114 wrong = true; 1115 break; 1116 } 1117 interForShape = '-' + interForShape; 1118 interForShape = strInteger + interForShape; 1119 } 1120 else 1121 { 1122 wrong = true; 1123 break; 1124 } 1125 } 1126 break; 1127 case 3: 1128 if (HasNegative) 1129 { 1130 value = (thisEquation[0]).value - (double)integer; 1131 if (value > MaxOfRange || value < MinOfRange) 1132 { 1133 wrong = true; 1134 break; 1135 } 1136 interForShape = interForShape + '-'; 1137 if (integer < 0) 1138 { 1139 //interForShape = interForShape + '(' + strInteger + ')'; 1140 interForShape = interForShape + '('; 1141 interForShape = interForShape + strInteger; 1142 interForShape = interForShape + ')'; 1143 } 1144 else 1145 { 1146 interForShape = interForShape + strInteger; 1147 } 1148 } 1149 else 1150 { 1151 if (integer > 0) 1152 { 1153 value = (thisEquation[0]).value - (double)integer; 1154 if (value > MaxOfRange || value < MinOfRange || value < 0) 1155 { 1156 wrong = true; 1157 break; 1158 } 1159 interForShape = interForShape + '-'; 1160 interForShape = interForShape + strInteger; 1161 } 1162 else 1163 { 1164 wrong = true; 1165 break; 1166 } 1167 } 1168 break; 1169 case 4: 1170 if (HasMultAndDivi) 1171 { 1172 value = (double)integer * (thisEquation[0]).value; 1173 if (value > MaxOfRange || value < MinOfRange) 1174 { 1175 wrong = true; 1176 break; 1177 } 1178 interForShape = '*' + interForShape; 1179 if (integer < 0) 1180 { 1181 //interForShape = '(' + strInteger + ')' + interForShape; 1182 interForShape = ')' + interForShape; 1183 interForShape = strInteger + interForShape; 1184 interForShape = '(' + interForShape; 1185 } 1186 else 1187 { 1188 interForShape = strInteger + interForShape; 1189 } 1190 } 1191 else 1192 { 1193 wrong = true; 1194 } 1195 break; 1196 case 5: 1197 if (HasMultAndDivi) 1198 { 1199 value = (thisEquation[0]).value * (double)integer; 1200 if (value > MaxOfRange || value < MinOfRange) 1201 { 1202 wrong = true; 1203 break; 1204 } 1205 interForShape = interForShape + '*'; 1206 if (integer < 0) 1207 { 1208 //interForShape = interForShape + '(' + strInteger + ')'; 1209 interForShape = interForShape + '('; 1210 interForShape = interForShape + strInteger; 1211 interForShape = interForShape + ')'; 1212 } 1213 else 1214 { 1215 interForShape = interForShape + strInteger; 1216 } 1217 } 1218 else 1219 { 1220 wrong = true; 1221 } 1222 break; 1223 case 6: 1224 if (HasMultAndDivi) 1225 { 1226 if ((thisEquation[0]).value < 1E-7 && (thisEquation[0]).value > -(1E-7)) //若除数为0 1227 { 1228 wrong = true; 1229 break; 1230 } 1231 if (HasRemainder) 1232 { 1233 value = (double)integer / (thisEquation[0]).value; 1234 if (value > MaxOfRange || value < MinOfRange) 1235 { 1236 wrong = true; 1237 break; 1238 } 1239 interForShape = '/' + interForShape; 1240 if (integer < 0) 1241 { 1242 //interForShape = '(' + strInteger + ')' + interForShape; 1243 interForShape = ')' + interForShape; 1244 interForShape = strInteger + interForShape; 1245 interForShape = '(' + interForShape; 1246 } 1247 else 1248 { 1249 interForShape = strInteger + interForShape; 1250 } 1251 } 1252 else 1253 { 1254 if ((thisEquation[0]).value - Math.Round(thisEquation[0].value) > 1E-7 || (thisEquation[0]).value - Math.Round(thisEquation[0].value) < -(1E-7)) 1255 { 1256 wrong = true; 1257 break; 1258 } 1259 if (integer % Math.Round(thisEquation[0].value) > 1E-7 || integer % Math.Round(thisEquation[0].value) < -(1E-7)) 1260 { 1261 wrong = true; 1262 break; 1263 } 1264 else 1265 {//// 1266 value = (double)integer / (thisEquation[0]).value; 1267 if (value > MaxOfRange || value < MinOfRange) 1268 { 1269 wrong = true; 1270 break; 1271 } 1272 interForShape = '/' + interForShape; 1273 if (integer < 0) 1274 { 1275 //interForShape = '(' + strInteger + ')' + interForShape; 1276 interForShape = ')' + interForShape; 1277 interForShape = strInteger + interForShape; 1278 interForShape = '(' + interForShape; 1279 } 1280 else 1281 { 1282 interForShape = strInteger + interForShape; 1283 } 1284 } 1285 } 1286 } 1287 else 1288 { 1289 wrong = true; 1290 } 1291 break; 1292 case 7: 1293 if (HasMultAndDivi) 1294 { 1295 if (0 == integer) //若除数为0 1296 { 1297 wrong = true; 1298 break; 1299 } 1300 if (HasRemainder) 1301 { 1302 value = (thisEquation[0]).value / (double)integer; 1303 if (value > MaxOfRange || value < MinOfRange) 1304 { 1305 wrong = true; 1306 break; 1307 } 1308 interForShape = interForShape + '/'; 1309 if (integer < 0) 1310 { 1311 //interForShape = interForShape + '(' + strInteger + ')'; 1312 interForShape = interForShape + '('; 1313 interForShape = interForShape + strInteger; 1314 interForShape = interForShape + ')'; 1315 } 1316 else 1317 { 1318 interForShape = interForShape + strInteger; 1319 } 1320 } 1321 else 1322 { 1323 if ((thisEquation[0]).value - Math.Round(thisEquation[0].value) > 1E-7 || (thisEquation[0]).value - Math.Round(thisEquation[0].value) < -(1E-7)) 1324 { 1325 wrong = true; 1326 break; 1327 } 1328 if (Math.Round(thisEquation[0].value) % integer > 1E-7 || Math.Round(thisEquation[0].value) % integer < -(1E-7)) 1329 { 1330 wrong = true; 1331 break; 1332 } 1333 else 1334 {//// 1335 value = (thisEquation[0]).value / (double)integer; 1336 if (value > MaxOfRange || value < MinOfRange) 1337 { 1338 wrong = true; 1339 break; 1340 } 1341 interForShape = interForShape + '/'; 1342 if (integer < 0) 1343 { 1344 //interForShape = interForShape + '(' + strInteger + ')'; 1345 interForShape = interForShape + '('; 1346 interForShape = interForShape + strInteger; 1347 interForShape = interForShape + ')'; 1348 } 1349 else 1350 { 1351 interForShape = interForShape + strInteger; 1352 } 1353 1354 } 1355 } 1356 } 1357 else 1358 { 1359 wrong = true; 1360 } 1361 break; 1362 } 1363 } 1364 } while (wrong); 1365 (thisEquation[0]).shape = interForShape; 1366 (thisEquation[0]).value = value; 1367 (thisEquation[0]).num += 1; 1368 if (ran.Next() % 3 < 0 && (thisEquation[0]).num < 5) 1369 { 1370 (thisEquation[0]).shape = '(' + (thisEquation[0]).shape + ')'; 1371 return Add(thisEquation, MaxOfRange, MinOfRange, HasNegative, HasMultAndDivi, HasRemainder); 1372 } 1373 else 1374 { 1375 return thisEquation[0]; 1376 } 1377 } 1378 1379 } 1380 } 1381 }
主程序:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Threading.Tasks; 5 using System.Windows.Forms; 6 7 namespace 二四_软件工程20160406 8 { 9 static class Program 10 { 11 /// <summary> 12 /// 应用程序的主入口点。 13 /// </summary> 14 [STAThread] 15 static void Main() 16 { 17 Application.EnableVisualStyles(); 18 Application.SetCompatibleTextRenderingDefault(false); 19 Application.Run(new Form2()); 20 } 21 } 22 }
程序截图:
点“确定”:
输入结果,点“确定”:
点“下一题”:
……
本次结对编程合作者为 李娜。
http://www.cnblogs.com/linanil/p/5360682.html