📄 学生选课系统.cpp
字号:
/*刘健,你好!在程序中我们确实遇到了许多的问题。比如密码的修改,对于管理
员的密码还可以改,因为管理员只有一个,我们建的文件没有搞追加所以,修改的密码可以覆盖
掉原密码;但是对于学生和教师就不同了,每个学生有自己的用户名和密码,不可能建N个文件来
存储,所以文件搞了追加,结果原密码和修改的密码都在文件里面,所以,输入原密码和新密码
都可以进去。另外就是学生输入用户名和密码登陆之后,我们不知道该怎么样把用户名(也就是
学号)记录下来(设为全局变量?)那该怎样存入文件呢? 所以进入选课的时候就要再输入一次
学号,这样也就造成了可以输入别人的学号帮别人选课,这样也就再安全性方面大打折扣。更令人
郁闷的是不知道哪个地方处了差错,每次选课输入的学号从头到尾都是对的,但是记录到文件的时
候学号却是我“student.txt”文件里的最后一条记录里的学号。这个问题实在是搞不清哪个地方出
错了,虽然程序很乱,但是仍然希望你能指点一下!你可一定要在百忙之中抽点时间出来改改啊!*/
#include<iostream>
#include<string>
#include<iomanip>
#include<fstream>
#include<stdlib.h>
#include<windows.h>
#include<conio.h>
#include <ctype.h>
void handle_Memu1();
void handle_Memu3();
void handle_Memu4();
void handle_Memu5();
int shuzi();
using namespace std;
/*********************************************************************
* *
*********************************************************************/
class CSubject; //选课类;
class CLand; //登陆类;
class CStudent //学生类;
{
public:
CStudent()//初始化一个学生最多能选的课程(5)及最初的课程(0);
{
m_imaxcourse=5;
m_ifactcourse=0;
}
friend class CSubject;
friend class CLand;
char *Getsno(void) //这个地方的返回值不知道会不会有什么问题,有警告,还没解决;
{
return m_csno;
}
void Setsname();
char *Setsno(char *fp);//不知道怎么输入重名的学号它会报错,但是流入文件的还是重名的学号。
void Setsex();
void Setbanji();
void Setzhuanye();
int Setmaxcourse();
int Setfactcourse();
void Readf(char *fp); //读取文件里的东西;
void Writef(char *fp); //数据流入到文件;
char *Queryf(char *fp); //本来是想处理学号的重名问题,结果每次流入的都是同一个学号;
//后来就没用了,但是用上面的那个Setsno();也出了问题。
void Output();
void Input();
void show(char *fp);
int static count;
~CStudent();
protected:
int m_imaxcourse,m_ifactcourse;
char m_csname[20],m_csno[20],m_csex,m_cbanji[20],m_czhuanye[20];
};
//*********************************************************************
int CStudent::count=0;
//*********************************************************************
int CStudent::Setmaxcourse()
{
int maxcourse;
cout<<"请输入学生最多可选课程:";
maxcourse=shuzi();
m_imaxcourse=maxcourse;
return m_imaxcourse;
}
//*********************************************************************
int CStudent::Setfactcourse()
{
return m_ifactcourse++;
}
//*********************************************************************
char* CStudent::Setsno(char *fp)//不知道怎么输入重名的学号它会报错,
//但是流入文件的还是重名的学号。
{
char sno[20];
cout<<"请输入学号:";
cin>>sno;
ifstream fin(fp);
if (!fin)
{
strcpy(m_csno,sno);
return m_csno;
}
fin.get();
while (!fin.eof())
{
fin>>m_csno>>m_csname>>m_csex>>m_cbanji>>m_czhuanye
>>m_imaxcourse>>m_ifactcourse;
if (strcmp(m_csno,sno)==0)
{
cout<<"学号重复,请重新输入!";
getch();
handle_Memu5();
break;
}
}
strcpy(m_csno,sno);
return m_csno;
fin.close();
}
//*********************************************************************
void CStudent::Setsname()
{
char sname[20];
cout<<"请输入姓名:";
cin>>sname;
strcpy(m_csname,sname);
}
//*********************************************************************
void CStudent::Setsex()
{
char sex;
cout<<"性别:(a=男,b=女)";
cin>>sex;
while(sex!='a'&&sex!='b')
{
cout<<"你的输入有误,请重新输入:";
cin>>sex;
}
m_csex=sex;
}
//*********************************************************************
void CStudent::Setbanji()
{
char banji[20];
cout<<"请输入班级:";
cin>>banji;
strcpy(m_cbanji,banji);
}
//*********************************************************************
void CStudent::Setzhuanye()
{
char zhuanye[20];
cout<<"请输入专业:";
cin>>zhuanye;
strcpy(m_czhuanye,zhuanye);
}
//*********************************************************************
void CStudent::Output()
{
cout<<"学号:"<<setw(20)<<m_csno<<endl;
cout<<"姓名:"<<setw(20)<<m_csname<<endl;
cout<<"性别:"<<setw(20);
switch(m_csex)
{
case'a':
cout<<"男"<<endl;
break;
case'b':
cout<<"女"<<endl;
break;
default:
cout<<"输入有误"<<endl;
}
cout<<"班级:"<<setw(20)<<m_cbanji<<endl;
cout<<"专业:"<<setw(20)<<m_czhuanye<<endl;
}
//*********************************************************************
void CStudent::Writef(char *fp)
{
ofstream fout(fp,ios::app);
if (!fout)
{
cout<<"文件不能打开"<<endl;
}
else
{
fout<<endl;
fout<<m_csno<<setw(20)<<m_csname<<setw(20)<<m_csex
<<setw(20)<<m_cbanji<<setw(20)<<m_czhuanye<<setw(20)
<<m_imaxcourse<<setw(20)<<m_ifactcourse;
}
fout.close();
}
//*********************************************************************
void CStudent::Readf(char *fp)
{
ifstream fin(fp);
if (!fin)
{
cout<<"文件不能打开"<<endl;
}
fin.get();
while (!fin.eof())
{
fin>>m_csno>>m_csname>>m_csex>>m_cbanji>>m_czhuanye
>>m_imaxcourse>>m_ifactcourse;
count++;
}
fin.close();
}
//*********************************************************************
/*char* CStudent::Queryf(char *fp)
{
ifstream fin(fp);
char sno[20];
strcpy(sno,Setsno());
if (!fin)
{
fin.close();
fin.clear();
}
fin.get();
while (!fin.eof())
{
fin>>m_csno>>m_csname>>m_csex>>m_cbanji>>m_czhuanye
>>m_imaxcourse>>m_ifactcourse;
if (strcmp(m_csno,sno)==0)
{
cout<<"学号重复,请重新输入!";
Queryf("student.txt");
break;
}
}
return sno;
fin.close();
} */
//*********************************************************************
void CStudent::show(char *fp)
{
ifstream fin(fp);
if (!fin)
{
cout<<"您还没有任何添加学生"<<endl;
return;
}
fin.get();
while (!fin.eof())
{
fin>>m_csno>>m_csname>>m_csex>>m_cbanji>>m_czhuanye
>>m_imaxcourse>>m_ifactcourse;
Output();
}
fin.close();
}
//********************************************************************
CStudent::~CStudent(){};
/*********************************************************************
* *
*********************************************************************/
class CCourse
{
public:
CCourse()
{
m_imaxcourse=5;
m_ifactcourse=0;
}
friend class CSubject;
char *Getcno(void)
{
return m_ccno;
}
void Setcname();
char *Setcno();
void Setxingzhi();//
void Setkeshi();//
void Settime();//
void Setaddress();//
void Setxuefen();//
void Readf(char *fp);
void Writef(char *fp);
void Queryf(char *fp);
int Setmaxcourse();
int Setfactcourse();
void Output();
void Input();
void show2(char *fp);
int static count;
~CCourse();
protected:
int m_imaxcourse,m_ifactcourse,m_ikeshi;
float m_fxuefen;
char m_ccname[20],m_ccno[20],m_cxingzhi[20],m_ctime[20],m_caddress[20];
};
//*********************************************************************
int CCourse::count=0;
//*********************************************************************
char* CCourse::Setcno()
{
char cno[20];
cout<<"请输入课程号:";
cin>>cno;
strcpy(m_ccno,cno);
return cno;
}
//*********************************************************************
void CCourse::Setcname()
{
char cname[20];
cout<<"请输入课程名:";
cin>>cname;
strcpy(m_ccname,cname);
}
//*********************************************************************
void CCourse::Setxingzhi()//
{
char xingzhi[20];
cout<<"请输入课程性质:";
cin>>xingzhi;
strcpy(m_cxingzhi,xingzhi);
}
//*********************************************************************
void CCourse::Setkeshi()//
{
int keshi;
cout<<"请输入课时:";
keshi=shuzi();
m_ikeshi=keshi;
}
//*********************************************************************
void CCourse::Settime()//
{
char time[20];
cout<<"请输入上课时间:";
cin>>time;
strcpy(m_ctime,time);
}
//*********************************************************************
void CCourse::Setaddress()//
{
char address[20];
cout<<"请输入上课地址:";
cin>>address;
strcpy(m_caddress,address);
}
//*********************************************************************
void CCourse::Setxuefen()//
{
float xuefen;
cout<<"请输入课程学分:";
xuefen=shuzi();
m_fxuefen=xuefen;
}
//*********************************************************************
int CCourse::Setmaxcourse()//
{
int maxcourse;
cout<<"请输入最多可选课程:";
maxcourse=shuzi();
m_imaxcourse=maxcourse;
return m_imaxcourse;
}
//*********************************************************************
int CCourse::Setfactcourse()
{
return ++m_ifactcourse;
}
//*********************************************************************
void CCourse::Output()
{
cout<<"课程号:"<<setw(20)<<m_ccno<<endl;
cout<<"课程名:"<<setw(20)<<m_ccname<<endl;
cout<<"课程性质:"<<setw(20)<<m_cxingzhi<<endl;//
cout<<"课程课时:"<<setw(20)<<m_ikeshi<<endl;
cout<<"上课地点:"<<setw(20)<<m_caddress<<endl;
cout<<"上课时间:"<<setw(20)<<m_ctime<<endl;
cout<<"学分:"<<setw(20)<<m_fxuefen<<endl;//
}
//*********************************************************************
void CCourse::Writef(char *fp)
{
ofstream fout(fp,ios::app);
if (!fout)
{
cout<<"文件不能打开"<<endl;
}
else
{
fout<<endl;
fout<<m_ccno<<setw(20)<<m_ccname<<setw(20)<<m_cxingzhi<<setw(20)
<<m_ikeshi<<setw(20)<<m_caddress<<setw(20)<<m_ctime<<setw(20)
<<m_fxuefen<<setw(20)<<m_imaxcourse<<setw(20)<<m_ifactcourse;
}
fout.close();
}
//*********************************************************************
void CCourse::Readf(char *fp)
{
ifstream fin(fp);
if (!fin)
{
cout<<"文件不能打开"<<endl;
}
fin.get();
while (!fin.eof())
{
fin>>m_ccno>>m_ccname>>m_cxingzhi>>m_ikeshi>>m_caddress>>m_ctime
>>m_fxuefen>>m_imaxcourse>>m_ifactcourse;
count++;
}
fin.close();
}
//*********************************************************************
void CCourse::Queryf(char *fp)
{
ifstream fin(fp);
char cno[20];
strcpy(cno,Setcno());
if (!fin)
{
fin.close();
fin.clear();
}
fin.get();
while (!fin.eof())
{
fin>>m_ccno>>m_ccname>>m_cxingzhi>>m_ikeshi>>m_caddress>>m_ctime
>>m_fxuefen>>m_imaxcourse>>m_ifactcourse;
if (strcmp(m_ccno,cno)==0)
{
cout<<"课程号重复,请重新输入!";
Queryf("course.txt");
}
}
fin.close();
}
//*********************************************************************
void CCourse::show2(char *fp)
{
ifstream fin(fp);
if (!fin)
{
cout<<"您还没有任何添加学生"<<endl;
return;
}
fin.get();
while (!fin.eof())
{
fin>>m_ccno>>m_ccname>>m_cxingzhi>>m_ikeshi>>m_caddress>>m_ctime
>>m_fxuefen>>m_imaxcourse>>m_ifactcourse;
Output();
}
fin.close();
}
//*********************************************************************
CCourse::~CCourse(){}
/*********************************************************************
* *
*********************************************************************/
class CTeacher
{
public:
CTeacher(){};
friend class CLand;
friend class CSubject;
char *Gettno(void)
{
return m_ctno;
}
void Settname();
char *Settno();
void Setsex();
void Setzhicheng();
void Setxi();
void Readf(char *fp);
void Writef(char *fp);
void Queryf(char *fp);
void Output();
void Input();
void show3(char *fp);
int static count;
~CTeacher();
protected:
char m_ctname[20],m_ctno[20],m_ctxi[20],m_ctsex,m_ctzhicheng[20];
};
//*********************************************************************
int CTeacher::count=0;
//*********************************************************************
char* CTeacher::Settno()
{
char tno[20];
cout<<"请输入教师号:";
cin>>tno;
strcpy(m_ctno,tno);
return tno;
}
//*********************************************************************
void CTeacher::Settname()
{
char tname[20];
cout<<"请输入教师名:";
cin>>tname;
strcpy(m_ctname,tname);
}
//*********************************************************************
void CTeacher::Setxi()
{
char txi[20];
cout<<"请输入教师所属系:";
cin>>txi;
strcpy(m_ctxi,txi);
}
//*********************************************************************
void CTeacher::Setsex()
{
char tsex;
cout<<"性别:(a=男,b=女)";
cin>>tsex;
while(tsex!='a'&&tsex!='b')
{
cout<<"你的输入有误,请重新输入:";
cin>>tsex;
}
m_ctsex=tsex;
}
//*********************************************************************
void CTeacher::Setzhicheng()
{
char tzhicheng[20];
cout<<"请输入教师职称:";
cin>>tzhicheng;
strcpy(m_ctzhicheng,tzhicheng);
}
//*********************************************************************
void CTeacher::Output()
{
cout<<"教师号:"<<setw(20)<<m_ctno<<endl;
cout<<"教师名:"<<setw(20)<<m_ctname<<endl;
cout<<"教师所属系:"<<setw(20)<<m_ctxi<<endl;
cout<<"性别:"<<setw(20);
switch(m_ctsex)
{
case'a':
cout<<"男"<<endl;
break;
case'b':
cout<<"女"<<endl;
break;
default:
cout<<"输入有误"<<endl;
}
cout<<"教师职称:"<<setw(20)<<m_ctzhicheng<<endl;
}
//*********************************************************************
void CTeacher::Writef(char *fp)
{
ofstream fout(fp,ios::app);
if (!fout)
{
cout<<"文件不能打开"<<endl;
}
else
{
fout<<endl;
fout<<m_ctno<<setw(20)<<m_ctname<<setw(20)<<m_ctxi<<setw(20)
<<m_ctsex<<setw(20)<<m_ctzhicheng;
}
fout.close();
}
//*********************************************************************
void CTeacher::Readf(char *fp)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -