功能模块设计

基于C++语言实现的学生选课管理系统,采用面向对象的思维进行设计,包含管理员和学生两个功能模块。

管理员模块

  • 添加课程信息
  • 删除课程信息
  • 修改课程信息
  • 查询课程信息
  • 学生信息管理
  • 课程信息一览表
  • 课程排序
  • 清空系统数据
  • 注销登录

用户模块

  • 课程信息一览表
  • 选课
  • 退课
  • 注销登录

主要运行界面

添加课程信息界面

添加课程信息界面

学生信息管理功能界面

学生信息管理功能界面

选课功能界面

选课功能

退课功能界面

退课管理


系统完整代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <string>
#include <iomanip>
#include <fstream>
#include <vector>
#include <sstream>
#include <ctime>
using namespace std;

//课程类
class Course
{
public:
string cNum; //课程编号
string cName; //课程名称
double cCredit; //课程学分
double cTime; //课程学时
string cMethod; //考核方式:考试 or 考察
string cNature; //课程性质:选修 or 必修 or 素质
//无参构造函数
Course()
{
}
//有参构造函数
Course(string cNum, string cName, double cCredit, double cTime, string cMethod, string cNature)
{
this->cNum = cNum;
this->cName = cName;
this->cCredit = cCredit;
this->cTime = cTime;
this->cMethod = cMethod;
this->cNature = cNature;
}
//输出课程表的某一行
void showCourseInfo()
{
cout << left << setw(16) << cNum;
cout << left << setw(30) << cName;
cout << left << setw(16) << cCredit;
cout << left << setw(16) << cTime;
cout << left << setw(16) << cMethod;
cout << left << setw(16) << cNature << endl;
}
//输出课程表表头
static void showHeader()
{
cout << left << setw(16) << "课程编号";
cout << left << setw(30) << "课程名称";
cout << left << setw(16) << "课程学分";
cout << left << setw(16) << "课程学时";
cout << left << setw(16) << "考核方式";
cout << left << setw(16) << "课程属性" << endl;
}
};

//人类 用来实例化管理员的
class Person
{
public:
string num; //账号
string pwd; //密码
Person()
{
}
Person(string num, string pwd)
{
this->num = num;
this->pwd = pwd;
}
};

//学生类
class Student
{
public:
string stuNum; //学号
string stuName; //姓名
string gradeNum; //年级
string department; //专业
string classNum; //班级
vector<Course> clist; //所选课程
int ctSize; //选课门数
string pwd; //教务密码
//无参构造函数
Student()
{
}
//有参构造函数
Student(string stuNum, string stuName, string gradeNum, string department, string classNum,vector<Course> clist, int ctSize, string pwd)
{
this->stuNum = stuNum;
this->stuName = stuName;
this->gradeNum = gradeNum;
this->department = department;
this->classNum = classNum;
this->clist = clist;
this->ctSize = ctSize;
this->pwd = pwd;
}
//析构函数
~Student()
{
}
//学生信息表的某一行
void showStudentInfo()
{
cout << left << setw(14) << stuNum;
cout << left << setw(14) << stuName;
cout << left << setw(12) << gradeNum;
cout << left << setw(25) << department;
cout << left << setw(12) << classNum;
cout << left << setw(15) << ctSize;
cout << left << setw(20) << pwd << endl;
}
////学生信息表的表头
static void showHeader()
{
cout << left << setw(14) << "学号";
cout << left << setw(14) << "姓名";
cout << left << setw(12) << "年级";
cout << left << setw(25) << "专业";
cout << left << setw(12) << "班级";
cout << left << setw(15) << "选课数量";
cout << left << setw(20) << "教务密码" << endl;
}
};

//选课管理系统类
class CourseSystem
{
public:
vector<Course> cs; //课程列表
vector<Student> ss; //学生列表
Person admin; //管理员
//无参构造函数
CourseSystem();
//有参构造函数
CourseSystem(vector<Course> cs, vector<Student> ss, Person admin);
//析构函数
~CourseSystem();
//设置管理员的账号密码
void setAdmin(string num, string pwd);
//初始化
void init();
//登录菜单
void menu();
//登录函数:学生和管理员都通过login函数登录
void login(string sel);
//管理员菜单
void aMenu();
//学生菜单
void sMenu(Student& s);
//读取文件
void readFile();
//写入文件
void writeFile();
//遍历课程列表 即遍历容器cs
void traverseCs();
//遍历学生列表 即遍历容器ss
void traverseSs();
//添加课程信息
void insertCs();
//删除课程信息
void deleteCs();
//修改课程信息
void updateCs();
//查询课程信息
void selectCs();
//管理学生功能:包含添加学生和删除学生
//管理员在这里添加学生信息后 就可以用学生信息进行学生登录 账号:学生学号 密码:学生教务密码
void managePs();
//课程信息一览表
void displayCs();
//将课程列表按课程编号升序排列
static bool cmpNum(const Course& c1,const Course& c2);
//将课程列表按课程学分升序排列,学分相同则按编号升序排列
static bool cmpCredit(const Course& c1,const Course& c2);
//课程排序:1.按课程编号排序 2.按课程学分排序
void sortCs();
//清空系统数据
void clearSystem();
//学生选课
void insertClist(Student& s);
//学生退课
void deleteClist(Student& s);
};

//CourseSystem类 无参构造函数 类外实现
CourseSystem::CourseSystem()
{
}
//CourseSystem类 有参构造函数 类外实现
CourseSystem::CourseSystem(vector<Course> cs, vector<Student> ss, Person admin)
{
this->cs = cs;
this->ss = ss;
this->admin = admin;
}
//CourseSystem类 析构函数 类外实现
CourseSystem::~CourseSystem()
{
}
//CourseSystem类 设置管理员的账号密码
void CourseSystem::setAdmin(string num, string pwd)
{
this->admin.num = num;
this->admin.pwd = pwd;
}

//CourseSystem类 初始化
void CourseSystem::init()
{
readFile(); //读取文件
}
//CourseSystem类 登录菜单
void CourseSystem::menu()
{
string sel = "0";
system("cls");
cout << "\t\t\t*********欢迎来到学生选课管理系统***********" << endl;
cout << "\t\t\t你可以进行以下操作:" << endl;
cout << "\t\t\t|------------------------------------------|" << endl;
cout << "\t\t\t| 1 管理员登录 |" << endl;
cout << "\t\t\t|------------------------------------------|" << endl;
cout << "\t\t\t| 2 学生登录 |" << endl;
cout << "\t\t\t|------------------------------------------|" << endl;
cout << "\t\t\t| 0 退出 |" << endl;
cout << "\t\t\t|------------------------------------------|" << endl;
cout << "\t\t\t请选择【0-2】:";
cin >> sel;
while(sel != "0" && sel != "1" && sel != "2")
{
cout << "\t\t\t输入不合法,请重新选择【0-2】:";
cin >> sel;
}
if(sel == "1")
{
this->login(sel);
this->menu();
}
else if(sel == "2")
{
this->login(sel);
this->menu();
}
else
{
exit(0);
}
}

//CourseSystem类 登录函数:学生和管理员都通过login函数登录
void CourseSystem::login(string sel)
{
cout<<"\t\t\t请填写用户信息:"<<endl;
if(sel == "1")
{
Person p;
cout<<"\t\t\t输入账号:";
cin>>p.num;
cout<<"\t\t\t输入密码:";
cin>>p.pwd;
if(p.num == this->admin.num && p.pwd == this->admin.pwd)
{
cout<<"\t\t\t管理员登录成功!"<<endl;
this->aMenu();
}
else
{
cout<<"\t\t\t用户名或密码输入错误"<<endl;
}
cout<<"\t\t\t";
system("pause");
}
else if(sel == "2")
{
bool flag = false;
Student s;
cout << "\t\t\t输入账号:";
cin >> s.stuNum;
cout << "\t\t\t输入密码:";
cin >> s.pwd;
for(int i = 0; i < this->ss.size(); ++i)
{
if(s.stuNum == ss[i].stuNum && s.pwd == ss[i].pwd)
{
flag = true;
cout<<"\t\t\t学生登录成功!"<<endl;
this->sMenu(ss[i]);
}
}
if(!flag) cout<<"\t\t\t用户名或密码输入错误!"<<endl;
cout<<"\t\t\t";
system("pause");
}
}

//CourseSystem类 管理员菜单
void CourseSystem::aMenu()
{
string sel = "0";
system("cls");
cout << "\t\t\t***********欢迎来到学生选课管理系统****************" << endl;
cout << "\t\t\t当前用户:" << this->admin.num <<"(管理员)"<< endl;
cout << "\t\t\t|-------------------------------------------------|" << endl;
cout << "\t\t\t| 1 添加课程信息 |" << endl;
cout << "\t\t\t|-------------------------------------------------|" << endl;
cout << "\t\t\t| 2 删除课程信息 |" << endl;
cout << "\t\t\t|-------------------------------------------------|" << endl;
cout << "\t\t\t| 3 修改课程信息 |" << endl;
cout << "\t\t\t|-------------------------------------------------|" << endl;
cout << "\t\t\t| 4 查询课程信息 |" << endl;
cout << "\t\t\t|-------------------------------------------------|" << endl;
cout << "\t\t\t| 5 学生信息管理 |" << endl;
cout << "\t\t\t|-------------------------------------------------|" << endl;
cout << "\t\t\t| 6 课程信息一览表 |" << endl;
cout << "\t\t\t|-------------------------------------------------|" << endl;
cout << "\t\t\t| 7 课程排序 |" << endl;
cout << "\t\t\t|-------------------------------------------------|" << endl;
cout << "\t\t\t| 8 清空系统数据 |" << endl;
cout << "\t\t\t|-------------------------------------------------|" << endl;
cout << "\t\t\t| 0 注销登录 |" << endl;
cout << "\t\t\t|-------------------------------------------------|" << endl;
cout << "\t\t\t请选择【0-8】:";
cin >> sel;
while(sel != "0" && sel != "1" && sel != "2" && sel != "3" && sel != "4" && sel != "5" && sel != "6" && sel != "7" && sel != "8")
{
cout << "\t\t\t输入不合法,请重新选择【0-8】:";
cin >> sel;
}
if(sel == "1")
{
this->insertCs();
this->aMenu();
}
else if(sel == "2")
{
this->deleteCs();
this->aMenu();
}
else if(sel == "3")
{
this->updateCs();
this->aMenu();
}
else if(sel == "4")
{
this->selectCs();
this->aMenu();
}
else if(sel == "5")
{
this->managePs();
this->aMenu();
}
else if(sel == "6")
{
this->displayCs();
this->aMenu();
}
else if(sel == "7")
{
this->sortCs();
this->aMenu();
}
else if(sel == "8")
{
this->clearSystem();
this->aMenu();
}
else if(sel == "0")
{
this->menu();
}
}

//CourseSystem类 学生菜单
void CourseSystem::sMenu(Student& s)
{
string sel = "0";
system("cls");
cout << "\t\t\t**********欢迎来到学生选课系统**************" << endl;
cout << "\t\t\t当前用户:" << s.stuName <<"(学生)"<< endl;
cout << "\t\t\t|------------------------------------------|" << endl;
cout << "\t\t\t| 1 课程信息一览表 |" << endl;
cout << "\t\t\t|------------------------------------------|" << endl;
cout << "\t\t\t| 2 选课 |" << endl;
cout << "\t\t\t|------------------------------------------|" << endl;
cout << "\t\t\t| 3 退课 |" << endl;
cout << "\t\t\t|------------------------------------------|" << endl;
cout << "\t\t\t| 0 注销登录 |" << endl;
cout << "\t\t\t|------------------------------------------|" << endl;
cout << "\t\t\t请选择【0-3】:";
cin >> sel;
while(sel != "0" && sel != "1" && sel != "2" && sel != "3")
{
cout << "\t\t\t输入不合法,请重新选择【0-3】:";
cin >> sel;
}
if(sel == "1")
{
this->displayCs(); //课程信息一览表
this->sMenu(s);
}
else if(sel == "2")
{
this->insertClist(s); //报名
this->sMenu(s);
}
else if(sel == "3")
{
this->deleteClist(s); //退课
this->sMenu(s);
}
else if(sel == "0") //注销登录
{
this->menu();
}
}

//CourseSystem类 读取文件
void CourseSystem::readFile()
{
//读取文件course.txt 对应容器 cs
ifstream ifs;
ifs.open("course.txt", ios::in);
int csSize = 0; //用来接收活动数量的值
ifs >> csSize;
for(int i = 0; i < csSize; i++)
{
Course c;
ifs >> c.cNum >> c.cName >> c.cCredit >> c.cTime >> c.cMethod >> c.cNature;
cs.push_back(c);
}
ifs.close();

//读取文件student 对应容器 ss
ifstream jfs;
jfs.open("student.txt", ios::in);
int ssSize = 0; //用来接收学生数量的值
jfs >> ssSize;
for(int i = 0; i < ssSize; i++)
{
Student s;
jfs >> s.stuNum >> s.stuName >> s.gradeNum >> s.department >> s.classNum >> s.ctSize >> s.pwd;
for(int j = 0; j < s.ctSize; j++)
{
Course cc;
jfs >> cc.cNum >> cc.cName >> cc.cCredit >> cc.cTime >> cc.cMethod >> cc.cNature;
s.clist.push_back(cc);
}
ss.push_back(s);
}
jfs.close();
}
//CourseSystem类 写入文件
void CourseSystem::writeFile()
{
//写入文件course.txt 对应容器 cs
ofstream ofs;
ofs.open("course.txt", ios::out);
ofs << cs.size() << endl; //先写入课程数量
for (int i = 0; i < cs.size(); i++)
{
ofs << cs[i].cNum << " " << cs[i].cName << " " << cs[i].cCredit << " "
<< cs[i].cTime << " " << cs[i].cMethod << " " << cs[i].cNature << endl;
}
ofs.close();

//写入文件student.txt 对应容器 ss
ofstream pfs;
pfs.open("student.txt", ios::out);
pfs << ss.size() << endl; //先写入课程数量
for (int i = 0; i < ss.size(); i++)
{
pfs << ss[i].stuNum << " " << ss[i].stuName << " " << ss[i].gradeNum << " "
<< ss[i].department << " " << ss[i].classNum << " " << ss[i].clist.size() << " " << ss[i].pwd << endl;
for(int j = 0; j < ss[i].clist.size(); j++)
{
pfs << ss[i].clist[j].cNum << " " << ss[i].clist[j].cName << " " << ss[i].clist[j].cCredit << " "
<< ss[i].clist[j].cTime << " " << ss[i].clist[j].cMethod << " " << ss[i].clist[j].cNature << endl;
}
}
pfs.close();
}

//CourseSystem类 遍历课程列表 即遍历容器cs
void CourseSystem::traverseCs()
{
cout << "\t------------------------------------------------------------------------------------------------------" << endl;
cout<<"\t";
Course::showHeader();
cout << "\t------------------------------------------------------------------------------------------------------" << endl;
for (int i = 0; i < cs.size(); i++)
{
cout << "\t";
cs[i].showCourseInfo();
}
cout << "\t------------------------------------------------------------------------------------------------------" << endl;
}

//CourseSystem类 遍历学生列表 即遍历容器ss
void CourseSystem::traverseSs()
{
cout << "\t------------------------------------------------------------------------------------------------------" << endl;
cout<<"\t";
Student::showHeader();
cout << "\t------------------------------------------------------------------------------------------------------" << endl;
for (int i = 0; i < ss.size(); i++)
{
cout << "\t";
ss[i].showStudentInfo();
}
cout << "\t------------------------------------------------------------------------------------------------------" << endl;
}

//CourseSystem类 添加课程信息
void CourseSystem::insertCs()
{
while(true)
{
system("cls");
cout << "\t*************************************欢迎来到添加课程信息功能*****************************************" << endl;
cout << "\t表1:课程信息一览表"<<endl;
this->traverseCs();
string sel = "0";
cout << "\t-----------------" << endl;
cout << "\t1 添加课程信息" << endl;
cout << "\t2 返回主菜单" << endl;
cout << "\t-----------------" << endl;
cout << "\t请进行选择【1-2】:";
cin >> sel;
while(sel != "1" && sel != "2")
{
cout << "\t输入不合法,请重新选择【1-2】:";
cin >> sel;
}
if(sel == "1")
{
string flag = "1";
while (flag == "1")
{
cout << "\t开始信息录入:"<<endl;
Course c;
bool check = false;
do
{
check = false;
cout << "\t课程编号(长度6位):";
cin >> c.cNum;
for(int i = 0; i < cs.size(); ++i)
{
if(c.cNum == cs[i].cNum)
{
cout<<"\t该课程编号已被其他课程使用,请重新输入!" << endl;
check = true;
break;
}
}
}
while(check);
cout << "\t课程名称:";
cin >> c.cName;
cout << "\t课程学分:";
cin >> c.cCredit;
cout << "\t课程学时:";
cin >> c.cTime;
cout << "\t考核方式(考试 or 考察):";
cin >> c.cMethod;
cout << "\t课程性质(选修 or 必修 or 素质):";
cin >> c.cNature;
cs.push_back(c);
writeFile();
cout << "\n\t课程信息添加成功!是否继续添加?(1 是 0 否)" << endl;
cout << "\t请进行选择【0-1】:";
cin >> flag;
while(flag != "0" && flag != "1")
{
cout << "\t输入不合法,请重新选择【0-1】:";
cin >> flag;
}
}
cout << "\t";
system("pause");
}
else
{
break;
}
}
}
//CourseSystem类 删除课程信息
void CourseSystem::deleteCs()
{
while (true)
{
system("cls");
cout << "\t*************************************欢迎来到删除课程信息功能*****************************************" << endl;
cout << "\t表1:课程信息一览表"<<endl;
this->traverseCs();
string sel = "0";
cout << "\t-----------------" << endl;
cout << "\t1 按课程编号删除" << endl;
cout << "\t2 返回主菜单" << endl;
cout << "\t-----------------" << endl;
cout << "\t请进行选择【1-2】:";
cin >> sel;
while(sel != "1" && sel != "2")
{
cout << "\t输入不合法,请重新选择【1-2】:";
cin >> sel;
}
if (sel == "1")
{
string keyNum;
bool flag = false;
cout << "\t请输入待删除的课程编号:";
cin >> keyNum;
for (vector<Course>::iterator it = cs.begin(); it != cs.end(); ++it)
{
if (it->cNum == keyNum)
{
flag = true;
cout<< "\n\t编号" << keyNum << "课程基本信息:"<<endl;

cout << "\t------------------------------------------------------------------------------------------------------" << endl;
cout<<"\t";
Course::showHeader();
cout << "\t------------------------------------------------------------------------------------------------------" << endl;
cout<<"\t";
it->showCourseInfo();
cout << "\t------------------------------------------------------------------------------------------------------" << endl;
cout << "\t如果删除该课程,则所有已选择该课程的学生的课表也会将该课程去除!"<<endl;
cout << "\t是否确认删除该课程?(1 是 0 否)" << endl;
cout << "\t请进行选择【0-1】:";
string ch = "0";
cin >> ch;
while(ch != "0" && ch != "1")
{
cout << "\t输入不合法,请重新选择【0-1】:";
cin >> ch;
}
if (ch == "0") break;
else
{
for(int j = 0; j < ss.size(); j++)
{
for(vector<Course>::iterator ie = ss[j].clist.begin(); ie != ss[j].clist.end(); ++ie)
{
if(ie->cNum == it->cNum)
{
ss[j].clist.erase(ie);
break;
}
}
}
cs.erase(it);
writeFile();
cout << "\t删除成功!" << endl;
break;
}
}
}
if (!flag) cout << "\t无法查询到此课程信息,无法删除!\n" << endl;
cout << "\t";
system("pause");
}
else
{
break;
}
}
}

//CourseSystem类 修改课程信息
void CourseSystem::updateCs()
{
while(true)
{
system("cls");
cout << "\t*************************************欢迎来到修改课程信息功能*****************************************" << endl;
cout << "\t表1:课程信息一览表"<<endl;
this->traverseCs();
string sel = "0";
cout << "\t---------------------------" << endl;
cout << "\t1 修改课程信息" << endl;
cout << "\t2 返回主菜单" << endl;
cout << "\t---------------------------" << endl;
cout << "\t请进行选择【1-2】:";
cin >> sel;
while(sel != "1" && sel != "2")
{
cout << "\t输入不合法,请重新选择【1-2】:";
cin >> sel;
}
if(sel == "1")
{
bool flag = false;
string keyNum;
cout << "\t请输入待修改课程的编号:";
cin >> keyNum;
for (int i = 0; i < cs.size(); i++)
{
if (cs[i].cNum == keyNum)
{
flag = true;
cout << "\t待修改课程当前信息如下:" << endl;
cout << "\t------------------------------------------------------------------------------------------------------" << endl;

cout<<"\t";
Course::showHeader();
cout << "\t------------------------------------------------------------------------------------------------------" << endl;
cout<<"\t";
cs[i].showCourseInfo();
cout << "\t------------------------------------------------------------------------------------------------------" << endl;
Course c = cs[i];
bool check = false;
do
{
check = false;
cout << "\t请输入修改后的课程名称:";
cin >> c.cName;
for(int j = 0; j < cs.size(); ++j)
{
if(c.cName == cs[j].cName && i != j)
{
cout<<"\t该课程名称已被系统录入,请重新输入:" << endl;
check = true;
break;
}
}
}
while(check);
cout << "\t请输入修改后的课程学分:";
cin >> c.cCredit;
cout << "\t请输入修改后的课程学时:";
cin >> c.cTime;
cout << "\t请输入修改后的考核方式:";
cin >> c.cMethod;
cout << "\t请输入修改后的课程性质:";
cin >> c.cNature;
cout << "\t是否确认修改?(1 是 0 否)" << endl;
cout << "\t请进行选择【0-1】:";
string ch = "0";
cin >> ch;
while(ch != "0" && ch != "1")
{
cout << "\t输入不合法,请重新选择【0-1】:";
cin >> ch;
}
if (ch == "0") break;
else
{
for(int j = 0; j < ss.size(); j++)
{
for(int k = 0; k < ss[j].clist.size(); k++)
{
if(ss[j].clist[k].cNum == keyNum)
{
ss[j].clist[k] = c;
}
}
}
cs[i] = c;
cout << "\t修改成功!" << endl;
writeFile();
break;
}
}
}
if (!flag) cout << "\t无法查询到此课程信息,无法修改!\n" << endl;
cout<<"\t";
system("pause");
}
else
{
break;
}
}
}
//CourseSystem类 查询课程信息
void CourseSystem::selectCs()
{
while (true)
{
system("cls");
cout << "\t*************************************欢迎来到查询课程信息功能*****************************************" << endl;
cout << "\t表1:课程信息一览表"<<endl;
this->traverseCs();
cout << "\t----------------------------" << endl;
cout << "\t1 按课程编号查询" << endl;
cout << "\t2 返课程名称查询" << endl;
cout << "\t3 按课程学分查询" << endl;
cout << "\t4 按课程性质查询" << endl;
cout << "\t5 返回主菜单" << endl;
cout << "\t----------------------------" << endl;
cout << "\t请进行选择【1-5】:";
string sel = "0";
cin >> sel;
while(sel != "1" && sel != "2" && sel != "3" && sel != "4" && sel != "5")
{
cout << "\t输入不合法,请重新选择【1-5】:";
cin >> sel;
}
if (sel == "1")
{
string key;
bool flag = false;
cout << "\t请输入待查询课程的编号:";
cin >> key;
cout << "\t查询结果如下:" << endl;
cout<<"\t课程基本信息:"<<endl;
cout << "\t------------------------------------------------------------------------------------------------------" << endl;
cout<<"\t";
Course::showHeader();
cout << "\t------------------------------------------------------------------------------------------------------" << endl;
for (int i = 0; i < cs.size(); i++)
{
if (cs[i].cNum == key)
{
flag = true;
cout<<"\t";
cs[i].showCourseInfo();
break;
}
}
cout << "\t------------------------------------------------------------------------------------------------------" << endl;
if (!flag) cout << "\t无法查询到此课程!\n" << endl;
cout << "\t";
system("pause");
}
else if (sel == "2")
{
string key;
bool flag = false;
cout << "\t请输入待查询课程的名称:";
cin >> key;
cout << "\t查询结果如下:" << endl;
cout<<"\t课程基本信息:"<<endl;
cout << "\t------------------------------------------------------------------------------------------------------" << endl;
cout<<"\t";
Course::showHeader();
cout << "\t------------------------------------------------------------------------------------------------------" << endl;
for (int i = 0; i < cs.size(); i++)
{
if (cs[i].cName == key)
{
flag = true;
cout<<"\t";
cs[i].showCourseInfo();
}
}
cout << "\t------------------------------------------------------------------------------------------------------" << endl;
if (!flag) cout << "\t无法查询到此课程!\n" << endl;
cout << "\t";
system("pause");
}
else if (sel == "3")
{
double credit;
bool flag = false;
cout << "\t请输入待查询课程的学分:";
cin >> credit;
cout << "\t查询结果如下:" << endl;
cout<<"\t课程基本信息:"<<endl;
cout << "\t------------------------------------------------------------------------------------------------------" << endl;
cout<<"\t";
Course::showHeader();
cout << "\t------------------------------------------------------------------------------------------------------" << endl;
for (int i = 0; i < cs.size(); i++)
{
if (cs[i].cCredit == credit)
{
flag = true;
cout<<"\t";
cs[i].showCourseInfo();
}
}
cout << "\t------------------------------------------------------------------------------------------------------" << endl;
if (!flag) cout << "\t无法查询到此课程!\n" << endl;
cout << "\t";
system("pause");
}
else if (sel == "4")
{
string key;
bool flag = false;
cout << "\t请输入待查询课程的性质:";
cin >> key;
cout << "\t查询结果如下:" << endl;
cout<<"\t课程基本信息:"<<endl;
cout << "\t------------------------------------------------------------------------------------------------------" << endl;
cout<<"\t";
Course::showHeader();
cout << "\t------------------------------------------------------------------------------------------------------" << endl;
for (int i = 0; i < cs.size(); i++)
{
if (cs[i].cNature == key)
{
flag = true;
cout<<"\t";
cs[i].showCourseInfo();
}
}
cout << "\t------------------------------------------------------------------------------------------------------" << endl;
if (!flag) cout << "\t无法查询到此课程!\n" << endl;
cout << "\t";
system("pause");
}
else
{
break;
}
}
}

//CourseSystem类 管理学生功能:包含添加学生和删除学生
//管理员在这里添加学生信息后 就可以用学生信息进行学生登录 账号:学生学号 密码:学生教务密码
void CourseSystem::managePs()
{
while(true)
{
system("cls");
cout << "\t*************************************欢迎来到学生信息管理功能*****************************************" << endl;
cout << "\t学生信息一览表"<<endl;
this->traverseSs();
string sel = "0";
cout << "\t-----------------" << endl;
cout << "\t1 添加学生" << endl;
cout << "\t2 删除学生" << endl;
cout << "\t3 返回主菜单" << endl;
cout << "\t-----------------" << endl;
cout << "\t请进行选择【1-3】:";
cin >> sel;
while(sel != "1" && sel != "2" && sel != "3")
{
cout << "\t输入不合法,请重新选择【1-3】:";
cin >> sel;
}
if(sel == "1")
{
Student s;
cout << "\t输入学生信息:" << endl;
cout << "\t学号(10位):";
bool check = false;
do
{
check = false;
cin >> s.stuNum;
for(int i = 0; i < ss.size(); ++i)
{
if(s.stuNum == ss[i].stuNum)
{
cout << "\t该学号已存在,请重新输入:";
check = true;
break;
}
}
}
while(check);
cout << "\t姓名:";
cin >> s.stuName;
cout << "\t年级:";
cin >> s.gradeNum;
cout << "\t专业:";
cin >> s.department;
cout << "\t班级:";
cin >> s.classNum;
cout << "\t设置该学生教务系统密码:";
cin >> s.pwd;
s.ctSize = 0;
ss.push_back(s);
writeFile();
cout<<"\t添加成功!"<<endl;
cout<<"\t";
system("pause");
}
else if(sel == "2")
{
string keyNum;
bool flag = false;
cout << "\t请输入待删除学生的学号:";
cin >> keyNum;

for (vector<Student>::iterator it = ss.begin(); it != ss.end(); ++it)
{
if (it->stuNum == keyNum)
{
flag = true;
cout << "\t待删除学生的基本信息如下:" << endl;
cout << "\t------------------------------------------------------------------------------------------------------" << endl;
cout<<"\t";
Student::showHeader();
cout << "\t------------------------------------------------------------------------------------------------------" << endl;
cout<<"\t";
it->showStudentInfo();
cout << "\t------------------------------------------------------------------------------------------------------" << endl;
cout << "\t是否确认删除该学生?(1 是 0 否)" << endl;
cout << "\t请进行选择【0-1】:";
string ch = "0";
cin >> ch;
while(ch != "0" && ch != "1")
{
cout << "\t输入不合法,请重新选择【0-1】:";
cin >> ch;
}
if (ch == "0") break;
else
{
ss.erase(it);
writeFile();
cout << "\t删除成功!" << endl;
break;
}
}
}
if (!flag) cout << "\t查无此人,无法删除!\n" << endl;
cout << "\t";
system("pause");
}
else
{
break;
}
}
}

//CourseSystem类 课程信息一览表
void CourseSystem::displayCs()
{
system("cls");
cout << "\t************************************欢迎来到课程信息一览表功能****************************************" << endl;
cout << "\t课程信息一览表"<<endl;
this->traverseCs();
cout << "\t";
system("pause");
}

//CourseSystem类 将课程列表按课程编号升序排列
bool CourseSystem::cmpNum(const Course& c1,const Course& c2)
{
return c1.cNum < c2.cNum;
}

//CourseSystem类 将课程列表按课程学分升序排列,学分相同则按编号升序排列
bool CourseSystem::cmpCredit(const Course& c1,const Course& c2)
{
if(c1.cNum != c2.cNum) return c1.cCredit < c2.cCredit;
else return c1.cNum < c2.cNum;
}

//CourseSystem类 课程排序
void CourseSystem::sortCs()
{
while(true)
{
system("cls");
cout << "\t*************************************欢迎来到课程信息排序功能*****************************************" << endl;
string sel = "0";
cout << "\t-----------------" << endl;
cout << "\t1 按课程编号排序" << endl;
cout << "\t2 按课程学分排序" << endl;
cout << "\t3 返回主菜单" << endl;
cout << "\t-----------------" << endl;
cout << "\t请进行选择【1-3】:";
cin >> sel;
while(sel != "1" && sel != "2" && sel != "3")
{
cout << "\t输入不合法,请重新选择【1-3】:";
cin >> sel;
}
if(sel == "1")
{
sort(cs.begin(), cs.end(), cmpNum);
cout<<"\t按课程编号升序排列如下:"<<endl;
this->traverseCs();
cout << "\t";
system("pause");
}
else if(sel == "2")
{
sort(cs.begin(), cs.end(), cmpCredit);
cout<<"\t\t按课程学分升序排列如下:"<<endl;
this->traverseCs();
cout << "\t";
system("pause");
}
else
{
break;
}
}
}

//CourseSystem类 清空系统数据
void CourseSystem::clearSystem()
{
while (true)
{
string sel = "0";
system("cls");
cout << "\t************************************欢迎来到清空系统数据功能***************************************" << endl;
cout << "\t当前用户:" << this->admin.num <<"(管理员)"<< endl;
cout << "\t------------------" << endl;
cout << "\t1 确认清空系统数据" << endl;
cout << "\t2 返回主菜单" << endl;
cout << "\t------------------" << endl;
cout << "\t请慎重选择【1-2】:";
cin >> sel;
while(sel != "1" && sel != "2")
{
cout << "\t输入不合法,请重新输入【1-2】:";
cin >> sel;
}
if (sel == "1")
{
cs.clear();
ss.clear();
cout << "\t清空成功!" << endl;
cout << "\t";
system("pause");
writeFile();
}
else
{
break;
}
}
}

//CourseSystem类 学生选课
void CourseSystem::insertClist(Student& s)
{
while(true)
{
system("cls");
cout << "\t******************************************欢迎来到选课功能********************************************" << endl;
cout << "\t表1:课程信息一览表"<<endl;
this->traverseCs();
cout << endl;
cout << "\t表2:您的当前课表如下:"<<endl;

cout << "\t------------------------------------------------------------------------------------------------------" << endl;
cout<<"\t";
Course::showHeader();
cout << "\t------------------------------------------------------------------------------------------------------" << endl;
for (int i = 0; i < s.clist.size(); i++)
{
cout << "\t";
s.clist[i].showCourseInfo();
}
cout << "\t------------------------------------------------------------------------------------------------------" << endl;
string sel = "0";
cout << "\t-----------------" << endl;
cout << "\t1 选课" << endl;
cout << "\t2 返回主菜单" << endl;
cout << "\t-----------------" << endl;
cout << "\t请进行选择【1-2】:";
cin >> sel;
while(sel != "1" && sel != "2")
{
cout << "\t输入不合法,请重新选择【1-2】:";
cin >> sel;
}
if(sel == "1")
{
cout << "\t请输入想选择的课程编号:";
Course c;
cin >> c.cNum;
bool check1 = false;
for (int i = 0; i < s.clist.size(); i++)
{
if(s.clist[i].cNum == c.cNum)
{
check1 = true;
break;
}
}
if(check1)
{
cout << "\t该课程已在您的课表中,无需重复选课!" << endl;
}
else
{
bool check2 = false;
for (int i = 0; i < cs.size(); i++)
{
if(cs[i].cNum == c.cNum)
{
cout << "\t该课程信息如下:" << endl;
cout << "\t------------------------------------------------------------------------------------------------------" << endl;
cout<<"\t";
Course::showHeader();
cout << "\t------------------------------------------------------------------------------------------------------" << endl;
cout<<"\t";
cs[i].showCourseInfo();
cout << "\t------------------------------------------------------------------------------------------------------" << endl;
c = cs[i];
check2 = true;
break;
}
}
if(check2)
{
cout << "\t是否确认选择该课程?(1 是 0 否)" << endl;
string str;
cout << "\t请进行选择【0-1】:";
cin >> str;
if(str == "1")
{
s.clist.push_back(c);
s.ctSize = s.clist.size();
writeFile();
cout << "\t选课成功!" << endl;
}
}
else
{
cout << "\t未查询到该课程信息!" << endl;
}
}
cout << "\t";
system("pause");
}
else
{
break;
}
}
}

//CourseSystem类 学生退课
void CourseSystem::deleteClist(Student& s)
{
while(true)
{
system("cls");
cout << "\t******************************************欢迎来到退课功能********************************************" << endl;
cout << "\t表2:已选课程一览表" << endl;

cout << "\t------------------------------------------------------------------------------------------------------" << endl;
cout<<"\t";
Course::showHeader();
cout << "\t------------------------------------------------------------------------------------------------------" << endl;
for (int i = 0; i < s.clist.size(); i++)
{
cout << "\t";
s.clist[i].showCourseInfo();
}
cout << "\t------------------------------------------------------------------------------------------------------" << endl;
string sel = "0";
cout << "\t-----------------" << endl;
cout << "\t1 退课" << endl;
cout << "\t2 返回主菜单" << endl;
cout << "\t-----------------" << endl;
cout << "\t请进行选择【1-2】:";
cin >> sel;
while(sel != "1" && sel != "2")
{
cout << "\t输入不合法,请重新选择【1-2】:";
cin >> sel;
}
if(sel == "1")
{
cout << "\t请输入想退出的课程编号:";
Course c;
cin >> c.cNum;
bool check1 = false;
for(vector<Course>::iterator it = s.clist.begin(); it != s.clist.end(); ++it)
{
if(it->cNum == c.cNum)
{
check1 = true;
cout << "\t该课程信息如下:" << endl;
cout << "\t------------------------------------------------------------------------------------------------------" << endl;
cout<<"\t";
Course::showHeader();
cout << "\t------------------------------------------------------------------------------------------------------" << endl;
cout<<"\t";
it->showCourseInfo();
cout << "\t------------------------------------------------------------------------------------------------------" << endl;
cout << "\t是否确认退出?(1 是 0 否)" << endl;
cout << "\t请进行选择【0-1】:";
string ch = "0";
cin >> ch;
while(ch != "0" && ch != "1")
{
cout << "\t输入不合法,请重新选择【0-1】:";
cin >> ch;
}
if(ch == "0") break;
else
{
s.clist.erase(it);
s.ctSize = s.clist.size();
writeFile();
cout << "\t退出成功!" << endl;
break;
}
}
}
if(!check1)
{
cout << "\t您的课表中没有该课程,无需退出!" << endl;
}
cout << "\t";
system("pause");
}
else
{
break;
}
}
}
//主函数
int main()
{
CourseSystem csm;
csm.init();
csm.setAdmin("admin","123"); //设置管理员的账号和密码
csm.menu();
return 0;
}

运行方法

  1. 新建一个cpp文件,命名为”main.cpp”。
  2. 将系统完整代码复制粘贴至”main.cpp”中。
  3. 利用Dev C++运行”main.cpp”即可(系统管理员默认账号:admin 密码:123)。