目录
要求:
其中包括:职工编号 姓名 所在部门编号
职工管理系统具有以下功能
1.增加职工信息2.删除指定职工信息
3.查找职工
4.修改职工信息
5.打印职工信息
6.排序
7.退出系统
1、动态版本
特点:具有存储功能 -- 简单说就是你下次用还可以找到之前存储的信息
职工管理系统.cpp
#include"workerManager.h"
#include"boss.h"
#include"employee.h"
#include"manager.h"
#include"worker.h"
int main()
{
int id = 0;
WorkerManager wm;
int choice = 0;
do
{
wm.Menu();
cout << "请输入你要进行的操作:";
cin >> choice;
switch (choice)
{
case 0://退出系统
cout << "退出系统成功!" << endl;
return 0;
break;
case 1://添加职工
wm.Add_Emp();
break;
case 2://显示职工
wm.ShowEmp();
break;
case 3://删除职工
cout << "请输入你要删除的员工编号:";
cin >> id;
wm.DeleEmp(id);
break;
case 4://修改职工
cout << "请输入你要修改的员工编号:";
cin >> id;
wm.ChangeEmp(id);
break;
case 5://查找职工
cout << "请输入你要查找的员工编号:";
cin >> id;
wm.FindEmpP(id);
break;
case 6://排序职工
wm.RankEmp();
break;
case 7://清空文件
wm.CleanFile();
break;
}
} while (1);
return 0;
}
worker.h
#include<iostream>
#pragma once//防止头文件重复包含
#include<string>
#include<fstream>
using namespace std;
class Worker
{
public:
//显示个人信息
virtual void ShowInfo() = 0;
//获取岗位名称
virtual string GetDeptName() = 0;
int m_Id;//职工编号
string m_Name;//职工姓名
int m_DeptId;//职工所在部门编号
};
workerManager.cpp
#include"workerManager.h"
#include"employee.h"
#include "manager.h"
#include"boss.h"
#include"worker.h"
//构造函数
WorkerManager::WorkerManager()
{
ifstream ifs;
ifs.open(FILENAME, ios::in);
//文件不存在情况
if (!ifs.is_open())
{
cout << "文件不存在" << endl;
//初始化人数
this->m_EmpNum = 0;
//初始化文件为空标志
this->m_FileIsEmpty = true;
//初始化数组指针
this->m_EmpArray = NULL;
//关闭文件
ifs.close();
return;
}
//文件存在,但是没有记录
char ch;
ifs >> ch;//右移一个字符去读
if (ifs.eof())
{
cout << "文件内容为空" << endl;
//初始化人数
this->m_EmpNum = 0;
//初始化文件为空标志
this->m_FileIsEmpty = true;
//初始化数组指针
this->m_EmpArray = NULL;
//关闭文件
ifs.close();
return;
}
//文件存在 且记录数据
int num = this->GetNum();
cout << "当前员工个数为:" << num << endl;
this->m_EmpNum = num;
//根据职工数创建数组
this->m_EmpArray = new Worker*[this->m_EmpNum];
//初始化职工
InitEmp();
/*for (int i = 0; i < this->m_EmpNum; i++)
{
cout << "职工号为:" << this->m_EmpArray[i]->m_Id
<< " 职工名称:" << this->m_EmpArray[i]->m_Name
<< " 部门编号:" << this->m_EmpArray[i]->m_DeptId << endl;
}*/
}
//析构函数
WorkerManager::~WorkerManager()
{
if (this->m_EmpArray != NULL)
{
delete[]this->m_EmpArray;
this->m_EmpArray = NULL;
}
}
//打印菜单
void WorkerManager::Menu()
{
cout << "*****************************" << endl;
cout << "*****欢迎使用职工管理系统****" << endl;
cout << "*******0、退出管理系统*******" << endl;
cout << "*******1、增加职工信息*******" << endl;
cout << "*******2、显示职工信息*******" << endl;
cout << "*******3、删除离职职工*******" << endl;
cout << "*******4、修改职工信息*******" << endl;
cout << "*******5、查找职工信息*******" << endl;
cout << "*******6、按照编号排序*******" << endl;
cout << "*******7、清空所有文档*******" << endl;
cout << "*****************************" << endl;
}
//增加职工
void WorkerManager::Add_Emp()
{
cout << "请输入添加员工的个数:";
int addnum = 0;
cin >> addnum;
if (addnum > 0)
{
//计算新空间的大小
int newsize = this->m_EmpNum + addnum;
//开辟新的空间
Worker** newspace = new Worker*[newsize];
//将原空间下内容放在新空间下
if (this->m_EmpArray != NULL )
{
for (int i = 0; i < this->m_EmpNum; i++)
{
newspace[i] = this->m_EmpArray[i];
}
}
//输入新的数据
for (int i = 0; i < addnum; i++)
{
int id;
string name;
int Dselect;
cout << "请输入第" << i + 1 << "个新员工的编号:" ;
cin >> id;
cout << "请输入第" << i + 1 << "个新员工的名字:" ;
cin >> name;
cout << "请输入第" << i + 1 << "个新员工的岗位:" << endl;
cout << "1、职工" << endl;
cout << "2、经理" << endl;
cout << "3、老板" << endl;
cin >> Dselect;
Worker* worker = NULL;
switch (Dselect)
{
case 1://普通员工
worker = new Employee(id, name, 1);
break;
case 2://经理
worker = new Manager(id, name, 2);
break;
case 3://老板
worker = new Boss(id, name, 3);
break;
default:
system("cls");
break;
}
newspace[this->m_EmpNum + i] = worker;
}
//释放原有的空间
delete[] this->m_EmpArray;
//更新新空间的指向
this->m_EmpArray = newspace;
//更新新的个数
this->m_EmpNum = newsize;
//更新职工不为空标志
this->m_FileIsEmpty = false;
cout << "成功添加" << addnum << "名新员工!" << endl;
this->save();//保存文件
}
else
{
cout << "输入有误" << endl;
}
}
//保存职工信息
void WorkerManager::save()
{
ofstream ofs;
ofs.open(FILENAME, ios::out);
for (int i = 0; i < this->m_EmpNum; i++)
{
ofs << this->m_EmpArray[i]->m_Id << " "
<< this->m_EmpArray[i]->m_Name << " "
<< this->m_EmpArray[i]->m_DeptId << endl;
}
ofs.close();
}
//统计人数
int WorkerManager::GetNum()
{
ifstream ifs;
ifs.open(FILENAME, ios::in);
int id;
string name;
int did;
int num = 0;
while (ifs >> id && ifs >> name&& ifs >> did)
{
//记录人数
num++;
}
ifs.close();
return num;
}
//初始化员工
void WorkerManager::InitEmp()
{
ifstream ifs;
ifs.open(FILENAME, ios::in);
int id;
string name;
int did;
int index = 0;
while (ifs >> id && ifs >> name&& ifs >> did)//一行一行的读取员工的信息
{
Worker* worker = NULL;
//根据不同的部门创建不同的对象
if (did == 1)
{
worker = new Employee(id, name, did);//普通员工
}
else if (did == 2)
{
worker = new Manager(id, name, did);//经理
}
else if (did == 3)
{
worker = new Boss(id, name, did);//老板
}
//把该信息存放在数组中
this->m_EmpArray[index] = worker;
index++;
}
}
//显示职工
void WorkerManager::ShowEmp()
{
if (this->m_FileIsEmpty )
{
cout << "职工信息为空" << endl;
}
else
{
for (int i = 0; i < this->m_EmpNum; i++)
{
//利用多态调用打印
this->m_EmpArray[i]->ShowInfo();
}
}
}
//根据职工编号查找职工
int WorkerManager::FindEmp(int id)
{
for (int i = 0; i < this->m_EmpNum; i++)
{
if (this->m_EmpArray[i]->m_Id == id)
{
return i;
}
}
return -1;
}
//根据职工编号查找职工并打印
void WorkerManager::FindEmpP(int id)
{
int ret =this-> FindEmp(id);
if (-1 == ret)
{
cout << "你要查找的编号不存在!" << endl;
}
else
{
this->m_EmpArray[ret]->ShowInfo();
}
}
//删除职工
void WorkerManager::DeleEmp(int id)
{
int ret =this-> FindEmp(id);
if (ret == -1)
{
cout << "你要删除的员工编号不存在!" << endl;
return;
}
for (int i = ret; i < this->m_EmpNum-1; i++)
{
this->m_EmpArray[i] = this->m_EmpArray[i + 1];
}
this->m_EmpNum--;
this->save();//同步到文件中
cout << "删除成功!" << endl;
}
//修改员工
void WorkerManager::ChangeEmp(int id)
{
int input = 0;
string name;
int m_id;
int did;
int ret = this->FindEmp(id);
if (-1 == ret)
{
cout << "你输入的员工编号不存在!" << endl;
}
else
{
cout << "*******************" << endl;
cout << "***1、修改编号*****" << endl;
cout << "***2、修改姓名*****" << endl;
cout << "***3、修改部门*****" << endl;
cout << "*******************" << endl;
cout << "请选择你要进行的操作:";
cin >> input;
FindEmpP(input);
switch (input)
{
case 1:
cout << "请输入修改后的编号:";
cin >> m_id;
this->m_EmpArray[ret]->m_Id = m_id;
break;
case 2:
cout << "请输入修改后的姓名:";
cin >> name;
this->m_EmpArray[ret]->m_Name = name;
break;
case 3:
cout << "请输入修改后的部门编号:";
cin >> did;
this->m_EmpArray[ret]->m_DeptId = did;
break;
default:
cout << "你的输入有误!" << endl;
break;
}
cout << "修改成功" << endl;
}
}
//排序职工
void WorkerManager::RankEmp()
{
for (int i = 0; i < this->m_EmpNum - 1; i++)
{
for (int j = 0; j < this->m_EmpNum - 1 - i; j++)
{
if (this->m_EmpArray[j]->m_Id > this->m_EmpArray[j + 1]->m_Id)
{
int m_id = 0;
m_id = this->m_EmpArray[j]->m_Id;
this->m_EmpArray[j]->m_Id = this->m_EmpArray[j+1]->m_Id;
this->m_EmpArray[j + 1]->m_Id = m_id;
}
}
}
cout << "排序完成" << endl;
}
void WorkerManager::CleanFile()
{
cout << "是否确认清空?" << endl;
cout << "1、确认" << endl;
cout << "2、返回" << endl;
int input = 0;
cin >> input;
if (input == 1)
{
//打开模式 ios::trync 如果存在则删除文件并重建
ofstream ofs(FILENAME, ios::trunc);
ofs.close();
if (this->m_EmpArray != NULL)
{
for (int i = 0; i < this->m_EmpNum; i++)
{
if (this->m_EmpArray[i] != NULL)
{
delete this->m_EmpArray[i];
}
}
this->m_EmpNum = 0;
delete[] this->m_EmpArray;
this->m_EmpArray = NULL;
this->m_FileIsEmpty = true;
}
cout << "清空完成" << endl;
}
else
{
return;
}
}
workerManager.h
#pragma once//防止头文件重复包含
#include<iostream>
#include<string>
#include<fstream>
#define FILENAME "empFile.txt"
#include"worker.h"
using namespace std;//使用标准的命名空间
class WorkerManager
{
public:
WorkerManager();
int m_EmpNum;//记录文件中的人数个数
Worker** m_EmpArray;//员工数组的指针
void Menu();//显示菜单
void Add_Emp();//添加员工
void save();//保存文件
bool m_FileIsEmpty;//标志文件是否为空
int GetNum();//统计人数
void InitEmp();//初始化员工
void ShowEmp();//显示职工
int FindEmp(int id);//查找职工
void FindEmpP(int id);//查找职工并打印
void DeleEmp(int id);//删除职工
void ChangeEmp(int id);//修改员工
void RankEmp();//排序职工
void CleanFile();//清空文件
~WorkerManager();
};
employee.cpp
#include "employee.h"
Employee::Employee(int id, string name, int did)//构造函数传入员工信息
{
this->m_Id = id;
this->m_Name = name;
this->m_DeptId = did;
}
//显示个人信息
void Employee::ShowInfo()
{
cout << "职工编号:" << this->m_Id;
cout << " 职工姓名:" << this->m_Name;
cout << " 所在岗位:" << this->GetDeptName();
cout << " 岗位职责:完成经理交给的任务!" << endl;
}
//获取岗位名称
string Employee::GetDeptName()
{
return string("员工");
}
employee.h
#include<iostream>
#include<string>
#include<fstream>
#include "worker.h"
using namespace std;
//员工类
class Employee:public Worker
{
public:
Employee(int id, string name, int did);//构造函数传入员工信息
//显示个人信息
virtual void ShowInfo();
//获取岗位名称
virtual string GetDeptName();
};
manager.cpp
#include "manager.h"
Manager::Manager(int id, string name, int did)//构造函数传入员工信息
{
this->m_Id = id;
this->m_Name = name;
this->m_DeptId = did;
}
//显示个人信息
void Manager::ShowInfo()
{
cout << "职工编号:" << this->m_Id;
cout << " 职工姓名:" << this->m_Name;
cout << " 所在岗位:" << this->GetDeptName();
cout << " 岗位职责:完成老板交给的任务!" << endl;
}
//获取岗位名称
string Manager::GetDeptName()
{
return string("经理");
}
manager.h
#include<iostream>
#include<string>
#include<fstream>
#include "worker.h"
using namespace std;
//经理类
class Manager:public Worker
{
public:
Manager(int id, string name, int did);
//显示个人信息
virtual void ShowInfo();
//获取岗位名称
virtual string GetDeptName();
};
boss.h
#include<iostream>
#include<string>
#include<fstream>
#include "worker.h"
using namespace std;
//老板类
class Boss :public Worker
{
public:
Boss(int id, string name, int did);
//显示个人信息
virtual void ShowInfo();
//获取岗位名称
virtual string GetDeptName();
};
Boss.cpp
#include<iostream>
#include<string>
#include<fstream>
#include "worker.h"
using namespace std;
//老板类
class Boss :public Worker
{
public:
Boss(int id, string name, int did);
//显示个人信息
virtual void ShowInfo();
//获取岗位名称
virtual string GetDeptName();
};
2、静态版本
特点:不具备存储功能 -- 每次运行程序都是一个新的开始 之前存储的信息都会消失
存放10000个好友信息
其中包括:名字 电话 性别 地址 年龄
通讯录具有以下功能
1.增加好友信息
2.删除指定名字的好友信息
3.查找好友
4.修改好友信息
5.打印好友信息
6.排序
contact.c
#include "contact.h"
void InitContact(struct Contact* ps)
{
memset(ps->data,0,sizeof(ps->data));
ps -> size = 0;
}
void AddContact(struct Contact* ps)
{
if(ps->size == NUM)
{
printf("通讯录存储已满");
}
else
{
printf("请输入信息\n");
printf("姓名:");
scanf("%s",ps->data[ps->size].name);
printf("\n");
printf("年龄:");
scanf("%d",&(ps->data[ps->size].age));//年龄为int型要取地址
printf("\n");
printf("电话:");
scanf("%s",ps->data[ps->size].tele);
printf("\n");
printf("地址:");
scanf("%s",ps->data[ps->size].addr);
printf("\n");
printf("性别:");
scanf("%s",ps->data[ps->size].sex);
printf("\n");
ps->size++;
printf("录入信息成功\n");
}
}
void ShowContact (struct Contact* ps)
{
if(ps->size == 0)
{
printf("通讯录为空\n");
}
else
{
int i = 0;
printf("%-20s\t%-4s\t%-5s\t%-12s\t%-20s\n","名字","年龄","性别","电话","地址");
for(i=0;i<ps->size;i++)
{
printf("%-20s\t%-4d\t%-5s\t%-12s\t%-20s\n",ps->data[i].name,
ps->data[i].age,
ps->data[i].sex,
ps->data[i].tele,
ps->data[i].addr);
}
}
}
int FindName(struct Contact* pss,char name[20])
{
//找到了返回其下标
int i = 0;
for(i=0;i<pss->size;i++)
{
if(0 == strcmp(pss->data[i].name,name))
{
return i;
}
}
//找不到返回-1
return -1;
}
void DelContact(struct Contact* ps)
{
char name[20];
printf("输入删除人的名字:");
scanf("%s",name);
//1.查找删除人的位置
int ret = FindName(ps,name);
if(ret == -1)
{
printf("你输入的名字不存在");
}
//2.执行删除
else
{
//删除
int j = 0;
for(j=ret;j<ps->size-1;j++)
{
ps->data[j] = ps->data[j+1];
}
ps->size--;//个数减一
printf("删除成功\n");
}
}
void SearchContact(struct Contact* ps)
{
printf("请输入查找人的名字:");
char name[20];
scanf("%s",name);
int ret = FindName(ps,name);
if(ret == -1)
{
printf("你输入的名字不存在");
}
else
{
printf("%-20s\t%-4s\t%-5s\t%-12s\t%-20s\n","名字","年龄","性别","电话","地址");
printf("%-20s\t%-4d\t%-5s\t%-12s\t%-20s\n",ps->data[ret].name,
ps->data[ret].age,
ps->data[ret].sex,
ps->data[ret].tele,
ps->data[ret].addr);
}
}
void ModifyContact(struct Contact* ps)
{
printf("请输入要修改人的名字:");
char name[20];
scanf("%s",name);
int ret = FindName(ps,name);
if(ret == -1)
{
printf("你输入的名字不存在");
}
else
{
printf("请输入信息\n");
printf("姓名:");
scanf("%s",ps->data[ret].name);
printf("\n");
printf("年龄:");
scanf("%d",&(ps->data[ret].age));//年龄为int型要取地址
printf("\n");
printf("电话:");
scanf("%s",ps->data[ret].tele);
printf("\n");
printf("地址:");
scanf("%s",ps->data[ret].addr);
printf("\n");
printf("性别:");
scanf("%s",ps->data[ret].sex);
printf("修改完成\n");
}
}
static int cmp_string(const void* pa ,const void* pb)
{
char* t1 = (char*)pa;
char* t2 = (char*)pb;
return strcmp(pa,pb);
}
void SortContact(struct Contact* ps)
{
//按照名字进行排序
qsort(ps->data,ps->size,sizeof(ps->data[0]),cmp_string);
}
contact.h
#include <stdio.h>
#include <string.h>
#define NUM 1000
struct PeopleInfor
{
char name[20];
int age;
char sex[5];
char tele[12];
char addr[10];
};
struct Contact
{
struct PeopleInfor data[NUM];
int size;
};
enum Option
{
Exit,
Add,
Del,
Search,
Modify,
Show,
Sort,
};
//声明函数
//初始化通讯录
void InitContact(struct Contact* ps);
//往通讯录增加一个信息
void AddContact(struct Contact* ps);
//展示通讯录中的信息
void ShowContact (struct Contact* ps);
//删除通讯录中某特定信息
void DelContact(struct Contact* ps);
//查找通讯录中的某特定信息
void SearchContact(struct Contact* ps);
//查找函数
int FindName(struct Contact* pss,char name[20]);
//替换指定
void ModifyContact(struct Contact* ps);
//对通讯录进行排序
void SortContact(struct Contact* ps);
通讯录.c
#include "address.h"
int main()
{
//创建通讯录结构体变量
addressbook adb;
//初始化通讯录中当前人员个数
adb.size = 0;
int input = 0;
string name;
do
{
menu();
cout << "请输入:";
cin >> input;
switch (input)
{
case 0: //0、退出通讯录
cout << "退出系统成功" << endl;
return 0;
break;
case 1: //1、添加联系人
AddPerson(&adb);
break;
case 2: //2、显示联系人
ShowPerson(&adb);
break;
case 3: //3、删除联系人
cout << "请输入你要删除人的姓名:";
cin >> name;
DelPerson(&adb,name);
break;
case 4: //4、查找联系人
cout << "请输入你要查找人的姓名:";
cin >> name;
SerchPerson(&adb, name);
break;
case 5: //5、修改联系人
cout << "请输入你要修改人的姓名:";
cin >> name;
ChangePerson(&adb, name);
break;
case 6: //6、清空联系人
ClearPerson(&adb);
break;
default:
cout << "你的输入有误"<<endl;
break;
}
} while (input);
return 0;
}
3、与动态内存设计通讯录区别
a、改变之处(初始化)
void InitContact(struct Contact* ps)
{
ps->data = (struct PeopleInfo*) malloc(DEFAULT_SZ*sizeof(struct PeopleInfor));
if(ps->data == NULL)
{
return;
}
ps->size = 0;
ps->capacity = DEFAULT_SZ;
}
struct Contact
{
PeopleInfor* data;//指向动态申请的空间 用来存放联系人信息
int size;//记录当前已有的元素个数
int capacity;//当前通讯录的最大容量
};
b、增加判断内存是否储存满
void CheckCapacity(struct Contact* ps)
{
if(ps->size == ps->capacity)
{
//增加空间
struct PeopleInfor* ptr = (struct PeopleInfor*)realloc(ps->data,(ps->capacity+2)*sizeof(PeopleInfor));
if(ptr == NULL)
{
printf("增容失败\n");
}
else
{
ps->data = ptr;
ps->capacity = ps->capacity + 2;
printf("增容成功\n");
}
}
}
c、程序结束 释放内存
void DestroyContact(struct Contact* ps)
{
free(ps->data);
ps->data = NULL;
}