C++实现【简易选课系统】

//typed by zbwer 2021.12.31:14:30
#include<bits/stdc++.h>
using namespace std;

//声明函数部分
void BuildMainMenu();//声明主菜单函数
void SelectSytem(int AID,int who);//声明选课系统
void MyCourse(int AID,int who);//声明我的课表
void PrintC(int who);//声明打印课表函数
void WrongW();//错误提示函数

/*
前置数据处理:
现有课程数据 course.txt
现有学生数据 students.txt
现有老师数据 teacher.txt
按顺序合并为date.txt.

初始状态为:
1.无任何课程->导入现有课程数据 (具体内容见Course类)
2.无任何老师数据->导入老师数据 (具体内容见teacher类)
3.此时老师并没有设置任何课程
	->需要老师登陆系统进行设置
	该步骤需要手动键入数据,参考数据见Setcourse.txt
4.无任何学生数据->导入学生数据 (具体内容见student类)
5.暂且令助教来自于学生,则助教的数据和学生数据一致
6.对于学生和助教,需通过学号和所设密码进入选课系统
*/

//用于打印边框
void PrintEgde(){puts("**********************************************");}

//课程类
class Course
{
public:
	int CourseId;//编号
	string cousreName;//名称
	int credit;//学分
	bool HaveSet=0;//判断该门课是否被老师设置,没有设置(0)则不能被学生选择

	Course(){}//默认构造函数,要记得写
	//自定义的含参构造函数
	void SetCourse(int a,string b,int c){CourseId=a;cousreName=b;credit=c;}
};
//AllList 存储所有课程的信息
Course AllList[200];
int C_totalnum;//代表课程的总数

class Person
{
public:
	string name;//姓名
	int age;//年龄
	//构造
	Person(){}
	void SetPerson(string a,int b){name=a;age=b;}
};

class teacher:public Person
{
public:
	int teacherId;//老师的编号
	int Len;//需要教授的课程数量
	int TeachCourse[10];//所授课程ID编号
	int now_have=0;//已设置课程数量
	teacher(){}//默认构造函数

	void Set_Teacher(string Name,int Age,int ID,int n)//构造
	{
		name=Name;
		age=Age;
		teacherId=ID;
		Len=n;
	}

	void teach(){cout<<"I am a teacher,I teach my student\n";}

	//删除已经设置的课程
	void DeleteC(int CID)
	{
		system("cls");//用于清空cmd窗口

		//如果当前没有课程可以删除就直接返回
		if(now_have==0) 
		{
			puts("You have set no course!");
			_sleep(500);//用于暂停0.5s
			return;
		}

		//CAN用于判断是否能取消设置某一门课(即要取消的课是否已被设置)
		int CAN=0;
		for(int i=1;i<=now_have;i++)
		{
			if(TeachCourse[i]==CID)
			{
				//可以取消,那么把原本设置好的最后一门课放到要取消的课的位置上
				TeachCourse[i]=TeachCourse[now_have];
				now_have--;
				CAN=1;
				break;
			}
		}
		if(CAN==1) puts("You successfully deleted the course!");
		else puts("There is no such course!");//不能取消设置的情况
		_sleep(800);
	}

	void PrintNowteach()//输出已设置课程
	{
		system("cls");
		puts("             <M y C o u r s e>              \n");
		PrintEgde();

		//如果没有课
		if(now_have==0) puts("You have no course now!");
		//如果有课
		for(int i=1;i<=now_have;i++)
		{
			int x=TeachCourse[i];//取出课程编号方便下一行书写
			printf("*%5d    %-29s %4d *\n",AllList[x].CourseId,AllList[x].cousreName.c_str(),AllList[x].credit);
		}
		PrintEgde();putchar('\n');
		printf({"You can input The CourseID to delete:(or -1 to return)"});

		int flag=0;scanf("%d",&flag);
		if(flag==-1) return;//返回上一级系统
		else if(flag>0 && flag<=C_totalnum)//如果输入的数字在限定范围之内
		{
			DeleteC(flag);//取消设置
			PrintNowteach();//并重置该页面
		}
		else//如果输入的数字不在预设范围内,进行报错提示
		{
			WrongW();
			PrintNowteach();
		}
	}
	void setCourse(int CourseID)
	//在已有课程中选择属于自己Teach的课程编号
	{
		system("cls");
		//如果已经选满了
		if(Len-now_have<=0) 
		{
			puts("You have already set all of your courses!");
			_sleep(800);return;
		}
		if(AllList[CourseID].HaveSet!=0)//如果已经被别人设置了
			puts("This course has been set!");
		else
		{
			puts("You successfully set the course!");
			TeachCourse[++now_have]=CourseID;
			AllList[CourseID].HaveSet=1;
		}
		_sleep(800);
	}
};
teacher A_T[200];//所有老师的信息
int T_totalnum;

class student:public Person
{
public:
	long long number;//学号
	int courseCount=0;//已选课程数目
	int mycourse[20];//已选课程的课程编号
	int LeastNum;//至少选择课程数目
	string key;//密码

	student(){}
	//此处age表示入学年份,如2021,2020
	void Set_student(string Name,int Age,long long Num,int n,string Key)
	{
		name=Name;
		age=Age;
		number=Num;//学号
		LeastNum=n;
		key=Key;
		memset(mycourse,0,sizeof(mycourse));//初始化已选课程数组
	}

	void selectCourse(int CourseID)
	{
		system("cls");
		//用于判断自己是否已经选过这门课程
		bool HaveChoose = 0;
		for(int i=1;i<=courseCount;i++)
			if(CourseID==mycourse[i]) 
				HaveChoose=1;

		//如果该门课已经被老师设置并且自己没有选过
		if(AllList[CourseID].HaveSet && HaveChoose==0)
		{
			puts("You successfully stlect the course!");
			mycourse[++courseCount]=CourseID;
		}
		//老师没有设置课程
		else if(AllList[CourseID].HaveSet==0) puts("There is no such course!");
		//自己已经选过
		else if(HaveChoose==1) puts("This course you have chosen!");
		_sleep(800);
	}

	void Delete(int CID)
	{
		system("cls");
		if(courseCount==0) return;
		int CAN;
		for(int i=1;i<=courseCount;i++)
		{
			if(mycourse[i]==CID)
			{
				mycourse[i]=mycourse[courseCount];
				courseCount--;
				CAN=1;
				break;
			}
		}
		if(CAN==1) puts("You successfully deleted the course!");
		else puts("There is no such course!");
		_sleep(800);
	}

	//判断是否满足学校要求
	void judge()
	{
		if(courseCount>=LeastNum) //比较已选课程和要求课程数量
			puts("You can complete the credits of this semester");
		else 
			printf("You need to choose %d more courses\n",LeastNum-courseCount);
	}
};
student A_S[2000];//所有学生的信息
int S_totalnum;//学生的总数

class TeachingAssistant:public teacher,public student
{
public:
	void teach(){puts("I am a teaching assistant,I help my teacher teach his students");}
	TeachingAssistant(){}

	void selectCourse(int CourseID)
	{
		system("cls");
		if(AllList[CourseID].HaveSet)
		{
			puts("You successfully stlect the course!");
			mycourse[++courseCount]=CourseID;
		}
		else puts("There is no such course!");
		_sleep(800);
	}

	void Delete(int CID)
	{
		system("cls");
		if(courseCount==0) return;
		int CAN;
		for(int i=1;i<=courseCount;i++)
		{
			if(mycourse[i]==CID)
			{
				mycourse[i]=mycourse[courseCount];
				courseCount--;
				CAN=1;
				break;
			}
		}
		if(CAN==1) puts("You successfully deleted the course!");
		else puts("There is no such course!");
		_sleep(800);
	}
};
TeachingAssistant A_TA[2500];

void Pre_course()
{
	//导入所有课程数据
	int a,b;string c;
	freopen("date.txt","r",stdin);
	scanf("%d",&C_totalnum);
	for(int i=1;i<=C_totalnum;i++)
	{
		cin>>a>>c>>b;
		//输入编号,名称,学分
		AllList[i].SetCourse(a,c,b);
	}
}

void Pre_teacher()
{
	//导入所有老师数据
	int a,b,c;string d;
	scanf("%d",&T_totalnum);
	for(int i=1;i<=T_totalnum;i++)
	{
		cin>>d>>a>>b>>c;
		//输入姓名,年龄,编号,应设置课程数量
		A_T[i].Set_Teacher(d,a,b,c);
	}
}

void Pre_student()
{
	//导入所有学生数据
	int a;long long b;string d,e;
	scanf("%d",&S_totalnum);
	for(int i=1;i<=S_totalnum;i++)
	{
		//姓名 入学年份 学号 至少选课数统一为2
		cin>>d>>a>>b>>e;
		A_S[i].Set_student(d,a,b,2,e);
		A_TA[i].Set_student(d,a,b,2,e);
	}
}

void Pre()
//选课系统前置准备工作
{
	Pre_course();//导入课程数据
	Pre_teacher();//导入老师数据
	Pre_student();//导入学生数据
}

void WrongW()//报错提示
{
	system("cls");
	puts("You entered the wrong one!");
	_sleep(500);
}

void MyCourse(int AID,int who)
{
	system("cls");
	puts("             <M y C o u r s e>              \n");
	PrintEgde();
	if(who==0)//学生
	{
		//没课的情况
		if(A_S[AID].courseCount==0) puts("You have no course now!");
		//有课的情况
		for(int i=1;i<=A_S[AID].courseCount;i++)
		{
			int x=A_S[AID].mycourse[i];
			printf("*%5d    %-29s %4d *\n",AllList[x].CourseId,AllList[x].cousreName.c_str(),AllList[x].credit);
		}
		PrintEgde();putchar('\n');
		A_S[AID].judge();
	}
	else
	{
		if(A_TA[AID].courseCount==0) puts("You have no course now!");
		for(int i=1;i<=A_TA[AID].courseCount;i++)
		{
			int x=A_TA[AID].mycourse[i];
			printf("*%5d    %-29s %4d *\n",AllList[x].CourseId,AllList[x].cousreName.c_str(),AllList[x].credit);
		}
		PrintEgde();putchar('\n');
	}
	//是否进行退课操作
	printf({"You can input The CourseID to delete:(or -1 to return)"});
	int flag=0;scanf("%d",&flag);
	if(flag==-1) SelectSytem(AID,who);
	else if(flag>0 && flag<=C_totalnum)
	{
		if(who==0) A_S[AID].Delete(flag);
		else A_TA[AID].Delete(flag);
		MyCourse(AID,who);
	}
	else {WrongW();MyCourse(AID,who);}
}

void PrintC(int who)
//打印所有课程信息
{
	puts("           <Course Information>             \n");
	PrintEgde();
	puts("*Course Id       Name               Credit   *");
    PrintEgde();
	if(who==1)//老师和助教
    	for(int i=1;i<=C_totalnum;i++)
			printf("*%5d    %-29s %4d *\n",AllList[i].CourseId,AllList[i].cousreName.c_str(),AllList[i].credit);
	else//学生
	    for(int i=1;i<=C_totalnum;i++)
			if(AllList[i].HaveSet)
				printf("*%5d    %-29s %4d *\n",AllList[i].CourseId,AllList[i].cousreName.c_str(),AllList[i].credit);
	PrintEgde();putchar('\n');
}

void SelectSytem(int AID,int who)
//who 0 代表学生 1 代表助教
{
	system("cls");
	//打印所有课程信息
	PrintC(0);
	//输出学生姓名 .c_str()的作用是将string类型的数据转成char类型,使得它可以用printf输出
	printf("Student: %s .",A_S[AID].name.c_str());
	if(who==0) 
		A_S[AID].judge();//学生
	printf("Please enter the Course Id to select\nYou can input -1 to exit\nYou can input -2 to check Mycourse and delete course\nNow enter a number:");
	int flag=0;scanf("%d",&flag);
	if(flag==-1) BuildMainMenu();//返回上一级菜单
	else if(flag==-2) MyCourse(AID,who);//查看已选课表
	else if(flag>0 && flag<=C_totalnum)//数据在预设范围内,则选课
	{
		if(who==0)//学生
			A_S[AID].selectCourse(flag),SelectSytem(AID,who);
		else A_TA[AID].selectCourse(flag),SelectSytem(AID,who);//助教
	}
	else {WrongW();SelectSytem(AID,who);}//报错
}

void StudentSystem(int who)
{
	system("cls");
	//切换到从控制台输入数据
	freopen("CON","r",stdin); 
	//接下来为登陆页面

	puts("Please enter your student number and  password\n");
	PrintEgde();putchar('\n');
	printf("Student number:");long long SN;//输入学号
	scanf("%lld",&SN);
	printf("Password:      ");
	char Pas[20],Acs[20];scanf("%s",Pas);//输入密码
	int AID;//在数据库中的序号
	//在数据库中找到学号对应的正确密码
	for(int i=1;i<=S_totalnum;i++)
		if(A_S[i].number==SN){strcpy(Acs,A_S[i].key.c_str());AID=i;break;}
	int times=2;//输入密码的机会
	while(strcmp(Acs,Pas)!=0 && times>0)
	//看输入的密码与正确密码是否匹配,以及次数是否耗尽
	{
		puts("Wrong Password!!!");
		printf("you have %d times to enter the correct password\n",times);
		times--;scanf("%s",Pas);
	}
	//次数耗尽推出系统
	if(times==0)
	{
		puts("I am sorry you can't enter our system!");
		_sleep(800);exit(0);
	}
	if(who==0) SelectSytem(AID,who);//学生
	else SelectSytem(AID,1);//助教
}

//老师设置课程
void Setcourse(int TID)
{
	system("cls");
	printf("Welcome : %s\n",A_T[TID].name.c_str());
	printf("You need to set %d courses\n\n",A_T[TID].Len-A_T[TID].now_have);
	PrintC(1);
	printf("Please enter the Course Id to set\nYou can input -1 to exit\nYou can input -2 to check Mycourse\nNow enter a number:");
	int flag=0;scanf("%d",&flag);
	if(flag==-1) BuildMainMenu();
	else if(flag==-2) A_T[TID].PrintNowteach(),Setcourse(TID);//查看已设置的课程
	else if(flag>0 && flag<=C_totalnum)//设置课程
		A_T[TID].setCourse(flag),Setcourse(TID);
	else {WrongW();Setcourse(TID);}
}

void TeacherSystem()
{
	system("cls");
	freopen("CON","r",stdin); //切换到从控制台输入数据
	puts("         Welcome  to  Teacher  system!        ");
	PrintEgde();putchar('\n');
	//输入教师编号以进入系统
	printf("Please enter your Teacher Id:");
	int TID;scanf("%d",&TID);
	if(TID>0 && TID<=T_totalnum)
		Setcourse(TID);
	else{
		WrongW();TeacherSystem();
	}
}

void TASystem()
{
	//实际上助教系统可以看错和学生是一个系统的
	StudentSystem(1);
}

//构建主菜单
void BuildMainMenu()
{
	system("cls");
	freopen("CON","r",stdin); //切换到从控制台输入数据
	puts("      Welcome to course selection system!     ");
	PrintEgde();
	puts("*         1.Student entrance                 *");
	puts("*         2.Teacher entrance                 *");
	puts("*         3.TeachingAssistant                *");
	puts("*        -1.Exit this system                 *");
	PrintEgde();putchar('\n');
	printf("Please input 1,2,3 or -1 to enter this system:");
	int flag=-1;scanf("%d",&flag);//进入子系统
	if(flag==1) StudentSystem(0);
	else if(flag==2) TeacherSystem();
	else if(flag==3) TASystem();
	else if(flag==-1) exit(0);
	else
	{
		WrongW();
		BuildMainMenu();
	}
}

int main()//主函数
{
    Pre();//前置数据导入
	BuildMainMenu();//构建主菜单
	return 0;
}
/*
date.txt
10 
1 Media_English 2
2 Literature_English 2
3 Drama_English 2
4 Academic_English 2
5 Cross-cultural_Communication 2
6 Public_Speaking 2
7 Intermediate_English_Speaking 2
8 Intermediate_English_Writing 2
9 Basic_Russian 2
10 Basic_French 2
5
Harry_potter 35 1 2
Hermione 34 2 3
Henry_rowen 36 3 1
Snape 55 4 2
Dumbledore 46 5 2
2
Jack 2021 2021001 123456!
Tom 2021 2021002 abc123
*/
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值