首页 > 代码库 > C#控制台应用程序之选课系统
C#控制台应用程序之选课系统
本程序以文本文件作为存储媒介,实现了一个简化版的选课系统,主要实现了以下功能
- 对学生信息的增删改查
- 对课程信息的增删改查
- 对学生选课的增删改查
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.IO; 6 using System.Diagnostics; 7 8 namespace curricula_Variable 9 { 10 //课程类 11 class course 12 { 13 public course() 14 { 15 ID = null; namOfCourse = null; 16 } 17 public course(string id, string nam) 18 { 19 ID = id; namOfCourse = nam; 20 } 21 public string namOfCourse; //课程名称 22 private string ID; //课程ID,课程的唯一标示 23 public string id 24 { 25 set { ID = value; } 26 get { return ID; } 27 } 28 } 29 30 //学生类;包含学生基本信息和选课内容 31 class student 32 { 33 public course[] COUR = new course[3]; //所选的课(最多3门) 34 public string name; //姓名 35 private string num; //学号,对学生唯一的标示 36 public string _num 37 { 38 get { return num; } 39 set { num = value; } 40 } 41 public string placeNative; //籍贯 42 public string tel; //电话 43 public string dormitoryRoom; //宿舍 44 } 45 46 class Program 47 { 48 //给定一个学生数组(最多100人) 49 static student[] stu = new student[100]; 50 static int countStudent = -1; 51 52 //给定一个课程数组(最多10门课) 53 static course[] cour = new course[10]; 54 static int countCourse = -1; 55 56 static void Main(string[] args) 57 { 58 //readStuInforFromTxt(); 59 //readCourInfoFromTxt(); 60 //readStuCourSelectInforFromTxt(); 61 homePage(); 62 HomePage: 63 Console.Write("请选择你想使用的功能:"); 64 int iInput; 65 //输入一个数字,如果是0到12,就进行相应操作,否则重新输入 66 Input: 67 string str = Console.ReadLine(); 68 if ((Int32.TryParse(str, out iInput)) == false) 69 { 70 Console.Write("输入的不是整数,请重新输入:"); 71 goto Input; 72 } 73 if (iInput >= 0 && iInput < 13) 74 switch (iInput) 75 { 76 case 1: 77 stuInforAdd(); 78 goto HomePage; 79 80 case 2: 81 stuInforDel(); 82 goto HomePage; 83 84 case 3: 85 stuInforAlter(); 86 goto HomePage; 87 88 case 4: 89 stuInforSelect(); 90 goto HomePage; 91 case 5: 92 courseInforAdd(); 93 goto HomePage; 94 95 case 6: 96 courseInforDel(); 97 goto HomePage; 98 99 case 7: 100 courseInforAlter(); 101 goto HomePage; 102 103 case 8: 104 courseInforSelect(); 105 goto HomePage; 106 107 case 9: 108 courseSelectAdd(); 109 goto HomePage; 110 111 case 10: 112 courseSelectDel(); 113 goto HomePage; 114 115 case 11: 116 courseSelectAlter(); 117 goto HomePage; 118 119 case 12: 120 courseSelectSelect(); 121 goto HomePage; 122 123 case 0: 124 Environment.Exit(0); 125 return; 126 } 127 else 128 { 129 Console.Write("输入数据超出范围,请重新输入:"); 130 goto Input; 131 } 132 Console.ReadKey(); 133 Environment.Exit(0); 134 } 135 136 //主页 137 static void homePage() 138 { 139 for (int i = 0; i < 50; i++) 140 { 141 Console.Write("*"); 142 } 143 Console.WriteLine(); 144 for (int i = 0; i < 17; i++) 145 Console.Write(" "); 146 Console.WriteLine("欢迎使用选课系统"); 147 for (int i = 0; i < 17; i++) 148 Console.Write(" "); 149 Console.WriteLine("1、 增加学生信息"); 150 for (int i = 0; i < 17; i++) 151 Console.Write(" "); 152 Console.WriteLine("2、 删除学生信息"); 153 for (int i = 0; i < 17; i++) 154 Console.Write(" "); 155 Console.WriteLine("3、 修改学生信息"); 156 for (int i = 0; i < 17; i++) 157 Console.Write(" "); 158 Console.WriteLine("4、 查询学生信息"); 159 for (int i = 0; i < 17; i++) 160 Console.Write(" "); 161 Console.WriteLine("5、 增加课程信息"); 162 for (int i = 0; i < 17; i++) 163 Console.Write(" "); 164 Console.WriteLine("6、 删除课程信息"); 165 for (int i = 0; i < 17; i++) 166 Console.Write(" "); 167 Console.WriteLine("7、 修改课程信息"); 168 for (int i = 0; i < 17; i++) 169 Console.Write(" "); 170 Console.WriteLine("8、 查询课程信息"); 171 for (int i = 0; i < 17; i++) 172 Console.Write(" "); 173 Console.WriteLine("9、 增加选课内容"); 174 for (int i = 0; i < 17; i++) 175 Console.Write(" "); 176 Console.WriteLine("10、删除已选课程"); 177 for (int i = 0; i < 17; i++) 178 Console.Write(" "); 179 Console.WriteLine("11、修改课程信息"); 180 for (int i = 0; i < 17; i++) 181 Console.Write(" "); 182 Console.WriteLine("12、查询选课信息"); 183 for (int i = 0; i < 17; i++) 184 Console.Write(" "); 185 Console.WriteLine("0、 退出选课系统"); 186 for (int i = 0; i < 50; i++) 187 { 188 Console.Write("*"); 189 } 190 Console.WriteLine(); 191 } 192 193 //判断手机号是否合法 194 public static bool isPhone(string str_handset) 195 { 196 return System.Text.RegularExpressions.Regex.IsMatch(str_handset, @"^[1]+[3,5,7,8]+\d{9}"); 197 } 198 199 //判断学号是否合法 200 public static bool isNumber(string str_handset) 201 { 202 return System.Text.RegularExpressions.Regex.IsMatch(str_handset, @"^[1]+[2,3,4,5,6]+[0,1]+\d{6}"); 203 } 204 205 /* 学生信息管理 206 * 1、添加学生信息 207 * 2、删除学生信息 208 * 3、修改学生信息 209 * 4、查询学生信息 */ 210 211 //学生信息添加,添加学号、姓名 212 static void stuInforAdd() 213 { 214 readStuInforFromTxt(); 215 Console.Clear(); 216 student stud = new student(); 217 Console.Write("请输入学生学号:"); 218 Input: 219 string str = Console.ReadLine(); 220 if (!isNumber(str) || str.Length != 9) 221 { 222 Console.Write("学号格式不正确,请重新输入:"); 223 goto Input; 224 } 225 else 226 { 227 int ct = 0, isExit = 0; 228 while (stu[ct] != null) 229 { 230 if (stu[ct]._num == str) 231 { 232 isExit = -1; 233 break; 234 } 235 else 236 ct++; 237 } 238 if (isExit == -1) 239 { 240 Console.Clear(); 241 homePage(); 242 Console.WriteLine("该学生信息已存在!"); 243 return; 244 } 245 else 246 stud._num = str; 247 } 248 Console.Write("请输入学生姓名:"); 249 Input1: 250 string str1 = Console.ReadLine(); 251 if (str1 == "") 252 { 253 Console.Write("姓名不能为空,请重新输入:"); 254 goto Input1; 255 } 256 else 257 stud.name = str1; 258 Console.Write("请输入籍贯:"); 259 Input2: 260 string str2 = Console.ReadLine(); 261 if (str2 == "") 262 { 263 Console.Write("籍贯不能为空,请重新输入:"); 264 goto Input2; 265 } 266 else 267 stud.placeNative = str2; 268 269 Console.Write("请输入学生联系电话:"); 270 Input3: 271 str = Console.ReadLine(); 272 if (!(isPhone(str)) || str.Length != 11) 273 { 274 Console.Write("联系电话格式不正确,请重新输入:"); 275 goto Input3; 276 } 277 else 278 stud.tel = str; 279 280 Console.Write("请输入学生宿舍号:"); 281 Input4: 282 int m; 283 str = Console.ReadLine(); 284 if (!(Int32.TryParse(str, out m))) 285 { 286 Console.Write("宿舍号格式不正确,请重新输入:"); 287 goto Input4; 288 } 289 else 290 stud.dormitoryRoom = str; 291 stu[++countStudent] = stud; 292 Console.Clear(); 293 homePage(); 294 saveStuInforToTxt(); 295 Console.WriteLine("添加成功!"); 296 } 297 298 //学生信息删除,删除个人所有信息 299 static void stuInforDel() 300 { 301 readStuInforFromTxt(); 302 Console.Clear(); 303 Console.Write("请输入您想删除的学生学号:"); 304 string i = Console.ReadLine(); 305 Input: 306 string str = Console.ReadLine(); 307 if (!isNumber(i) || i.Length != 9) 308 { 309 Console.Write("学号格式不正确,请重新输入:"); 310 goto Input; 311 } 312 int n = 0; 313 homePage(); 314 while (stu[n] != null) 315 { 316 if (stu[n]._num == i) 317 { 318 List<student> list = stu.ToList(); 319 list.RemoveAt(n); 320 student[] newarr = list.ToArray(); 321 stu = newarr; 322 countStudent--; 323 Console.WriteLine("删除成功!"); 324 saveStuInforToTxt(); 325 return; 326 } 327 else 328 n++; 329 } 330 Console.WriteLine("查无此人!"); 331 } 332 333 //学生信息修改,修改除学号以外的信息 334 static void stuInforAlter() 335 { 336 readStuInforFromTxt(); 337 Console.Clear(); 338 Console.Write("请输入您想修改的学生的学号:"); 339 Input: 340 string i = Console.ReadLine(); 341 if (!isNumber(i) || i.Length != 9) 342 { 343 Console.Write("学号格式不正确,请重新输入:"); 344 goto Input; 345 } 346 string str = ""; 347 int n = 0; 348 homePage(); 349 while (stu[n] != null) 350 { 351 if (stu[n]._num == i) 352 { 353 Console.Write("请输入修改后的姓名:"); 354 Input1: 355 str = Console.ReadLine(); 356 if (str == "") 357 { 358 Console.Write("姓名不能为空,请重新输入:"); 359 goto Input1; 360 } 361 else 362 stu[n].name = str; 363 364 Console.Write("请输入修改后的籍贯:"); 365 Input2: 366 str = Console.ReadLine(); 367 if (str == "") 368 { 369 Console.Write("籍贯不能为空,请重新输入:"); 370 goto Input2; 371 } 372 else 373 stu[n].placeNative = str; 374 375 Console.Write("请输入修改后的联系电话:"); 376 Input3: 377 str = Console.ReadLine(); 378 if (!isPhone(str) || str.Length != 11) 379 { 380 Console.Write("联系电话格式不正确,请重新输入:"); 381 goto Input3; 382 } 383 else 384 stu[n].tel = str; 385 386 Console.Write("请输入修改后的宿舍号:"); 387 Input4: 388 int m; 389 str = Console.ReadLine(); 390 if (!(Int32.TryParse(str, out m))) 391 { 392 Console.Write("宿舍号格式不正确,请重新输入:"); 393 goto Input4; 394 } 395 else 396 stu[n].dormitoryRoom = str; 397 Console.Clear(); 398 homePage(); 399 saveStuInforToTxt(); 400 Console.WriteLine("修改成功!"); 401 return; 402 } 403 else 404 n++; 405 } 406 Console.WriteLine("查无此人!"); 407 } 408 409 //学生信息查询,通过学号查询姓名 410 static void stuInforSelect() 411 { 412 readStuInforFromTxt(); 413 Console.Clear(); 414 Console.Write("请输入您想查询的学生学号:"); 415 Input: 416 string i = Console.ReadLine(); 417 if (!isNumber(i)) 418 { 419 Console.Write("学号格式不正确,请重新输入:"); 420 goto Input; 421 } 422 int n = 0; 423 homePage(); 424 while (stu[n] != null) 425 { 426 if (stu[n]._num == i) 427 { 428 Console.WriteLine("您所查询的学生的姓名为:{0}", stu[n].name); 429 Console.WriteLine("您所查询的学生的籍贯为:{0}", stu[n].placeNative); 430 Console.WriteLine("您所查询的学生的电话为:{0}", stu[n].tel); 431 Console.WriteLine("您所查询的学生的宿舍号:{0}", stu[n].dormitoryRoom); 432 saveStuInforToTxt(); 433 return; 434 } 435 else 436 n++; 437 } 438 Console.WriteLine("查无此人!"); 439 } 440 441 442 /* 课程信息管理 443 * 1、增加课程信息 444 * 2、删除课程信息 445 * 3、修改课程信息 446 * 4、查询课程信息 */ 447 448 //课程信息添加,通过添加课程ID添加课程 449 static void courseInforAdd() 450 { 451 readCourInfoFromTxt(); 452 if (countCourse == 9) 453 { 454 Console.Clear(); 455 homePage(); 456 saveCourseInforToTxt(); 457 Console.WriteLine("课程已满!若要继续添加,请先删除部分课程!"); 458 return; 459 } 460 Console.Clear(); 461 course Cour = new course(); 462 Console.Write("请输入课程ID:"); 463 Input: 464 string str = Console.ReadLine(); 465 int m; 466 if (!(Int32.TryParse(str, out m))) 467 { 468 Console.Write("课程ID格式不正确,请重新输入:"); 469 goto Input; 470 } 471 else 472 { 473 int ct = 0, isExit = 0; 474 while (cour[ct] != null) 475 { 476 if (cour[ct].id == str) 477 { 478 isExit = -1; 479 break; 480 } 481 else 482 ct++; 483 } 484 if (isExit == -1) 485 { 486 Console.Clear(); 487 homePage(); 488 saveCourseInforToTxt(); 489 Console.WriteLine("该课程信息已存在!"); 490 return; 491 } 492 else 493 Cour.id = str; 494 } 495 Console.Write("请输入课程名称:"); 496 Input1: 497 string str1 = Console.ReadLine(); 498 if (str1 == "") 499 { 500 Console.Write("课程名称不能为空,请重新输入:"); 501 goto Input1; 502 } 503 else 504 Cour.namOfCourse = str1; 505 cour[++countCourse] = Cour; 506 Console.Clear(); 507 homePage(); 508 Console.WriteLine("添加成功!"); 509 saveCourseInforToTxt(); 510 } 511 512 //课程信息删除,通过课程ID删除相关课程 513 static void courseInforDel() 514 { 515 readCourInfoFromTxt(); 516 Console.Clear(); 517 Console.Write("请输入您想删除的课程ID:"); 518 string i = Console.ReadLine(); 519 int n = 0; 520 homePage(); 521 Console.Clear(); 522 while (n<10&&cour[n] != null) 523 { 524 if (cour[n].id == i) 525 { 526 List<course> list = cour.ToList(); 527 list.RemoveAt(n); 528 course[] newarr = list.ToArray(); 529 cour = newarr; 530 countCourse--; 531 homePage(); 532 Console.WriteLine("删除成功!"); 533 for (int k = 0; k < countStudent; k++) 534 { 535 for (int j = 0; stu[k].COUR[j] != null; j++) 536 { 537 if (stu[k].COUR[j].id == i) 538 { 539 List<course> list1 = stu[k].COUR.ToList(); 540 list1.RemoveAt(j); 541 course[] newarr1 = list1.ToArray(); 542 stu[k].COUR = newarr1; 543 } 544 } 545 } 546 saveCourseInforToTxt(); 547 return; 548 } 549 else 550 n++; 551 } 552 homePage(); 553 saveCourseInforToTxt(); 554 Console.WriteLine("查无此课程!"); 555 } 556 557 //课程信息修改,通过ID删除除ID以外的信息 558 static void courseInforAlter() 559 { 560 readCourInfoFromTxt(); 561 Console.Clear(); 562 Console.Write("请输入您想修改的课程的ID:"); 563 Input1: 564 string i = Console.ReadLine(); 565 int n = 0; 566 while (cour[n] != null) 567 { 568 if (cour[n].id == i) 569 { 570 Console.Write("请输入修改后的课程名称:"); 571 string str1 = Console.ReadLine(); 572 if (str1 == "") 573 { 574 Console.Write("课程名称不能为空,请重新输入:"); 575 goto Input1; 576 } 577 else 578 cour[n].namOfCourse = str1; 579 homePage(); 580 saveCourseInforToTxt(); 581 Console.WriteLine("修改成功!"); 582 return; 583 } 584 else 585 n++; 586 } 587 homePage(); 588 saveCourseInforToTxt(); 589 Console.WriteLine("查无此课程!"); 590 } 591 592 //课程查询,通过ID查询课程 593 static void courseInforSelect() 594 { 595 readCourInfoFromTxt(); 596 Console.Clear(); 597 Console.Write("请输入您想查询的课程ID:"); 598 string i = Console.ReadLine(); 599 int n = 0; 600 homePage(); 601 while (cour[n] != null) 602 { 603 if (cour[n].id == i) 604 { 605 saveCourseInforToTxt(); 606 Console.WriteLine("您所查询的课程名称为:{0}", cour[n].namOfCourse); 607 return; 608 } 609 else 610 n++; 611 } 612 saveCourseInforToTxt(); 613 Console.WriteLine("查无此课程!"); 614 } 615 616 617 /* 选课管理 618 * 1、增加所选课程 619 * 2、取消已选课程 620 * 3、修改已选课程 621 * 4、查询已选课程 */ 622 623 //通过学号和课程ID增加个人选的课程 624 static void courseSelectAdd() 625 { 626 readCourInfoFromTxt(); 627 readStuInforFromTxt(); 628 readStuCourSelectInforFromTxt(); 629 if (stu[0] == null) 630 { 631 Console.Clear(); 632 homePage(); 633 Console.WriteLine("暂无学生信息!"); 634 saveCourseInforToTxt(); 635 saveStuCourSelectInforToTxt(); 636 saveStuInforToTxt(); 637 return; 638 } 639 if (cour[0] == null) 640 { 641 Console.Clear(); 642 homePage(); 643 Console.WriteLine("暂无课程信息!"); 644 saveCourseInforToTxt(); 645 saveStuCourSelectInforToTxt(); 646 saveStuInforToTxt(); 647 return; 648 } 649 Console.Clear(); 650 Console.Write("请输入您的学号:"); 651 Input: 652 string str = Console.ReadLine(); 653 if (!isNumber(str) || str.Length != 9) 654 { 655 Console.Write("学号格式不正确,请重新输入:"); 656 goto Input; 657 } 658 else 659 { 660 int i = 0; 661 while(stu[i]._num != str) 662 { 663 i++; 664 if (i >countStudent) 665 { 666 Console.Clear(); 667 homePage(); 668 Console.WriteLine("查无此人!"); 669 saveCourseInforToTxt(); 670 saveStuCourSelectInforToTxt(); 671 saveStuInforToTxt(); 672 return; 673 } 674 } 675 Console.Write("请输入您想选的课程名称:"); 676 str = Console.ReadLine(); 677 int l = 0; 678 for (; l<10&&cour[l] != null; l++) { } 679 int j = 0; 680 here: 681 if (cour[j] != null && cour[j].namOfCourse != str) 682 { 683 if (j < l ) 684 { 685 j++; 686 goto here; 687 } 688 else 689 { 690 Console.Clear(); 691 homePage(); 692 Console.WriteLine("查无此课程!"); 693 saveCourseInforToTxt(); 694 saveStuCourSelectInforToTxt(); 695 saveStuInforToTxt(); 696 return; 697 } 698 } 699 else 700 { 701 if (cour[j] == null) 702 { 703 Console.Clear(); 704 homePage(); 705 Console.WriteLine("查无此课程!"); 706 saveCourseInforToTxt(); 707 saveStuCourSelectInforToTxt(); 708 saveStuInforToTxt(); 709 return; 710 } 711 if ((stu[i].COUR[0]!=null&&stu[i].COUR[0].id == cour[j].id) || (stu[i].COUR[1]!=null&&stu[i].COUR[1].id== cour[j].id) || (stu[i].COUR[2]!=null&&stu[i].COUR[2].id == cour[j].id)) 712 { 713 Console.Clear(); 714 homePage(); 715 Console.WriteLine("该门课已选,不能重复提交!"); 716 saveCourseInforToTxt(); 717 saveStuCourSelectInforToTxt(); 718 saveStuInforToTxt(); 719 return; 720 } 721 if (stu[i].COUR[0] == null) 722 stu[i].COUR[0] = cour[j]; 723 else if 724 (stu[i].COUR[1] == null) 725 stu[i].COUR[1] = cour[j]; 726 else if 727 (stu[i].COUR[2] == null) 728 stu[i].COUR[2] = cour[j]; 729 else 730 { 731 homePage(); 732 Console.WriteLine("您所选课程已达最大数额!"); 733 saveCourseInforToTxt(); 734 saveStuCourSelectInforToTxt(); 735 saveStuInforToTxt(); 736 return; 737 } 738 Console.Clear(); 739 homePage(); 740 Console.WriteLine("选课成功!"); 741 saveCourseInforToTxt(); 742 saveStuCourSelectInforToTxt(); 743 saveStuInforToTxt(); 744 } 745 } 746 } 747 748 //删除选课,通过学号和课程ID删除个人选的课程 749 static void courseSelectDel() 750 { 751 readStuInforFromTxt(); 752 readCourInfoFromTxt(); 753 readStuCourSelectInforFromTxt(); 754 if (stu[0] == null) 755 { 756 Console.Clear(); 757 homePage(); 758 Console.WriteLine("暂无学生信息!"); 759 saveCourseInforToTxt(); 760 saveStuCourSelectInforToTxt(); 761 saveStuInforToTxt(); 762 return; 763 } 764 if (cour[0] == null) 765 { 766 Console.Clear(); 767 homePage(); 768 Console.WriteLine("暂无课程信息!"); 769 saveCourseInforToTxt(); 770 saveStuCourSelectInforToTxt(); 771 saveStuInforToTxt(); 772 return; 773 } 774 Console.Clear(); 775 Console.Write("请输入您的学号:"); 776 Input: 777 string str = Console.ReadLine(); 778 if (!isNumber(str) || str.Length != 9) 779 { 780 Console.Write("学号格式不正确,请重新输入:"); 781 goto Input; 782 } 783 else 784 { 785 int l = 0; 786 for (; stu[l] != null; l++) { } 787 int i = 0; 788 Judge: 789 if (stu[i]._num != str) 790 { 791 if (i < l - 1) 792 { 793 i++; 794 goto Judge; 795 } 796 else 797 { 798 homePage(); 799 Console.WriteLine("查无此人!"); 800 } 801 saveCourseInforToTxt(); 802 saveStuCourSelectInforToTxt(); 803 saveStuInforToTxt(); 804 return; 805 } 806 else 807 Console.Write("请输入您想取消的课程名称:"); 808 str = Console.ReadLine(); 809 l = 0; 810 for (; l<10&&cour[l] != null; l++) { } 811 int j = 0; 812 Here: 813 if (cour[j]!=null&&cour[j].namOfCourse != str) 814 { 815 if (j < l) 816 { 817 j++; 818 goto Here; 819 } 820 else 821 { 822 Console.Clear(); 823 homePage(); 824 Console.Write("查无此课程!"); 825 saveCourseInforToTxt(); 826 saveStuCourSelectInforToTxt(); 827 saveStuInforToTxt(); 828 return; 829 } 830 } 831 else 832 { 833 if (stu[i].COUR[0] == null) 834 { 835 Console.Clear(); 836 homePage(); 837 Console.WriteLine("您尚未选该门课!"); 838 saveCourseInforToTxt(); 839 saveStuCourSelectInforToTxt(); 840 saveStuInforToTxt(); 841 return; 842 } 843 else if (stu[i].COUR[1] == null) 844 { 845 if (stu[i].COUR[0].namOfCourse == str) 846 { 847 stu[i].COUR[0] = null; 848 Console.Clear(); 849 homePage(); 850 Console.WriteLine("取消成功!"); 851 saveCourseInforToTxt(); 852 saveStuCourSelectInforToTxt(); 853 saveStuInforToTxt(); 854 return; 855 } 856 else 857 { 858 Console.Clear(); 859 homePage(); 860 Console.WriteLine("您尚未选该门课!"); 861 saveCourseInforToTxt(); 862 saveStuCourSelectInforToTxt(); 863 saveStuInforToTxt(); 864 return; 865 } 866 } 867 else if (stu[i].COUR[2] == null) 868 { 869 if (stu[i].COUR[0].namOfCourse == str) 870 { 871 stu[i].COUR[0] = stu[i].COUR[1]; 872 stu[i].COUR[1] = null; 873 Console.Clear(); 874 homePage(); 875 Console.WriteLine("取消成功!"); 876 saveCourseInforToTxt(); 877 saveStuCourSelectInforToTxt(); 878 saveStuInforToTxt(); 879 return; 880 } 881 else if (stu[i].COUR[1].namOfCourse == str) 882 { 883 stu[i].COUR[1] = null; 884 Console.Clear(); 885 homePage(); 886 saveCourseInforToTxt(); 887 saveStuCourSelectInforToTxt(); 888 saveStuInforToTxt(); 889 Console.WriteLine("取消成功!"); 890 return; 891 } 892 else 893 { 894 Console.Clear(); 895 homePage(); 896 Console.WriteLine("您尚未选该门课!"); 897 saveCourseInforToTxt(); 898 saveStuCourSelectInforToTxt(); 899 saveStuInforToTxt(); 900 return; 901 } 902 } 903 else 904 { 905 if (stu[i].COUR[0].namOfCourse == str) 906 { 907 stu[i].COUR[0] = stu[i].COUR[1]; 908 stu[i].COUR[1] = stu[i].COUR[2]; 909 stu[i].COUR[2] = null; 910 Console.Clear(); 911 homePage(); 912 Console.WriteLine("取消成功!"); 913 saveCourseInforToTxt(); 914 saveStuCourSelectInforToTxt(); 915 saveStuInforToTxt(); 916 return; 917 } 918 else if (stu[i].COUR[1].namOfCourse == str) 919 { 920 stu[i].COUR[1] = stu[i].COUR[2]; 921 stu[i].COUR[2] = null; 922 Console.Clear(); 923 homePage(); 924 Console.WriteLine("取消成功!"); 925 saveCourseInforToTxt(); 926 saveStuCourSelectInforToTxt(); 927 saveStuInforToTxt(); 928 return; 929 } 930 else if (stu[i].COUR[2].namOfCourse == str) 931 { 932 stu[i].COUR[2] = null; 933 Console.Clear(); 934 homePage(); 935 Console.WriteLine("取消成功!"); 936 saveCourseInforToTxt(); 937 saveStuCourSelectInforToTxt(); 938 saveStuInforToTxt(); 939 return; 940 } 941 else 942 { 943 Console.Clear(); 944 homePage(); 945 Console.WriteLine("您尚未选该门课!"); 946 saveCourseInforToTxt(); 947 saveStuCourSelectInforToTxt(); 948 saveStuInforToTxt(); 949 return; 950 } 951 } 952 } 953 } 954 } 955 956 //修改选课,通过学号和课程ID修改所选的课程 957 static void courseSelectAlter() 958 { 959 readStuInforFromTxt(); 960 readCourInfoFromTxt(); 961 readStuCourSelectInforFromTxt(); 962 if (stu[0] == null) 963 { 964 Console.Clear(); 965 homePage(); 966 Console.WriteLine("暂无学生信息!"); 967 saveCourseInforToTxt(); 968 saveStuCourSelectInforToTxt(); 969 saveStuInforToTxt(); 970 return; 971 } 972 if (cour[0] == null) 973 { 974 Console.Clear(); 975 homePage(); 976 Console.WriteLine("暂无课程信息!"); 977 saveCourseInforToTxt(); 978 saveStuCourSelectInforToTxt(); 979 saveStuInforToTxt(); 980 return; 981 } 982 Console.Clear(); 983 Console.Write("请输入您的学号:"); 984 string str1 = ""; 985 Input: 986 string str = Console.ReadLine(); 987 if (!isNumber(str) || str.Length != 9) 988 { 989 Console.Write("学号格式不正确,请重新输入:"); 990 goto Input; 991 } 992 else 993 { 994 int l = 0; 995 for (; stu[l] != null; l++) { } 996 int i = 0; 997 hERe: 998 if (stu[i]._num != str) 999 {1000 if (i < l - 1)1001 {1002 i++;1003 goto hERe;1004 }1005 else1006 {1007 homePage();1008 Console.WriteLine("查无此人!");1009 saveCourseInforToTxt();1010 saveStuCourSelectInforToTxt();1011 saveStuInforToTxt();1012 return;1013 }1014 }1015 else1016 Console.Write("请输入您想修改之后的课程名称:");1017 str = Console.ReadLine();1018 l = 0;1019 for (; cour[l] != null; l++) { }1020 int j = 0;1021 hEre:1022 if (cour[j].namOfCourse != str)1023 {1024 if (j < l - 1)1025 {1026 j++;1027 goto hEre;1028 }1029 else1030 Console.Write("查无此课程!");1031 saveCourseInforToTxt();1032 saveStuCourSelectInforToTxt();1033 saveStuInforToTxt();1034 return;1035 }1036 else1037 {1038 //输入不想要的课,判断已选课中是否存在,若存在进行替换,反之函数结束1039 Console.Write("请输入您不想要的课程名称:");1040 str1 = Console.ReadLine();1041 1042 if (stu[i].COUR[0] == null)1043 {1044 Console.Clear();1045 homePage();1046 Console.Write("您尚未选门课,不能进行修改操作!");1047 saveCourseInforToTxt();1048 saveStuCourSelectInforToTxt();1049 saveStuInforToTxt();1050 return;1051 }1052 else if (stu[i].COUR[1] == null)1053 {1054 if (stu[i].COUR[0].namOfCourse == str1)1055 {1056 stu[i].COUR[0] = cour[j];1057 Console.Clear();1058 homePage();1059 Console.WriteLine("修改成功!");1060 saveCourseInforToTxt();1061 saveStuCourSelectInforToTxt();1062 saveStuInforToTxt();1063 return;1064 }1065 else1066 {1067 Console.Clear();1068 homePage();1069 Console.WriteLine("您尚未选该门课!");1070 saveCourseInforToTxt();1071 saveStuCourSelectInforToTxt();1072 saveStuInforToTxt();1073 return;1074 }1075 }1076 else if (stu[i].COUR[2] == null)1077 {1078 if (stu[i].COUR[0].namOfCourse == str1)1079 {1080 stu[i].COUR[0] = cour[j];1081 Console.Clear();1082 homePage();1083 Console.WriteLine("修改成功!");1084 saveCourseInforToTxt();1085 saveStuCourSelectInforToTxt();1086 saveStuInforToTxt();1087 return;1088 }1089 else if (stu[i].COUR[1].namOfCourse == str1)1090 {1091 stu[i].COUR[1] = cour[j];1092 Console.Clear();1093 homePage();1094 Console.WriteLine("修改成功!");1095 saveCourseInforToTxt();1096 saveStuCourSelectInforToTxt();1097 saveStuInforToTxt();1098 return;1099 }1100 else1101 {1102 Console.Clear();1103 homePage();1104 Console.WriteLine("您尚未选该门课!");1105 saveCourseInforToTxt();1106 saveStuCourSelectInforToTxt();1107 saveStuInforToTxt();1108 return;1109 }1110 }1111 else1112 {1113 if (stu[i].COUR[0].namOfCourse == str1)1114 {1115 stu[i].COUR[0] = cour[j];1116 Console.Clear();1117 homePage();1118 Console.WriteLine("修改成功!");1119 saveCourseInforToTxt();1120 saveStuCourSelectInforToTxt();1121 saveStuInforToTxt();1122 return;1123 }1124 else if (stu[i].COUR[1].namOfCourse == str1)1125 {1126 stu[i].COUR[1] = cour[j];1127 Console.Clear();1128 homePage();1129 Console.WriteLine("修改成功!");1130 saveCourseInforToTxt();1131 saveStuCourSelectInforToTxt();1132 saveStuInforToTxt();1133 return;1134 }1135 else if (stu[i].COUR[2].namOfCourse == str1)1136 {1137 stu[i].COUR[2] = cour[j];1138 Console.Clear();1139 homePage();1140 Console.WriteLine("修改成功!");1141 saveCourseInforToTxt();1142 saveStuCourSelectInforToTxt();1143 saveStuInforToTxt();1144 return;1145 }1146 else1147 {1148 Console.Clear();1149 homePage();1150 Console.WriteLine("您尚未选该门课!");1151 saveCourseInforToTxt();1152 saveStuCourSelectInforToTxt();1153 saveStuInforToTxt();1154 return;1155 }1156 }1157 }1158 }1159 }1160 1161 //查找选课,通过学号查找所选的课程1162 static void courseSelectSelect()1163 {1164 readStuInforFromTxt();1165 readCourInfoFromTxt();1166 readStuCourSelectInforFromTxt();1167 int i = 0;1168 if (stu[0] == null)1169 {1170 Console.Clear();1171 homePage();1172 Console.WriteLine("暂无学生信息!");1173 saveCourseInforToTxt();1174 saveStuCourSelectInforToTxt();1175 saveStuInforToTxt();1176 return;1177 }1178 if (cour[0] == null)1179 {1180 Console.Clear();1181 homePage();1182 Console.WriteLine("暂无课程信息!");1183 saveCourseInforToTxt();1184 saveStuCourSelectInforToTxt();1185 saveStuInforToTxt();1186 return;1187 }1188 Console.Clear();1189 Console.Write("请输入您的学号:");1190 Input:1191 string str = Console.ReadLine();1192 if (!isNumber(str) || str.Length != 9)1193 {1194 Console.Write("学号格式不正确,请重新输入:");1195 goto Input;1196 }1197 else1198 {1199 int l = 0;1200 for (; stu[l] != null; l++) { }1201 HERE:1202 if (stu[i]!=null&&stu[i]._num != str)1203 {1204 if (i < l - 1)1205 {1206 i++;1207 goto HERE;1208 }1209 else1210 {1211 saveCourseInforToTxt();1212 saveStuCourSelectInforToTxt();1213 saveStuInforToTxt();1214 homePage();1215 Console.WriteLine("查无此人!");1216 return;1217 }1218 }1219 else1220 {1221 homePage();1222 if (stu[i].COUR[2] != null)1223 Console.WriteLine("您所查找的学生的选课内容为:{0}、{1}、{2}", stu[i].COUR[0].namOfCourse, stu[i].COUR[1].namOfCourse, stu[i].COUR[2].namOfCourse);1224 else if (stu[i].COUR[1] != null)1225 Console.WriteLine("您所查找的学生的选课内容为:{0}、{1}", stu[i].COUR[0].namOfCourse, stu[i].COUR[1].namOfCourse);1226 else if (stu[i].COUR[0] != null)1227 Console.WriteLine("您所查找的学生的选课内容为:{0}", stu[i].COUR[0].namOfCourse);1228 else1229 Console.WriteLine("该生尚未选课!");1230 }1231 }1232 saveCourseInforToTxt();1233 saveStuCourSelectInforToTxt();1234 saveStuInforToTxt();1235 }1236 1237 1238 /* 文件流操作1239 * 1、读取学生信息1240 * 2、写入学生信息1241 * 3、读取课程信息1242 * 4、写入课程信息1243 * 5、读取选课信息1244 * 6、写入选课信息*/1245 1246 //从txt文件中读取原始学生数据1247 static void readStuInforFromTxt()1248 {1249 FileStream mFile = new FileStream("stuInfor.txt", FileMode.OpenOrCreate);1250 mFile.Close();1251 mFile.Dispose();1252 int countLinesInTxt = File.ReadAllLines("stuInfor.txt").Length;1253 FileStream aFile = new FileStream("stuInfor.txt", FileMode.OpenOrCreate);1254 1255 StreamReader sr = new StreamReader(aFile, Encoding.Default); //没有Encoding.Default,读取到的汉字会乱码1256 string[] data;1257 string strReadFromTxt = "";1258 for (int i = 0; i < countLinesInTxt; i++)1259 {1260 strReadFromTxt = sr.ReadLine();1261 data = http://www.mamicode.com/strReadFromTxt.Split(‘ ‘);1262 if (data[0] == "")1263 continue;1264 student studFromTxt = new student();1265 studFromTxt._num = data[0];1266 studFromTxt.name = data[1];1267 studFromTxt.placeNative = data[2];1268 studFromTxt.tel = data[3];1269 studFromTxt.dormitoryRoom = data[4];1270 for (int j = 0; j < data.Length - 5; j += 2)1271 {1272 course Cour = new course();1273 Cour.id = data[j / 2];1274 Cour.namOfCourse = data[j / 2 + 1];1275 studFromTxt.COUR[j / 2] = Cour;1276 Cour = null;1277 }1278 stu[++countStudent] = studFromTxt;1279 studFromTxt = null;1280 }1281 sr.Close();1282 sr.Dispose();1283 aFile.Close();1284 aFile.Dispose();1285 }1286 1287 //将学生数据保存到txt文件中1288 static void saveStuInforToTxt()1289 {1290 FileStream saveFile = new FileStream("stuInfor.txt", FileMode.Truncate);1291 StreamWriter sw = new StreamWriter(saveFile, Encoding.Default);1292 1293 int m = -1;1294 do1295 {1296 m++;1297 }1298 while (stu[m] != null);1299 for (int i = 0; i < m; i++)1300 {1301 string str = stu[i]._num + " " + stu[i].name + " " + stu[i].placeNative + " " + stu[i].tel + " " + stu[i].dormitoryRoom;1302 sw.WriteLine(str);1303 countStudent--;1304 }1305 sw.Close();1306 saveFile.Close();1307 }1308 1309 //从txt文件中读取原始课程数据1310 static void readCourInfoFromTxt()1311 {1312 FileStream mFile = new FileStream("courseInfor.txt", FileMode.OpenOrCreate);1313 mFile.Close();1314 int countLinesInTxt = File.ReadAllLines("courseInfor.txt").Length;1315 FileStream aFile = new FileStream("courseInfor.txt", FileMode.OpenOrCreate);1316 1317 StreamReader sr = new StreamReader(aFile, Encoding.Default); //没有Encoding.Default,读取到的汉字会乱码1318 string[] data;1319 string strReadFromTxt = "";1320 for (int i = 0; i < countLinesInTxt; i++)1321 {1322 strReadFromTxt = sr.ReadLine();1323 data = http://www.mamicode.com/strReadFromTxt.Split(‘ ‘);1324 if (data[0] == "")1325 continue;1326 course courseFromTxt = new course();1327 courseFromTxt.id = data[0];1328 courseFromTxt.namOfCourse = data[1];1329 cour[++countCourse] = courseFromTxt;1330 courseFromTxt = null;1331 }1332 sr.Close();1333 aFile.Close();1334 }1335 1336 //将课程数据保存到txt文件中1337 static void saveCourseInforToTxt()1338 {1339 FileStream saveFile = new FileStream("courseInfor.txt", FileMode.Truncate);1340 StreamWriter sw = new StreamWriter(saveFile, Encoding.Default);1341 1342 int m = -1;1343 do1344 {1345 m++;1346 if (m == 10)1347 break;1348 }1349 while (cour[m] != null);1350 for (int i = 0; i < m; i++)1351 {1352 string str = cour[i].id + " " + cour[i].namOfCourse;1353 sw.WriteLine(str);1354 countCourse--;1355 }1356 sw.Close();1357 saveFile.Close();1358 }1359 1360 //从txt文件中读取原始选课数据1361 static void readStuCourSelectInforFromTxt()1362 {1363 FileStream mFile = new FileStream("CourSelect.txt", FileMode.OpenOrCreate);1364 mFile.Close();1365 mFile.Dispose();1366 int countLinesInTxt = File.ReadAllLines("CourSelect.txt").Length;1367 FileStream aFile = new FileStream("CourSelect.txt", FileMode.Open);1368 1369 StreamReader sr = new StreamReader(aFile, Encoding.Default); //没有Encoding.Default,读取到的汉字会乱码1370 string strReadFromTxt = "";1371 for (int i = 0; i < countLinesInTxt; i++)1372 {1373 strReadFromTxt = sr.ReadLine();1374 string[] strData = http://www.mamicode.com/strReadFromTxt.Split(‘ ‘);1375 if (strData[0] == "")1376 continue;1377 student STU = new student();1378 STU._num = strData[0];1379 STU.name = strData[1];1380 STU.placeNative = strData[2];1381 STU.tel = strData[3];1382 STU.dormitoryRoom = strData[4];1383 for (int k = 0; k < strData.Length - 5; k += 2)1384 {1385 course Cou = new course();1386 Cou.id = strData[5+k];1387 Cou.namOfCourse = strData[6+k];1388 STU.COUR[k / 2] = Cou;1389 Cou = null;1390 }1391 1392 //在已导入的数据中查找该学生信息,并补充其选课信息1393 for (int j = 0; j <= countStudent; j++) 1394 {1395 if (stu[j]._num == STU._num)1396 stu[j] = STU;1397 }1398 STU = null;1399 }1400 aFile.Close();1401 aFile.Dispose();1402 }1403 1404 //将学生数据保存到txt文件中1405 static void saveStuCourSelectInforToTxt()1406 {1407 FileStream saveStuCourFile = new FileStream("CourSelect.txt", FileMode.Truncate);1408 StreamWriter sw = new StreamWriter(saveStuCourFile, Encoding.Default);1409 int m = -1;1410 do1411 {1412 m++;1413 }1414 while (stu[m] != null);1415 for (int i = 0; i < m; i++)1416 {1417 string str = "";1418 if (stu[i].COUR[0] != null)1419 {1420 str = stu[i]._num + " " + stu[i].name + " " + stu[i].placeNative + " " + stu[i].tel + " "1421 + stu[i].dormitoryRoom;1422 for (int j = 0; j < 3&&stu[i].COUR[j] != null; j++)1423 str += " " + stu[i].COUR[j].id + " " + stu[i].COUR[j].namOfCourse;1424 sw.WriteLine(str);1425 }1426 }1427 sw.Close();1428 saveStuCourFile.Close();1429 }1430 }1431 }
C#控制台应用程序之选课系统
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。