选课管理系统
Tan Yunhao Lv5
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
#include <iostream>
#include <sstream>
#include <string>
#include <ctime>
#include <vector>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
// 教师类
class Teacher
{
public:
string id, name, gender, college, hireDate;
int age;

Teacher() = default;
Teacher(string id, string name, string gender, int age, string college, string hireDate)
: id(id), name(name), gender(gender), age(age), college(college), hireDate(hireDate) {}

// 输入输出运算符重载
friend istream& operator>>(istream& in, Teacher& t)
{
in >> t.id >> t.name >> t.gender >> t.age >> t.college >> t.hireDate;
return in;
}
friend ostream& operator<<(ostream& out, const Teacher& t)
{
out << t.id << " " << t.name << " " << t.gender << " " << t.age << " " << t.college << " " << t.hireDate;
return out;
}

void display() const
{
cout << setw(10) << id << setw(10) << name << setw(10) << gender
<< setw(5) << age << setw(15) << college << setw(15) << hireDate << endl;
}
};

// 学生类
class Student
{
public:
string id, name, gender, category, college, admissionDate;
int age;

Student() = default;
Student(string id, string name, string gender, int age, string category, string college, string admissionDate)
: id(id), name(name), gender(gender), age(age), category(category), college(college), admissionDate(admissionDate) {}

friend istream& operator>>(istream& in, Student& s)
{
in >> s.id >> s.name >> s.gender >> s.age >> s.category >> s.college >> s.admissionDate;
return in;
}
friend ostream& operator<<(ostream& out, const Student& s)
{
out << s.id << " " << s.name << " " << s.gender << " " << s.age << " "
<< s.category << " " << s.college << " " << s.admissionDate;
return out;
}

void display() const
{
cout << setw(10) << id << setw(10) << name << setw(10) << gender
<< setw(5) << age << setw(10) << category << setw(15) << college
<< setw(15) << admissionDate << endl;
}
};

// 课程类
class Course
{
public:
string id, name, category;
int credits;

Course() = default;
Course(string id, string name, int credits, string category)
: id(id), name(name), credits(credits), category(category) {}

friend istream& operator>>(istream& in, Course& c)
{
in >> c.id >> c.name >> c.credits >> c.category;
return in;
}
friend ostream& operator<<(ostream& out, const Course& c)
{
out << c.id << " " << c.name << " " << c.credits << " " << c.category;
return out;
}

void display() const
{
cout << setw(10) << id << setw(15) << name << setw(5) << credits
<< setw(15) << category << endl;
}
};

// 授课信息类
class TeachingInfo
{
public:
string id, teacherId, courseCode, startTime;

TeachingInfo() = default;
TeachingInfo(string id, string teacherId, string courseCode, string startTime)
: teacherId(teacherId), courseCode(courseCode), startTime(startTime), id(id) {}

friend istream& operator>>(istream& in, TeachingInfo& t)
{
in >> t.id >> t.teacherId >> t.courseCode >> t.startTime;
return in;
}
friend ostream& operator<<(ostream& out, const TeachingInfo& t)
{
out << t.id << t.teacherId << " " << t.courseCode << " " << t.startTime;
return out;
}

void display() const
{
cout << setw(10) << id << setw(10) << teacherId << setw(10) << courseCode << setw(15) << startTime << endl;
}
};

// 选课信息类
class Enrollment
{
public:
string studentId, teacherId, courseCode, enrollTime, id;
double grade;

Enrollment() = default;
Enrollment(string id, string studentId, string teacherId, string courseCode, double grade, string enrollTime)
: id(id), studentId(studentId), teacherId(teacherId), courseCode(courseCode), grade(grade), enrollTime(enrollTime) {}

friend istream& operator>>(istream& in, Enrollment& e)
{
in >> e.id >> e.studentId >> e.teacherId >> e.courseCode >> e.grade >> e.enrollTime;
return in;
}
friend ostream& operator<<(ostream& out, const Enrollment& e)
{
out << e.id << e.studentId << " " << e.teacherId << " " << e.courseCode << " " << e.grade << " " << e.enrollTime;
return out;
}

void display() const
{
cout << setw(10) << id << setw(10) << studentId << setw(10) << teacherId << setw(10) << courseCode
<< setw(5) << grade << setw(15) << enrollTime << endl;
}
};

bool isLeapYear(int year) {
// 闰年判断:能被4整除且不能被100整除,或者能被400整除
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}

bool checkDate(string dateStr) {
// 格式:YYYY-MM-DD
if (dateStr.size() != 10 || dateStr[4] != '-' || dateStr[7] != '-') {
return false;
}

int year, month, day;
char dash1, dash2;

std::istringstream iss(dateStr);
iss >> year >> dash1 >> month >> dash2 >> day;

// 如果格式不正确,或者读取失败,返回false
if (iss.fail() || dash1 != '-' || dash2 != '-') {
return false;
}

// 检查年份是否合理(例如,假设年份范围在 1900 到 9999 之间)
if (year < 1900 || year > 9999) {
return false;
}

// 检查月份范围
if (month < 1 || month > 12) {
return false;
}

// 每个月天数的最大值
int daysInMonth;
switch (month) {
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
daysInMonth = 31;
break;
case 4: case 6: case 9: case 11:
daysInMonth = 30;
break;
case 2:
daysInMonth = isLeapYear(year) ? 29 : 28;
break;
default:
return false;
}

// 检查日期是否合法
if (day < 1 || day > daysInMonth) {
return false;
}

return true;
}

// 文件保存与加载
template <typename T>
void saveToFile(const string& filename, const vector<T>& data) {
ofstream outFile(filename);
for (const auto& item : data) outFile << item << endl;
}

template <typename T>
void loadFromFile(const string& filename, vector<T>& data) {
ifstream inFile(filename);
if (!inFile) {
cerr << "Warning: 未找到文件 " << filename << ",从空白开始!" << endl;
return;
}
T item;
while (inFile >> item) data.push_back(item);
}

template <typename T>
void deleteItem(vector<T>& data, const string& identifier, const string& filename) {
for (auto it = data.begin(); it != data.end(); ++it) {
if (it->id == identifier) { // 匹配唯一标识符(如工号、学号等)
data.erase(it);
saveToFile(filename, data);
cout << "删除成功!" << endl;
return;
}
}
cout << "未找到指定项!" << endl;
}

template <typename T>
void modifyItem(vector<T>& data, const string& identifier, const string& filename) {
bool found = false;
for (auto& item : data) {
if (item.id == identifier) { //
cout << "找到项,请输入新的数据(包括编号):" ;
cin >> item;
found = true;
break;
}
}
if (found) {
saveToFile(filename, data);
cout << "修改成功!" << endl;
}
else {
cout << "未找到指定项!" << endl;
}
}

void modifyTeacherItem(vector<Teacher>& data, const string& identifier, const string& filename) {
bool found = false;
vector<string> collages = { "计算机学院" ,"土木学院","经济学院","生化学院","教育学院" };
vector<string> sexs = { "男","女" };
for (auto& item : data) {
if (item.id == identifier) { //
cout << "找到项,请输入新的数据" << endl;
cout << "输入姓名:";
cin >> item.name;
for (auto temp : sexs) {
cout << temp << " ";
}
cout << "输入性别(0:男,1:女):";
int ij;
while (true) {
cin >> ij;
if (ij != 0 && ij != 1) {
cout << "重新输入性别" << endl;
}
else if (ij == 0) {
item.gender = "男";
break;
}
else {
item.gender = "女";
break;
}
}
cout << "输入年龄:";
cin >> item.age;
int tt1 = 0;
for (auto temp : collages) {
cout << tt1 << "." << temp << endl;
tt1 += 1;
}
cout << "选择学院(输入数字):";
cin >> ij;
item.college = collages[ij];
cout << "输入入职时间(yyyy-mm-dd):";
string time_;
while (true) {
cin >> time_;
if (checkDate(time_)) {
item.hireDate = time_;
break;
}
else {
cout << "日期不规范,请重新输入:";
}
}
found = true;
break;
}
}
if (found) {
saveToFile(filename, data);
cout << "修改成功!" << endl;
}
else {
cout << "未找到指定项!" << endl;
}
}

void modifyStudentItem(vector<Student>& data, const string& identifier, const string& filename) {
bool found = false;
vector<string> collages = { "计算机学院" ,"土木学院","经济学院","生化学院","教育学院" };
vector<string> sexs = { "男","女" };
for (auto& item : data) {
if (item.id == identifier) { //
cout << "找到项,请输入新的数据" << endl;
cout << "输入姓名:";
cin >> item.name;
for (auto temp : sexs) {
cout << temp << " ";
}
cout << "输入性别(0:男,1:女):";
int ij;
while (true) {
cin >> ij;
if (ij != 0 && ij != 1) {
cout << "重新输入性别" << endl;
}
else if (ij == 0) {
item.gender = "男";
break;
}
else {
item.gender = "女";
break;
}
}
cout << "输入年龄:";
cin >> item.age;
cout << "输入类别:";
cin >> item.category;

int tt1 = 0;
for (auto temp : collages) {
cout << tt1 << "." << temp << endl;
tt1 += 1;
}
cout << "选择学院(输入数字):";
cin >> ij;
item.college = collages[ij];


cout << "输入入学时间(yyyy-mm-dd):";
string time_;
while (true) {
cin >> time_;
if (checkDate(time_)) {
item.admissionDate = time_;
break;
}
else {
cout << "日期不规范,请重新输入:";
}
}
found = true;
break;
}
}
if (found) {
saveToFile(filename, data);
cout << "修改成功!" << endl;
}
else {
cout << "未找到指定项!" << endl;
}
}

// 显示主菜单
void showMenu() {
cout << "****************************" << endl;
cout << "***** 1.教师信息 *****" << endl;
cout << "***** 2.学生信息 *****" << endl;
cout << "***** 3.课程信息 *****" << endl;
cout << "***** 4.授课信息 *****" << endl;
cout << "***** 5.选课信息 *****" << endl;
cout << "***** 6.退出 *****" << endl;
cout << "****************************" << endl;
cout << "请输入您的选择: ";
}


void teacherMenu(vector<Teacher>& teachers) {
int choice;
vector<string> collages = { "计算机学院" ,"土木学院","经济学院","生化学院","教育学院"};
vector<string> sexs = { "男","女" };
int temp;
do {
cout << "1. 添加教师\n2. 修改教师\n3. 删除教师\n4. 查看所有教师" << endl;
cout << "0. 退出\n请输入您的选择: ";
cin >> choice;
switch (choice) {
case 1: {
system("cls");
Teacher t;
//cout << "输入工号 名称 性别 年龄 学院 入职时间: ";
cout << "输入工号:";
cin >> t.id;
cout << "输入姓名:";
cin >> t.name;
for (auto temp : sexs) {
cout << temp << " ";
}
cout << "输入性别(0:男,1:女):";
int ij;
while (true) {
cin >> ij;
if (ij != 0 && ij != 1) {
cout << "重新输入性别" << endl;
}
else if (ij == 0) {
t.gender = "男";
break;
}
else {
t.gender = "女";
break;
}
}
cout << "输入年龄:";
cin >> t.age;
int tt1 = 0;
for (auto temp : collages) {
cout << tt1 <<"." << temp << endl;
tt1 += 1;
}
cout << "选择学院(输入数字):";
cin >> ij;
t.college = collages[ij];
cout << "输入入职时间(yyyy-mm-dd):";
string time_;
while (true) {
cin >> time_;
if (checkDate(time_)) {
t.hireDate = time_;
break;
}
else {
cout << "日期不规范,请重新输入:";
}
}
teachers.push_back(t);
saveToFile("teachers.txt", teachers);
cout << "添加成功" << endl;
break;
}

case 2: { // 修改教师信息
string id;
system("cls");
cout << "输入要修改的教师工号: ";
cin >> id;
modifyTeacherItem(teachers, id, "teachers.txt");
break;
}

case 3: { // 修改教师信息
system("cls");
string id;
cout << "输入要删除的教师工号: ";
cin >> id;
deleteItem(teachers, id, "teachers.txt");
break;
}
case 4: {
system("cls");
for (const auto& t : teachers) t.display();
break;
}
case 0: cout << "退出" << endl; break;
default: cout << "输入错误,请重新输入!" << endl;
}
} while (choice != 0);
}
void studentMenu(vector<Student>& students) {
int choice;
vector<string> collages = { "计算机学院" ,"土木学院","经济学院","生化学院","教育学院" };
vector<string> sexs = { "男","女" };
do {

cout << "1. 添加学生\n2. 修改学生\n3. 删除学生\n4. 查看所有学生" << endl;
cout << "0. 退出\n请输入您的选择: ";
cin >> choice;
switch (choice) {
case 1: {
system("cls");
Student s;
//cout << "输入学号 名称 性别 年龄 类别 学院 入学时间: ";
cout << "输入学号:";
cin >> s.id;
cout << "输入姓名:";
cin >> s.name;
for (auto temp : sexs) {
cout << temp << " ";
}
cout << "输入性别(0:男,1:女):";
int ij;
while (true) {
cin >> ij;
if (ij != 0 && ij != 1) {
cout << "重新输入性别" << endl;
}
else if (ij == 0) {
s.gender = "男";
break;
}
else {
s.gender = "女";
break;
}
}
cout << "输入年龄:";
cin >> s.age;
cout << "输入类别:";
cin >> s.category;

int tt1 = 0;
for (auto temp : collages) {
cout << tt1 << "." << temp << endl;
tt1 += 1;
}
cout << "选择学院(输入数字):";
cin >> ij;
s.college = collages[ij];


cout << "输入入学时间(yyyy-mm-dd):";
string time_;
while (true) {
cin >> time_;
if (checkDate(time_)) {
s.admissionDate = time_;
break;
}
else {
cout << "日期不规范,请重新输入:";
}
}

students.push_back(s);
saveToFile("students.txt", students);
cout << "添加成功" << endl;
break;
}

case 2: {
system("cls");
string id;
cout << "输入要修改的学生学号: ";
cin >> id;
modifyStudentItem(students, id, "students.txt");
break;
}

case 3: {
system("cls");
string id;
cout << "输入要删除的学生学号: ";
cin >> id;
deleteItem(students, id, "students.txt");
break;
}
case 4: {
system("cls");
for (const auto& s : students) s.display();
break;
}
case 0: cout << "退出系统!" << endl; break;
default: cout << "输入错误,请重新输入!" << endl;
}
} while (choice != 0);
}
void courseMenu(vector<Course>& courses) {
int choice;
do {

cout << "1. 添加课程\n2. 修改课程\n3. 删除课程\n4. 查看所有课程" << endl;
cout << "0. 退出\n请输入您的选择: ";
cin >> choice;
switch (choice) {

case 1: {
system("cls");
Course c;
cout << "输入课程编号 名称 学分 类别: ";
cin >> c;
courses.push_back(c);
saveToFile("courses.txt", courses);
cout << "添加成功" << endl;
break;
}

case 2: {
string id;
system("cls");
cout << "输入要修改的课程编号: ";
cin >> id;
modifyItem(courses, id, "courses.txt");
break;
}
case 3: {
string id;
system("cls");
cout << "输入要删除的课程编号: ";
cin >> id;
deleteItem(courses, id, "courses.txt");
break;
}
case 4: {
system("cls");
for (const auto& c : courses) c.display();
break;
}
case 0: cout << "退出系统!" << endl; break;
default: cout << "输入错误,请重新输入!" << endl;
}
} while (choice != 0);
}
void teachInfoMenu(vector<TeachingInfo> teachingInfos) {
int choice;
do {

cout << "1. 添加授课信息\n2. 修改授课信息\n3. 删除授课信息\n4. 查看所有授课信息" << endl;
cout << "0. 退出\n请输入您的选择: ";
cin >> choice;
switch (choice) {

case 1: {
system("cls");
TeachingInfo t;
cout << "输入授课信息编号 教师工号 课程编号 开设时间: ";
cin >> t;
teachingInfos.push_back(t);
saveToFile("teaching_info.txt", teachingInfos);
cout << "添加成功" << endl;
break;
}

case 2: {
system("cls");
string id;
cout << "输入要修改的授课信息编号: ";
cin >> id;
modifyItem(teachingInfos, id, "teaching_info.txt");

break;
}

case 3: {
string id;
system("cls");
cout << "输入要删除的授课信息编号: ";
cin >> id;
deleteItem(teachingInfos, id, "teaching_info.txt");
break;
}
case 4: {
system("cls");
for (const auto& t : teachingInfos) t.display();
break;
}
case 0: cout << "退出系统!" << endl; break;
default: cout << "输入错误,请重新输入!" << endl;
}
} while (choice != 0);
}
void enrollMenu(vector<Enrollment> enrollments) {
int choice;
do {

cout << "1. 添加选课信息\n2. 修改选课信息\n3. 删除选课信息\n4. 查看所有选课信息" << endl;
cout << "0. 退出\n请输入您的选择: ";
cin >> choice;
switch (choice) {
case 1: {
Enrollment e;
cout << "输入选课信息编号 学生学号 教师工号 课程编号 成绩 选修时间: ";
cin >> e;
enrollments.push_back(e);
saveToFile("enrollments.txt", enrollments);
system("cls");
break;
}
case 2: { // 修改学生信息
string id;
cout << "输入要修改的选课信息编号: ";
cin >> id;
modifyItem(enrollments, id, "enrollments.txt");
system("cls");
break;
}
case 3: { // 修改学生信息
string id;
cout << "输入要修改的选课信息编号: ";
cin >> id;
deleteItem(enrollments, id, "enrollments.txt");
system("cls");
break;
}
case 4: for (const auto& e : enrollments) e.display(); break;
case 0: cout << "退出系统!" << endl; break;
default: cout << "输入错误,请重新输入!" << endl;
}
} while (choice != 0);
}

int main() {
vector<Teacher> teachers;
vector<Student> students;
vector<Course> courses;
vector<TeachingInfo> teachingInfos;
vector<Enrollment> enrollments;

// 加载数据
loadFromFile("teachers.txt", teachers);
loadFromFile("students.txt", students);
loadFromFile("courses.txt", courses);
loadFromFile("teaching_info.txt", teachingInfos);
loadFromFile("enrollments.txt", enrollments);

int choice;
do {
showMenu();
cin >> choice;
switch (choice) {
case 1: {
system("cls");
teacherMenu(teachers);
system("cls");
break;
}
case 2: {
system("cls");
studentMenu(students);
system("cls");
break;
}
case 3: {
system("cls");
courseMenu(courses);
system("cls");
break;
}
case 4: {
system("cls");
teachInfoMenu(teachingInfos);
system("cls");
break;
}
case 5: {
system("cls");
enrollMenu(enrollments);
system("cls");
break;
}
case 6: cout << "退出系统!" << endl; break;
default: cout << "输入错误,请重新输入!" << endl;
}
} while (choice != 6);

return 0;
}
 评论
评论插件加载失败
正在加载评论插件
由 Hexo 驱动 & 主题 Keep
访客数 访问量