#pragma once
#ifndef __student_H__
#define __student_H__
#include<cstdio>
#include<iostream>
#include<conio.h>
#include<cstdlib>
#include<cassert>
#include<iomanip>
#include<fstream>
#include<string>
#define ll long long
using namespace std;
//定义一个课程
typedef struct _course
{
//public:
string name;//课程名称
string class_number;//课程编号
string class_pro;//课程性质
string class_hour1;//总学时
string class_hour2;//授课学时
string class_hour3;//实验或上机学时
string score;// 学分
string class_hour;//开课学期
}course;
typedef struct _Node
{
//public:
course cor;//课程
struct _Node* pNEXT = NULL;//指向下一个节点的指针
}Node;
void printstu();
void menu1();
void menu1();
void inputsudent();
void saveclass();
void readclass();
void modify();
Node* find_class();
void deleteclass();
void chooseclass();
void newmenu();
void run1();
void run2();
#endif
#include"student.h"
Node* g_head = NULL;//链表的头节点
Node* m_A[1000] = { NULL };//储存按学分查找的课程结构体的地址
Node* m_B[1000] = { NULL };//储存按课程性质查找到的课程的地址
int main()//主函数
{
while (1)
{
newmenu();//主菜单,决定用户以什么身份进入
int n;
cin >> n;
switch (n)
{
case 1:
run1();
break;
case 2:
run2();
break;
case 3:
cout << "退出成功" << endl;
return 0;
default:
cout << "输入错误,请重新输入!" << endl;
system("pause");
system("cls");//清屏
break;
}
}
return 0;
}
void newmenu()//主菜单
{
cout << "-----------------------" << endl;
cout << "1.以管理员身份进入" << endl;
cout << "2.以学生身份进入" << endl;
cout << "3.退出系统" << endl;
cout << "-----------------------" << endl;
}
void menu1()//以管理员身份进入的子菜单
{
cout << "*****************************************" << endl;
cout << "*\t欢迎使用学生选修课程管理系统\t*" << endl;
cout << "*****************************************" << endl;
cout << "*\t 请选择功能列表 \t\t*" << endl;
cout << "*****************************************" << endl;
cout << "*\t 1.录入课程信息 \t\t*" << endl;
cout << "*\t 2.浏览课程信息 \t\t*" << endl;
cout << "*\t 3.查询课程信息 \t\t*" << endl;
cout << "*\t 4.保存课程信息 \t\t*" << endl;
cout << "*\t 5.读取课程信息 \t\t*" << endl;
cout << "*\t 6.学生选取课程 \t\t*" << endl;
cout << "*\t 7.修改课程信息 \t\t*" << endl;
cout << "*\t 8.删除课程信息 \t\t*" << endl;
cout << "*\t 9.退出系统 \t\t*" << endl;
cout << "*****************************************" << endl;
}
void menu2()//以学生身份进入的子菜单
{
cout << "*****************************************" << endl;
cout << "*\t欢迎使用学生选修课程管理系统\t*" << endl;
cout << "*****************************************" << endl;
cout << "*\t 请选择功能列表 \t\t*" << endl;
cout << "*****************************************" << endl;
cout << "*\t 1.查询课程信息 \t\t*" << endl;
cout << "*\t 2.浏览课程信息 \t\t*" << endl;
cout << "*\t 3.读取课程信息 \t\t*" << endl;
cout << "*\t 4.学生选取课程 \t\t*" << endl;
cout << "*\t 5.退出系统 \t\t*" << endl;
cout << "*****************************************" << endl;
}
void run1()//管理员执行
{
while (1)
{
menu1();
char ch = _getch();
switch (ch)
{
case '1'://录入课程信息
inputsudent();
break;
case '2'://浏览课程信息
printstu();
break;
case '3'://查询课程信息
{
int i = 1;
Node* p = find_class();
if (p == m_A[1])//如果与储存的第一个结构体相等,表示找到了
{
while (p != NULL)//输出所有找到课程的信息
{
cout << "课程编号: " << p->cor.class_number << " " << "课程名称: " << p->cor.name << " " << "课程性质: " << p->cor.class_pro << " " << "课程总学时: " << p->cor.class_hour1 << " " << "课程授课学时: " << p->cor.class_hour2 << " " << "课程实验或上机学时: " << p->cor.class_hour3 << " " << "课程学分: " << p->cor.score << " " << "课程开设学期: " << p->cor.class_hour << endl;
cout << endl;
p = m_A[++i];
}
}
if (p == m_B[1])//如果与储存的第一个课程结构体相等,表示找到了
{
while (p != NULL)//输出所有找到的课程的信息
{
cout << "课程编号: " << p->cor.class_number << " " << "课程名称: " << p->cor.name << " " << "课程性质: " << p->cor.class_pro << " " << "课程总学时: " << p->cor.class_hour1 << " " << "课程授课学时: " << p->cor.class_hour2 << " " << "课程实验或上机学时: " << p->cor.class_hour3 << " " << "课程学分: " << p->cor.score << " " << "课程开设学期: " << p->cor.class_hour << endl;
cout << endl;
p = m_B[++i];
}
}
system("pause");
system("cls");//清屏
break;
}
case '4'://保存选课信息
saveclass();
break;
case '5'://读取课程信息
readclass();
break;
case '6'://学生选取课程
chooseclass();
break;
case '7'://修改课程信息
modify();
break;
case '8'://删除课程信息
deleteclass();
break;
case '9'://退出系统
cout << "退出系统" << endl;
return;
}
}
}
void run2()//以学生的身份进入
{
while (1)
{
menu2();
char ch = _getch();
switch (ch)
{
case '1'://查询课程信息
{
void find_class2();
int i = 1;
Node* p = find_class();
/*if(p == NULL)
{
cout << "很抱歉,未找到这个课程!" << endl;
}*/
if (p == m_A[1])//如果与储存的第一个结构体相等,表示找到了
{
while (p != NULL)//输出所有找到课程的信息
{
cout << "课程编号: " << p->cor.class_number << " " << "课程名称: " << p->cor.name << " " << "课程性质: " << p->cor.class_pro << " " << "课程总学时: " << p->cor.class_hour1 << " " << "课程授课学时: " << p->cor.class_hour2 << " " << "课程实验或上机学时: " << p->cor.class_hour3 << " " << "课程学分: " << p->cor.score << " " << "课程开设学期: " << p->cor.class_hour << endl;
cout << endl;
p = m_A[++i];
}
}
if (p == m_B[1])//如果与储存的第一个结构体相等,表示找到了
{
while (p != NULL)//输出所有找到课程的信息
{
cout << "课程编号: " << p->cor.class_number << " " << "课程名称: " << p->cor.name << " " << "课程性质: " << p->cor.class_pro << " " << "课程总学时: " << p->cor.class_hour1 << " " << "课程授课学时: " << p->cor.class_hour2 << " " << "课程实验或上机学时: " << p->cor.class_hour3 << " " << "课程学分: " << p->cor.score << " " << "课程开设学期: " << p->cor.class_hour << endl;
cout << endl;
p = m_B[++i];
}
}
system("pause");
system("cls");//清屏
break;
}
case '2'://浏览课程信息
printstu();
break;
case '3'://读取课程信息
readclass();
break;
case '4'://学生选取课程
chooseclass();
break;
case '5'://退出菜单
cout << "退出菜单" << endl;
return;
default:
cout << "输入错误,请重新输入" << endl;
system("pause");
system("cls");//清屏
break;
}
}
}
void inputsudent()
{
Node* NEWp = new Node();//创造链表的头节点
assert(NEWp != NULL);//断言链表的头节点的动态内存分配是否成功
//将数据插入链表的之中
NEWp->pNEXT = NULL;
if (g_head == NULL)
{
g_head = NEWp;
}
else
{
NEWp->pNEXT = g_head;
g_head = NEWp;
}
/*将课程信息填入链表之中*/
cout << "请输入课程名称:" << endl;
cin >> NEWp->cor.name;
cout << "请输入课程编号:" << endl;
cin >> NEWp->cor.class_number;
cout << "请输入课程性质:" << endl;
cin >> NEWp->cor.class_pro;
cout << "请输入课程总学时:" << endl;
cin >> NEWp->cor.class_hour1;
cout << "请输入课程授课学时:" << endl;
cin >> NEWp->cor.class_hour2;
cout << "请输入课程实验或上机学时:" << endl;
cin >> NEWp->cor.class_hour3;
cout << "请输入课程学分:" << endl;
cin >> NEWp->cor.score;
cout << "请输入课程开课学期:" << endl;
cin >> NEWp->cor.class_hour;
cout << "课程信息录入成功!" << endl;
system("pause");
system("cls");//清屏
}
//打印课程信息
void printstu()
{
cout << "***********************************************************************" << endl;
cout << "*\t\t 欢迎使用学生选修课程管理系统 \t\t*" << endl;
cout << "***********************************************************************" << endl;
/*cout << "课程编号 课程名称 课程性质 总学时 授课学时 实验或上机学时 学分 开设学期" << endl;
cout << "***********************************************************************" << endl;*/
Node* p = g_head;
while (p != NULL)//遍历链表,将链表中所有课程的信息全部打印出来
{
cout << "课程编号: " << p->cor.class_number << " " << "课程名称: " << p->cor.name << " " << "课程性质: " << p->cor.class_pro << " " << "课程总学时: " << p->cor.class_hour1 << " " << "课程授课学时: "<< p->cor.class_hour2 <<" "<< "课程实验或上机学时: " << p->cor.class_hour3 << " "<<"课程学分: " << p->cor.score << " " << "课程开设学期: " << p->cor.class_hour << endl;
cout << endl;
p = p->pNEXT;
}
cout << "***********************************************************************" << endl;
system("pause");
system("cls");//清屏
}
//保存课程信息/
void saveclass()
{
fstream ofs;
//打开文件
ofs.open("student.txt", ios::out);
if (!ofs.is_open())//如果文件打开失败,提示文件打开失败,并退出程序
{
cout << "文件打开失败" << endl;
return;
}
Node* p2 = g_head;//若文件打开成功,将链表中的所有课程信息存入文本文件中
while (p2 != NULL)
{
ofs << p2->cor.class_number << " " << p2->cor.name << " " << p2->cor.class_pro << " " << p2->cor.class_hour1 << " " << p2->cor.class_hour2 << " " << p2->cor.class_hour3 << " " << p2->cor.score << " " << p2->cor.class_hour << endl;
p2 = p2->pNEXT;
}
ofs.close();//关闭文件
system("pause");
system("cls");//清屏
cout << "数据保存成功" << endl;
}
//读取课程信息/
void readclass()
{
int n = 0;
fstream ifs;//创建文件流对象
//打开文件
ifs.open("student.txt", ios::in);
if (!ifs.is_open())//如果文件打开失败,提示文件打开失败,并退出程序
{
cout << "文件打开失败" << endl;
return;
}
string buf;//创建储存文件信息的字符串
string buf2;
string buf3;
/*cout << "课程编号 课程名称 课程性质 总学时 授课学时 实验学时 学分 开设学期" << endl;*/
/**while (ifs >> buf )
{
cout << buf << " ";
}*/
while (!ifs.eof())//将文件中的课程信息全部读入到链表之中
{
Node* p2 = new Node();
assert(p2 != NULL);
p2->pNEXT = NULL;
if (g_head == NULL)
{
g_head = p2;
}
else
{
p2->pNEXT = g_head;
g_head = p2;
}
buf = " ";
buf2 = " ";
buf3 = " ";
ifs >> buf;
p2->cor.class_number = buf;
ifs >> buf;
p2->cor.name = buf;
ifs >> buf;
p2->cor.class_pro = buf;
ifs >> buf2;
p2->cor.class_hour1 = buf2;
ifs >> buf2;
p2->cor.class_hour2 = buf2;
ifs >> buf2;
p2->cor.class_hour3 = buf2;
ifs >> buf3;
p2->cor.score = buf3;
ifs >> buf;
p2->cor.class_hour = buf;
}
ifs.close();
system("pause");
system("cls");//清屏
cout << "数据读取成功" << endl;//提示数据读取成功
}
Node* find_class()//查找课程
{
int n,i = 0, m = 0, ok1 = 0, ok2 = 0,x= 0,g = 0,ok3 = 0;
string a;
string name;
cout << "请选择查找方式(‘1’为按学分查找,‘2’为按课程性质查找,'3'为查找同学选择的课程)" << endl;
cin >> n;//选择查找课程信息的模式
if (n == 1)
{
cout << "请输入您想查找的课程的学分:" << endl;//按学分查找课程
cin >> a;
Node* p = g_head;//遍历链表,看链表中是否存在与填入学分相等的课程
while (p != NULL)
{
if (p->cor.score == a)
{
ok1 = 1;
m_A[++i] = p;
}
p = p->pNEXT;
}
if (ok1 == 1)//如果找到,返回存储课程信息的第一个
{
m_A[++i] = NULL;
return m_A[1];
}
}
else if(n==2)
{
cout << "请输入您想查找的课程的性质:" << endl;//按课程性质查找
cin >> name;
Node* p2 = g_head;//遍历链表,查找链表中是否存在与输入的课程信息相等的课程
while (p2 != NULL)
{
if (p2->cor.class_pro == name)
{
ok2 = 1;
m_B[++m] = p2;
}
p2 = p2->pNEXT;
}
if (ok2 == 1)//如果找到,返回存储课程的第一个
{
m_B[++m] = NULL;
return m_B[1];
}
}
else if(n == 3)
{
string stunum;
int i = 0;
cout << "请输入你想查询课程学生的学号" << endl;//按填报学生的学号查找该学生填报了哪些课程
cin >> stunum;
fstream ifs;//创建文件流对象
ifs.open("stunum.txt", ios::in);//以只读的方式打开文件
if(!ifs.is_open())//若打开失败,则提示打开失败,并关闭程序
{
cout << "文件打开失败" << endl;
return NULL;
}
string num,num2[1000];
while (ifs >> num)//如果查找到该学生填报的课程,将其储存到数组中
{
if (num == stunum)
{
getline(ifs, num2[i++]);
}
}
for (int j = 0; j < i; j++)//将存储到数组中的课程信息打印到屏幕上
{
cout << num2[j] << endl;
}
return NULL;
}
else//若输入的模式错误,则退出查找模式
{
cout << "选择错误" << endl;
return NULL;
}
cout << "很抱歉,未找到这个课程!" << endl;//若未能找到该课程,则返回NULL
return NULL;
}
void modify()
{
string num;
string n;
cout << "请输入需要修改课程的编号" << endl;//输入需要修改课程的编号
cin >> num;
Node* p = g_head;
while (p != NULL)//遍历链表,查找是否存在与输入课程编号相等的课程,若找到该课程,则修改课程的信息
{
if (p->cor.class_number == num)
{
cout << "请输入要修改的课程信息" << endl;
cout << "(1)修改课程名称" << endl;
cout << "(2)修改课程编号" << endl;
cout << "(3)修改课程性质" << endl;
cout << "(4)修改总学时" << endl;
cout << "(5)修改课程授课学时" << endl;
cout << "(6)修改课程实验或上机学时" << endl;
cout << "(7)修改课程学分" << endl;
cout << "(8)修改课程开课学期" << endl;
cin >> p->cor.name >> p->cor.class_number >> p->cor.class_pro >> p->cor.class_hour1 >> p->cor.class_hour2 >> p->cor.class_hour3 >> p->cor.score >> p->cor.class_hour;
cout << "修改成功" << endl;
break;
}
p = p->pNEXT;
}
if (p == NULL)//若找到该课程,则提示未能找到该课程
{
cout << "未找到该课程!" << endl;
}
}
void deleteclass()//删除课程信息
{
string num;
cout << "请输入想删去课程的课程编号" << endl;
cin >> num;
Node* p1,*p;//遍历链表,查找链表中是否存在与输入课程编号相等的课程,若存在,则删除该课程节点
if (g_head->cor.class_number == num)
{
p1 = g_head;
g_head = g_head->pNEXT;
delete p1;
system("pause");
system("cls");//清屏
cout << "删除成功" << endl;
return;
}
Node* p2;
p = g_head;
while (p->pNEXT != NULL)
{
if (p->pNEXT->cor.class_number == num)
{
p2 = p->pNEXT;
p->pNEXT = p->pNEXT->pNEXT;
delete p2;
system("pause");
system("cls");//清屏
cout << "删除成功" << endl;
return;
}
p = p->pNEXT;
if (p->pNEXT == NULL)
{
break;
}
}
if (p->pNEXT == NULL)//若未查找到该课程,则提示未能找到该课程
{
cout << "未找到该课程" << endl;
return;
}
}
void chooseclass()//选课
{
string stunum;
string classnum;
cout << "请输入填报人的学号" << endl;//输入填报人的学号
cin >> stunum;
ofstream ofs("stunum.txt",ios::app|ios::out);//打开文本文件
if (!ofs.is_open())//若打开失败,则提示打开失败,并关闭程序
{
cout << "文件打开失败" << endl;
return;
}
cout << "请输入你想填报的课程编号" << endl;
cin >> classnum;
Node* p = g_head;
while (p != NULL)//遍历链表查找是否存在与输入课程编号相等的课程,若存在,则将填报人的学号和姓名,还有课程的信息输入到文本文件中
{
if (p->cor.class_number == classnum)
{
ofs << stunum << " " << p->cor.class_number << " " << p->cor.name << " " << p->cor.class_pro << " " << p->cor.class_hour1 << " " << p->cor.class_hour2 << " " << p->cor.class_hour3 << " " << p->cor.score << " " << p->cor.class_hour << endl;
cout << "选课成功!" << endl;
return;
}
p = p->pNEXT;
}
if (p == NULL)//若未能找到该文件,则提示未能找到该课程
{
cout << "未找到该课程" << endl;
return;
}
}