C++大作业——公司员工管理系统报告和源码

公司员工管理系统报告

题目:设计一个虚基类 Staff(员工),包含编号、姓名和年龄保护数据成员以及相关的成员函数;由 Staff 派生出工程师类 Engineer,包含专业和职称保护数据成员以及相关的成员函数;再由 Staff 派生出领导类 Leader,包含职务和部门保护数据成员以及相关的成员函数;然后由 Engineer 和 Leader 派生出主任工程师类 Chairman。

设计一个利用文件处理方式实现对公司人员(包括工程师、领导和主任工程师)进行管理,具有增加数据、更新数据、查询数据、删除数据以及重组文件的功能。编程思想:

  1. 首先设计虚基类 Staff,然后再设计派生类 Engineer 和 Leader,最后再设计 Chariman 类。将成员函数 Show()与 Set()设置成虚函数实现动态绑定,为了避免多重继承下的二义性问题,编写了 output()与 get()两个辅助函数,为了让 Chairman 只继承 Staff 类的一个对象,将 Staff 类设置成虚基类。
  2. 为了以文件处理的方式实现对公司人员的管理,编写了三个文件,分别存储工程师、领导和主人工程师的信息,为了实现目的,本题使用二进制文件的随机访问技术,首先在各个类中定义记录格式,然后以二进制格式把数据以二进制形式写入文件,因为类中有虚函数,所以不能用 read()和 write()一次将一个对象存储到文件中,但可以一次读写一个数据成员。在数据的存储和读取技术的基础上,设计了一个操作员工信息的 Oper 类,将所有针对数据记录的操作封装在一起,包括更新,添加,查询,删除等。为了能够访问类中的私有成员,将 Oper 设置成其它类的友元类。

源代码:

1. chairman.h

#ifndef CHAIRMAN_H_ #define CHAIRMAN_H_ #include<string> #include<cstring>

using namespace std;//Staff 类的声明

class Staff

{private:

long Id;//编号 char fullname[15];//姓名 int Age; //年龄

protected:

virtual void output()const;//辅助模块 virtual void get();//辅助模块

public:

void setName( char*name); //设置雇员的名字 void setId(long id);//设置雇员的编号

void setAge(int age);//设置雇员的年龄 char* getName();//返回雇员的名字

long getId();//返回编号 intgetAge();//返回年龄

Staff(long id=0,char*name="noname",int age=0);virtual ~Staff(){} virtual void show() const=0;//显示员工信息

virtual void set()=0;//设置员工信息 friendclass Oper;

};

//Engineer 类的声明

class Engineer:virtual public Staff

{private:

char specialty[20];//专业 char position[20];//职位 protected:

void output()const;//辅助模块 void get(); //辅助模块 public:

Engineer(longid=0,char*name="noname",intage=0,char*spe="null",char*pos="null"):Staff(i d,name,age)

{

strcpy(specialty,spe); strcpy(position,pos);

}

void show() const;void set();

void setSpecialty(char*spe);//设置专业 void setPosition(char*pos);//设置职位

char* getSpecialty(); //返回专业 char* getPosition();//返回职位 friendclass Oper;

} ;

//Leader 类的声明

class Leader: virtual public Staff

{

private:

char department[20];//部门 char title[20];//职务 protected:

void get();//辅助模块 void output() const;//辅助模块 public:

Leader(longid=0,char*name="noname",intage=0,char*dep="null",char*tit="null"):Staff(id,n ame,age)

{strcpy(department,dep); strcpy(title,tit);

}

void show()const;//显示 leader 的信息

void set() ;//设置 leader 的信息

void setDepartment(char *depar); //设置部门 void setTitle(char* tit);//设置职 char*getDepartment();//返回部门

char*getTitle();//返回职务 friendclass Oper;};

//Chairman 类的声明

class Chairman:public Engineer,public Leader

{public:

Chairman(longid=0,char*name="noname",intage=0,char*spe="null", char*pos="null",char*dep="null",char*tit="null"):Staff(id,name,age),Engineer(id,name,age,spe,p os),Leader(id,name,age,dep,tit){}

void show() const; //显示 Chairman 的信息

void set() ; //设置 Chariman 的信息

protected:

} ;

#endif


void get() ; //辅助模块

void output()const ; //辅助模块 friendclass Oper;

Chairman.cpp #include "chairman.h" #include <iostream>

//Staff methods

void Staff:: output()const//辅助模块

{

cout<<"Name:"<<fullname<<endl; cout<<"Age:"<<Age<<endl; cout<<"Id:"<<Id<<endl;

}

void Staff::get()//辅助模块

{

cin>>fullname;cout<<"Enter staff's age:";cin>>Age; cout<<"Enter staff's id:";cin>>Id;

}

Staff::Staff(long id, char*name,int age)//构造函数

{ Id=id; Age=age; strcpy(fullname,name);}

void Staff:: setName( char*name)//设置雇员的名字

{strcpy(fullname,name);

}

void Staff::setId(long id)//设置雇员的编号

{ Id=id;

}

void Staff::setAge(int age)//设置雇员的年龄

{Age=age}}

char* Staff::getName()//返回雇员的名字

{ return fullname;}

long Staff:: getId()//返回编号

{ return Id;}

intStaff:: getAge()//返回年龄

{return Age;}

//Engineer methods

void Engineer:: output()const//辅助模块

{cout<<"Specialty:"<<specialty<<endl; cout<<"Position:"<<position<<endl;}

void Engineer:: get()//辅助模块

{

cout<<"Enter Specialty:";cin>>specialty; cout<<"Enter Position:";cin>>position;

}

void Engineer:: show() const//显示 engineer 的信息

{

cout<<"###Engineer###"<<endl; Staff::output();output();

}

void Engineer:: set()//设置 engineer 的信息

{cout<<"Enter Engineer's name:"; Staff::get();get();}

void Engineer:: setSpecialty(char*spe)//设置专业

{strcpy(specialty,spe);}

void Engineer::setPosition(char*pos)//设置职位

{strcpy(position,pos);}

char* Engineer:: getSpecialty()//返回专业

{return specialty;}

char* Engineer::getPosition()//返回职位

{ return position;}

//Leader methods

void Leader::output() const

{ cout<<"Department:"<<department<<endl;cout<<"Title:"<<title<<endl;} void Leader::get()

{cout<<"Enter Department:";cin>>department; cout<<endl; cout<<"Enter Title:";cin>>title; cout<<endl;

}

void Leader:: show()const

{cout<<"###Leader###"<<endl; Staff::output();output();

}

void Leader::set()

{cout<<"Enter Leader's name:"; Staff::get(); get();

}

void Leader::setDepartment(char *depar) //设置部门

{ strcpy(department,depar);}

void Leader::setTitle(char* tit)//设置职务

{ strcpy(title,tit);}

char* Leader::getDepartment()//返回部门

{ return department;}

char*Leader::getTitle()//返回职务

{ return title;

}

//Chairman methods void Chairman::get()

{Engineer::get();Leader::get() ;} void Chairman::output() const

{Engineer::output();Leader::output();}

void Chairman:: show() const //显示 Chairman 的信息

{ cout<<"###Chairman###"<<endl; Staff::output();

output();

}

voidChairman::set()//设置Chariman 的信息

{

}

Oper.h


cout<<"Enter Chairman's name:"; Staff::get(); get();

#ifndef OPER_H #define OPER_H #include <iostream> #include <fstream> #include "chairman.h" #include<string> #include<string> using namespace std;

enum Choice{UPDATE,NEW,DELETE,QUERY,END};

class Oper//操作

{

public:

void CreateFile();//创建文件 int enterChoice();//输入选项

void updateRecord(fstream&); //更新记录

void newRecord(fstream&);//创建和插入记录 void queryRecord(fstream&);//查询信息

void deleteRecord(fstream&);//删除记录 long getId(const char* );//得到编号 void CreateEngineerFile();

void UpdateEngineer (fstream& updateFile) ; //更新 engineer 文件

void NewEngineerRecord(fstream& insertFile);//插入,创建 Engineer 文件

void DeleteEngineerRecord(fstream& deleteFile);//删除 engineer 记

void queryEngineerRecord(fstream& queryFile);//查询 engineer 信息

void CreateLeaderFile() ;//创建 Leader 文件

void UpdateLeaderFile(fstream& updateFile);//更新 Leader 文件

voidNewLeaderRecord(fstream& insertFile);//插入,创建 Leader 文件

void DeleteLeaderRecord(fstream& deleteFile);//删除 Leader 记录 void queryLeaderRecord(fstream& queryFile);//查询 Leader 信息 void CreateChairmanFile();//创建 chairman 文件

void UpdateChairmanFile(fstream& updateFile);//更新 chairman 文件

voidNewChairmanRecord(fstream& insertFile);//插入,创建 Chairman 文件

void DeleteChairmanRecord(fstream& deleteFile);//删除 Chairman 记录

void queryChairmanRecord(fstream& queryFile);//查询 Leader 信息

};

void Oper:: CreateEngineerFile()//创建文 Engineer 件

{

fstream outFile("e.txt",ios_base::binary | ios_base::in | ios_base::out); if(!outFile) {cerr<<"File could not be opened."<<endl; exit(1);}

int choice;

cout<<"Enter your choice:***1***输入***0***结束"<<endl; cin>>choice;

//输入信息复制到文件 while(choice)

{

Engineer person;

person.set();//创建 Engineer 对象

//在文件中查找记录 outFile.seekp((person.getId()-1)*sizeof(Engineer));

//将信息写入文件 outFile.write((char*)&person.Id,sizeof(person.Id)); outFile.write((char*)&person.fullname,sizeof(person.fullname)); outFile.write((char*)&person.Age,sizeof(person.Age)); outFile.write((char*)&person.specialty,sizeof(person.specialty)); outFile.write((char*)&person.position,sizeof(person.position)); cout<<"Enter your choice:***1***输入***0***结束"<<endl; cin>>choice;

}

}

void Oper:: CreateLeaderFile() //创建 Leader 文件

{

fstream outFile("l.txt",ios_base::binary|ios_base::in|ios_base::out); if(!outFile) {cerr<<"File could not be opened."<<endl; exit(1);}

int choice;

cout<<"Enter your choice:***1***输入***0***结束"<<endl; cin>>choice;

Leader person;

//输入信息复制到文件 while(choice)

{

person.set();//创建Engineer 对象

//在文件中查找记录

outFile.seekp((person.getId()-1)*sizeof(Leader));

//将信息写入文件 outFile.write((char*)&person.Id,sizeof(person.Id)); outFile.write((char*)&person.fullname,sizeof(person.fullname)); outFile.write((char*)&person.Age,sizeof(person.Age)); outFile.write((char*)&person.department,sizeof(person.department)); outFile.write((char*)&person.title,sizeof(person.title));

cout<<"Enter your choice:***1***输入***0***结束"<<endl; cin>>choice;

}

}

void Oper:: CreateChairmanFile()//创建 chairman 文件

{

fstream outFile("c.txt",ios_base::binary|ios_base::in|ios_base::out); if(!outFile) {cerr<<"File could not be opened."<<endl; exit(1);}

int choice;

cout<<"Enter your choice:***1***输入***0***结束"<<endl; cin>>choice;

//输入信息复制到文件 Chairman person; while(choice)

{

person.set();//创建 chairman 对象

//在文件中查找记录 outFile.seekp((person.getId()-1)*sizeof(Chairman));

//将信息写入文件 outFile.write((char*)&person.Id,sizeof(person.Id)); outFile.write((char*)&person.fullname,sizeof(person.fullname)); outFile.write((char*)&person.Age,sizeof(person.Age)); outFile.write((char*)&person.specialty,sizeof(person.specialty)); outFile.write((char*)&person.position,sizeof(person.position)); outFile.write((char*)&person.department,sizeof(person.department)); outFile.write((char*)&person.title,sizeof(person.title));

cout<<"Enter your choice:***1***输入***0***结束"<<endl; cin>>choice;

}

}

void Oper:: UpdateEngineer (fstream& updateFile)//更新 engineer 文件

{

updateFile.open("e.txt",ios_base::in | ios_base::out|ios_base::binary); //打开 Engineer 文件用于读写操作

long id=getId("Enter Id to update");//获取要更新的编号 updateFile.seekg((id-1)*sizeof(Engineer));//移动指针到正确的位置 Engineer person;

//读取记录 updateFile.read((char*)&person.Id,sizeof(person.Id));

updateFile.read((char*)&person.fullname,sizeof(person.fullname)); updateFile.read((char*)&person.Age,sizeof(person.Age)); updateFile.read((char*)&person.specialty,sizeof(person.specialty)); updateFile.read((char*)&person.position,sizeof(person.position)); if(person.getId()!=0)//更新记录

{//显示旧记录

cout<<"显示旧值"<<endl;person.show(); char choice; cout<<"enter your choice"<<endl;

cout<<"a: update agep: update position"<<endl; cin>>choice;

switch(choice)

{

case 'a':

{

}

case 'p':

int age;cout<<"Inter new age:"; cin>>age;person.setAge(age); break;

{char newpos[20]; cout<<"Inter new position:"; cin>>newpos; person.setPosition(newpos); break;

}

}

}

else

{


cout<<"显示新值"<<endl; person.show();

//用新的纪录覆盖旧的记录 updateFile.write((char*)&person.Id,sizeof(person.Id)); updateFile.write((char*)&person.fullname,sizeof(person.fullname)); updateFile.write((char*)&person.Age,sizeof(person.Age)); updateFile.write((char*)&person.specialty,sizeof(person.specialty)); updateFile.write((char*)&person.position,sizeof(person.position));

cerr<<"Id:"<<id<<"has no information."<<endl;

}

updateFile.close(); updateFile.clear();

}

void Oper::UpdateLeaderFile(fstream& updateFile)//更新 Leader 文件

{

updateFile.open("l.txt",ios_base::in|ios_base::out|ios_base::binary); long id=getId("Enter Id to update");//获取要更新的编号 updateFile.seekg((id-1)*sizeof(Leader));//移动指针到正确的位置 Leader person;

//读取记录 updateFile.read((char*)&person.Id,sizeof(person.Id));

updateFile.read((char*)&person.fullname,sizeof(person.fullname)); updateFile.read((char*)&person.Age,sizeof(person.Age)); updateFile.read((char*)&person.department,sizeof(person.department)); updateFile.read((char*)&person.title,sizeof(person.title));

if(person.getId()!=0)//更新记录

{//显示旧记录

cout<<"显示旧值"<<endl; person.show();

char choice;

cout<<"enter your choice"<<endl;

cout<<"a: update aged: update departmentt:title"<<endl; cin>>choice;

switch(choice)

{

case 'a':

{

}

case 'd':

{

int age;

cout<<"Inter new age:"; cin>>age; person.setAge(age); break;

char newdpa[20];

cout<<"Inter new department:"; cin>>newdpa; person.setDepartment(newdpa); break;

}

case 't':

{

}

}

char tit[20]; cout<<"Inter new title:"; cin>>tit; person.setTitle(tit); break;

}

else

{

}


cout<<"显示新值"<<endl; person.show();

//用新的纪录覆盖旧的记录 updateFile.write((char*)&person.Id,sizeof(person.Id)); updateFile.write((char*)&person.fullname,sizeof(person.fullname)); updateFile.write((char*)&person.Age,sizeof(person.Age)); updateFile.write((char*)&person.department,sizeof(person.department)); updateFile.write((char*)&person.title,sizeof(person.title));

cerr<<"Id:"<<id<<"has no information."<<endl;

updateFile.close(); updateFile.clear();

}

void Oper:: UpdateChairmanFile(fstream& updateFile)//更新 chairman 文件

{

updateFile.open("c.txt",ios_base::in|ios_base::out|ios_base::binary); long id=getId("Enter Id to update");//获取要更新的编号 updateFile.seekg((id-1)*sizeof(Chairman));//移动指针到正确的位置 Chairman person;

//读取记录 updateFile.read((char*)&person.Id,sizeof(person.Id));

updateFile.read((char*)&person.fullname,sizeof(person.fullname)); updateFile.read((char*)&person.Age,sizeof(person.Age)); updateFile.read((char*)&person.specialty,sizeof(person.specialty)); updateFile.read((char*)&person.position,sizeof(person.position)); updateFile.read((char*)&person.department,sizeof(person.department)); updateFile.read((char*)&person.title,sizeof(person.title));

if(person.getId()!=0)//更新记录

{//显示旧记录

cout<<"显示旧值"<<endl;

person.show(); char choice;

cout<<"enter your choice"<<endl;

cout<<"a: update agep:positiond: update departmentt:title"<<endl; cin>>choice;

switch(choice)

{

case 'a':

{

}

case 'p':

{

}

case 'd':

{

}

case 't':

{

}

}

int age;

cout<<"Inter new age:"; cin>>age; person.setAge(age); break;

char newpos[20]; cout<<"Inter new position:"; cin>>newpos; person.setPosition(newpos); break;

char newdpa[20];

cout<<"Inter new department:"; cin>>newdpa; person.setDepartment(newdpa); break;

char tit[20]; cout<<"Inter new title:"; cin>>tit; person.setTitle(tit); break;

cout<<"显示新值"<<endl; person.show();

//用新的纪录覆盖旧的记录 updateFile.write((char*)&person.Id,sizeof(person.Id));

}

else

{

}


updateFile.write((char*)&person.fullname,sizeof(person.fullname)); updateFile.write((char*)&person.Age,sizeof(person.Age)); updateFile.write((char*)&person.specialty,sizeof(person.specialty)); updateFile.write((char*)&person.position,sizeof(person.position)); updateFile.write((char*)&person.department,sizeof(person.department)); updateFile.write((char*)&person.title,sizeof(person.title));

cerr<<"Id:"<<id<<"has no information."<<endl;

updateFile.close(); updateFile.clear();

}

void Oper:: NewEngineerRecord(fstream& insertFile)//插入,创建 Engineer 文件

{

insertFile.open("e.txt",ios_base::in | ios_base::out|ios_base::binary); long id=getId("Enter Id to add");//获取要添加的编号 insertFile.seekg((id-1)*sizeof(Engineer));//移动指针到正确的位置 Engineer person;

//读取编号 insertFile.read((char*)&person.Id,sizeof(person.Id)); if(person.getId()==0 )//如果记录不存在,则创建记录

{

//Engineer person;

person.set();//创建 Engineer 对象 insertFile.seekp((id-1)*sizeof(Engineer));//插入位置 insertFile.write((char*)&person.Id,sizeof(person.Id));

insertFile.write((char*)&person.fullname,sizeof(person.fullname)); insertFile.write((char*)&person.Age,sizeof(person.Age)); insertFile.write((char*)&person.specialty,sizeof(person.specialty)); insertFile.write((char*)&person.position,sizeof(person.position));

}

else

{

}

cerr<<"Id:"<<id<<"already contains information."<<endl;

insertFile.close(); insertFile.clear();

}

void Oper:: NewLeaderRecord(fstream& insertFile)//插入,创建 Leader 文件

{

insertFile.open("l.txt",ios_base::in | ios_base::out|ios_base::binary); long id=getId("Enter Id to add");//获取要添加的编号 insertFile.seekg((id-1)*sizeof(Leader));//移动指针到正确的位置 Leader person;

//读取编号 insertFile.read((char*)&person.Id,sizeof(person.Id)); if(person.getId()==0 )//如果记录不存在,则创建记录

{

Leader person;

person.set();//创建 leader 对象 insertFile.seekp((id-1)*sizeof(Leader));//插入位置 insertFile.write((char*)&person.Id,sizeof(person.Id));

insertFile.write((char*)&person.fullname,sizeof(person.fullname)); insertFile.write((char*)&person.Age,sizeof(person.Age)); insertFile.write((char*)&person.department,sizeof(person.department)); insertFile.write((char*)&person.title,sizeof(person.title));

}

else

{

}

cerr<<"Id:"<<id<<"already contains information."<<endl;

insertFile.close(); insertFile.clear();

}

void Oper:: NewChairmanRecord(fstream& insertFile)//插入,创建 Chairman 文件

{

insertFile.open("c.txt",ios_base::in | ios_base::out|ios_base::binary); long id=getId("Enter Id to add");//获取要添加的编号 insertFile.seekg((id-1)*sizeof(Chairman));//移动指针到正确的位置 Chairman person;

//读取编号 insertFile.read((char*)&person.Id,sizeof(person.Id)); if(person.getId()==0 )//如果记录不存在,则创建记录

{

Chairman person;

person.set();//创建 Chariman 对象 insertFile.seekp((id-1)*sizeof(Chairman));//插入位置 insertFile.write((char*)&person.Id,sizeof(person.Id));

insertFile.write((char*)&person.fullname,sizeof(person.fullname)); insertFile.write((char*)&person.Age,sizeof(person.Age)); insertFile.write((char*)&person.specialty,sizeof(person.specialty));

insertFile.write((char*)&person.position,sizeof(person.position)); insertFile.write((char*)&person.department,sizeof(person.department)); insertFile.write((char*)&person.title,sizeof(person.title));

}

else

{

}

cerr<<"Id:"<<id<<"already contains information."<<endl;

insertFile.close(); insertFile.clear();

}

void Oper:: DeleteEngineerRecord(fstream& deleteFile)//删除 engineer 记录

{

deleteFile.open("e.txt",ios_base::in | ios_base::out|ios_base::binary); long id=getId("Enter Id to delete");//获取要删除的编号 deleteFile.seekg((id-1)*sizeof(Engineer));//移动指针到正确的位置 Engineer person; deleteFile.read((char*)&person.Id,sizeof(person.Id)); deleteFile.read((char*)&person.fullname,sizeof(person.fullname)); deleteFile.read((char*)&person.Age,sizeof(person.Age)); deleteFile.read((char*)&person.specialty,sizeof(person.specialty)); deleteFile.read((char*)&person.position,sizeof(person.position));

if(person.getId()!=0)//如果记录存在,则删除记录(用空白记录代替)

{

}

else

{

}


Engineer blankper;

deleteFile.seekg((id-1)*sizeof(Engineer));//移动指针到正确的位置 deleteFile.write((char*)&blankper.Id,sizeof(blankper.Id)); deleteFile.write((char*)&blankper.fullname,sizeof(blankper.fullname)); deleteFile.write((char*)&blankper.Age,sizeof(blankper.Age)); deleteFile.write((char*)&blankper.specialty,sizeof(blankper.specialty)); deleteFile.write((char*)&blankper.position,sizeof(blankper.position)); cout<<"Id: "<<id<<"is deleted."<<endl;

cerr<<"Id: "<<id<<"is empty."<<endl;

deleteFile.close(); deleteFile.clear();

}

void Oper:: DeleteLeaderRecord(fstream& deleteFile)//删除 Leader 记录

{

deleteFile.open("l.txt",ios_base::in | ios_base::out|ios_base::binary); long id=getId("Enter Id to delete");//获取要删除的编号 deleteFile.seekg((id-1)*sizeof(Leader));//移动指针到正确的位置 Leader person;

deleteFile.read((char*)&person.Id,sizeof(person.Id)); deleteFile.read((char*)&person.fullname,sizeof(person.fullname)); deleteFile.read((char*)&person.Age,sizeof(person.Age)); deleteFile.read((char*)&person.department,sizeof(person.department)); deleteFile.read((char*)&person.title,sizeof(person.title)); if(person.getId()!=0)//如果记录存在,则删除记录(用空白记录代替)

{

}

else

{

}


Leader blankper;

deleteFile.seekg((id-1)*sizeof(Leader));//移动指针到正确的位置 deleteFile.write((char*)&blankper.Id,sizeof(blankper.Id)); deleteFile.write((char*)&blankper.fullname,sizeof(blankper.fullname)); deleteFile.write((char*)&blankper.Age,sizeof(blankper.Age)); deleteFile.write((char*)&blankper.department,sizeof(blankper.department)); deleteFile.write((char*)&blankper.title,sizeof(blankper.title));

cout<<"Id: "<<id<<"is deleted."<<endl;

cerr<<"Id: "<<id<<"is empty."<<endl;

deleteFile.close(); deleteFile.clear();

}

void Oper:: DeleteChairmanRecord(fstream& deleteFile)//删除 Chairman 记录

{

deleteFile.open("c.txt",ios_base::in | ios_base::out|ios_base::binary); long id=getId("Enter Id to delete");//获取要删除的编号 deleteFile.seekg((id-1)*sizeof(Chairman));//移动指针到正确的位置 Chairman person; deleteFile.read((char*)&person.Id,sizeof(person.Id)); deleteFile.read((char*)&person.fullname,sizeof(person.fullname)); deleteFile.read((char*)&person.Age,sizeof(person.Age)); deleteFile.read((char*)&person.specialty,sizeof(person.specialty)); deleteFile.read((char*)&person.position,sizeof(person.position));

deleteFile.read((char*)&person.department,sizeof(person.department)); deleteFile.read((char*)&person.title,sizeof(person.title)); if(person.getId()!=0)//如果记录存在,则删除记录(用空白记录代替)

{

}

else

{

}

Chairman blankper;

deleteFile.seekg((id-1)*sizeof(Chairman));//移动指针到正确的位置 deleteFile.write((char*)&blankper.Id,sizeof(blankper.Id)); deleteFile.write((char*)&blankper.fullname,sizeof(blankper.fullname)); deleteFile.write((char*)&blankper.Age,sizeof(blankper.Age)); deleteFile.write((char*)&blankper.specialty,sizeof(blankper.specialty)); deleteFile.write((char*)&blankper.position,sizeof(blankper.position)); deleteFile.write((char*)&blankper.department,sizeof(blankper.department)); deleteFile.write((char*)&blankper.title,sizeof(blankper.title));

cout<<"Id: "<<id<<"is deleted."<<endl;

cerr<<"Id: "<<id<<"is empty."<<endl;

deleteFile.close(); deleteFile.clear();

}

void Oper:: queryEngineerRecord(fstream& queryFile)//查询 engineer 信息

{

queryFile.open("e.txt",ios_base::in | ios_base::out|ios_base::binary); long id=getId("Enter Id to delete");//获取要查询的编号 queryFile.seekg((id-1)*sizeof(Engineer));//移动指针到正确的位置 Engineer person; queryFile.read((char*)&person.Id,sizeof(person.Id)); queryFile.read((char*)&person.fullname,sizeof(person.fullname)); queryFile.read((char*)&person.Age,sizeof(person.Age)); queryFile.read((char*)&person.specialty,sizeof(person.specialty)); queryFile.read((char*)&person.position,sizeof(person.position)); person.show(); // 显示信息

queryFile.close(); queryFile.clear();

}

void Oper:: queryLeaderRecord(fstream& queryFile)//查询 Leader 信息

{

queryFile.open("l.txt",ios_base::in | ios_base::out|ios_base::binary); long id=getId("Enter Id to delete");//获取要查询的编号 queryFile.seekg((id-1)*sizeof(Leader));//移动指针到正确的位置 Leader person;

queryFile.read((char*)&person.Id,sizeof(person.Id)); queryFile.read((char*)&person.fullname,sizeof(person.fullname)); queryFile.read((char*)&person.Age,sizeof(person.Age)); queryFile.read((char*)&person.department,sizeof(person.department));

queryFile.read((char*)&person.title,sizeof(person.title)); person.show(); // 显示信息

queryFile.close(); queryFile.clear();

}

void Oper::queryChairmanRecord(fstream& queryFile)//查询 Leader 信息

{

queryFile.open("c.txt",ios_base::in | ios_base::out|ios_base::binary); long id=getId("Enter Id to delete");//获取要查询的编号 queryFile.seekg((id-1)*sizeof(Chairman));//移动指针到正确的位置 Chairman person; queryFile.read((char*)&person.Id,sizeof(person.Id)); queryFile.read((char*)&person.fullname,sizeof(person.fullname)); queryFile.read((char*)&person.Age,sizeof(person.Age)); queryFile.read((char*)&person.specialty,sizeof(person.specialty)); queryFile.read((char*)&person.position,sizeof(person.position));

queryFile.read((char*)&person.department,sizeof(person.department)); queryFile.read((char*)&person.title,sizeof(person.title));

person.show(); // 显示信息 queryFile.close(); queryFile.clear();

}

voidOper:: CreateFile()// 创建文件

{

char choice;

cout<<"Enter the categary of the menu:\n"<<"e: Engineer l: Leader c: Chairman q:Quit\n"; cin>>choice;

while(choice!='q')

{

while(strchr("elcq",choice)==NULL)

{

cout<<"Please enter e,l,c or q:"; cin>>choice;

}

if(choice=='q') break; switch(choice)

{

case 'e':

{

}

CreateEngineerFile(); //创建文 Engineer 件

break;

case 'l':

{

CreateLeaderFile(); //创建 leader 文件

break;

}

case 'c':

{

}

}

CreateChairmanFile(); //创建 chairman 文件

break;

q:Quit\n";

}

}


cout<<"Enter the category of the menu:\n"<<"e: Engineer l: Leader c: Chairman cin>>choice;

void Oper:: updateRecord(fstream& updateFile)//更新文件

{

char choice;

cout<<"Enter the category of the menu:\n"<<"e: Engineer l: Leader c: Chairman q:Quit\n"; cin>>choice;

while(choice!='q')

{

while(strchr("elcq",choice)==NULL)

{

cout<<"Please enter e,l,c or q:"; cin>>choice;

}

if(choice=='q') break; switch(choice)

{

case 'e':

case 'l':

case 'c':

{

UpdateEngineer (updateFile) ; break;

}

{

UpdateLeaderFile(updateFile); break;

}

{

UpdateChairmanFile(updateFile); break;

}

}

q:Quit\n";

}


cout<<"Enter the category of the menu:\n"<<"e: Engineer l: Leader c: Chairman cin>>choice;

updateFile.close(); updateFile.clear();

}

long Oper:: getId(const char* content ) //得到编号

{

long id; do{

cout<<"enter(1-100)"<<endl; cin>>id;

} while(id<1||id>100); return id;

}

//创建和插入记录

void Oper:: newRecord(fstream& insertFile)

{

char choice;

cout<<"Enter the category of the menu:\n"<<"e: Engineer l: Leader c: Chairman q:Quit\n"; cin>>choice;

while(choice!='q')

{

while(strchr("elcq",choice)==NULL)

{

cout<<"Please enter e,l,c or q:"; cin>>choice;

}

if(choice=='q') break; switch(choice)

{

case 'e':

{

}

case 'l':

NewEngineerRecord(insertFile); break;

case 'c':

}


{

NewLeaderRecord(insertFile); break;

}

{

NewChairmanRecord(insertFile); break;

}

q:Quit\n";

}


cout<<"Enter the category of the menu:\n"<<"e: Engineer l: Leader c: Chairman cin>>choice;

insertFile.close(); insertFile.clear();

}

void Oper:: deleteRecord(fstream& deleteFile)//删除记录

{

char choice;

cout<<"Enter the category of the menu:\n"<<"e: Engineer l: Leader c: Chairman q:Quit\n"; cin>>choice;

while(choice!='q')

{

while(strchr("elcq",choice)==NULL)

{

cout<<"Please enter e,l,c or q:"; cin>>choice;

}

if(choice=='q') break; switch(choice)

{

case 'e':

case 'l':

case 'c':

{

DeleteEngineerRecord( deleteFile); break;

}

{

DeleteLeaderRecord( deleteFile); break;

}

{

DeleteLeaderRecord( deleteFile); break;

}

q:Quit\n";

}


}

cout<<"Enter the category of the menu:\n"<<"e: Engineer l: Leader c: Chairman cin>>choice;

deleteFile.close(); deleteFile.clear();

}

void Oper:: queryRecord(fstream& queryFile)//查询信息

{

char choice;

cout<<"Enter the category of the menu:\n"<<"e: Engineer l: Leader c: Chairman q:Quit\n"; cin>>choice;

while(choice!='q')

{

while(strchr("elcq",choice)==NULL)

{

cout<<"Please enter e,l,c or q:"; cin>>choice;

}

if(choice=='q') break; switch(choice)

{

case 'e':

{

}

case 'l':

{

}

case 'c':

{

}

queryEngineerRecord(queryFile); break;

queryLeaderRecord(queryFile); break;

queryChairmanRecord(queryFile); break;

q:Quit\n";

}


}

cout<<"Enter the category of the menu:\n"<<"e: Engineer l: Leader c: Chairman cin>>choice;

queryFile.close(); queryFile.clear();

}

#endif Chairmanuse.cpp #include"oper.h" #include<string> void welcome()

{

cout << "\n\n\n";

cout << "/ ̄\_/ ̄\Welcome"<<endl;

cout << "▏▏▔▔▔▔\" << endl;
cout << "\ /﹨to" << endl;
cout << "/)" << endl;
cout << "the" << endl;
cout << "▔█◤" << endl;
cout << "◣\__/Staff" << endl;
cout << "██◣/" << endl;
cout << "██████████◣Management" <<
endl;
cout << " cout << " cout << " cout << "███████████▆▄█◣◥█████████◤\██ ████████◤\██ ██████◤" << endl;System " << endl; " << endl;(*^ ^*) " <<
endl;cout << "│█████◤|" << endl;
}
int main()
{
Oper op;char ch;
welcome();
system("pause");
system("cls");
cout<<"Create Data File?(Y/N)"<<endl;

cin>>ch;

if(ch=='Y'||ch=='y')op.CreateFile();//创建文件 fstream ioFile;

system("pause");

system("cls");

cout<<"Enteryourchoice:\n"<<"a:***AddStaffu:***UpdateStaffq:***QueryStaff d:***DeleteStaff e:***EXIT\n";

cin>>ch; while(ch!='e')

{

while(strchr("auqde",ch)==NULL)

{

cout<<"Please enter a a,u,q,d,or e:"; cin>>ch;

}

if(ch=='e') break; switch(ch)

{

case 'a':

case 'u':

case 'q':

case 'd':

{

op.newRecord(ioFile); ioFile.close(); ioFile.clear(); system("pause");

system("cls"); break;

}

{

op.updateRecord(ioFile); ioFile.close(); ioFile.clear(); system("pause");

system("cls"); break;

}

{

op.queryRecord(ioFile); ioFile.close(); ioFile.clear(); system("pause");

system("cls"); break;

}

{

op.deleteRecord(ioFile);

case 'e':


ioFile.close(); ioFile.clear(); system("pause");

system("cls"); break;

}

break;

}

e:EXIT\n";

}


cout<<"Enter your choice:\n"<<"a: ADDu: UPDATE q: QUERY d:DELETE cin>>ch;

system("pause"); return 0;

}

运行结果:

主界面
然后刷屏继续

  1. 创建文件(engineer、leader、chairman)


然后刷屏继续

  1. 添加员工的信息


然后刷屏继续

  1. 更新员工的信息

然后刷屏继续

  1. 查询员工的信息


然后刷屏继续

  1. 删除员工的信息


然后刷屏继续

  1. 显示删除了的员工的信息

问题解决:

这个题设计的类比较多,再加上要以文件处理的方式实现功能,明显感觉到比以前的上机作业要难很多,在做的过程中遇到了很多以往没有出现的问题,尤其是文件部分。

  1. 显示 Chairman 对象时,出现了二义性问题,后来通过对成员访问加限定符解决了问题。
  2. 直接用 read()和 write()函数将一个对象读写到文件中时出现了错误,通过看书查资料才知道,因为类中有虚方法,则也将复制隐藏指针(该指针指向虚函数的指针表)。由于下次运行程序时,虚函数可能在不同的位置,因此将文件的旧指针信息复制到对象中,将可能造成混乱。
  3. 调用 Oper 中的成员函数对员工的信息管理时出现了错误,因为员工的数据成员是私有的,将 Oper 类设置成各类的友元类解决了。
  4. 用一个流对象打开多个文件时出现了错误,因为对一个文件操作完时,流的状态可能设置了failbit 等位,因此需要将流对象与此文件断开,并且要用clear()重新设置成有效状态。
  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值