#include <iostream>
#include <fstream>
using namespace std;
#define Mc_CourseNameLength 61 /*课程名称不超过30个字*/
#define Mc_FileName ("CourseData.csd") /*存储课程信息的文件名称*/
#define Mc_OpenFileError 0 /*打开文件失败*/
#define Mc_OpenFileSucceed 1 /*打开文件成功*/
class ElectiveSystem /*选课系统*/
{
protected:
struct Course /*课程*/
{
long ID; /*课程编号,0 到 65535*/
char name[Mc_CourseNameLength]; /*课程名称*/
float credit; /*学分*/
short TimeTotal; /*总学时*/
short TimeLesson; /*授课学时*/
short TimeExperiment; /*实验学时*/
short nature; /*课程性质,值域为 1 到 3,每种数值代表一种性质。因为不知道什么是课程的性质,所以没给出具体的含义*/
bool term; /*为真表示上学期,为假表示下学期*/
short number; /*节点编号,从 1 开始*/
struct Course *next; /*下一个节点*/
} *PtHead; /*头结点*/
int AddNewCourse(); /*增加新课程信息*/
int SaveCourseData(struct Course *cs); /*保存单个课程信息*/
int LoadCourseData(); /*加载课程信息*/
void ShowCourseData(); /*显示课程信息*/
int MainInterface(); /*系统主界面*/
void InputCourseData(); /*录入课程信息界面*/
void SelectCourse(); /*学生选课界面*/
void SearchCourse(); /*课程查询界面*/
void SearchCredit(); /*按学分查询*/
void SearchNature(); /*按性质查询*/
void FreePtHeadMemery(); /*释放PtHead指向的空间*/
public:
ElectiveSystem(); /*构造函数*/
~ElectiveSystem(); /*析构函数*/
void BeginRun(); /*开始运行此系统*/
};
int ElectiveSystem::AddNewCourse()
{
struct Course cs;
cout<<"请输入课程信息,每输入完一个项目按一次Enter键"<<endl<<endl<<endl;
cout<<"课程编号 (0 到 65535,且必须是唯一的,不能与其他课程编号重复): ";
cin>>cs.ID;
cout<<endl;
cout<<"课程名称 (不超过30个文字): ";
cin>>cs.name;
cout<<endl;
cout<<"课程学分 (0.5 到 5): ";
cin>>cs.credit;
cout<<endl;
cout<<"总学时 (0 到 300): ";
cin>>cs.TimeTotal;
cout<<endl;
cout<<"授课学时: ";
cin>>cs.TimeLesson;
cout<<endl;
cout<<"实验学时: ";
cin>>cs.TimeExperiment;
cout<<endl;
cout<<"课程性质 ( 1 到 3): ";
cin>>cs.nature;
cout<<endl;
cout<<"开课学期 (输入 1 表示上学期,0 表示下学期): ";
cin>>cs.term;
cout<<endl;
return SaveCourseData(&cs);
}
int ElectiveSystem::SaveCourseData(struct Course *cs)
{
fstream file;
file.open(Mc_FileName, ios::binary | ios::out | ios::app);
if (!file)
return Mc_OpenFileError;
file.write((char*)&cs->ID, sizeof(long));
file.write(cs->name, Mc_CourseNameLength * sizeof(char));
file.write((char*)&cs->credit, sizeof(float));
file.write((char*)&cs->TimeTotal, sizeof(short));
file.write((char*)&cs->TimeLesson, sizeof(short));
file.write((char*)&cs->TimeExperiment, sizeof(short));
file.write((char*)&cs->nature, sizeof(short));
file.write((char*)&cs->term, sizeof(bool));
file.close();
return Mc_OpenFileSucceed;
}
int ElectiveSystem::LoadCourseData()
{
ifstream file;
FreePtHeadMemery();
PtHead = NULL;
file.open(Mc_FileName, ios::binary);
if (!file)
return Mc_OpenFileError;
struct Course *cur, *New, test;
short num = 1;
while (!file.eof())
{
if (PtHead == NULL)
{
test.nature = 0;
file.read(reinterpret_cast<char*> (&test.ID), sizeof(long));
file.read(reinterpret_cast<char*> (test.name), Mc_CourseNameLength * sizeof(char));
file.read(reinterpret_cast<char*> (&test.credit), sizeof(float));
file.read(reinterpret_cast<char*> (&test.TimeTotal), sizeof(short));
file.read(reinterpret_cast<char*> (&test.TimeLesson), sizeof(short));
file.read(reinterpret_cast<char*> (&test.TimeExperiment), sizeof(short));
file.read(reinterpret_cast<char*> (&test.nature), sizeof(short));
file.read(reinterpret_cast<char*> (&test.term), sizeof(bool));
if (test.nature != 0)
{
cur = PtHead = new struct Course;
*cur = test;
cur->number = num ++;
}
else
break;
}
else
{
test.nature = 0;
file.read(reinterpret_cast<char*> (&test.ID), sizeof(long));
file.read(reinterpret_cast<char*> (test.name), Mc_CourseNameLength * sizeof(char));
file.read(reinterpret_cast<char*> (&test.credit), sizeof(float));
file.read(reinterpret_cast<char*> (&test.TimeTotal), sizeof(short));
file.read(reinterpret_cast<char*> (&test.TimeLesson), sizeof(short));
file.read(reinterpret_cast<char*> (&test.TimeExperiment), sizeof(short));
file.read(reinterpret_cast<char*> (&test.nature), sizeof(short));
file.read(reinterpret_cast<char*> (&test.term), sizeof(bool));
if (test.nature !=0)
{
New = new struct Course;
*New = test;
cur->next = New;
cur = New;
cur->number = num ++;
}
else /*nature的值域为1 到 3。之前有test.nature = 0,如果能正确读到数据,那么不应该是0;如果是0,表明到了文件尾部*/
break; /*因为eof()这个函数到了文件尾部会多停留一次,所以为了解决这个问题,采用此策略*/
}
}
cur->next = PtHead;
file.close();
return Mc_OpenFileSucceed;
}
void ElectiveSystem::ShowCourseData()
{
bool flag = true;
if (PtHead == NULL)
{
LoadCourseData();
if (PtHead == NULL)
{
flag = false;
cout<<endl<<"管理员尚未录入任何课程信息! 请管理员录入课程信息后,再来浏览!"<<endl;
system("pause");
}
}
if (flag)
{
struct Course *cs = PtHead->next;
cout<<"编号 学分 总学时 授课学时 实验学时 性质 开课学期 名称"<<endl<<endl;
cout.precision(2);
cout.setf(ios::left);
while (cs->number != 1)
{
cout.width(9);
cout<<cs->ID;
cout.width(9);
cout<<cs->credit;
cout.width(9);
cout<<cs->TimeTotal;
cout.width(9);
cout<<cs->TimeLesson;
cout.width(9);
cout<<cs->TimeExperiment;
cout.width(9);
cout<<cs->nature;
if (cs->term == 1)
cout<<"上学期 ";
else
cout<<"下学期 ";
cout<<cs->name<<endl<<endl;
cs = cs->next;
}
cout.width(9);
cout<<cs->ID;
cout.width(9);
cout<<cs->credit;
cout.width(9);
cout<<cs->TimeTotal;
cout.width(9);
cout<<cs->TimeLesson;
cout.width(9);
cout<<cs->TimeExperiment;
cout.width(9);
cout<<cs->nature;
if (cs->term == 1)
cout<<"上学期 ";
else
cout<<"下学期 ";
cout<<cs->name<<endl<<endl;
system("pause");
}
}
ElectiveSystem::ElectiveSystem()
{
PtHead = NULL;
LoadCourseData();
}
ElectiveSystem::~ElectiveSystem()
{
FreePtHeadMemery();
}
int ElectiveSystem::MainInterface()
{
short selection = 0;
cout<<"学生选课系统: 请输入相应的序号,然后按Enter键"<<endl<<endl<<endl;
cout<<"1.课程录入"<<endl<<endl;
cout<<"2.浏览课程信息"<<endl<<endl;
cout<<"3.学生选课"<<endl<<endl;
cout<<"4.课程查询"<<endl<<endl;
cout<<"5.退出"<<endl<<endl;
cout<<"请在此输入你的选择: ";
cin>>selection;
return selection;
}
void ElectiveSystem::InputCourseData()
{
LoadCourseData();
if (PtHead == NULL)
{
short CourseCount = 0;
int loop = 0;
cout<<"你将要录入多少个课程的信息?请输入课程个数,然后按Enter键"<<endl<<endl<<endl;
cout<<"请在此输入你要录入的课程的个数: ";
cin>>CourseCount;
system("cls");
for ( ; loop < CourseCount; loop ++)
{
cout<<"正在录入第 "<<loop + 1<<" 个课程信息,共 "<<CourseCount<<" 个课程信息"<<endl<<endl;
AddNewCourse();
system("cls");
}
cout<<"录入完成 ! 按任意键返回主界面 ......"<<endl;
system("pause");
}
else
{
cout<<"你已经进行过数据录入,要想重新录入数据请将文件 "<<Mc_FileName<<" 删除!"<<endl<<endl<<endl;
cout<<"按任意键返回主界面 ......"<<endl<<endl;
system("pause");
}
}
void main()
{
ElectiveSystem es;
es.BeginRun();
}