大学里的工作人员(Employee)可以分为管理人员(Staff)和教师(Faculty)两类。运用C++语言和面向对象编程思想,设计一个学校教职工管理系统,要求实现如下功能:创建职工信息数据,包括职工编号、姓名、 性别、出生时间、部门、参加工作时间、年龄和工龄等(须根据当前时间计算得到),初始模拟数据通过文本文件创建(记录不少于6个),通过程序读文件载入。能够查找、添加或删除一个员工;浏览全部教职工信息(管理人员和教师分开);统计职工的平均年龄和工龄等(管理人员和教师可分开统计)。
代码如下:
//教职工信息管理系统
#include<iostream>
#include<string> //用string定义数据类型
#include<vector> // vector数组
#include<fstream> //文件
#include<ctime> //时间
using namespace std;
//创建基类 工作人员类
class Employee{
public:
string type; //类型
int number; //编号
string name; //姓名
string gender; //性别
int birth; //出生时间
string department; //部门
int worktime; //参加工作时间
int age; //年龄
int workage; //工龄
//构造函数
Employee(int num=0,string n=" ",string g=" ",int b=0, string d=" ",int wt=0){
//type = "employee";
number =num;
name = n;
gender = g;
birth = b;
department = d;
worktime = wt;
}
//析构函数
~Employee() {
}
//虚函数
virtual void display(){
}
void Calculate(){//计算年龄工龄
int nowyear;
time_t curtime;
time(&curtime);
tm *nowtime = localtime(&curtime);
nowyear= 1900 + nowtime->tm_year;
age=nowyear -birth;
workage=nowyear -worktime;
}
};
//继承工作人员类 教师类
class Faculty: public Employee{
private:
string title;//职称
public:
//构造函数
Faculty(int num=0,string n=" ",string g=" ",int b=0, string d=" ",int wt=0, string t=""):Employee(num,n,g,b,d,wt) {
type="faculty";
title = t;
}
~Faculty(){
}
virtual void display()
{
cout<<"\t"<<number<<"\t"<<name<<"\t"<<gender<<"\t"<<birth<<"\t\t"<<department<<"\t"<<worktime<<"\t\t"<<title<<"\t"
<<age<<"\t"<<workage<<endl;
}
friend istream& operator>>(istream& in, Faculty& f);
friend ostream& operator<<(ostream& out, Faculty& f);
};
istream& operator>>(istream& in, Faculty& f) {
in>>f.number>>f.name>>f.gender>>f.birth>>f.department>>f.worktime>>f.title;
return in;
}
ostream& operator<<(ostream& out, Faculty& f){
out<<f.type<<"\t"<<f.number<<"\t"<<f.name<<"\t"<<f.gender<<"\t"<<f.birth<<"\t"<<f.department<<"\t"<<f.worktime<<"\t"
<<f.title<<"\t"<<f.age<<"\t"<<f.workage<<endl;
return out;
}
//继承工作人员类 管理人员类
class Staff: public Employee{
private:
string level;//级别
public:
//构造函数
Staff(int num=0,string n=" ",string g=" ",int b=0, string d=" ",int wt=0, string l=" "):Employee(num,n,g,b,d,wt) {
type="staff";
level=l;
}
~Staff(){
}
virtual void display()
{
cout<<"\t"<<number<<"\t"<<name<<"\t"<<gender<<"\t"<<birth<<"\t\t"<<department<<"\t"<<worktime<<"\t\t"
<<level<<"\t"<<age<<"\t"<<workage<<endl;
}
friend istream& operator>>(istream& in, Staff& s);
friend ostream& operator<<(ostream& out, Staff& s);
};
istream& operator>>(istream& in, Staff& s) {
in>> s.number>>s.name>> s.gender>> s.birth >> s.department >> s.worktime>>s.level;
return in;
}
ostream& operator<<(ostream& out, Staff& s) {
out<<s.type<<"\t"<<s.number<<"\t"<<s.name<<"\t"<<s.gender<<"\t"<<s.birth<<"\t"<<s.department<<"\t"<<s.worktime<<"\t"
<<s.level<<"\t"<<s.age<<"\t"<<s.workage<<endl;
return out;
}
//继承工作人员类 辅导员类
class Counselor: public Employee{
private:
string level;//级别
public:
//构造函数
Counselor(int num=0,string n=" ",string g=" ",int b=0, string d=" ",int wt=0, string l=" "):Employee(num,n,g,b,d,wt) {
type="counselor";
level=l;
}
~Counselor(){
}
virtual void display()
{
cout<<"\t"<<number<<"\t"<<name<<"\t"<<gender<<"\t"<<birth<<"\t\t"<<department<<"\t"<<worktime<<"\t\t"
<<level<<"\t"<<age<<"\t"<<workage<<endl;
}
friend istream& operator>>(istream& in, Counselor& c);
friend ostream& operator<<(ostream& out, Counselor& c);
};
istream& operator>>(istream& in, Counselor& c) {
in>>c.number>>c.name>> c.gender>> c.birth >>c.department >>c.worktime>>c.level;
return in;
}
ostream& operator<<(ostream& out, Counselor& c) {
out<<c.type<<"\t"<<c.number<<"\t"<<c.name<<"\t"<<c.gender<<"\t"<<c.birth<<"\t"<<c.department<<"\t"<<c.worktime<<"\t"
<<c.level<<"\t"<<c.age<<"\t"<<c.workage<<endl;
return out;
}
//管理类
class Administer{
public:
//打开文件
bool Open(vector<Employee*>&emp)
{
emp.clear();
ifstream ifs("教职工信息.txt");
if (!ifs)
return false;
string type;
while (ifs >> type)//读取文件中的数据
{
if (type == "faculty"){
Faculty *f = new Faculty();
ifs >> *f;
f->Calculate();
emp.push_back(f);
}else if (type == "staff"){
Staff *s = new Staff();
ifs >> *s;
s->Calculate();
emp.push_back(s);
}else if (type == "counselor"){
Counselor *c= new Counselor();
ifs >> *c;
c->Calculate();
emp.push_back(c);
}
}
ifs.close();
return true;
}
//存入文件
void Save(const vector<Employee*>&emp)
{
ofstream ofs("教职工信息.txt");
for (int i = 0; i < (int)emp.size(); i++)
{
if (emp[i]->type== "faculty"){
ofs<<*(Faculty*)emp[i]<<endl;
}
else if (emp[i]->type == "staff"){
ofs<<*(Staff*)emp[i]<<endl;
}
else if (emp[i]->type == "counselor"){
ofs<<*(Counselor*)emp[i]<<endl;
}
}
ofs.close();
}
//添加人员 1教师 2 管理人员 3 辅导员
void Add(vector<Employee*>&emp)
{
Administer a;
while(true)
{
int t;
cout<<endl<<"\t请选择录入人员类型"<<endl;
cout<<"\t1.教师人员"<<endl;
cout<<"\t2.管理人员"<<endl;
cout<<"\t3.辅导员"<<endl;
cout<<"\t0.退出录入功能"<<endl;
cout<<" ";
cin>>t;
if(t==0){
break;
}
cout<<endl<<endl;
Faculty *f = new Faculty();
Staff *s = new Staff();
Counselor *c = new Counselor();
switch(t){
case 1:
cout<<"\t请依次输入编号 姓名 性别 出生时间 部门 参加工作时间 职称 "<<endl;
cin>>*f;
f->Calculate();
emp.push_back(f);
cout<<"\t录入成功!"<<endl<<endl;
break;
case 2:
cout<<"\t请依次输入编号 姓名 性别 出生时间 部门 参加工作时间 管理级别 "<<endl;
cin>>*s;
s->Calculate();
emp.push_back(s);
cout<<"\t录入成功!"<<endl<<endl;
break;
case 3:
cout<<"\t请依次输入编号 姓名 性别 出生时间 部门 参加工作时间 级别 "<<endl;
cin>>*c;
c->Calculate();
emp.push_back(c);
cout<<"\t录入成功!"<<endl<<endl;
break;
default:
cout<<"\t请输入正确的数字"<<endl;
break;
}
}
a.Save(emp);
}
//删除人员 1 编号 2 姓名
void Delete(vector<Employee*>&emp){
Administer a;
while (true)
{
int t;
string name;
cout<<endl<<"\t请选择删除方式"<<endl;
cout<<"\t1.按员工编号删除"<<endl;
cout<<"\t2.按姓名删除"<<endl;
cout<<"\t0.退出删除功能"<<endl;
cout<<" ";
cin>>t;
if(t==0){
break;
}
cout<<endl<<endl;
switch(t){
case 1:
int number;
cout<<"\t请输入要删除员工编号"<<endl;
cout<<"\t(输入“-1”退出删除):"<<endl;
cout<<" ";
cin>>number;
if (number==-1){
break;
}
int i;
for (i=0; i<(int)emp.size();i++)
{
if (emp[i]->number==number){
break;
}
}
if (i < (int)emp.size())
{
emp.erase(emp.begin() + i);
cout<<"\t删除成功!"<<endl<<endl;
}else{
cout<<"\t错误,删除失败!"<<endl<<endl;
}
break;
case 2:
//string name;
cout<<"\t请输入要删除员工姓名"<<endl;
cout<<"\t(输入“-1”退出删除):"<<endl;
cout<<" ";
cin >> name;
if (name =="-1"){
break;
}
for ( i = 0; i < (int)emp.size(); i++)
{
if (emp[i]->name==name){
break;
}
}
if (i < (int)emp.size())
{
emp.erase(emp.begin() + i);
cout<<"\t删除成功!"<<endl<<endl;
}else{
cout<<"\t错误,删除失败!"<<endl<<endl;
}
break;
default:
cout<<"\t请输入正确的数字"<<endl;
break;
}
}
a.Save(emp);
}
//浏览人员信息 1教师 2 管理人员 3辅导员
void Show(const vector<Employee*>&emp)
{
while(true)
{
int in,i,t;
in=1;
cout<<endl<<"\t请选择浏览人员类型"<<endl;
cout<<"\t1.教师人员"<<endl;
cout<<"\t2.管理人员"<<endl;
cout<<"\t3.辅导员"<<endl;
cout<<"\t0.退出浏览功能"<<endl;
cout<<" ";
cin>>t;
if(t==0){
break;
}
cout<<endl<<endl;
switch(t){
case 1:
cout<<"\t教师人员信息如下:"<<endl;
cout<<"\t序号\t编号\t姓名\t性别\t出生时间\t部门\t参加工作时间\t职称\t年龄\t工龄" << endl;
for (i = 0; i < (int)emp.size(); i++){
if (emp[i]->type== "faculty"){
cout<<"\t"<<in;
emp[i]->display();
in++;
}
}
break;
case 2:
cout<<"\t管理人员信息如下:"<<endl;
cout<<"\t序号\t编号\t姓名\t性别\t出生时间\t部门\t参加工作时间\t管理级别\t年龄\t工龄"<<endl;
for(i=0; i<(int)emp.size();i++){
if(emp[i]->type == "staff"){
cout<<"\t"<<in;
emp[i]->display();
in++;
}
}
break;
case 3:
cout<<"\t辅导员信息如下:"<<endl;
cout<<"\t序号\t编号\t姓名\t性别\t出生时间\t部门\t参加工作时间\t级别\t年龄\t工龄"<<endl;
for(i=0; i<(int)emp.size();i++){
if(emp[i]->type == "counselor"){
cout<<"\t"<<in;
emp[i]->display();
in++;
}
}
break;
default:
cout<<"\t请输入正确的数字"<<endl;
break;
}
}
}
//查找员工信息 1 姓名 2 编号
void Search(const vector<Employee*>&emp)
{
while (true)
{
bool find=false;
string name;
int number;
int t;
cout<<endl<<"\t请选择查找人员的方式"<<endl;
cout<<"\t1.按姓名查找"<<endl;
cout<<"\t2.按编号查找"<<endl;
cout<<"\t0.退出查找功能"<<endl;
cout<<" ";
cin>>t;
if(t==0){
break;
}
cout<<endl<<endl;
switch(t){
case 1:
cout<<"\t请输入要查找员工姓名"<<endl;
cout<<"\t(输入“-1”退出查找):"<<endl;
cin>>name;
if (name == "-1"){
break;
}
cout<<"\t查询信息如下:"<<endl;
cout<<"\t编号\t姓名\t性别\t出生时间\t部门\t参加工作时间\t职称/级别\t年龄\t工龄"<<endl;
for(int i=0;i<(int)emp.size();i++){
if(emp[i]->name==name){
emp[i]->display();
find = true;
}
}
if(!find)
cout<<"\t很抱歉,没有查询到相关人员!"<<endl<<endl;
break;
case 2:
cout<<"\t请输入要查找员工编号"<<endl;
cout<<"\t(输入“-1”退出查找):"<<endl;
cin>>number;
if (number==-1){
break;
}
//遍历查找
cout<<"\t查询信息如下:" << endl;
cout<<"\t编号\t姓名\t性别\t出生时间\t部门\t参加工作时间\t职称/级别\t年龄\t工龄" << endl;
for(int i=0;i<(int)emp.size();i++){
if(emp[i]->number==number){
emp[i]->display();
find = true;
}
}
if(!find)
cout<<"\t很抱歉,没有查询到相关人员!"<<endl<<endl;
break;
default:
cout<<"\t请输入正确的数字"<<endl;
break;
}
}
}
//平均年龄
void CalAge(const vector<Employee*>&emp)
{
int num, i;
double avg = 0;
num = 0;
for (i = 0; i < (int)emp.size(); i++)
{
avg += emp[i]->age;
num++;
}
if (num != 0)
{
cout<<"\t教职工人员平均年龄:"<<avg/num <<endl<<endl;
}
}
//平均工龄
void CalWorkYear(const vector<Employee*>&emp)
{
int num, i;
double avg = 0;
num=0;
for (i = 0; i < (int)emp.size(); i++)
{
avg += emp[i]->workage;
num++;
}
if (num != 0)
{
cout<<"\t教职工人员平均工龄:"<<avg/num <<endl<<endl;
}
}
};
//主函数
int main(){
Administer a;
vector <Employee*>emp;
a.Open(emp);
int n = 0;
while (true)
{
cout<<"******************************************************"<<endl;
cout<<"**************欢迎使用教职工信息管理系统**************"<<endl<<endl;
cout<<"\t具体功能如下:"<<endl<<endl;
cout<<"\t\t"<<" 1、添加人员"<<endl;
cout<<"\t\t"<<" 2、删除人员"<<endl;
cout<<"\t\t"<<" 3、浏览人员信息"<<endl;
cout<<"\t\t"<<" 4、查找员工信息"<<endl;
cout<<"\t\t"<<" 5、教职工平均年龄"<<endl;
cout<<"\t\t"<<" 6、教职工平均工龄"<<endl;
cout<<"\t\t"<<" 7、退出系统"<<endl<<endl;
cout<<"\t请选择您需要的功能:"<<endl;
cout<<" ";
cin>>n;
switch (n) {
case 1://添加人员
a.Add(emp);
system("pause");
system("cls");
break;
case 2://删除人员
a.Delete(emp);
system("pause");
system("cls");
break;
case 3://浏览人员信息
a.Show(emp);
system("pause");
system("cls");
break;
case 4://按姓名查找
a.Search(emp);
system("pause");
system("cls");
break;
case 5://平均年龄
a.CalAge(emp);
system("pause");
system("cls");
break;
case 6://平均工龄
a.CalWorkYear(emp);
system("pause");
system("cls");
break;
case 7://退出
cout<<"*********************欢迎下次使用*********************"<<endl;
cout<<"******************************************************"<<endl;
exit(0);
break;
default:
cout<<"\t请输入正确的数字"<<endl;
system("pause");
system("cls");
break;
}
}
a.Save(emp);
return 0;
}