⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 这是一个学生选修课系统开发的程序!!很好用的啊!
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
#include <conio.h>
using namespace std;
class course
{
public:
	course(){previous=next=NULL;score=0;selectedStu=0;maxstu=0;}
	~course(){}
	void set();
	bool isSelected(course *first) const;
	friend class admin;
	friend class student;
private:
	string name,number,property;
	double credit,score;
	int selectedStu,maxstu;
	course *previous,*next;
};
class student
{
public:
    student(){previous=next=NULL;first=last=NULL;}
	~student(){}
	void set();
	void getCourse() const;
	void selectCourse(course *&cfirst,string num);
	void deleteCourse(course *&cfirst,string num);
	course* searchCourse(course *&cfirst,string num);
	void saveCourseInfoForStu();
	void readCourseInfoTostu();
	string getPassword(){return password;}
	void setPassword();
	friend class admin;
private:
	string name,number,Class,grade,password,department;
	double totalCredit;
	student *previous,*next;
	course *first,*last;
};
class admin
{
public:
	void addStudent(student *&sfirst,student *&slast);
	void getStudent(student *&sfirst,string num);
	void setStudent(student *&sfirst,string num);
	student* searchStudent(student *&sfirst,string num);
	void deleteStudent(student *&sfirst,student *&slast,string num);
	void addCourse(course *&cfirst,course *&clast);
	void getCourse(course *&cfirst,string num);
	course* searchCourse(course *&cfirst,string num);
	void setCourse(course *&cfirst,string num);
	void deleteCourse(course *&cfirst,course *&clast,string num);
	void saveStuInfo(student *&sfirst);
	void readStuInfo(student *&sfirst,student *&slast);
	void readCourseInfo(course *&cfirst,course *&clast);
	void saveCourseInfo(course *&cfirst);
	void printStudent(student *&sfirst);
	void printCourse(course *&cfirst);
	void releaseStudent(student *&target);
	void releaseCourse(course *&target);
};
void course::set()
{
	cout<<"请输入课程名:";
	cin>>name;
	cout<<"请输入课程号:";
	cin>>number;
	cout<<"请输入学分:";
	cin>>credit;
	cout<<"请输入课程性质(必修/选修):";
	cin>>property;
	cout<<"请输入最高选课人数:";
	cin>>maxstu;
}
bool course::isSelected(course *first) const
{
	course *pos=first;
	if(pos==NULL)
		return true;
	else
	{
		while(pos)
		{
			if(pos->number==number)
				return true;
			else
				pos=pos->next;
		}
		return false;
	}
}
void student::set()
{
	cout<<"请输入姓名:";
	cin>>name;
	cout<<"请输入学号:";
	cin>>number;
	cout<<"请输入专业:";
	cin>>department;
	cout<<"请输入年级:";
	cin>>grade;
	cout<<"请输入班级:";
	cin>>Class;
	password=number;
	totalCredit=0;
}
void student::getCourse() const
{
	course *pos=first;
	if(pos==NULL)
		cout<<"无课程信息!\n";
	else
	{
		cout.flags(ios::left);
		cout<<setw(20)<<"课程名"<<setw(10)<<"课程号"<<setw(4)<<"学分"<<setw(6)<<"成绩"<<setw(10)<<"课程性质"<<endl;
		while(pos)
		{
			cout<<setw(20)<<pos->name<<setw(10)<<pos->number<<setw(4)<<pos->credit<<setw(6)<<pos->score<<setw(10)<<pos->property<<endl;
			pos=pos->next;
		}
		cout<<"\n已选学分为: "<<totalCredit<<"学分\n";
	}
}
void student::setPassword()
{
	system("cls");
	int i=0,j;
	char ps[20],ch;
	cout<<"输入新密码:";
    while(true)
	{
	    ch=getch();
        if(isdigit(ch)||isalpha(ch))
		{
			ps[i]=ch;
			i++;
			cout<<"*";
		}
		else if(ch==13)
			break;
	}
    ps[i]='\0';
	j=i;
	for(i=0;i<j;i++)
		password[i]=ps[i];
}
course* student::searchCourse(course *&cfirst,string num)
{
	course *pos=cfirst;
	if(cfirst==NULL)
		cout<<"无课程数据!";
	else
	{
		while(pos)
		{
			if(pos->number==num)
				break;
			else
				pos=pos->next;
		}
	}
	return pos;
}
void student::selectCourse(course *&cfirst,string num)
{
	course *pos=searchCourse(cfirst,num);
	if(pos==NULL)
	    cout<<"无此课程!\n";
	else
	{
		if(pos->isSelected(first))
			cout<<"操作失败:"<<pos->name<<" 已在课程表中!\n";
	    else
		{
			totalCredit+=pos->credit;
			pos->selectedStu++;	
			if(totalCredit>20)
			{
				cout<<"操作失败:所选学分已超过20学分!\n";
				totalCredit-=pos->credit;
			}
			else if(pos->selectedStu>pos->maxstu)
			{
				cout<<"操作失败:选课人数超过 "<<pos->name<<"课程容量("<<pos->maxstu<<"人)\n";
				pos->selectedStu--;
			}
			else
			{
		        course *cou=new course();
		        cou->name=pos->name;
		        cou->number=pos->number;
		        cou->credit=pos->credit;
		        cou->score=pos->score;
				cou->property=pos->property;
		        cou->previous=cou->next=NULL;
		        if(first==NULL)
		            first=last=cou;
	            else
				{
		            cou->previous=last;
		            last->next=cou;
		            last=cou;
				}
	            cout<<cou->name<<"  添加成功!\n";
			}
		}
	}
}
void student::deleteCourse(course *&cfirst,string num)
{
	course *pos=searchCourse(first,num);
	if(pos==NULL)
	    cout<<"无此课程!\n";
	else
	{
		course *cpos=searchCourse(cfirst,num);
		course *before=pos->previous;
		course *after=pos->next;
		if(pos==first)
			first=after;
		else
			before->next=after;
		if(pos==last)
			last=before;
		else
			after->previous=before;
		totalCredit-=pos->credit;
		cout<<pos->name<<" 删除成功!\n";
		cpos->selectedStu--;
		delete pos;
	}
}
void student::readCourseInfoTostu()
{
	string fileName="studentcourse\\"+number+".txt";
	ifstream fin(fileName.c_str());
	if(fin.fail());
	else
	{
		do
		{
			course *cou=new course();
			if(!fin.eof())
				fin>>cou->name>>cou->number>>cou->credit>>cou->score>>cou->property;
			else
				break;
			if(first==NULL)
				first=last=cou;
			else
			{
				last->next=cou;
				cou->previous=last;
				last=cou;
			}
		}while(!fin.eof());
	}
}
void student::saveCourseInfoForStu()
{
	string fileName="studentcourse\\"+number+".txt";
	course *pos=first;
	if(first==NULL)
		cerr<<"无课程信息,不能保存!"<<endl;
	else
	{
		ofstream fout(fileName.c_str());
		while(pos)
		{
	        fout<<pos->name<<" "<<pos->number<<" "<<pos->credit<<" "<<pos->score<<" "<<pos->property;
			pos=pos->next;
			if(pos)
                fout<<endl;
		}
		fout.close();
	}
}
void admin::addStudent(student *&sfirst,student *&slast)
{
	student *stu=new student();
	stu->set();
	if(sfirst==NULL)
	    sfirst=slast=stu;
	else
	{
		slast->next=stu;
		stu->previous=slast;
		slast=stu;
	}
}
student* admin::searchStudent(student *&sfirst,string num)
{
	student *pos=sfirst;
	if(sfirst==NULL)
		cout<<"无学生数据!";
	else
	{
		while(pos)
		{
			if(pos->number==num)
				break;
			else
				pos=pos->next;
		}
	}
	return pos;
}
void admin::getStudent(student *&sfirst,string num)
{
	student *pos=searchStudent(sfirst,num);
    if(pos==NULL)
		cout<<"无此学生信息\n";
	else
	{
		cout.flags(ios::left);
		cout<<setw(10)<<"学号"<<setw(6)<<"姓名"<<setw(10)<<"专业"<<setw(6)<<"年级"<<setw(5)<<"班级"<<setw(5
			)<<"学分"<<endl;
        cout<<setw(10)<<pos->number<<setw(6)<<pos->name<<setw(10)<<pos->department<<setw(6)<<pos->grade<<setw(5)<<pos->Class<<setw(5)<<pos->totalCredit<<"\n";
		cout<<"\n所选课程:\n\n";
		pos->getCourse();
	}
}
void admin::setStudent(student *&sfirst,string num)
{
	student *pos=searchStudent(sfirst,num);
    if(pos==NULL)
		cout<<"无学生信息\n";
    else
		pos->set();
}
void admin::deleteStudent(student *&sfirst,student *&slast,string num)
{
	student *pos=searchStudent(sfirst,num);
	if(pos==NULL)
	    cout<<"无此人!\n";
	else
	{
		student *before=pos->previous;
		student *after=pos->next;
		if(pos==sfirst)
			sfirst=after;
		else
			before->next=after;
		if(pos==slast)
			slast=before;
		else
			after->previous=before;
		delete pos;
		cout<<"删除成功!\n";
	}
}
void admin::printStudent(student *&sfirst)
{
	student *pos=sfirst;
	if(pos==NULL)
		cout<<"无学生数据";
	else
	{
		cout.flags(ios::left);
		while(pos)
		{
			cout<<"个人信息:\n";
		    cout<<setw(10)<<"学号"<<setw(6)<<"姓名"<<setw(10)<<"专业"<<setw(6)<<"年级"<<setw(5)<<"班级"<<setw(5)<<"学分"<<endl;
            cout<<setw(10)<<pos->number<<setw(6)<<pos->name<<setw(10)<<pos->department<<setw(6)<<pos->grade<<setw(5)<<pos->Class<<setw(5)<<pos->totalCredit<<"\n";
			cout<<"已选课程信息:\n";
			pos->getCourse();
			cout<<endl;
			pos=pos->next;
		}
	}
}
void admin::addCourse(course *&cfirst,course *&clast)
{
	course *cou=new course();
	cou->set();
	if(cfirst==NULL)
		cfirst=clast=cou;
	else
	{
		cou->previous=clast;
		clast->next=cou;
		clast=cou;
	}
}
course* admin::searchCourse(course *&cfirst,string num)
{
	course *pos=cfirst;
	if(cfirst==NULL)
		cout<<"无课程数据!";
	else
	{
		while(pos)
		{
			if(pos->number==num)
				break;
			else
				pos=pos->next;
		}
	}
	return pos;
}
void admin::getCourse(course *&cfirst,string num)
{
	course *pos=searchCourse(cfirst,num);
    if(pos==NULL)
		cout<<"无课程信息\n";
	else
	{
		cout.flags(ios::left);
		cout<<setw(20)<<"课程名"<<setw(10)<<"课程号"<<setw(5)<<"学分"<<setw(10)<<"选课人数"<<setw(10)<<"人数上限"<<setw(10)<<"课程性质"<<endl;
        cout<<setw(20)<<pos->name<<setw(10)<<pos->number<<setw(5)<<pos->credit<<setw(10)<<pos->selectedStu<<setw(10)<<pos->maxstu<<setw(10)<<pos->property<<"\n";
	}
}
void admin::setCourse(course *&cfirst,string num)
{
	course *pos=searchCourse(cfirst,num);
    if(pos==NULL)
		cout<<"无课程信息\n";
    else
		pos->set();
}
void admin::deleteCourse(course *&cfirst,course *&clast,string num)
{
	course *pos=searchCourse(cfirst,num);
	if(pos==NULL)
	    cout<<"无此课程!\n";
	else
	{
		course *before=pos->previous;
		course *after=pos->next;
		if(pos==cfirst)
			cfirst=after;
		else
			before->next=after;
		if(pos==clast)
			clast=before;
		else
			after->previous=before;
		delete pos;
		cout<<"删除成功!\n";
	}
}
void admin::printCourse(course *&cfirst)
{
	course *pos=cfirst;
	if(pos==NULL)
		cout<<"无学生数据";
	else
	{
		cout.flags(ios::left);
		cout<<setw(20)<<"课程名"<<setw(10)<<"课程号"<<setw(6)<<"学分"<<setw(10)<<"选课人数"<<setw(10)<<"人数上限"<<setw(10)<<"课程性质"<<endl;
		while(pos)
		{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -