#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <list>
#include <map>
using namespace std;
typedef struct Student
{
int sid; // 学号
char sname[20]; // 姓名
} Student;
typedef struct Course
{
int cid;//编号
char cname[20];// 课程名
} Course;
typedef struct StudentCourse
{
int sid;
int cid;
} StudentCourse;
#define STUDENT_DB "stuinfo.txt"
#define COURSE_DB "courinfo.txt"
#define STUCOUR_DB "stucourinfo.txt"
void startMenu()
{
cout << "1. 录入课程" << endl;
cout << "2. 学生注册" << endl;
cout << "3. 浏览课程" << endl;
cout << "4. 查看学生" << endl;
cout << "5. 学生选课" << endl;
cout << "6. 浏览选课" << endl;
cout << "7. 删除选课" << endl;
cout << "8. 根据学生名查询选课" << endl;
cout << "9. 根据选课查询学生" << endl;
cout << "#. 统计排序" << endl;
}
// 录入课程
void addCourse()
{
// 清空输入缓冲
int c;
while ((c = getchar()) != '
' && c != EOF);
Course *pCourse = (Course *)malloc(sizeof(Course));
cout << "课程名:";
fgets(pCourse->cname, 20, stdin);
// 去掉最后的换行符
pCourse->cname[strlen(pCourse->cname) - 1] = ' ';
cout << "课程编号:";
char buf[100];
gets_s(buf);
pCourse->cid = atoi(buf);
FILE *fp;
if ((fp = fopen(COURSE_DB, "ab")) == NULL)
{
exit(1);
}
fwrite(pCourse, sizeof(Course), 1, fp);
free(pCourse);
fclose(fp);
}
// 录入学生
void addStudent()
{
// 清空输入缓冲
int c;
while ((c = getchar()) != '
' && c != EOF);
Student *pStudent = (Student *)malloc(sizeof(Student));
cout << "学号:";
char buf[100];
fgets(buf, 100, stdin);
pStudent->sid = atoi(buf);
cout << "姓名:";
gets_s(pStudent->sname);
FILE *fp;
if ((fp = fopen(STUDENT_DB, "ab")) == NULL)
{
exit(1);
}
fwrite(pStudent, sizeof(Student), 1, fp);
free(pStudent);
fclose(fp);
}
// 浏览课程
void browseCourses()
{
FILE *fp;
if ((fp = fopen(COURSE_DB, "rb")) == NULL)
{
exit(1);
}
Course *pCourse = (Course *)malloc(sizeof(Course));
while (fread(pCourse, sizeof(Course), 1, fp) == 1)
{
cout << "课程名:" << pCourse->cname << " ";
cout << "编号:" << pCourse->cid << endl;
}
free(pCourse);
fclose(fp);
}
// 查看学生
void browseStudent()
{
FILE *fp;
if ((fp = fopen(STUDENT_DB, "rb")) == NULL)
{
exit(1);
}
Student *pStudent = (Student *)malloc(sizeof(Student));
while (fread(pStudent, sizeof(Student), 1, fp) == 1)
{
cout << "名字:" << pStudent->sname << " ";
cout << "学号:" << pStudent->sid << endl;
}
free(pStudent);
fclose(fp);
}
// 选课
void selectCourse()
{
// 清空输入缓冲
int c;
while ((c = getchar()) != '
' && c != EOF);
FILE *fp;
if ((fp = fopen(STUCOUR_DB, "ab")) == NULL)
{
exit(1);
}
StudentCourse *pSC = (StudentCourse *)malloc(sizeof(StudentCourse));
cout << "学生学号:";
char buf[100];
gets_s(buf);
pSC->sid = atoi(buf);
cout << "课程编号:";
gets_s(buf);
pSC->cid = atoi(buf);
fwrite(pSC, sizeof(StudentCourse), 1, fp);
free(pSC);
fclose(fp);
}
// 浏览选课
void browseSelectCourse()
{
FILE *fp;
if ((fp = fopen(STUCOUR_DB, "rb")) == NULL)
{
exit(1);
}
StudentCourse *pSC = (StudentCourse *)malloc(sizeof(StudentCourse));
while (fread(pSC, sizeof(StudentCourse), 1, fp) == 1)
{
cout << "学号:" << pSC->sid << " " << "课程编号:" << pSC->cid << endl;
}
free(pSC);
fclose(fp);
}
// 删除选课
void deleteCourse()
{
// 清空输入缓冲
int c;
while ((c = getchar()) != '
' && c != EOF);
cout << "输入学生学号:";
char buf[100];
gets_s(buf);
StudentCourse *pSC1 = (StudentCourse *)malloc(sizeof(StudentCourse));
pSC1->sid = atoi(buf);
cout << "输入选修课编号:";
gets_s(buf);
pSC1->cid = atoi(buf);
FILE *fp;
if ((fp = fopen(STUCOUR_DB, "rb")) == NULL)
{
exit(1);
}
StudentCourse pSC2;
list<StudentCourse> lsc;
bool flag = false;
while (fread(&pSC2, sizeof(StudentCourse), 1, fp) == 1)
{
if (pSC2.cid == pSC1->cid&&pSC2.sid == pSC1->sid)
{
flag = true;
}
else
{
lsc.push_back(pSC2);
}
}
if (flag)
{
fclose(fp);
if ((fp = fopen(STUCOUR_DB, "wb")) == NULL)
{
exit(1);
}
while (!lsc.empty())
{
fwrite(&lsc.front(), sizeof(StudentCourse), 1, fp);
lsc.pop_front();
}
}
free(pSC1);
fclose(fp);
}
// 根据学生名查询选课
void browseCourseBySName()
{
// 清空输入缓冲
int c;
while ((c = getchar()) != '
' && c != EOF);
cout << "输入姓名:";
char buf[100];
gets_s(buf);
// 根据姓名查出学号
FILE *fp;
if ((fp = fopen(STUDENT_DB, "rb")) == NULL)
{
exit(1);
}
int sid;
Student *pStudent = (Student *)malloc(sizeof(Student));
while (fread(pStudent, sizeof(Student), 1, fp) == 1)
{
if (strcmp(pStudent->sname, buf) == 0)
{
sid = pStudent->sid;
break;
}
}
free(pStudent);
fclose(fp);
// 根据学号查出选课
if ((fp = fopen(STUCOUR_DB, "rb")) == NULL)
{
exit(1);
}
FILE *fp1;
if ((fp1 = fopen(COURSE_DB, "rb")) == NULL)
{
exit(1);
}
StudentCourse *pSC = (StudentCourse *)malloc(sizeof(StudentCourse));
Course *pCourse = (Course *)malloc(sizeof(Course));
//list<char[100]> lbuf;
cout << "已选课程:" << endl;
while (fread(pSC, sizeof(StudentCourse), 1, fp) == 1)
{
if (sid == pSC->sid)
{
// 根据 cid 查出课程
while (fread(pCourse, sizeof(Course), 1, fp1) == 1)
{
if (pCourse->cid == pSC->cid)
{
//strcpy(buf, pCourse->cname);
//lbuf.push_back(buf);
// 打印
cout << " " << pCourse->cname << endl;
break;
}
}
}
}
fclose(fp);
fclose(fp1);
free(pSC);
free(pCourse);
}
// 根据选课查询学生
void browseStudentByCourse()
{
// 清空输入缓冲
int c;
while ((c = getchar()) != '
' && c != EOF);
cout << "输入课程名:";
char buf[100];
gets_s(buf);
// 课程查出课程 cid
FILE *fp;
if ((fp = fopen(COURSE_DB, "rb")) == NULL)
{
exit(1);
}
Course *pCourse = (Course *)malloc(sizeof(Course));
int cid;
while (fread(pCourse, sizeof(Course), 1, fp) == 1)
{
if (strcmp(pCourse->cname, buf) == 0)
{
cid = pCourse->cid;
break;
}
}
fclose(fp);
free(pCourse);
if ((fp = fopen(STUCOUR_DB, "rb")) == NULL)
{
exit(1);
}
FILE *fp1;
if ((fp1 = fopen(STUDENT_DB, "rb")) == NULL)
{
exit(1);
}
StudentCourse *pSC = (StudentCourse *)malloc(sizeof(StudentCourse));
Student *pStudent = (Student *)malloc(sizeof(Student));
cout << "已选学生:" << endl;
while (fread(pSC, sizeof(StudentCourse), 1, fp) == 1)
{
if (cid == pSC->cid)
{
while (fread(pStudent, sizeof(Student), 1, fp1) == 1)
{
if (pStudent->sid == pSC->sid)
{
cout << " " << pStudent->sname << endl;
break;
}
}
}
}
fclose(fp1);
fclose(fp);
free(pSC);
free(pStudent);
}
bool compareNocase(list<int>& first, list<int>& second)
{
return first.size() > second.size();
}
// 统计排序
void statisticalSorting()
{
list<list<int>> llist;
FILE *fp;
if ((fp = fopen(STUCOUR_DB, "rb")) == NULL)
{
exit(1);
}
StudentCourse *pSC = (StudentCourse *)malloc(sizeof(StudentCourse));
while (fread(pSC, sizeof(StudentCourse), 1, fp) == 1)
{
bool flag = false;
// 寻找 llist 中是否存在 cid
for (auto &l : llist)
{
if (l.front() == pSC->cid)
{
flag = true;
l.push_back(0);
break;
}
}
if (!flag)
{
list<int> lint;
lint.push_back(pSC->cid);
llist.push_back(lint);
}
}
cout << "课程编号" << " " << "选课人数" << endl;
llist.sort(compareNocase);
for (auto &l : llist)
{
cout << l.front() << " " << l.size() << endl;
}
}
int main()
{
while (1)
{
startMenu();
start:
cout << "请选择:";
int ch;
ch = getchar();
switch (ch)
{
case '1':
addCourse();
break;
case '2':
addStudent();
break;
case '3':
browseCourses();
break;
case '4':
browseStudent();
break;
case '5':
selectCourse();
break;
case '6':
browseSelectCourse();
break;
case '7':
deleteCourse();
break;
case '8':
browseCourseBySName();
break;
case '9':
browseStudentByCourse();
break;
case '#':
// todo 统计排序
statisticalSorting();
break;
case '
':
goto start;
default:
cout << "非法输入" << endl;
}
}
return 0;
}