C++入门项目(二)——职工管理系统
引言
这个小项目是基于C++类的知识,主要为了深入掌握C++类而做的一个入门级小项目。主要参考了黑马程序员的C++教程,下面是黑马程序员的C++教学视频链接。
黑马程序员C++教程
系统功能
本系统的功能是管理职工信息,同时将职工信息保存到文件中,以便下次使用时直接获取职工信息,并支持在此基础上进行增删改查等操作。
本系统需要实现的功能如下:
1.退出管理程序
2.增加职工信息
3.显示职工信息
4.删除离职职工
5.修改职工信息
6.查找职工信息
7.按照编号排序
8.清空所有文档
程序需要实现的类
程序中需要实现的类共有5个,分别是:
1.职工管理类
该类是用来管理职工的,主要是对职工的信息进行增删改查等操作。该类有个数组用来保存职工,由于职工是以指针形式建立的,所以职工数组指针是 ** 形式,具体代码如下:
#pragma once
#include <iostream>
#include <fstream>
#include "worker.h"
using namespace std;
//管理类
//与用户的沟通菜单界面
//对职工增删改查的操作
//与文件的读写交互
class WorkerManager
{
public:
WorkerManager(); //构造函数
void ShowMenu(); //显示菜单
void Exit(); //退出
void AddEmp(); //添加职工
void save(); //保存文件
int GetNum(); //返回职工人数
void InitEmp(); //初始化员工
void ShowEmp(); //显示职工
void DelEmp(); //删除职工
void ChangeEmp(); //修改职工
void FineEmp(); //查找职工
void SortEmp(); //排序职工编号
void ClearFile(); //清空文件
int isExist(int id); //判断职工是否存在
bool is_empty_; //判断文件是否为空
int emp_num_; //记录职工人数
Worker** emp_array_; //职工数组指针
~WorkerManager(); //析构函数
};
2.职工抽象类
该类是用来派生出普通员工类,经理类,老板类。具体代码如下:
#pragma once
#include <iostream>
#include <string>
using namespace std;
class Worker
{
public:
virtual void ShowInfo() = 0; //显示个人信息
virtual string getDeptName() = 0; //获取岗位名称
int id_; //职工编号
string name_; //职工姓名
int dept_id_; //部门编号
};
3.普通员工类
#pragma once
#include <iostream>
#include <string>
using namespace std;
#include "worker.h"
class Employee : public Worker
{
public :
Employee(int id,string name,int dpid); //构造函数
virtual void ShowInfo(); //显示个人信息
virtual string getDeptName(); //获取岗位名称
};
该类的具体实现代码如下:
#include "Employee.h"
//构造函数
Employee::Employee(int id, string name, int dpid)
{
this->id_ = id;
this->name_ = name;
this->dept_id_ = dpid;
}
//显示个人信息
void Employee::ShowInfo()
{
cout << "职工编号:" << this->id_
<< "\t职工姓名:" << this->name_
<< "\t岗位:" << this->getDeptName()
<< "\t岗位职责:完成经理布置的任务" << endl;
}
//获取岗位名称
string Employee::getDeptName()
{
return string("员工");
}
4.经理类
#pragma once
#include <iostream>
#include <string>
using namespace std;
#include "worker.h"
class Manager : public Worker
{
public:
Manager(int id, string name, int dpid); //构造函数
virtual void ShowInfo(); //显示个人信息
virtual string getDeptName(); //获取岗位名称
};
该类的具体实现代码如下:
#include "Manager.h"
//构造函数
Manager::Manager(int id, string name, int dpid)
{
this->id_ = id;
this->name_ = name;
this->dept_id_ = dpid;
}
//显示个人信息
void Manager::ShowInfo()
{
cout << "职工编号:" << this->id_
<< "\t职工姓名:" << this->name_
<< "\t岗位:" << this->getDeptName()
<< "\t岗位职责:完成老板布置的任务,并下发任务给员工" << endl;
}
//获取岗位名称
string Manager::getDeptName()
{
return string("经理");
}
5.老板类
#pragma once
#include <iostream>
#include <string>
using namespace std;
#include "worker.h"
class Boss : public Worker
{
public:
Boss(int id, string name, int dpid); //构造函数
virtual void ShowInfo(); //显示个人信息
virtual string getDeptName(); //获取岗位名称
};
该类的具体实现代码如下:
#include "boss.h"
//构造函数
Boss::Boss(int id, string name, int dpid)
{
this->id_ = id;
this->name_ = name;
this->dept_id_ = dpid;
}
//显示个人信息
void Boss::ShowInfo()
{
cout << "职工编号:" << this->id_
<< "\t职工姓名:" << this->name_
<< "\t岗位:" << this->getDeptName()
<< "\t岗位职责:管理公司所有事务" << endl;
}
//获取岗位名称
string Boss::getDeptName()
{
return string("总裁");
}
功能实现
这里是实现职工管理类功能的具体代码。其中的FILENAME是指:
#define FILENAME "empfile.txt"
1.构造函数
该类的构造函数有三种情况,分别是:1.文件不存在;2.文件存在但无数据;3.文件存在且有数据。具体代码如下:
//构造函数
WorkerManager::WorkerManager()
{
ifstream ifs;
ifs.open(FILENAME, ios::in); //读文件
//如果文件为空
if (!ifs.is_open()) {
//cout << "文件不存在!" << endl;
this->emp_num_ = 0;
this->emp_array_ = NULL;
this->is_empty_ = true;
ifs.close();
return;
}
//文件存在但无数据
char ch;
ifs >> ch;
if (ifs.eof()) {
//cout << "文件为空!" << endl;
this->emp_num_ = 0;
this->emp_array_ = NULL;
this->is_empty_ = true;
ifs.close();
return;
}
//文件存在且有数据
int num = this->GetNum();
//cout << "职工人数为:" << num << endl;
this->emp_num_ = num;
//开辟空间
this->emp_array_ = new Worker * [this->emp_num_];
//将文件中的数据存放到数组中
this->InitEmp();
}
2.这里的几个函数是用来辅组以下功能的实现。
1.显示菜单函数
//显示菜单
void WorkerManager::ShowMenu()
{
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;
}
2.保存文件
//保存文件
void WorkerManager::save()
{
ofstream outfile;
outfile.open(FILENAME, ios::out); //以写的方式打开文件
for (int i = 0; i < this->emp_num_; i++) {
outfile << this->emp_array_[i]->id_ << " "
<< this->emp_array_[i]->name_ << " "
<< this->emp_array_[i]->dept_id_ << endl;
}
outfile.close(); //关闭文件
}
3.返回职工人数
//返回职工人数
int WorkerManager::GetNum()
{
ifstream ifs;
ifs.open(FILENAME, ios::in);//以读的方式打开文件
int id;
string name;
int dpid;
int num = 0;
while (ifs >> id && ifs >> name && ifs >> dpid) {
num++;
}
ifs.close(); //关闭文件
return num;
}
4.初始化员工
//初始化员工
void WorkerManager::InitEmp()
{
ifstream ifs;
ifs.open(FILENAME, ios::in);
int id;
string name;
int dpid;
int index = 0;
while (ifs >> id && ifs >> name && ifs >> dpid) {
Worker* worker = NULL;
if (dpid == 1) {
worker = new Employee(id, name, dpid);
}
else if (dpid == 2) {
worker = new Manager(id, name, dpid);
}
else {
worker = new Boss(id, name, dpid);
}
this->emp_array_[index] = worker;
index++;
}
ifs.close();
}
5.判断职工是否存在
//判断职工是否存在
int WorkerManager::isExist(int id)
{
int index = -1;
for (int i = 0; i < this->emp_num_; i++) {
if (this->emp_array_[i]->id_ == id) {
index = i;
break;
}
}
return index;
}
3.退出管理程序
void WorkerManager::Exit()
{
system("cls");
cout << "欢迎下次使用!" << endl;
exit(0);
}
4.增加职工信息
//添加职工
void WorkerManager::AddEmp()
{
cout << "请输入添加的职工数量:";
int addnum = 0; //保存用户的输入数量
cin >> addnum;
if (addnum > 0) {
int newsize = this->emp_num_ + addnum; //计算新空间的大小
//按黑马程序员教程中是
//Worker** newSpace = new Worker * [newsize];//开辟新空间
//如果是以上这样开辟新空间会报内存溢出的警告
Worker** newSpace = new Worker * [newsize*sizeof(Worker)];//开辟新空间
if (this->emp_array_ != NULL) {
//将原数据拷贝到新空间中
for (int i = 0; i < this->emp_num_; i++) {
newSpace[i] = this->emp_array_[i];
}
}
//输入新数据到新空间中
for (int i = 0; i < addnum; i++) {
int id;
string name;
int dpid;
cout << "请输入第" << i + 1 << "个新职工的编号:";
cin >> id;
cout << "请输入第" << i + 1 << "个新职工的姓名:";
cin >> name;
cout << "请选择该职工岗位" << endl;
cout << "1.普通职工 " << "2.经理 " << "3.总裁" << endl;
cin >> dpid;
Worker* worker = NULL;
switch (dpid) {
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:
break;
}
newSpace[this->emp_num_ + i] = worker;
}
//释放原有空间
delete[] this->emp_array_;
//更改新空间指向
this->emp_array_ = newSpace;
//更新人数
this->emp_num_ = newsize;
//更新文件不为空的标志
this->is_empty_ = false;
cout << "已成功添加" << addnum << "名新职工!" << endl;
this->save();
}
else {
cout << "输入有误!";
}
system("pause");
system("cls");
}
5.显示职工信息
//显示职工
void WorkerManager::ShowEmp()
{
if (this->is_empty_) {
cout << "文件不存在或记录为空!" << endl;
}
else {
for (int i = 0; i < this->emp_num_; i++) {
this->emp_array_[i]->ShowInfo();
}
}
system("pause");
system("cls");
}
6.删除离职职工
//删除职工
void WorkerManager::DelEmp()
{
if (this->is_empty_) {
cout << "文件不存在或记录为空!" << endl;
}
else {
cout << "请输入要删除的职工编号:";
int id = 0;
cin >> id;
int index = this->isExist(id);
if (index != -1) {
for (int i = index; i < this->emp_num_ - 1; i++) {
this->emp_array_[i] = this->emp_array_[i + 1];
}
this->emp_num_--;
this->save(); //更新到文件中
cout << "删除成功!" << endl;
}
else {
cout << "删除失败,未找到该职工!" << endl;
}
}
system("pause");
system("cls");
}
7.修改职工信息
//修改职工
void WorkerManager::ChangeEmp()
{
if (this->is_empty_) {
cout << "文件不存在或记录为空!" << endl;
}
else {
cout << "请输入要修改的职工编号:";
int id;
cin >> id;
int ret = this->isExist(id);
if (ret != -1) {
delete this->emp_array_[ret];
int newid = 0;
string newname;
int newdpid = 0;
cout << "请输入新的职工号:";
cin >> newid;
cout << "请输入新姓名:";
cin >> newname;
cout << "请输入岗位" << endl;
cout << "1.普通职工 " << "2.经理 " << "3.总裁" << endl;
cin >> newdpid;
Worker* worker = NULL;
switch (newdpid) {
case 1:
worker = new Employee(newid, newname, 1);
break;
case 2:
worker = new Manager(newid, newname, 2);
break;
case 3:
worker = new Boss(newid, newname, 3);
break;
default:
break;
}
this->emp_array_[ret] = worker;
cout << "修改成功!" << endl;
this->save();
}
else {
cout << "查无此人!" << endl;
}
}
system("pause");
system("cls");
}
8.查找职工信息
//查找职工
void WorkerManager::FineEmp()
{
if (this->is_empty_) {
cout << "文件不存在或记录为空!" << endl;
}
else {
cout << "请输入查找的方式:" << endl;
cout << "1.按职工编号查找\t2.按职工姓名查找" << endl;
int select = 0;
cin >> select;
if (select == 1) {
//按编号查找
int id = 0;
cout << "请输入要查找的编号:";
cin >> id;
int ret = this->isExist(id);
if (ret != -1) {
cout << "查找成功!该职工信息如下:" << endl;
this->emp_array_[ret]->ShowInfo();
}
else {
cout << "查无此人!" << endl;
}
}
else if (select == 2) {
//按姓名查找
string name;
cout << "请输入要查找的职工姓名:" << endl;
cin >> name;
bool flag = false; //判断是否存在此人
for (int i = 0; i < this->emp_num_; i++) {
if (this->emp_array_[i]->name_ == name) {
cout << "查找成功!该职工为:"
<< this->emp_array_[i]->id_
<< "号员工,信息如下:" << endl;
flag = true;
this->emp_array_[i]->ShowInfo();
}
}
if (!flag) {
cout << "查无此人!" << endl;
}
}
else {
cout << "输入错误!" << endl;
}
}
system("pause");
system("cls");
}
9.按照编号排序
//排序职工编号
void WorkerManager::SortEmp()
{
if (this->is_empty_) {
cout << "文件不存在或记录为空!" << endl;
system("pause");
system("cls");
}
else {
cout << "请选择排序方式:" << endl;
cout << "1.按职工号进行升序\t2.按职工号进行降序" << endl;
int select = 0;
cin >> select;
if (select == 1 || select == 2) {
for (int i = 0; i < this->emp_num_ - 1; i++) {
int minormax = i;//声明最值下标
for (int j = i+1; j < this->emp_num_; j++) {
if (select == 1) {
if (this->emp_array_[minormax]->id_ > this->emp_array_[j]->id_) {
minormax = j;
}
}
else if (select == 2) {
if (this->emp_array_[minormax]->id_ < this->emp_array_[j]->id_) {
minormax = j;
}
}
}
if (i != minormax) {
Worker* temp = this->emp_array_[i];
this->emp_array_[i] = this->emp_array_[minormax];
this->emp_array_[minormax] = temp;
}
}
}
else {
cout << "输入错误!";
system("pause");
system("cls");
return;
}
cout << "排序成功!排序后的结果为:" << endl;
this->save();
this->ShowEmp();
}
}
10.清空所有文档
//清空文件
void WorkerManager::ClearFile()
{
cout << "确定清空?" << endl;
cout << "1.确定\t2.返回" << endl;
int select = 0;
cin >> select;
if (select == 1) {
ofstream ofs(FILENAME, ios::trunc); //删除文件后重新创建
ofs.close();
if (this->emp_array_ != NULL) {
//删除堆区的每个职工对象
for (int i = 0; i < this->emp_num_; i++) {
delete this->emp_array_[i];
this->emp_array_[i] = NULL;
}
//删除堆区数组指针
delete[] this->emp_array_;
this->emp_array_ = NULL;
this->emp_num_ = 0;
this->is_empty_ = true;
}
cout << "清空成功!" << endl;
}
system("pause");
system("cls");
}
11.析构函数
//析构函数
WorkerManager::~WorkerManager()
{
if (this->emp_array_ != NULL) {
delete[] this->emp_array_;
this->emp_array_ = NULL;
}
}
主函数
#include <iostream>
#include "WorkerManager.h"
using namespace std;
int main()
{
WorkerManager wm;
int select = 0;
while (true) {
wm.ShowMenu(); //显示菜单
cout << "请输入您的选择:";
cin >> select;
switch (select) {
case 0:
wm.Exit(); //退出程序
break;
case 1:
wm.AddEmp(); //添加职工
break;
case 2:
wm.ShowEmp(); //显示职工
break;
case 3:
wm.DelEmp(); //删除职工
break;
case 4:
wm.ChangeEmp(); //修改职工
break;
case 5:
wm.FineEmp(); //查找职工
break;
case 6:
wm.SortEmp(); //排序职工
break;
case 7:
wm.ClearFile(); //清空文件
break;
default:
system("cls"); //清屏
break;
}
}
system("pause");
return 0;
}
结尾
如果需要源码的小伙伴可以联系我!