数据结构之链表的实现-------C++课程设计-----学生选课管理系统

题目简介:

  教师开设选修课程,每门选修课程有课程编号、课程名称、总学时、学分、授课教师信息;学生信息包括学号、姓名、专业、班级等信息,每位学生只能选修一门课程。试设计选修课程系统,使之能提供以下功能:

1、课程信息录入(课程信息用文件保存)

2、课程信息浏览、删除功能

3、学生信息录入功能(学生信息用文件保存)

4、学生信息浏览,删除功能

5、学生选修课程:按教师选择或学分选择

代码实现一:面向对象:

// file: FileDir.h

#include <string>
using namespace std;


class FileDir
{
public:
	FileDir(string &strFilePathName);
	~FileDir();
	bool FileExist();
	string GetCreatTime();
	char GetFileDrive();
	long GetFileSize();
	string GetModifiedTime();
	bool IsDir();
public:
	string f_strFilePathName;
};
// file: FileDir.cpp

#include <string>
#include <ctime>
using namespace std;
#include <sys\stat.h>
#include "FileDir.h"

//

FileDir::FileDir(string & strFilePathName)
{
	f_strFilePathName = strFilePathName;
};

FileDir::~FileDir()
{
	
};

bool FileDir::FileExist()
{
	struct _stat buf;
	int result;

	result = _stat(f_strFilePathName.c_str(),&buf);
	
	return (result == 0);
};

string FileDir::GetCreatTime()
{
	struct _stat buf;

	int result;

	result = _stat(f_strFilePathName.c_str(),&buf);

	if(result == 0)
	{
		return string(ctime(&buf.st_ctime));
	}
	
	return "0";
};

char FileDir::GetFileDrive()
{
	struct _stat buf;

	int result ;

	result = _stat(f_strFilePathName.c_str() , &buf);

	if(result == 0)
	{
		return (buf.st_dev + 'A');
	}

	return '0';

};

long FileDir::GetFileSize()
{
	struct _stat buf;

	int result ;

	result = _stat(f_strFilePathName.c_str(),&buf);

	if(result == 0)
	{
		return (buf.st_size);
	}

	return 0;
};

string FileDir::GetModifiedTime()

{

    struct _stat buf;

    int result;

    result = _stat(f_strFilePathName.c_str(), &buf);

    if ( result == 0 )

    {

    return ( ctime(&buf.st_atime) );
	// st_atime 文件上一次被访问的时间

    }

    return "0";

};

bool FileDir::IsDir()

{

    struct _stat buf;

    /

    if ( _stat(f_strFilePathName.c_str(), &buf) != 0 )

    {

    return false;

    }

    return ( (buf.st_mode & S_IFDIR) !=0 );
	// st_mode 文件权限和文件类型信息
};
// file:  Students.h
#include <string>
using namespace std;

class Students
{
public:
	Students();
	void Display(int num = 0);  // 输出 Students
	void SetnCourNum(int ncournum);
	void SetName(char chname[15]);
	void SetProfession(char chprofession[15]);
	void SetClass(char chclass[15]);
	void SetNum(int num);
public:
	int nNum;       // 学号
	char chName[15];  // 姓名
	char chProfession[15];  // 专业
	char chClass[15];     //  班级
	int nCourNum;        //  已选课程编号
	Students* pNext;    //  指向下一个Students结点
};
// file: Students.cpp

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
#include "Students.h"

Students::Students()
{
	strcpy(chClass,"");
	strcpy(chName,"");
	strcpy(chProfession,"");
	nCourNum = 0;
	nNum = 0;
	pNext = NULL;
};

void Students::Display(int num)
{
	if(num == 1)
	{
	cout << setw(15) << nNum;
	cout << setw(15) << chName ;
	cout << setw(15)<< chProfession ;
	cout << setw(15) << chClass ;
	cout << setw(15) << nCourNum << endl;
	}

	if(num == 0)
	{
	cout << setw(15) << nNum ;
	cout << setw(15) << chName ;
	cout << setw(15)<< chProfession ;
	cout << setw(15) << chClass ;
	cout << setw(15) << "未选课" <<endl;
	}
};

void Students::SetnCourNum(int ncournum)
{
	nCourNum = ncournum;
}

void Students::SetClass(char chclass[15])
{
	strcpy(chClass ,chclass);
}

void Students::SetName(char chname[15])
{
	strcpy(chName,chname);
}

void Students::SetProfession(char chprofession[15])
{
	strcpy(chProfession,chprofession);
}

void Students::SetNum(int num)
{
	nNum = num;
}
//  file:  Course.h
#include <string>
using namespace std;

class Course
{
public:
	Course();
	~Course();
	void Display(int num = 0);   // 输出 Course
	void SetName(char chname[15]);
	void SetHour(int nhour);
	void SetCredit(int ncredit);
	void SetTeacher(char chteacher[15]);
	void SetNum(int num);
public:
	int nNum;         // 编号
	char chName[15];  // 课程名
	int nHour;        // 学时
	int nCredit;      // 学分
	char chTeacher[15];   // 教师
	Course* pNext;    // 指向下一个 
};
// file: Course.cpp

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
#include "Course.h"

//

Course::Course()
{
	nNum = 0;
	strcpy(chName , "");
	nHour = 0;
	nCredit = 0;
	strcpy(chTeacher,"");
	pNext = NULL;
};

Course::~Course()
{

};

void Course::Display(int num)
{
	if(num == 0)
	{
	cout << setw(15) << nNum ;
	cout << setw(15) << chName ;
	cout << setw(15) << nHour ;
	cout << setw(15) << nCredit ;
	cout << setw(15) << chTeacher << endl;
	}
	else
	{
	cout <<"课程编号:"<< nNum << endl;
	cout << "课程名称:" << chName << endl;
	cout << "课程学时:"<< nHour << endl;
	cout << "课程学分:"<< nCredit << endl;
	cout << "授课教师:" << chTeacher << endl;
	}
};

void Course::SetCredit(int ncredit)
{
	nCredit = ncredit;
}

void Course::SetHour(int nhour)
{
	nHour = nhour;
}

void Course::SetName(char chname[15])
{
	strcpy(chName,chname);
}

void Course::SetTeacher(char chteacher[15])
{
	strcpy(chTeacher,chteacher);
}

void Course::SetNum(int num)
{
	nNum = num;
}
// file: StuCourList.h

#include <string>
using namespace std;
#include "Course.h"
#include "Students.h"

class StuCourList
{
public:
	StuCourList(string strCourPathName = "",string strStuPathName = "");
	
	~StuCourList(); // 析构

	void SetStuPathName(string strStuPathName);  // 设置文件路径
	void SetCourPathName(string strCourPathName);

	void DisplayStu();  // 输出
	void DisplayCour();

	bool InputStu(int num = 0);   // 录入
	bool InputCour(int num = 0);

	bool FromFileStu();  // 从文件读取
	bool FromFileCour();

	bool ToFileStu(int num = 0); // 保存到文件
	bool ToFileCour(int num = 0);

	void ChoiceByNum(int num);   // 按编号选择
	void ChoiceByTeach(char chTeach[15]); // 按教师选择

	void DeleteStu(int num);  // 删除
	void DeleteCour(int num);

	void ModifyStu(int num);  // 修改
	void ModifyCour(int num);

private:
	void FreeCour();  // 释放内存
	void FreeStu();

private:
	Course* pCourHead;  //course 链表头
	string m_strCourPathName;  //course 路径

	Students* pStuHead;  // students 链表头
	string m_strStuPathName;  // students 路径
};
// file: StuCourList.cpp

#include <string>
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
#include "StuCourList.h"
#include "Course.h"
#include "Students.h"

StuCourList::StuCourList(string strCourPathName,string strStuPathName)
{
	pCourHead = NULL;
	pStuHead = NULL;
	m_strCourPathName = strCourPathName;
	m_strStuPathName = strStuPathName;
};

void StuCourList::SetCourPathName(string strCourPathName)
{
	m_strCourPathName = strCourPathName;
};

void StuCourList::SetStuPathName(string strStuPathName)
{
	m_strStuPathName = strStuPathName;
};

StuCourList::~StuCourList()
{
	FreeCour();
	FreeStu();
};

bool StuCourList::InputCour(int num)
{
	Course *pCourNew ;
	Course *pCourPrev ;

	FreeCour();

	cout << endl << "请依次输入每个课程信息,课程编号为 -1 结束 !" << endl;

	do
	{
		pCourNew = new Course;

		cout <<endl<< "课程编号:" ;
		cin >> pCourNew->nNum;

		if(pCourNew->nNum == -1)
		{
			delete pCourNew;
			break;
		}

		pCourNew->pNext = NULL;
		cout << "课程名称:" ; 
		cin >> pCourNew->chName;

		cout << "课程学时:";  
		cin >> pCourNew->nHour;

		cout << "课程学分:";  
		cin >> pCourNew->nCredit;

		cout << "授课教师:";  
		cin >> pCourNew->chTeacher;

		if(pCourHead == NULL)
		{
			pCourHead = pCourNew;
		}
		else
		{
			pCourPrev = pCourHead;
			while(pCourPrev->pNext != NULL)
				pCourPrev = pCourPrev->pNext;

			pCourPrev->pNext = pCourNew;
		}
	}while(true);

	ToFileCour(num);

	return true;
};

bool StuCourList::InputStu(int num)
{
	Students *pStuNew ;
	Students *pStuPrev ;

	FreeStu();

	cout <<endl<< "请依次输入每个学生信息,学生学号为 -1 结束 !" << endl;

	do
	{
		pStuNew = new Students;

		cout <<endl<< "学生学号:";
		cin >> pStuNew->nNum;

		if(pStuNew->nNum == -1)
		{
			delete pStuNew ;
			break;
		}

		pStuNew->pNext = NULL;
		pStuNew->nCourNum = 0;

		cout << "学生姓名:";
		cin >> pStuNew->chName;

		cout << "学生专业:";  
		cin >> pStuNew->chProfession;
		
		cout << "学生班级:";  
		cin >> pStuNew->chClass;

		if(pStuHead == NULL)
		{
			pStuHead = pStuNew;
		}
		else
		{
			pStuPrev = pStuHead;
			while(pStuPrev->pNext != NULL)
				pStuPrev = pStuPrev->pNext;

			pStuPrev->pNext = pStuNew;
		}
	}while(true);

	ToFileStu(num);
	
	return true;
};

bool StuCourList::FromFileCour()
{
	Course *pCourNew;
	ifstream fileIn;

	FreeCour();

	fileIn.open(m_strCourPathName.c_str(),ios::in|ios::binary);

	if(fileIn.fail())
	{
		cout <<endl<<"Can not open file:" << m_strCourPathName << endl;
		return false;
	}

	while(!fileIn.eof())
	{
		pCourNew = new Course;

		fileIn.read((char *)pCourNew,sizeof(*pCourNew));

		int ng = fileIn.gcount();
		if(ng == 0)
		{
			delete pCourNew;
			break;
		}

		pCourNew->pNext = NULL;
		if(pCourHead == NULL)
		{
			pCourHead = pCourNew;
		}
		else
		{
			pCourNew->pNext = pCourHead;
			pCourHead = pCourNew;
		}
	}
	if(pCourHead == NULL)
	{
		cout <<endl<< "提示:" << m_strCourPathName<< "为空!" << endl;
	}
	fileIn.close();

	return true;
};

bool StuCourList::FromFileStu()
{
	Students *pStuNew ;
	ifstream fileIn;

	FreeStu();

	fileIn.open(m_strStuPathName.c_str(),ios::in|ios::binary);

	if(fileIn.fail())
	{
		cout <<endl<< "Can not open file:" <<m_strStuPathName << endl;
		return false;
	}
	
	while(!fileIn.eof())
	{
	    pStuNew = new Students;
		pStuNew->nCourNum = 0;

		fileIn.read((char*)pStuNew,sizeof(*pStuNew));



		int ng = fileIn.gcount();
		if(ng == 0)
		{
			delete pStuNew;
			break;
		}

		pStuNew->pNext = NULL;
		if(pStuHead == NULL)
		{
			pStuHead = pStuNew;
		}
		else
		{
			pStuNew->pNext = pStuHead;
			pStuHead = pStuNew;
		}
	}
	if(pStuHead ==NULL)
	{
		cout <<endl<< "提示:"<<m_strStuPathName << "为空!"<< endl;
	}
	fileIn.close();

	return true;
};

bool StuCourList::ToFileCour(int num)
{
	Course *pCourCur;

	ofstream fileOut;

	if(pCourHead == NULL)
	{
		fileOut.open(m_strCourPathName.c_str(),ios::out|ios::binary);
		return true;
	}

	if(num == 1)
	{
		fileOut.open(m_strCourPathName.c_str(),ios::out|ios::binary);
	}
	else
	{
		fileOut.open(m_strCourPathName.c_str(),ios::out|ios::app|ios::binary);
	}

	if(fileOut.fail())
	{
		cout <<endl<< "Can not open file:" << m_strCourPathName<<endl;
		return false;
	}

	pCourCur = pCourHead;

	while(pCourCur != NULL)
	{
		int nSize = sizeof(*pCourCur);
		fileOut.write((char*)pCourCur ,sizeof(*pCourCur));
		pCourCur = pCourCur->pNext;
	}

	fileOut.close();
	FreeCour();

	return true;
};

bool StuCourList::ToFileStu(int num)
{
	Students *pStuCur ;

	ofstream fileOut;

	if(pStuHead == NULL)
	{
		fileOut.open(m_strStuPathName.c_str(),ios::out|ios::binary);
		return true;
	}

	if(num == 1)
	{
		fileOut.open(m_strStuPathName.c_str(),ios::out|ios::binary);
	}
	else
	{
		fileOut.open(m_strStuPathName.c_str(),ios::out|ios::app|ios::binary);
	}

	if(fileOut.fail())
	{
		cout<<endl <<"Can not open file:" << m_strStuPathName << endl;
		return false;
	}

	pStuCur = pStuHead;
	while(pStuCur != NULL)
	{
		int nSize = sizeof(*pStuCur);
		fileOut.write((char*)pStuCur,sizeof(*pStuCur));
		pStuCur = pStuCur->pNext;
	}

	fileOut.close();

	FreeStu();

	return true;
};

void StuCourList::DisplayCour()
{
	Course *pCourCur;

	if(pCourHead == NULL)
	{
		return ;
	}

	pCourCur = pCourHead;

	cout <<endl
		<<setw(15)<< "课程编号:" 
		<<setw(15)<<"课程名称:"
		<<setw(15)<<"课程学时:"
		<<setw(15)<<"课程学分:"
		<<setw(15)<<"授课教师:"<<endl;

	do
	{
		pCourCur->Display();
		pCourCur = pCourCur->pNext;
	}while(pCourCur != NULL);
};

void StuCourList::DisplayStu()
{
	Students *pStuCur;

	if(pStuHead == NULL)
	{
		return ;
	}

	pStuCur = pStuHead;

	cout<<endl
		<< setw(15)<<"学生学号:"
		<<setw(15)<<"学生姓名:"
		<<setw(15)<<"学生专业:"
		<<setw(15)<<"学生班级:"
		<<setw(15)<<"课程编号:"<<endl;

	do
	{
		if(pStuCur->nCourNum == 0)
		{
			pStuCur->Display(0);
		}
		else
		{
			pStuCur->Display(1);
		}
		pStuCur = pStuCur->pNext; 
	}while(pStuCur != NULL);
};

void StuCourList::ChoiceByNum(int num) // int num 是输入的课程学分
{
	int nNum1 = 0; // 判断该课程学分是否录入
	int nNum2; // 学生学号
	int nNum3 = 0; // 判断该学生是否录入
	int nNum4; // 课程编号

	Course *pCourCur;
	Students *pStuCur;

	if(pCourHead == NULL)
		return;
	if(pStuHead == NULL)
		return ;

	pCourCur = pCourHead;
	while(pCourCur != NULL)
	{
		if(pCourCur->nCredit == num)
		{
			pCourCur->Display(1);
			nNum1++;
		}
		pCourCur = pCourCur->pNext;
	}
	if(nNum1 == 0)
	{
		cout <<endl<< "编号为"<<num<<"的课程未被录入 !" << endl;
		return ;
	}

	cout <<endl<<"输入课程的编号:" ;
	cin>>nNum4 ;

	cout <<endl<<"输入学生的学号:" ;
	cin >> nNum2;

	pStuCur = pStuHead;
	while(pStuCur != NULL)
	{
		if(pStuCur->nNum == nNum2)
		{
			pStuCur->SetnCourNum(nNum4);
			nNum3++;
			break;
		}
		pStuCur = pStuCur->pNext;
	}

	if(nNum3 == 0)
	{
		cout <<endl<<"学号为"<<nNum2<<"的学生未被录入 !"<<endl;
		return ;
	}
	else
	{
		cout <<endl<< "选课成功 !"<<endl <<endl;
	}
	
	ToFileStu(1);
};

void StuCourList::ChoiceByTeach(char chTeach[15])
{
	int nNum1 = 0; // 判断该课程是否录入
	int nNum2 ;  // 学生学号
	int nNum3 = 0; // 判断该学号的学生是否录入
	int nNum4 ; // 课程编号

	Course *pCourCur;
	Students *pStuCur;

	if(pCourHead == NULL)
		return ;
	if(pStuHead == NULL)
		return ;

	pCourCur = pCourHead;
	while(pCourCur != NULL)
	{
		if(strcmp( pCourCur->chTeacher,chTeach) == 0)
		{
			pCourCur->Display(1);
			nNum1++;
		}
		pCourCur = pCourCur->pNext;
	}
	if(nNum1 == 0)
	{
		cout<<endl<<"授课教师为"<<chTeach<<"的课程未被录入 !"<<endl;
		return ;
	}

	cout <<endl<< "输入课程的编号:" ;
	cin >> nNum4;

	cout <<endl<< "输入学生的编号:";
	cin >> nNum2;

	pStuCur = pStuHead;
	while(pStuCur != NULL)
	{
		if(pStuCur->nNum == nNum2)
		{
			pStuCur->SetnCourNum(nNum4);
			nNum3++;
		}
		pStuCur = pStuCur->pNext;
	}
	if(nNum3 == 0)
	{
		cout <<endl<< "学生学号为"<<nNum2<<"的学生未被录入 !"<<endl;
		return ;
	}
	else
	{
		cout << endl<< "选课成功 !"<< endl<<endl;
	}

	ToFileStu(1);
};

void StuCourList::DeleteCour(int num)
{
	int nDelete = 0;
	Course *pCourCur, *pCourPrev;

	if(pCourHead == NULL)
		return ;

	if(pCourHead->pNext == NULL)
	{
		if(pCourHead->nNum == num)
		{
			delete pCourHead;
			pCourHead = NULL;
			ToFileCour(1);
			cout <<endl<< "删除成功!"<<endl;
			return ;
		}
		else
		{
			cout <<endl<< "编号为"<<num<<"的课程未被录入!" <<endl;
			FreeCour();
			return ;
		}
	}

	pCourCur = pCourHead;
	if(pCourHead ->nNum == num)
	{
		pCourHead = pCourHead->pNext;
		delete pCourCur;
		ToFileCour(1);
		cout <<endl<< "删除成功!" << endl;
		return ;
	}
	
	pCourPrev = pCourHead;
	pCourCur = pCourHead->pNext;
	while(pCourCur != NULL)
	{
		if(pCourCur->nNum == num)
		{
			pCourPrev->pNext = pCourCur->pNext;
			delete pCourCur;
			nDelete++;
			break;
		}

		pCourPrev = pCourCur;
		pCourCur = pCourCur->pNext;
	}
	if(nDelete == 0)
	{
		cout <<endl<< "编号为"<<num<<"的课程未被录入 !"<< endl;
	}
	else
	{
		cout <<endl<< "删除成功 !" << endl;
	}

	ToFileCour(1);
};

void StuCourList::DeleteStu(int num)
{
	int nDe = 0;
	Students *pStuCur, *pStuPrev, *pStuTemp;

	if(pStuHead == NULL)
		return ;

	if(pStuHead->pNext == NULL)
	{
		if(pStuHead->nNum == num)
		{
			delete pStuHead;
			pStuHead = NULL;
			cout <<endl<< "删除成功!" << endl;
			ToFileStu(1);
			return;
		}
		else
		{
			cout <<endl<< "学号为" <<num<<"的学生未被录入!"<< endl;
			FreeStu();
			return;
		}
	}

	pStuCur = pStuHead;
	if(pStuHead ->nNum == num)
	{
		pStuHead = pStuHead->pNext;
		delete pStuCur;
		cout<<endl << "删除成功!" << endl;
		ToFileStu(1);
		return ;
	}
	

	pStuCur = pStuHead;
	pStuPrev = pStuHead->pNext;
	while(pStuCur != NULL)
	{
		if(pStuCur->nNum == num)
		{
			pStuPrev->pNext = pStuCur->pNext;
			delete pStuCur;
			nDe++;
			break;
		}
		pStuPrev = pStuCur;
		pStuCur = pStuCur->pNext;
	}

	if(nDe == 0)
	{
		cout <<endl<< "学号为" <<num << "的学生未被录入 !" << endl;
	}
	else
	{
		cout <<endl<< "删除成功 !" << endl;
	}

	ToFileStu(1);
};

void StuCourList::ModifyCour(int num)
{
	int nModify = 0;

	Course *pCourCur;
	Course *pCourNew;

	pCourNew  = new Course;
	pCourNew->pNext = NULL;

	if(pCourHead == NULL)
		return ;

	cout << endl;
	cout << "课程名称:";  cin >> pCourNew->chName;
	cout << "课程学时:";  cin >> pCourNew->nHour;
	cout << "课程学分:";  cin >> pCourNew->nCredit;
	cout << "授课老师:";  cin >> pCourNew->chTeacher;

	pCourCur = pCourHead;
	while(pCourCur != NULL)
	{
		if(pCourCur->nNum == num)
		{
			pCourCur->SetName(pCourNew->chName);
			pCourCur->SetHour(pCourNew->nHour);
			pCourCur->SetCredit(pCourNew->nCredit);
			pCourCur->SetTeacher(pCourNew->chTeacher);
			nModify++;
			delete pCourNew;
			break;
		}
		pCourCur = pCourCur->pNext;
	}
	if(nModify == 0)
	{
		cout <<endl<< "编号为" <<num<< "的课程未被录入 !" << endl;
	}
	else
	{
		cout <<endl<< "修改成功 !" << endl;
	}

	ToFileCour(1);
};

void StuCourList::ModifyStu(int num)
{
	int nModify = 0;
	Students *pStuCur;
	Students *pStuNew;
	pStuNew = new Students;
	pStuNew->pNext = NULL;

	if(pStuHead == NULL)
		return ;

	cout << endl;
	cout<<"学生姓名:";cin>>pStuNew->chName;
	cout<<"学生专业:";cin>>pStuNew->chProfession;
	cout<<"学生班级:";cin>>pStuNew->chClass;

	pStuCur = pStuHead;
	while(pStuCur != NULL)
	{
		if(pStuCur->nNum == num)
		{
			pStuCur->SetClass(pStuNew->chClass);
			pStuCur->SetName(pStuNew->chName);
			pStuCur->SetProfession(pStuNew->chProfession);
			nModify++;
			delete pStuNew;
			break;
		}

		pStuCur = pStuCur->pNext;
	}

	if(nModify == 0)
	{
		cout <<endl<< "学号为"<<num << "的学生未被录入 !" << endl;
	}
	else
	{
		cout <<endl<< "修改成功 !" << endl;
	}

	ToFileStu(1);
};
//-------------------私有函数-------------------//
void StuCourList::FreeCour()
{
	Course *pCourCur;

	if(pCourHead != NULL)
	{
		do
		{
			pCourCur = pCourHead;
			pCourHead = pCourHead->pNext;
			delete pCourCur;
		}while(pCourHead != NULL);
	}
	pCourHead = NULL;
};

void StuCourList::FreeStu()
{
	Students *pStuCur;

	if(pStuHead != NULL)
	{
		do
		{
			pStuCur = pStuHead;
			pStuHead = pStuHead->pNext;
			delete pStuCur;
		}while(pStuHead != NULL);
	}
	pStuHead = NULL;
};
// file: CppRMS.h

#include <string>
using namespace std;

class CppRMS
{
public:
	CppRMS(string strCourFileDir = "",string strCourFileName = "",
		string strStuFileDir = "" , string strStuFileName = "");
	
	string GetCourPathName()
	{return c_strCourFilePathName;};
	string GetStuPathName()
	{return c_strStuFilePathName;};

	void SetCourFileName(string strCourFileName);
	void SetCourFileDir(string strCourFileDir);

	void SetStuFileName(string strStuFileName);
	void SetStuFileDir(string strStuFileDir);

	int MenuCour();
	int MenuStu();
	int MenuMain();
	int MenuChoice();
private:
	void CreatCourFilePathName();
	void CreatStuFilePathName();
private:
	//
	string c_strCourFileName;   // 课程文件名
	string c_strCourFileDir;    // 课程文件目录
	string c_strCourFilePathName; // 课程文件路径
	//
	string c_strStuFileName;    // 学生文件名
	string c_strStuFileDir;     // 学生文件目录
	string c_strStuFilePathName; //  学生文件路径
};
// file: CppRMS.cpp

#include <iostream>
#include <string>
using namespace std;
#include "CppRMS.h"

//

CppRMS::CppRMS(string strCourFileDir,string strCourFileName,
			   string strStuFileDir ,string strStuFileName)
{
	c_strCourFileDir = strCourFileDir;
	c_strCourFileName = strCourFileName;
	CreatCourFilePathName();
	/
	c_strStuFileDir = strStuFileDir;
	c_strStuFileName = strStuFileName;
	CreatStuFilePathName();
};

int CppRMS::MenuCour()
{
	int nChoice ;

	do
	{
		cout << endl;
		cout << "☆☆☆ 课程信息管理 ☆☆☆" <<endl<< endl;
		cout << " 1 课程信息录入" << endl;
		cout << " 2 课程信息浏览" << endl;
		cout << " 3 课程信息修改" << endl;
		cout << " 4 课程信息删除" << endl;
		cout << " 0 退出" << endl<< endl;

		cout << "请选择: " ;
		cin >> nChoice ;

	}while(nChoice < 0 || nChoice > 4);

	return nChoice ;
}

int CppRMS::MenuStu()
{
	int nChoice ;

	do
	{
		cout << endl;
		cout << "☆☆☆ 学生信息管理 ☆☆☆" <<endl<< endl;
		cout << " 1 学生信息录入" << endl;
		cout << " 2 学生信息浏览" << endl;
		cout << " 3 学生信息修改" << endl;
		cout << " 4 学生信息删除" << endl;
		cout << " 0 退出" << endl<<endl;

		cout << "请选择: " ;
		cin >> nChoice ;
	}while(nChoice < 0 || nChoice > 4);

	return nChoice ;
}

int CppRMS::MenuMain()
{
	int nChoice ;

	do
	{
		cout << endl ;
		cout << "☆☆☆ 学生选课管理系统 ☆☆☆" <<endl<<endl;
		cout << " 1 课程信息管理" << endl;
		cout << " 2 学生信息管理" << endl;
		cout << " 3 学生选课" << endl;
		cout << " 0 退出 " << endl << endl;
		cout << " 请选择:" ;
		cin >> nChoice ;

	}while(nChoice <0 || nChoice > 3);

	return nChoice ;

};

int CppRMS::MenuChoice()
{
	int nChoice;

	do
	{
		cout << endl << "☆☆☆ 学生选课 ☆☆☆" << endl << endl;
		cout << " 1 按教师选择 " << endl;
		cout << " 2 按学分选择 " << endl;
		cout << " 0  退出 " << endl << endl;

		cout << " 请选择:" ;
		cin >> nChoice ;

	}while(nChoice < 0 || nChoice > 2);

	return nChoice ;
};

void CppRMS::SetCourFileDir(string strCourFileDir)
{
	c_strCourFileDir = strCourFileDir;
	CreatCourFilePathName();
};

void CppRMS::SetCourFileName(string strCourFileName)
{
	c_strCourFileName = strCourFileName;
	CreatCourFilePathName();
};

void CppRMS::SetStuFileDir(string strStuFileDir)
{
	c_strStuFileDir = strStuFileDir;
	CreatStuFilePathName();
};

void CppRMS::SetStuFileName(string strStuFileName)
{
	c_strStuFileName = strStuFileName;
	CreatStuFilePathName();
};

// -------------私有函数--------------

void CppRMS::CreatCourFilePathName()
{
	int nLen = c_strCourFileDir.length();
	int nPos = c_strCourFileDir.find_last_of('\\',nLen);

	if(nLen != nPos)
	{
		c_strCourFilePathName = c_strCourFileDir + string("\\")
			+ c_strCourFileName + string(".dat");
	}
	else
	{
		c_strCourFilePathName = c_strCourFileDir  + c_strCourFileName
			+ string(".dat");
	}
};

void CppRMS::CreatStuFilePathName()
{
	int nLen = c_strStuFileDir.length();
	int nPos = c_strStuFileDir.find_last_of('\\',nLen);

	if(nLen != nPos)
	{
		c_strStuFilePathName = c_strStuFileDir + string("\\") + c_strStuFileName + string(".dat");
	}
	else
	{
		c_strStuFilePathName = c_strStuFileDir + c_strStuFileName + string(".dat");
	}
};
// file: ChoiceCouseSystem.cpp

#include<iostream>
#include <string>
#include <iomanip>
using namespace std;
#include "StuCourList.h"
#include "FileDir.h"
#include "CppRMS.h"
const int FilePathLen = 1024;
const int FileNameLen = 32;

// -------------------主函数中的函数---------------------//
bool CourseMessage(string strCourPathName); // 课程管理信息
bool StudentsMessage(string strStuPathName);// 学生管理信息

bool CourseOfInput(string  strCourPathName);  //课程录入
bool CourseBrowser(string  strCourPathName);  // 课程浏览
bool CourseModify(string  strCourPathName);  //课程修改
bool CourseDelete(string  strCourPathName);  //课程删除

bool StudentsOfInput(string  strStuPathName);  // 学生录入
bool StudentsBrowser(string  strStuPathName); // 学生浏览
bool StudentsModify(string  strStuPathName);  //学生修改
bool StudentsDelete(string  strStuPathName);  //学生删除

bool Choice(string strStuPathName,string  strCourPathName);  // 选课


/
bool CourseOfInput(string strCourPathName) //课程录入
{
	char ch;
	StuCourList scList;
	scList.SetCourPathName(strCourPathName);
	FileDir fd(strCourPathName);

	bool bFileExist = fd.FileExist();
	if(bFileExist)
	{
		cout <<"文件"<<strCourPathName <<"已存在,是否增加数据(Y/N): ";
		cin >> ch;

		if(toupper(ch) != 'Y')
			return false;

		if(scList.FromFileCour())
			scList.InputCour();
	}
	else
	{
		scList.InputCour(1);
	}
	//scList.ToFileCour();
	return true;
}
bool CourseBrowser(string strCourPathName)  // 课程浏览
{
	StuCourList scList;
	scList.SetCourPathName(strCourPathName);
	FileDir fd(strCourPathName);

	bool bFileExist = fd.FileExist();
	if(!bFileExist)
	{
		cout << "文件不存在 :" << strCourPathName<<endl;
		return false;
	}

	if(scList.FromFileCour())
	{
		scList.DisplayCour();
	}
	else
	{
		return false;
	}

	return true;
}
bool CourseModify(string strCourPathName)  //课程修改
{
	int nModify; // 课程的编号
	StuCourList scList;
	scList.SetCourPathName(strCourPathName);
	FileDir fd(strCourPathName);

	bool bFileExist = fd.FileExist();
	if(!bFileExist)
	{
		cout << "文件不存在:" << strCourPathName<<endl;
		return false;
	}

	if(scList.FromFileCour())
	{
		cout <<"请输入要修改的课程编号:";
		cin >> nModify;

		scList.ModifyCour(nModify);
	}
	else
	{
		return false;
	}

	//scList.ToFileCour();
	return true;
}
bool CourseDelete(string strCourPathName)  //课程删除
{
	int nDelete;
	StuCourList scList;
	scList.SetCourPathName(strCourPathName);
	FileDir fd(strCourPathName);

	bool bFileExist = fd.FileExist();
	if(!bFileExist)
	{
		cout << "文件不存在:"<<strCourPathName<<endl;
		return false;
	}

	if(scList.FromFileCour())
	{
		cout <<"请输入要删除的课程编号:";
		cin >> nDelete;

		scList.DeleteCour(nDelete);
	}
	else
	{
		return false;
	}

	//scList.ToFileCour();

	return true;
}

bool StudentsOfInput(string strStuPathName)  // 学生录入
{
	char ch;
	StuCourList scList;
	scList.SetStuPathName( strStuPathName );
	FileDir fd(strStuPathName);

	bool bFileExist = fd.FileExist();
	if(bFileExist)
	{
		cout <<"文件"<<strStuPathName<<"已存在,是否增加数据(Y/N): ";
		cin >> ch;

		if(toupper(ch) != 'Y')
			return false;

		if(scList.FromFileStu())
		{
			scList.InputStu();
		}
	}
	else
	{
		scList.InputStu(1);
	}
	return true;
}

bool StudentsBrowser(string strStuPathName) // 学生浏览
{
	StuCourList scList;
	scList.SetStuPathName(strStuPathName);
	FileDir fd(strStuPathName);

	bool bFileExist = fd.FileExist();
	if(!bFileExist)
	{
		cout << "文件不存在:"<<strStuPathName<<endl;
		return false;
	}

	if(scList.FromFileStu())
	{
		scList.DisplayStu();
	}
	else
	{
		return false;
	}

	return true;
}
bool StudentsModify(string strStuPathName)  //学生修改
{
	int nMocify;
	StuCourList scList;
	scList.SetStuPathName(strStuPathName);
	FileDir fd(strStuPathName);
	
	bool bFileExist = fd.FileExist();
	if(!bFileExist)
	{
		cout <<"文件不存在:"<<strStuPathName<<endl;
		return false;
	}

	if(scList.FromFileStu())
	{
		cout << "请输入要修改的学生编号:";
		cin >>nMocify;

		scList.ModifyStu(nMocify);
	}
	else
	{
		return false;
	}

	return true;
}
bool StudentsDelete(string strStuPathName)  //学生删除
{
	int nDelete  = 0;
	StuCourList scList;
	scList.SetStuPathName(strStuPathName);
	FileDir fd(strStuPathName);

	bool bFileExist = fd.FileExist();
	if(!bFileExist)
	{
		cout <<"文件不存在:"<<strStuPathName<<endl;
		return false;
	}

	if(scList.FromFileStu())
	{
		cout <<"请输入要删除的学生编号:";
		cin >>nDelete;

		scList.DeleteStu(nDelete);
	}
	else
	{
		return false;
	}

	return false;
}

bool Choice(string strStuPathName,string strCourPathName)  // 选课
{
	int nChoice;
	int nCredit;
	char chTeacher[15];

	StuCourList scList;

	scList.SetCourPathName(strCourPathName);
	scList.SetStuPathName(strStuPathName);

	FileDir fdStu(strStuPathName);
	FileDir fdCour(strCourPathName);

	CppRMS cppRMS;

	bool bFileExistStu = fdStu.FileExist();
	bool bFileExistCour = fdCour.FileExist();
	if(!bFileExistCour || !bFileExistStu)
	{
		cout <<"文件不存在:"<<strCourPathName<<"或"<<strStuPathName<<endl;
		return false;
	}

	if(!scList.FromFileCour() || !scList.FromFileStu())
	{
		return false;
	}

	do
	{
		nChoice = cppRMS.MenuChoice();
		switch(nChoice)
		{
		case 0:
			break;
		case 1:
			cout << "请输入教师姓名:";
			cin.ignore();
			cin.getline(chTeacher,15);
			scList.ChoiceByTeach(chTeacher);
			//scList.ToFileStu();
			break;
		case 2:
			cout <<"请输入学生学分:";
			cin>>nCredit;
			scList.ChoiceByNum(nCredit);
			//scList.ToFileStu();
			break;
		default:
			cout <<endl<<"选择错误 !"<<endl;
			break;
		}
	}while(nChoice != 0);

	return true;
}

bool CourseMessage(string strCourPathName)
{
	int nChoice ;
	CppRMS cppRMS;

	do
	{
		nChoice = cppRMS.MenuCour();

		switch(nChoice)
		{
		case 0:
			break;
		case 1:
			CourseOfInput(strCourPathName);
			break;
		case 2:
			CourseBrowser(strCourPathName);
			break;
		case 3:
			CourseModify(strCourPathName);
			break;
		case 4:
			CourseDelete(strCourPathName);
			break;
		default:
			cout <<endl<< "输入错误!" <<endl;
			break;
		}
	}while(nChoice != 0);
	
	return true;
}

bool StudentsMessage(string strStuPathName)
{
	int nChoice ;
	CppRMS cppRMS;

	do
	{
		nChoice = cppRMS.MenuStu();

		switch(nChoice )
		{
		case 0:
			break;
		case 1:
			StudentsOfInput(strStuPathName);
			break;
		case 2:
			StudentsBrowser(strStuPathName);
			break;
		case 3:
			StudentsModify(strStuPathName);
			break;
		case 4:
			StudentsDelete(strStuPathName);
			break;
		default:
			cout <<endl<<"输入错误!"<<endl;
			break;
		}
	}while(nChoice != 0);

	return true;
}

int main()
{
	char chCourFileDir[FilePathLen];
	char chCourFileName[FileNameLen ];

	char chStuFileDir[FilePathLen ];
	char chStuFileName[FileNameLen ];

	cout <<"请输入课程文件名:";
	cin.getline(chCourFileName ,FileNameLen );
	cout <<"请输入课程文件目录:";
	cin.getline(chCourFileDir,FilePathLen ); 

	cout <<"请输入学生文件名:";
	cin.getline(chStuFileName ,FileNameLen );
	cout <<"请输入学生文件目录:";
	cin.getline(chStuFileDir ,FilePathLen);

	CppRMS cppRMS;
	string strCourPathName;
	string strStuPathName;

	cppRMS.SetCourFileName(string(chCourFileName));
	cppRMS.SetCourFileDir(string(chCourFileDir));
	cppRMS.SetStuFileName(string(chStuFileName));
	cppRMS.SetStuFileDir(string(chStuFileDir));

	strCourPathName = cppRMS.GetCourPathName();
	strStuPathName  = cppRMS.GetStuPathName();

	int nChoice = 0;

	do
	{
		nChoice = cppRMS.MenuMain();
		switch(nChoice)
		{
		case 0:
			break;
		case 1:
			CourseMessage(strCourPathName);
			break;
		case 2:
			StudentsMessage(strStuPathName);
			break;
		case 3:
			Choice(strStuPathName,strCourPathName);
			break;
		default:
			cout<<endl<<"输入错误 !"<<endl;
			break;
		}
	}while(nChoice != 0);

	return 0;
}

代码实现二:面向过程:

#include <iostream>
#include <iomanip>
#include <cstring>
#include <fstream>
#include <sys\stat.h>
using namespace std;
const string StusPathName="D:\\Students.txt";
const string ProjPathName="D:\\Project.txt";
bool FileExist(string PathName)
{
    int result;
    struct _stat buf;
    result = _stat(PathName.c_str(), &buf);
    return (result == 0);
}
struct Students//学生信息
{
    int  StusNo;//学号
    char StusNam[20];//姓名
    char Prof[20];//专业
    char ClaNam[20];//班级
    char SeledProjNam[20];//选课名
    Students *sNext;
};
bool FreeMem(Students *sHead)//链表释放内存
{
    Students *sCur;
    if (sHead == NULL)
        return false;
    do
    {
        sCur=sHead;
        sHead=sHead->sNext;
        delete sCur;
    }
    while(sHead!=NULL);
    return true;
}
Students* StusFromFile(string StusPathName)//从文件中读出数据
{
    Students *pRsNew,*sHead=NULL;
    ifstream StusfileIn;
    StusfileIn.open(StusPathName.c_str(),ios::in);
    if(StusfileIn.fail())
    {
        cout<<"Can not open file:"<<StusPathName<<endl;
        return sHead;
    }
    while(!StusfileIn.eof())
    {
        pRsNew=new Students;
        StusfileIn.read((char*)pRsNew,sizeof(*pRsNew));
        pRsNew->sNext=NULL;
        int ng=StusfileIn.gcount();//结尾处理
        if(ng==0)
        {
            delete pRsNew;
            break;
        }
        if (sHead==NULL)
            sHead=pRsNew;
        else
        {
            pRsNew->sNext=sHead;
            sHead=pRsNew;
        }
    }
    StusfileIn.close();
    return sHead;
}
bool StusToFile(string StusPathName,Students* sHead)//保存
{
    Students *sCur;
    ofstream StusfileOut;
    StusfileOut.open(StusPathName.c_str(),ios::out|ios::app|ios::binary);
    if(StusfileOut.fail())
    {
        cout<<"Can not open file:"<<StusPathName<<endl;
        return false;
    }
    sCur=sHead;
    while(sCur!= NULL)
    {
        StusfileOut.write((char*)sCur,sizeof(*sCur));
        sCur=sCur->sNext;//指向下一个
    };
    StusfileOut.close();
    FreeMem(sHead);
    return true;
}
//保存
bool StusToFileDele(string StusPathName,Students* sHead)
{
    Students *sCur;
    ofstream StusfileOut;
    StusfileOut.open(StusPathName.c_str(),ios::out);
    if(StusfileOut.fail())
    {
        cout<<"Cant not open file:"<<StusPathName<<endl;
        return false;
    }
    sCur=sHead;
    while(sCur!= NULL)
    {
        StusfileOut.write((char*)sCur,sizeof(*sCur));
        sCur=sCur->sNext;
    };
    StusfileOut.close();
    FreeMem(sHead);
    return true;
}
int MenuStus();//学生信息菜单
int MenuStusInquiry();//学生信息查询菜单
int MenuStusDelete();//学生信息删除菜单
bool StusInput();//录入
bool StusInquiry();//查询
void DispiayT();//浏览
bool Display(Students *sHead,int num);
bool InquiryAllStus(Students *sHead);
bool StusDelete();
bool DeleteByStusNo(Students *sHead);
int MenuMain()
{
    int nChoice=0;
    do
    {
        cout<<"学生选课系统"<<endl<<"1: 学生信息管理"<<endl<<"2: 课程信息管理"<<endl<<"3: 学生选课"<<endl
            <<"0: 退出"<<endl<<" 请选择:";
        cin>>nChoice;
    }
    while(nChoice<0 || nChoice>3);
    return nChoice;
}
int MenuStus()
{
    int nChoice=0;
    do
    {
        cout<<"学生信息管理"<<endl<<"1:录入学生信息"<<endl<<"2:浏览学生信息"<<endl<<"3:删除学生信息"<<endl
            <<"0:返回主菜单"<<endl<<"请选择:";
        cin>>nChoice;
    }
    while(nChoice<0 || nChoice>3);
    switch(nChoice)
    {
    case 0:
        break;
    case 1:
        StusInput();
        break;
    case 2:
        StusInquiry();
        break;
    case 3:
        StusDelete();
        break;
    }
    return 0;
}
int MenuStusInquiry()
{
    int nChoice=0;
    do
    {
        cout<<"浏览学生信息"<<endl<<"1:查看全部信息"<<endl<<"2: 返回上一级"<<endl<<"0:返回主菜单"<<endl<<" 请选择:";
        cin>>nChoice;
    }
    while(nChoice<0 || nChoice>2);
    return nChoice;
}
int MenuStusDelete()
{
    int nChoice=0;
    do
    {
        cout<<"删除学生信息"<<endl<<"1:通过学号删除"<<endl
            <<"2: 返回上一级"<<endl<<"0:返回主菜单"<<endl<<" 请选择:";
        cin>>nChoice;
    }
    while(nChoice<0 || nChoice>2);
    return nChoice;
}
bool StusInput()
{
    char chChoice;
    Students *sHead = NULL;
    Students *pRsNew,*pRsPrev;
    if (FileExist(StusPathName))
    {
        cout<<" 文件"<<StusPathName<<"已存在,是否继续(Y/N)?";
        cin>>chChoice;
        if (toupper(chChoice) == 'N')
            return  MenuStus();
    }
    cout<<" 请依次输入每位学生信息,学号输入0 结束录入"<<endl;
    do
    {
        pRsNew = new Students;
        pRsNew->sNext=NULL;
        strcpy(pRsNew->SeledProjNam,"无");
        cout<<"学号:";
        cin>>pRsNew->StusNo;
        if (pRsNew->StusNo==0)
        {
            delete pRsNew;
            break;
        }
        cout<<"姓名:";
        cin>>pRsNew->StusNam;
        cout<<"专业:";
        cin>>pRsNew->Prof;
        cout<<"班级:";
        cin>>pRsNew->ClaNam;

        if (sHead==NULL)
            sHead=pRsNew;
        else
        {
            pRsPrev=sHead;
            while(pRsPrev->sNext!=NULL)
                pRsPrev=pRsPrev->sNext;
            pRsPrev->sNext = pRsNew;
        }
    }
    while(true);
    bool bRet;
    bRet=StusToFile(StusPathName,sHead);
    if(bRet==true)
        cout<<" 存储成功"<<endl;
    else
        cout<<" 存储失败"<<endl;
    return MenuStus();
}
bool StusInquiry()
{
    int nChoice;
    Students* sHead=NULL;
    if(!FileExist(StusPathName))
    {
        cout<<" 无法找到存储学生信息文件,请先录入学生信息"<<endl;
        return  MenuStus();
    }
    sHead =StusFromFile(StusPathName);
    if(sHead==NULL)
    {
        cout<<"无学生信息,请录入学生信息"<<endl;
        return MenuStus();
    }
    do
    {
        nChoice=MenuStusInquiry();
        switch(nChoice)
        {
        case 0:
            return 0;
        case 1:
            InquiryAllStus(sHead);
            return  StusInquiry();
        case 2:
            return MenuStus();
        }
        break;
    }
    while(nChoice!=0);
    FreeMem(sHead);
    return MenuStus();;
}
void DispiayT()
{
    cout<<"   "<<"学号"<<setw(13)<<"姓名"<<setw(15)<<"专业"<<setw(13)
        <<"班级"<<setw(13)<<"已选课程"<<" "<<endl;
    return;
}
bool Display(Students *sHead,int num)
{
    Students *sCur;
    if(sHead==NULL)
        return false;
    sCur=sHead;
    if(sCur->SeledProjNam!=0)
    {
    cout<<"   "<<sCur->StusNo;
    cout<<setw(15)<<sCur->StusNam<<setw(15)<<sCur->Prof<<setw(13)<<sCur->ClaNam<<setw(13)
        <<sCur->SeledProjNam<<" "<<endl;
    }
    else
    {
    cout<<"   "<<sCur->StusNo;
    cout<<setw(15)<<sCur->StusNam<<setw(15)<<sCur->Prof<<setw(13)<<sCur->ClaNam<<endl;
    }
    return true;
}
bool InquiryAllStus(Students *sHead)
{
    int num=0;
    Students *sCur;
    sCur=sHead;
    DispiayT();
    while(sCur!=NULL)
    {
        num++;
        Display(sCur,num);
        sCur=sCur->sNext;
    }
    return true;
}
bool StusDelete()
{
    int nChoice;
    Students* sHead;
    if(!FileExist(StusPathName))
    {
        cout<<" 无存储学生信息的外部文件,请录入"<<endl;
        return MenuStus();
    }
    sHead = new Students;
    sHead->sNext=StusFromFile(StusPathName);
    if(sHead->sNext==NULL)
    {
        cout<<" 无没有学生信息,请录入"<<endl;
        return MenuStus();
    }
    do
    {
        nChoice=MenuStusDelete();
        switch(nChoice)
        {
        case 0:
            return 0;
        case 1:
            DeleteByStusNo(sHead);
           break;
        case 2:
            return MenuStus();
        }
        break;
    }
    while(nChoice!=0);
    bool bRet;
    bRet=StusToFileDele(StusPathName,sHead->sNext);
    if(bRet==true)
        cout<<" 删除完毕"<<endl;
    else
        cout<<" 删除失败"<<endl;
    return StusDelete();
}
Students* Delete(Students *sHead,Students *sCur)
{
    Students *p;
    p=sHead;
    while(p!=NULL)
    {
        if(p->sNext==sCur)
        {
            p->sNext=sCur->sNext;
            delete sCur;
            return p->sNext;
        }
        p=p->sNext;
    }
    return p;
}
bool DeleteByStusNo(Students *sHead)
{
    Students *sCur;
    int StusNo;
    int count=0;
    cout<<" 输入要删除学生学号:";
    cin>>StusNo;
    sCur=sHead->sNext;
    while(sCur!=NULL)
    {
        while (sCur->StusNo==StusNo)
        {
            count++;
            sCur=Delete(sHead,sCur);
            if(sCur==NULL)
                return true;
        }
        sCur=sCur->sNext;
    }
    if(count==0)
    {
        cout<<"无"<<StusNo<<"的学生信息";
    }
    return false;
}
struct Projects
{
    int ProjNo;//课程编号
    char ProjNam[16];//课程名
    int Hours;//学时
    float Score;//学分
    char TNam[10];//教师名
    Projects *pNext;
};
bool FreeMem(Projects *pHead)
{
    Projects *pCur;
    if (pHead == NULL)
        return false;
    do
    {
        pCur=pHead;
        pHead=pHead->pNext;
        delete pCur;
    }
    while(pHead!=NULL);
    return true;
}
Projects* ProjFromFile(string ProjPathName)
{
    Projects *pRsNew,*pHead=NULL;
    ifstream ProjfileIn;
    ProjfileIn.open(ProjPathName.c_str(),ios::in);
    if(ProjfileIn.fail())
    {
        cout<<"Can not open file:"<<ProjPathName<<endl;
        return pHead;
    }
    while(!ProjfileIn.eof())
    {
        pRsNew=new Projects;
        ProjfileIn.read((char*)pRsNew,sizeof(*pRsNew));
        pRsNew->pNext=NULL;
        int ng=ProjfileIn.gcount();
        if(ng==0)
        {
            delete pRsNew;
            break;
        }
        if(pHead==NULL)
            pHead=pRsNew;
        else
        {
            pRsNew->pNext = pHead;
            pHead = pRsNew;
        }
    }
    ProjfileIn.close();
    return pHead;
}
bool ProjToFile(string ProjPathName,Projects* pHead)
{
    Projects *pCur;
    ofstream ProjfileOut;
    ProjfileOut.open(ProjPathName.c_str(),ios::out|ios::app);
    if(ProjfileOut.fail())
    {
        cout<<"Can not open file:"<<ProjfileOut<<endl;
        return false;
    }
    pCur=pHead;
    while(pCur!= NULL)
    {
        ProjfileOut.write((char*)pCur,sizeof(*pCur));
        pCur=pCur->pNext;
    };
    ProjfileOut.close();
    FreeMem(pHead);
    return true;
}
bool ProjToFileDele(string ProjPathName,Projects* pHead)
{
    Projects *pCur;
    ofstream ProjfileOut;
    ProjfileOut.open(ProjPathName.c_str(),ios::out);
    if(ProjfileOut.fail())
    {
        cout<<"Can not open file:"<<ProjfileOut<<endl;
        return false;
    }
    pCur=pHead;
    while(pCur!= NULL)
    {
        ProjfileOut.write((char*)pCur,sizeof(*pCur));
        pCur=pCur->pNext;
    };
    ProjfileOut.close();
    FreeMem(pHead);
    return true;
}
int MenuProj();
int MenuProjInquiry();
int MenuProjDelete();
bool ProjInput();
bool ProjInquiry();
void DispiayP();
bool Display(Projects *pHead,int num);
bool InquiryAllProj(Projects *pHead);
bool ProjDelete();
bool DeleteByTNam(Projects *pHead);
int MenuProj()
{
    int nChoice=0;
    do
    {
        cout<<"课程信息管理"<<endl<<"1:录入课程信息"<<endl<<"2:浏览课程信息"<<endl<<"3:删除课程信息"
            <<endl<<"0:返回主菜单"<<endl<<" 请选择:";
        cin>>nChoice;
    }
    while(nChoice<0 || nChoice>3);
    switch(nChoice)
    {
    case 0:
        break;
    case
            1:
        ProjInput();
        break;
    case 2:
        ProjInquiry();
        break;
    case 3:
        ProjDelete();
        break;
    }
    return 0;
}
int MenuProjInquiry()
{
    int nChoice=0;
    do
    {
        cout<<" 浏览课程信息"<<endl<<"1:查看全部信息"<<endl
            <<"2: 返回上一级"<<endl<<"0:返回主菜单"<<endl<<"请选择:";
        cin>>nChoice;
    }
    while(nChoice<0 || nChoice>2);
    return nChoice;
}
int MenuProjDelete()
{
    int nChoice=0;
    do
    {
        cout<<"删除课程信息"<<endl<<"1:通过教师姓名删除"
            <<endl<<"2: 返回上一级"<<endl<<"0:返回主菜单"<<endl;
        cout<<" 请选择:";
        cin>>nChoice;
    }
    while(nChoice<0 || nChoice>2);
    return nChoice;
}
bool ProjInput()
{
    char chChoice;
    bool bFileExist;
    bFileExist = FileExist(ProjPathName);
    if (bFileExist)
    {
        cout<<" 文件"<<ProjPathName<<"已存在,是否继续(Y/N)?";
        cin>>chChoice;
        if (toupper(chChoice) == 'N')
            return MenuProj();
    }
    Projects *pHead = NULL;
    Projects *pRsNew,*pRsPrev;
    cout<<"录入课程信息,编号输入0 结束录入"<<endl;
    do
    {
        pRsNew = new Projects;
        pRsNew->pNext=NULL;
        cout<<"编号:";
        cin>>pRsNew->ProjNo;
        if (pRsNew->ProjNo==0)
        {
            delete pRsNew;
            break;
        }
        cout<<"课名:";
        cin>>pRsNew->ProjNam;
        cout<<"学时:";
        cin>>pRsNew->Hours;
        cout<<"学分:";
        cin>>pRsNew->Score;
        cout<<"授课老师:";
        cin>>pRsNew->TNam;
        if (pHead==NULL)
            pHead=pRsNew;
        else
        {
            pRsPrev=pHead;
            while(pRsPrev->pNext!=NULL)
                pRsPrev=pRsPrev->pNext;
            pRsPrev->pNext = pRsNew;
        }
    }
    while(true);
    bool bRet;
    bRet=ProjToFile(ProjPathName,pHead);
    if(bRet==true)
        cout<<"录入成功"<<endl;
    else
        cout<<"存储失败"<<endl;
    return MenuProj();
}
bool ProjInquiry()
{
    int nChoice;
    Projects* pHead=NULL;
    if(!FileExist(ProjPathName))
    {
        cout<<"找不到存储课程信息的文件,请录入"<<endl;
        return MenuProj();
    }
    pHead =ProjFromFile(ProjPathName);
    if(pHead==NULL)
    {
        cout<<"无课程信息,请录入"<<endl;
        return MenuProj();
    }
    do
    {
        nChoice=MenuProjInquiry();
        switch(nChoice)
        {
        case 0:
            break;
        case 1:
            InquiryAllProj(pHead);
            return ProjInquiry();
        case 2:
            return MenuProj();
        }
        break;
    }
    while(nChoice!=0);
    FreeMem(pHead);
    return 1;
}
void DispiayP()
{
    cout<<" "<<"编号"<<setw(13)<<"课名"<<setw(13)<<"学时"<<setw(13)<<"学分"
        <<setw(15)<<"授课老师"<<"  "<<endl;
    return;
}
bool Display(Projects *pHead,int num)
{
    Projects *pCur;
    if(pHead==NULL)
        return false;
    pCur=pHead;
    cout<<"  "<<pCur->ProjNo<<setw(13)<<pCur->ProjNam<<setw(13)<<pCur->Hours<<setw(13)
        <<pCur->Score<<setw(13)<<pCur->TNam<<"  "<<endl;
    return true;
}
bool InquiryAllProj(Projects *pHead)
{
    int num=0;
    Projects *pCur;
    pCur=pHead;
    DispiayP();
    while(pCur!=NULL)
    {
        num++;
        Display(pCur,num);
        pCur = pCur->pNext;
    }
    return true;
}
bool ProjDelete()
{
    int nChoice;
    Projects* pHead;
    if(!FileExist(ProjPathName))
    {
        cout<<" 无存储课程信息的外部文件,请先录入"<<endl;
        return MenuProj();
    }
    pHead = new Projects;
    pHead->pNext=ProjFromFile(ProjPathName);
    if(pHead->pNext==NULL)
    {
        cout<<"外部文件内无课程信息,请先录入"<<endl;
        return MenuProj();
    }
    do
    {
        nChoice=MenuProjDelete();
        switch(nChoice)
        {
        case 0:
            return 0;
        case 1:
            DeleteByTNam(pHead);
            break;
        case 2:
            return MenuProj();
        default:
            cout<<"选择错误"<<endl;
            break;
        }
        break;
    }
    while(nChoice!=0);
    bool bRet=false;
    bRet=ProjToFileDele(ProjPathName,pHead->pNext);
    if(bRet==true)
        cout<<"删除完毕"<<endl;
    else
        cout<<"删除失败"<<endl;
    return ProjDelete();
}
Projects* Delete(Projects *pHead,Projects *pCur)
{
    Projects *p;
    p=pHead;
    while(p!=NULL)
    {
        if(p->pNext==pCur)
        {
            p->pNext=pCur->pNext;
            delete pCur;
            return p->pNext;
        }
        p=p->pNext;
    }
    return p;
}
bool DeleteByTNam(Projects *pHead)
{
    Projects *pCur;
    char TNam[10];
    int count=0;
    cout<<"请输入要删除课程授课老师的姓名:";
    cin>>TNam;
    pCur=pHead->pNext;
    while(pCur!=NULL)
    {
        while ((strcmp(pCur->TNam,TNam)==0))
        {
            count++;
            pCur=Delete(pHead,pCur);
            if(pCur==NULL)
                return true;
        }
        pCur=pCur->pNext;
    }
    if(count==0)
    {
        cout<<"无授课老师为"<<TNam<<"的课程信息"<<endl;
    }
    return false;
}
int MenSeleProj()
{
    int nChoice=0;
    do
    {
        cout<<"学生选课"<<endl<<"1:按学分选课"<<endl
            <<"2:按老师选课"<<endl<<"0:返回主菜单"<<endl<<" 请选择:";
        cin>>nChoice;
    }
    while(nChoice<0 || nChoice>2);
    return nChoice;
}
Students *StudentNo(Students *sHead)
{
    Students *sCur;
    sCur=sHead;
    int count=0;
    int StusNo;
    cout<<"请输入学生学号:";
    cin>>StusNo;
    while(sCur!=NULL)
    {
        if(sCur->StusNo==StusNo)
        {
            count++;
            return sCur;
        }
        sCur=sCur->sNext;
    }
    if(count==0)
        cout<<"无"<<StusNo<<"的学生信息"<<endl;
    return sCur;
}
Projects *ProjectNo(Projects *pHead)
{
    Projects *pCur;
    pCur=pHead;
    int count=0;
    int ProjNo;
    cout<<"输入课程编号:";
    cin>>ProjNo;
    while(pCur!=NULL)
    {
        if (pCur->ProjNo==ProjNo)
        {
            count++;
            return pCur;
        }
        pCur=pCur->pNext;
    }
    if(count==0)
        cout<<"无编号为"<<ProjNo<<"的课程信息"<<endl;
    return pCur;
}
Projects *Score(Projects *pHead)
{
    Projects *pCur;
    pCur=pHead;
    int count=0;
    float Score;
    cout<<"输入学分:";
    cin>>Score;
    while(pCur!=NULL)
    {
        if (pCur->Score==Score)
        {
            count++;
            return pCur;
        }
        pCur=pCur->pNext;
    }
    if(count==0)
        cout<<"无学分为"<<Score<<" 的课程信息"<<endl;
    return pCur;
}
Projects *TNam(Projects *pHead)
{
    Projects *pCur;
    pCur=pHead;
    int count=0;
    char TNam[16];
    cout<<"输入老师姓名:";
    cin>>TNam;
    while(pCur!=NULL)
    {
        if ((strcmp(pCur->TNam,TNam)==0))
        {
            count++;
            return pCur;
        }
        pCur=pCur->pNext;
    }
    if(count==0)
        cout<<"无"<<TNam<<"老师的课程信息"<<endl;
    return pCur;
}
bool SeleProj()
{
    Students *sHead;
    Students *sCur;
    Projects *pHead;
    Projects *pCur;
    if(!FileExist(ProjPathName))
    {
        cout<<"无法找到储存课程信息的文件,请录入"<<endl;
        return false;
    }
    if(!FileExist(StusPathName))
    {
        cout<<"无法找到储存课程信息的文件,请录入"<<endl;
        return false;
    }
    sHead=StusFromFile(StusPathName);
    if(sHead==NULL)
    {
        cout<<"无学生信息,请先录入"<<endl;
        return false;
    }
    pHead=ProjFromFile(ProjPathName);
    if(pHead==NULL)
    {
        cout<<"无课程信息,请先录入"<<endl;
        return false;
    }
    int nChoice=MenSeleProj();
    switch(nChoice)
    {
    case 0:
        break;
    case 1:
        sCur=StudentNo(sHead);
        if(sCur==NULL)
            return false;
        pCur=Score(pHead);
        if(pCur==NULL)
        {
            cout<<"选课失败"<<endl;
            return SeleProj();
        }
        strcpy(sCur->SeledProjNam,pCur->ProjNam);
        StusToFileDele(StusPathName,sHead);
        cout<<"选课成功"<<endl;
        FreeMem(pHead);
        return SeleProj();
    case 2:
        sCur=StudentNo(sHead);
        if(sCur==NULL)
            return false;
        pCur=TNam(pHead);
        if(pCur==NULL)
        {
            cout<<"选课失败"<<endl;
            return SeleProj();
        }
        strcpy(sCur->SeledProjNam,pCur->ProjNam);
        StusToFileDele(StusPathName,sHead);
        cout<<endl<<"选课成功"<<endl;
        FreeMem(pHead);
        return SeleProj();
    default:
        cout<<"错误选择"<<endl;
        break;
    }

    return true;
}
int main()
{
    int nChoice=0;
    do
    {
        nChoice = MenuMain();
        switch(nChoice)
        {
        case 0:
            break;
        case 1:
            MenuStus();
            break;
        case 2:
            MenuProj();
            break;
        case 3:
            SeleProj();
            break;
        }
    }
    while(nChoice!=0);
    return 0;
}





版权声明:本文为u010368749原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。