资源描述
#include<iostream>
#include<conio.h>
#include<string>
#include<stdlib.h>
#include<windows.h>
#include<iomanip>
#include<fstream>
using namespace std;
//日期类
class date
{ private:
int year,month,day; //年、月、日三个成员
public:
void set_date(int y,int m,int d)
{ year=y;month=m;day=d;}
int get_year() {return year;}//取年份
int get_month() {return month;}//取月份
int get_day() {return day;}//取日期
};
//人员类
class person
{
protected:
char name[10];
char id[6];
char sex[2];
date birth;
char idCard[18];
public:
char *get_name(){return name;}
char *get_id(){return id;}
char *get_sex(){return sex;}
char *get_idCard(){return idCard;}
int get_year(){return birth.get_year();}
int get_month(){return birth.get_month();}
int get_day(){return birth.get_day();}
};
// 学生类 (继承人员类)
class student : public person
{
protected:
char classNum[15];
public:
char *get_classNum() {return classNum;}
void printS();
void get_stu();
void showS();
void enterStu(int&); //输入学生记录
void showStu(); //显示学生记录
bool searchStu(); //按姓名查找学生记录
void changeStu(); //更改学生记录
void insertStu(); //插入学生记录
void delStu(int&); //删除学生记录
};
void student::printS() //输出学生信息
{ cout<<setw(3)<<get_id();
cout<<setw(8)<<get_name();
cout<<setw(4)<<get_sex();
cout<<setw(10)<<get_year()<<"年"<<get_month()<<"月"<<get_day()<<"日";
cout<<setw(20)<<get_idCard();
cout<<setw(15)<<get_classNum()<<endl;
}
void student::showS()
{
cout<<setw(4)<<"编号";
cout<<setw(6)<<"姓名";
cout<<setw(6)<<"性别";
cout<<setw(15)<<"出生日期";
cout<<setw(18)<<"身份证号";
cout<<setw(15)<<"学号"<<endl;
}
int s=0; //定义全局变量,用于记录人事管理系统中的学生数
void student::get_stu()
{
ifstream file("stu.dat",ios::binary|ios::in);//打开用于读的binary文件
if(!file)
{
ofstream outfile("stu.dat",ios::app|ios::binary);//打开文件,并将指针指向文件尾
if(!outfile)
{
cerr<<"stu.dat 文件打开错误!"<<endl;
return;
}
return;
}
file.read((char*)(this),sizeof(*this));//读取一个类对象数据放入*this中
while(!file.eof())
{
s++;//每调用一次该函数,职员总数增加1
file.read((char*)(this),sizeof(*this));//再读取一个类对象数据放入*this中
}
file.close();
}
void student::enterStu(int& s) //输入学生信息
{
int year,month,day;
cout<<"输入学生信息:"<<endl;
ofstream outfile("stu.dat",ios::app|ios::binary);//打开文件,并将指针指向文件尾
cout<<"编号:";
cin>>id;
cout<<"姓名:";
cin>>name;
cout<<"性别:";
cin>>sex;
cout<<"身份证号:";
cin>>idCard;
cout<<"学号:";
cin>>classNum;
cout<<"出生日期(年月日用空格键区分):";
cin>>year>>month>>day;
birth.set_date(year, month, day);
cout<<endl;
outfile.write((char*)(this),sizeof(*this));//将输入的信息写入文件
outfile.close();
cout<<"信息建立成功!"<<endl;
s++; //每调用一次该函数,学生总数增加1
}
void student::showStu()//显示所有学生信息
{
if(s==0)
cout<<"学生数据为空,请先添加学生信息!"<<endl;
else
{
ifstream infile("stu.dat",ios::binary); //打开用于读的binary文件
infile.read((char*)(this),sizeof(*this)); //读取一个类对象数据放入*this中
cout<<"所有学生的信息如下:"<<endl;
showS();
while(!infile.eof()) //沿未到达文件尾,继续处理
{
printS();
infile.read((char*)(this),sizeof(*this));
}
infile.close();
}
}
bool student::searchStu()//按姓名查找学生信息
{
char na[10];
cout<<"请输入你要查找学生的姓名:"<<endl;
cin>>na;
ifstream infile("stu.dat",ios::binary);
infile.read((char*)(this),sizeof(*this));//打开文件,读取数据
while(!infile.eof())
{
if(strcmp(name,na)==0)
{
cout<<"该学生的个人信息如下:"<<endl;
showS();
printS();
infile.close();
return true;
}
infile.read((char*)(this),sizeof(*this));//文件指针未到文件尾,循环读取数据
}
infile.close();
cout<<"对不起,你要查找的学生不存在!"<<endl;
return false;
}
void student::changeStu() //修改数据
{
int year,month,day;
int changemark=0; //用于标记该编号信息是否已找到
char stuname[20];
cout<<"请输入你要修改信息的学生的:"<<endl;
cin>>stuname;
ofstream outfile("stuchange.dat",ios::app|ios::binary);//打开用于写的二进制文件
ifstream infile("stu.dat",ios::binary);//打开用于读的二进制文件
infile.read((char*)(this),sizeof(*this));//从"stu.txt"中读取一个类对象数据放入*this中
showS();
while(!infile.eof())
{
if(strcmp(name,stuname)==0)
{
changemark=1;
cout<<"输入学生修改信息:"<<endl;
ofstream outfile("stuchange.dat",ios::binary);//打开文件
cout<<"编号:";
cin>>id;
cout<<"姓名:";
cin>>name;
cout<<"性别:";
cin>>sex;
cout<<"身份证号:";
cin>>idCard;
cout<<"学号:";
cin>>classNum;
cout<<"请输入出生日期(年月日用空格键区分):";
cin>>year>>month>>day;
birth.set_date(year, month, day);
outfile.write((char*)(this),sizeof(*this));//将输入的信息写入文件
outfile.close();
cout<<"信息修改成功!"<<endl;
}
else
outfile.write((char*)(this),sizeof(*this));//将读得的数据写入文件
infile.read((char*)(this),sizeof(*this));//再次读取一个类对象数据,放于*this中
}
outfile.close();
infile.close();
if(changemark==1) //将修改后的信息写入原文件
{
ofstream outfile("stu.dat",ios::binary);//打开用于写的二进制文件"stu.txt"
ifstream infile("stuchange.dat",ios::binary);//打开用于读的二进制文件
infile.read((char*)(this),sizeof(*this));//读取一个类对象数据放入*this中
while(!infile.eof())
{
outfile.write((char*)(this),sizeof(*this));
infile.read((char*)(this),sizeof(*this));
}
infile.close();
outfile.close();
}
if(changemark==0)
cout<<"欲修改的学生不存在!"<<endl;
}
void student::insertStu() //插入数据
{
int year,month,day;
cout<<"添加学生信息:"<<endl;
ofstream outfile("stu.dat",ios::app|ios::binary);//打开文件,并将指针指向文件尾
cout<<"编号:";
cin>>id;
cout<<"姓名:";
cin>>name;
cout<<"性别:";
cin>>sex;
cout<<"身份证号:";
cin>>idCard;
cout<<"学号:";
cin>>classNum;
cout<<"出生日期:(年月日中间用空格区分)";
cin>>year>>month>>day;
birth.set_date(year, month, day);
cout<<endl;
outfile.write((char*)(this),sizeof(*this));//将输入的信息写入文件
outfile.close();
cout<<"信息添加成功!"<<endl;
s++; //每调用一次该函数,学生总数增加1
}
void student::delStu(int& s) //删除学生信息
{
char b;
int delmark=0; //用于标记该编号信息是否已找到
char stuname[20];
cout<<"请输入要删除学生的姓名:"<<endl;
cin>>stuname;
ofstream outfile("studel.dat",ios::binary);//打开用于写的二进制文件
ifstream infile("stu.dat",ios::binary);//打开用于读的二进制文件
infile.read((char*)(this),sizeof(*this));//从"managestu.txt"中读取一个类对象数据放入*this中
cout<<"该学生的个人信息如下:"<<endl;
cout<<setw(4)<<"编号";
cout<<setw(6)<<"姓名";
cout<<setw(6)<<"性别";
cout<<setw(15)<<"出生日期";
cout<<setw(18)<<"身份证号";
cout<<setw(15)<<"学号"<<endl;
while(!infile.eof())
{
if(strcmp(name,stuname)==0)
{
printS();
cout<<endl<<"你确定要删除该学生的信息?(Y=yes/N=no)"<<endl;
cin>>b;
if(b=='y'||b=='Y')
{
delmark=1; //将删除标记置为1,表示已删除信息
s--; //确定删除信息,人事档案管理系统中成员总数减1
cout<<"该学生信息已删除!"<<endl;
}
else
delmark=-1;
}
else
outfile.write((char*)(this),sizeof(*this));//将读得的数据写入文件
infile.read((char*)(this),sizeof(*this));//再次读取一个类对象数据,放于*this中
}
outfile.close();
infile.close();
if(delmark==1) //若找到了删除对象,就用"tu1.txt"文件内容去充当"stu.txt"
{
ofstream outfile("stu.dat",ios::binary);//打开用于写的二进制文件"stu.txt"
ifstream infile("studel.dat",ios::binary);//打开用于读的二进制文件"managestu1.txt"
infile.read((char*)(this),sizeof(*this));//读取一个类对象数据放入*this中
while(!infile.eof())
{
outfile.write((char*)(this),sizeof(*this));
infile.read((char*)(this),sizeof(*this));
}
infile.close();
outfile.close();
}
if(delmark==0)
cout<<"欲删除的学生不存在!"<<endl;
}
//教师类 (继承人员类)
class teacher : public person
{
protected:
char work[10];
char party[20];
public:
char *get_work() {return work;}
char *get_party(){return party;}
void printT();
void printT1();
void inputT();
void get_tch();
void enterTch(int&); //输入教师记录
void showTch(); //显示教师记录
bool searchTch(); //按姓名查找教师记录
void changeTch(); //更改教师记录
void insertTch(); //插入教师记录
void delTch(int&); //删除教师记录
};
void teacher::printT() //输出教师信息
{ cout<<setw(3)<<get_id();
cout<<setw(8)<<get_name();
cout<<setw(4)<<get_sex();
cout<<setw(10)<<get_year()<<"年"<<get_month()<<"月"<<get_day()<<"日";
cout<<setw(20)<<get_idCard();
cout<<setw(10)<<get_work();
cout<<setw(14)<<get_party()<<endl;
}
void teacher::printT1() //用于研究生输出导师信息
{
cout<<setw(6)<<"姓名";
cout<<setw(6)<<"性别";
cout<<setw(15)<<"出生日期";
cout<<setw(18)<<"身份证号";
cout<<setw(13)<<"职务";
cout<<setw(13)<<"部门"<<endl;
cout<<"信息";
cout<<setw(6)<<get_name();
cout<<setw(4)<<get_sex();
cout<<setw(10)<<get_year()<<"年"<<get_month()<<"月"<<get_day()<<"日";
cout<<setw(20)<<get_idCard();
cout<<setw(10)<<get_work();
cout<<setw(14)<<get_party()<<endl;
}
void teacher::inputT() // 用于研究生输入导师信息
{ int year,month,day;
cout<<"姓名:";
cin>>name;
cout<<"性别:";
cin>>sex;
cout<<"身份证号:";
cin>>idCard;
cout<<"职务:";
cin>>work;
cout<<"部门:";
cin>>party;
cout<<"出生日期(年月日用空格键区分):";
cin>>year>>month>>day;
birth.set_date(year, month, day);
}
int t=0;
void teacher::get_tch()
{
ifstream file("tch.dat",ios::binary|ios::in);
if(!file)
{
ofstream outfile("tch.dat",ios::app|ios::binary);
if(!outfile)
{
cerr<<"tch.dat 文件打开错误!"<<endl;
return;
}
return;
}
file.read((char*)(this),sizeof(*this));
while(!file.eof())
{
t++;
file.read((char*)(this),sizeof(*this));
}
file.close();
}
void teacher::enterTch(int&)
{
int year,month,day;
cout<<"输入教师信息:"<<endl;
ofstream outfile("tch.dat",ios::app|ios::binary);
cout<<"编号:";
cin>>id;
cout<<"姓名:";
cin>>name;
cout<<"性别:";
cin>>sex;
cout<<"身份证号:";
cin>>idCard;
cout<<"职务:";
cin>>work;
cout<<"部门:";
cin>>party;
cout<<"出生日期(年月日用空格键区分):";
cin>>year>>month>>day;
birth.set_date(year, month, day);
cout<<endl;
outfile.write((char*)(this),sizeof(*this));
outfile.close();
cout<<"信息建立成功!"<<endl;
t++;
}
void teacher::showTch()
{
if(t==0)
cout<<"教师数据为空,请先添加教师信息!"<<endl;
else
{
ifstream infile("tch.dat",ios::binary);
infile.read((char*)(this),sizeof(*this));
cout<<"所有教师的信息如下:"<<endl;
cout<<setw(4)<<"编号";
cout<<setw(6)<<"姓名";
cout<<setw(6)<<"性别";
cout<<setw(15)<<"出生日期";
cout<<setw(18)<<"身份证号";
cout<<setw(13)<<"职务";
cout<<setw(13)<<"部门"<<endl;
while(!infile.eof())
{
printT();
infile.read((char*)(this),sizeof(*this));
}
infile.close();
}
}
bool teacher::searchTch()
{
char na[10];
cout<<"请输入你要查找教师的姓名:"<<endl;
cin>>na;
ifstream infile("tch.dat",ios::binary);
infile.read((char*)(this),sizeof(*this));
while(!infile.eof())
{
if(strcmp(name,na)==0)
{
cout<<"该教师的个人信息如下:"<<endl;
cout<<setw(4)<<"编号";
cout<<setw(6)<<"姓名";
cout<<setw(6)<<"性别";
cout<<setw(15)<<"出生日期";
cout<<setw(18)<<"身份证号";
cout<<setw(8)<<"职务";
cout<<setw(16)<<"部门"<<endl;
printT();
infile.close();
return true;
}
infile.read((char*)(this),sizeof(*this));
}
infile.close();
cout<<"对不起,你要查找的教师不存在!"<<endl;
return false;
}
void teacher::changeTch()
{
int year,month,day;
int changemark=0;
char tchname[20];
cout<<"请输入你要修改的教师的姓名:"<<endl;
cin>>tchname;
ofstream outfile("tchchange.dat",ios::app|ios::binary);
ifstream infile("tch.dat",ios::binary);
infile.read((char*)(this),sizeof(*this));
while(!infile.eof())
{
if(strcmp(name,tchname)==0)
{
changemark=1;
cout<<"输入修改信息:"<<endl;
ofstream outfile("tchchange.dat",ios::binary);
cout<<"编号:";
cin>>id;
cout<<"姓名:";
cin>>name;
cout<<"性别:";
cin>>sex;
cout<<"身份证号:";
cin>>idCard;
cout<<"职务:";
cin>>work;
cout<<"部门:";
cin>>party;
cout<<"出生日期(年月日用空格键区分):";
cin>>year>>month>>day;
birth.set_date(year, month, day);
cout<<endl;
outfile.write((char*)(this),sizeof(*this));
outfile.close();
cout<<"信息修改成功!"<<endl;
}
else
outfile.write((char*)(this),sizeof(*this));
infile.read((char*)(this),sizeof(*this));
}
outfile.close();
infile.close();
if(changemark==1)
{
ofstream outfile("tch.dat",ios::binary);
ifstream infile("tchchange.dat",ios::binary);
infile.read((char*)(this),sizeof(*this));
while(!infile.eof())
{
outfile.write((char*)(this),sizeof(*this));
infile.read((char*)(this),sizeof(*this));
}
infile.close();
outfile.close();
}
if(changemark==0)
cout<<"欲修改的教师不存在!"<<endl;
}
void teacher::insertTch()
{
int year,month,day;
cout<<"添加教师信息:"<<endl;
ofstream outfile("tch.dat",ios::app|ios::binary);
cout<<"编号:";
cin>>id;
cout<<"姓名:";
cin>>name;
cout<<"性别:";
cin>>sex;
cout<<"身份证号:";
cin>>idCard;
cout<<"职务:";
cin>>work;
cout<<"部门:";
cin>>party;
cout<<"出生日期:(年月日之间用空格区分)";
cin>>year>>month>>day;
birth.set_date(year, month, day);
cout<<endl;
outfile.write((char*)(this),sizeof(*this));
outfile.close();
cout<<"信息添加成功!"<<endl;
t++;
}
void teacher::delTch(int&)
{
char b;
int delmark=0;
char stuname[20];
cout<<"请输入要删除教师的姓名:"<<endl;
cin>>stuname;
ofstream outfile("tchdel.dat",ios::binary);
ifstream infile("tch.dat",ios::binary);
infile.read((char*)(this),sizeof(*this));
cout<<"该教师的个人信息如下:"<<endl;
cout<<setw(4)<<"编号";
cout<<setw(6)<<"姓名";
cout<<setw(6)<<"性别";
cout<<setw(15)<<"出生日期";
cout<<setw(18)<<"身份证号";
cout<<setw(13)<<"职务";
cout<<setw(13)<<"部门"<<endl;
while(!infile.eof())
{
if(strcmp(name,stuname)==0)
{
printT();
cout<<endl<<"你确定要删除该教师的信息?(Y=yes/N=no)"<<endl;
cin>>b;
if(b=='y'||b=='Y')
{
delmark=1;
s--;
cout<<"该教师信息已删除!"<<endl;
}
else
delmark=-1;
}
else
outfile.write((char*)(this),sizeof(*this));
infile.read((char*)(this),sizeof(*this));
}
outfile.close();
infile.close();
if(delmark==1)
{
ofstream outfile("tch.dat",ios::binary);
ifstream infile("tchdel.dat",ios::binary);
infile.read((char*)(this),sizeof(*this));
while(!infile.eof())
{
outfile.write((char*)(this),sizeof(*this));
infile.read((char*)(this),sizeof(*this));
}
infile.close();
outfile.close();
}
if(delmark==0)
cout<<"欲删除的教师不存在!"<<endl;
}
//研究生类 (继承学生类)
class postgraduate : public student
{
protected:
char major[20];
char tutor[10];
teacher tch;
public:
void set_postgraduate(char *major,char *party);
char *get_major() {return major;}
char *get_tutor() {return tutor;}
void printPG();
void get_PG();
void enterPG(int&);
void showPG();
bool searchPG();
void changePG();
void insertPG();
void delPG(int&);
};
void postgraduate::printPG()
{ cout<<setw(3)<<get_id();
cout<<setw(8)<<get_name();
cout<<setw(4)<<get_sex();
cout<<setw(10)<<get_year()<<"年"<<get_month()<<"月"<<get_day()<<"日";
cout<<setw(20)<<get_idCard();
cout<<setw(15)<<get_classNum();
cout<<setw(12)<<get_maj
展开阅读全文