高校人员信息管理系统
问题描述
根据高校人员的特点,先设计一个高校人员抽象类,应包含编号、姓名、年龄、性别、系部、联系方式等数据成员,以及显示、统计等成员函数。然后分别设计教师类、学生类和机关人员类,它们的基类均为高校人员抽象类。
功能要求
创建人员对象,通过链表或对象数组的形式组织全体高校人员(可以定义一个管理类,该类具有新增、修改、删除、查询、统计及保存文件功能)。利用多态理论统一处理高校人员的信息。
#include<iostream>
#include<string.h>
#include<fstream>
#include <stdlib.h>
using namespace std;
void menu(); //函数声明
void tongji();
//全局变量
int zrs=0; //总人数
int lsrs=0; //老师人数
int xsrs=0; //学生人数
int zgrs=0; //职工人数
class People //高校人员抽象类
{
protected:
int Daihao; //代号
char Id[20]; //编号
char Name[40]; //姓名
int Age; //年龄
char Sex[5]; //性别
char Departments[10]; //系部
int Phone; //电话
People *next; // 基类的指针作为next指针
public:
friend class manage;
People(int daihao,char id[],char name[],int age,char sex[],char departments[],int phone); //构造
~People(); //析构
virtual void change(const char*s)=0; // vietual标志一个虚函数为纯虚函数
virtual void show()=0; //显示 // 抽象类 不能够实例化
virtual void save()=0; //保存 // 只能被子类覆盖
void tongji(); //统计
};
class teacher:public People //派生教师类
{
protected:
int Income; //工资
char Title[20]; //职称
char Zhuanye[20]; //专业
public:
teacher(int daihao,char id[],char name[],int age,char sex[],char departments[],int phone,int Income,char Title[20],char Zhuanye[20]);
~teacher();
void change(const char*s);
void show(); //覆盖基类的虚函数
void save();
};
class student:public People //派生学生类
{
protected:
char Banji[10];
char Zhuanye[20];
double Score;
public:
student(int daihao,char id[],char name[],int age,char sex[],char departments[],int phone,char banji[10],char zhuanye[20],double score);
~student();
void change(const char*s);
void show(); //覆盖基类的虚函数
void save();
};
class worker:public People //派生机关人员类
{
protected:
char Zhiwu[20];
char Jibie[20];
public:
worker(int daihao,char id[],char name[],int age,char sex[],char departments[],int phone,char zhiwu[20],char jibie[20]);
~worker();
void change(const char*s);
void show();
void save();
};
class manage
{
protected:
static int count; //人员数量
People *head;
public:
manage();
~manage();
void insert(People*); //添加人员信息 插入结点
void add();
void search(const char*); //按学号查询信息
void del(const char*); //删除人员信息
void change(const char *s); //修改人员信息
void dispaly(); //显示结点信息
void save(); //保存信息
};
People::People(int daihao,char id[],char name[],int age,char sex[],char departments[],int phone)
{
Daihao=daihao;
strcpy(Id,id);
strcpy(Name,name);
Age=age;
strcpy(Sex,sex);
strcpy(Departments,departments);
Phone=phone;
}
People::~People()
{}
manage::~manage()
{}
student::student(int daihao,char id[],char name[],int age,char sex[],
char departments[],int phone,char banji[],char zhuanye[],double score):People(daihao,id,name,age,sex,departments,phone)
{
strcpy(Banji,banji);
strcpy(Zhuanye,zhuanye);
Score=score;
}
teacher::teacher(int daihao,char id[],char name[],int age,char sex[],
char departments[],int phone,int income,char title[20],char zhuanye[20]):People(daihao,id,name,age,sex,departments,phone)
{
strcpy(Title,title);
strcpy(Zhuanye,zhuanye);
Income=income;
}
worker::worker(int daihao,char id[],char name[],int age,char sex[],
char departments[],int phone,char zhiwu[20],char jibie[20]):People(daihao,id,name,age,sex,departments,phone)
{
strcpy(Zhiwu,zhiwu);
strcpy(Jibie,jibie);
}
void student::change(const char*)
{
cout<<"请输入修改后的信息"<<endl;
cout<<"编号 姓名 性别 年龄 部门 电话 班级 专业 成绩"<<endl;
cin>>Id>>Name>>Age>>Sex>>Departments>>Phone>>Banji>>Zhuanye>>Score;
}
void student::show()
{
cout<<"编号:"<<Id<<endl;
cout<<"姓名:"<<Name<<endl;
cout<<"年龄:"<<Age<<endl;
cout<<"性别:"<<Sex<<endl;
cout<<"系部:"<<Departments<<endl;
cout<<"电话:"<<Phone<<endl;
cout<<"班级:"<<Banji<<endl;
cout<<"专业:"<<Zhuanye<<endl;
cout<<"成绩:"<<Score<<endl;
}
void student::save()
{
fstream file;
file.open("student.txt",ios::out); //打开student.txt文件
if(!file) //非1
{
cout<<"打开文件失败"<<endl;
}
file<<"编号:"<<Id<<" 姓名:"<<Name<<" 年龄:"<<Age<<" 性别:"<<Sex<<" 系部:"<<Departments<<" 电话:"<<Phone<<" 班级:"<<Banji<<" 专业:"<<Zhuanye<<" 成绩:"<<Score;
file.close();
file.open("t1.txt",ios::out|ios::app); //打开t1.txt文件 直接添加 而不清除后添加
if(!file) //非1
{
cout<<"打开文件失败"<<endl;
}
file<<"编号:"<<Id<<" 姓名:"<<Name<<" 年龄:"<<Age<<" 性别:"<<Sex<<" 系部:"<<Departments<<" 电话:"<<Phone<<" 班级:"<<Banji<<" 专业:"<<Zhuanye<<" 成绩:"<<Score;
file.close();
}
void teacher::save()
{
fstream file;
file.open("teacher.txt",ios::out);
if(!file) //非1
{
cout<<"打开文件失败"<<endl;
}
file<<"编号:"<<Id<<" 姓名:"<<Name<<" 年龄:"<<Age<<" 性别:"<<Sex<<" 系部:"<<Departments<<" 电话:"<<Phone<<"工资 "<<Income<<" 职称"<<Title<<" 专业:"<<Zhuanye;
file.close();
file.open("t1.txt",ios::out|ios::app);
if(!file) //非1
{
cout<<"打开文件失败"<<endl;
}
file<<"编号:"<<Id<<" 姓名:"<<Name<<" 年龄:"<<Age<<" 性别:"<<Sex<<" 系部:"<<Departments<<" 电话:"<<Phone<<"工资 "<<Income<<" 职称"<<Title<<" 专业:"<<Zhuanye;
file.close();
}
void worker::save()
{
fstream file;
file.open("worker.txt",ios::out);
if(!file) //非1
{
cout<<"打开文件失败"<<endl;
}
file<<"编号:"<<Id<<" 姓名:"<<Name<<" 年龄:"<<Age<<" 性别:"<<Sex<<" 系部:"<<Departments<<" 电话:"<<Phone<<" 职务:"<<Zhiwu<<" 级别:"<<Jibie;
file.close();
file.open("t1.txt",ios::out|ios::app);
if(!file) //非1
{
cout<<"打开文件失败"<<endl;
}
file<<"编号:"<<Id<<" 姓名:"<<Name<<" 年龄:"<<Age<<" 性别:"<<Sex<<" 系部:"<<Departments<<" 电话:"<<Phone<<" 职务:"<<Zhiwu<<" 级别:"<<Jibie;
file.close();
}
void manage::save()
{
People *node;
node=head; //头结点
while(node)
{
node->save();
node=node->next; //指向下一个结点
}
}
void teacher::change(const char*)
{
cout<<"请输入修改后的信息"<<endl;
cout<<"编号 姓名 性别 年龄 部门 电话 工资 职称 专业"<<endl;
cin>>Id>>Name>>Age>>Sex>>Departments>>Phone>>Income>>Title>>Zhuanye;
}
void teacher::show()
{
cout<<"编号:"<<Id<<endl;
cout<<"姓名:"<<Name<<endl;
cout<<"年龄:"<<Age<<endl;
cout<<"性别:"<<Sex<<endl;
cout<<"系部:"<<Departments<<endl;
cout<<"电话:"<<Phone<<endl;
cout<<"工资:"<<Income<<endl;
cout<<"职称:"<<Title<<endl;
cout<<"专业"<<Zhuanye<<endl;
}
void worker::change(const char*)
{
cout<<"请输入修改后的信息"<<endl;
cout<<"编号 姓名 性别 年龄 部门 电话 职务 级别"<<endl;
cin>>Id>>Name>>Age>>Sex>>Departments>>Phone>>Zhiwu>>Jibie;
}
void worker::show()
{
cout<<"编号:"<<Id<<endl;
cout<<"姓名:"<<Name<<endl;
cout<<"年龄:"<<Age<<endl;
cout<<"性别:"<<Sex<<endl;
cout<<"系部:"<<Departments<<endl;
cout<<"电话:"<<Phone<<endl;
cout<<"职务:"<<Zhiwu<<endl;
cout<<"级别:"<<Jibie<<endl;
}
manage::manage()
{
head=0;
}
void manage::insert( People *i)
{
People *node; //定义一个指向people类的指针
if(head==0) //如果没有头结点 则创造一个
{
head=i;
head->next=0;
}
else
{
node=head; //等于头结点
while(node->next) //当尾结点不为0时 一直循环
node=node->next;
node->next=i; //创造一个新的尾结点
i->next=0; //尾结点置0
}
}
void manage::search(const char *s) //按编号查询人员信息
{
People *node=head;
int flog=0; //起到标记的作用
while(node) //循环找符合编号的结点
{
if(strcmp(node->Id,s)==0) //如果找到
{
flog=1;
node->show(); //显示查询结果
break;
}
else //没找到则找下个结点
node=node->next;
}
if(flog) //如果找到 则flog为1
{
cout<<"查找成功"<<endl;
}
else
cout<<"未找到该编号"<<endl;
}
void manage::del(const char *s) //按学号删除人员信息
{
People *node1,*node2;
int flog=0;
node1=head;
if(strcmp(head->Id,s)==0) //如果找到 则使该结点删除
{
if(head->Daihao==1)
{
xsrs--;
zrs--;
cout<<"减少一名学生"<<endl;
}
if(head->Daihao==2)
{
lsrs--;
zrs--;
cout<<"减少一名老师"<<endl;
}
if(head->Daihao==3)
{
zgrs--;
zrs--;
cout<<"减少一名职工"<<endl;
}
head=head->next;
node1->~People(); //析 消亡
flog=1; //确认删除成功 标记为1
}
else
{
while(node1)
if(strcmp(head->Id,s)==0)
{
node2->next=node1->next;
node1->~People();
flog=1;
break;
}
else
{
node2=node1;
node1=node1->next;
}
}
if(!flog)
cout<<"未找到要删除的结点"<<endl;
else
cout<<"结点被删除"<<endl;
}
void manage::dispaly()
{
People *node;
if(head==0)
cout<<"空链表"<<endl;
else
{
node=head;
while(node)
{
node->show();
node=node->next;
}
}
}
void manage::change(const char *s) //按编号修改人员信息
{
People *node=head;
int flog=0;
while(node)
{
if(strcmp(node->Id,s)==0)
{
flog=1;
node->change(s); //显示修改结果
break;
}
else
node=node->next;
}
if(flog)
cout<<"修改成功"<<endl;
if(!flog)
cout<<"未找到该结点"<<endl;
}
void manage::add() //添加人员信息
{
int Daihao;
char Id[20];
char Name[40];
int Age;
char Sex[5];
char Departments[10];
int Phone;
int Income;
char Title[20];
char Zhuanye[20];
char Banji[10];
double Score;
char Zhiwu[20];
char Jibie[20];
int i; //选择
People *a;
cout<<" ***********请选择您需要操作的对象:*********"<<endl;
cout<<" 1.学生"<<endl;
cout<<" 2.老师"<<endl;
cout<<" 3.职工"<<endl;
cin>>i;
cout<<"请输入基础信息"<<endl;
cout<<"请输入代号:"<<endl;
cin>>Daihao;
cout<<"请输入编号:"<<endl;
cin>>Id;
cout<<"请输入姓名:"<<endl;
cin>>Name;
cout<<"请输入性别:"<<endl;
cin>>Sex;
cout<<"请输入年龄:"<<endl;
cin>>Age;
cout<<"请输入系部:"<<endl;
cin>>Departments;
cout<<"请输入电话号码:"<<endl;
cin>>Phone;
if(i==1)
{
cout<<"请输入班级、专业、成绩"<<endl;
cin>>Banji>>Zhuanye>>Score;
a=new student(Daihao,Id,Name,Age,Sex,Departments,Phone,Banji,Zhuanye,Score); //调用默认构造函数
xsrs++; //学生人数+1
cout<<"添加成功!";
}
if(i==2)
{
cout<<"请输入工资、职称、专业"<<endl;
cin>>Income>>Title>>Zhuanye;
a=new teacher(Daihao,Id,Name,Age,Sex,Departments,Phone,Income,Title,Zhuanye);
lsrs++;
cout<<"添加成功!";
}
if(i==3)
{
cout<<"请输入职务、级别"<<endl;
cin>>Zhiwu>>Jibie;
a=new worker(Daihao,Id,Name,Age,Sex,Departments,Phone,Zhiwu,Jibie);
zgrs++;
cout<<"添加成功!";
}
insert(a);
zrs++;
}
int main()
{
system("title:软件151:何金洋");
int z;
char Id[20]={0};
manage hjy; //定义一个对象
while(1)
{
system("cls");
menu(); //菜单
cin>>z;
switch(z)
{
case 1: //添加
{
hjy.add();
system("pause");
}break;
case 2: //修改
{
cout<<"请输入您需要修改的学号:";
cin>>Id;
hjy.change(Id);
system("pause");
}break;
case 3: //删除
{
cout<<"请输入需要删除的号码:";
cin>>Id;
hjy.del(Id);
system("pause");
}break;
case 4: //查询
{
cout<<"请输入需要查询的号码:"<<endl;
cin>>Id;
hjy.search(Id);
system("pause");
}break;
case 5: //统计
{
tongji();
}break;
case 6: //保存
{
hjy.save();
cout<<"保存成功!"<<endl;
system("pause");
}break;
case 7:
{
exit(0);
}
}
}
}
void menu()
{ system("cls");
cout<<" ********欢迎进入高校人员信息管理系统********"<<endl;
cout<<" ******** 请选择以下功能: ********"<<endl;
cout<<" ******** 1、新增 ********"<<endl;
cout<<" ******** 2、修改 ********"<<endl;
cout<<" ******** 3、删除 ********"<<endl;
cout<<" ******** 4、查询 ********"<<endl;
cout<<" ******** 5、统计 ********"<<endl;
cout<<" ******** 6、保存 ********"<<endl;
cout<<" ******** 7、退出 ********"<<endl;
cout<<"请输入您选择功能的序号:";
}
void tongji()
{
cout<<"总人数为:"<<zrs<<"人"<<endl;
cout<<"学生人数:"<<xsrs<<"人"<<endl;
cout<<"老师人数:"<<lsrs<<"人"<<endl;
cout<<"职工人数:"<<zgrs<<"人"<<endl;
system("pause");
}