C++动态分配内存实现职工管理系统,保存到文件(相对路径),较全面。

本文介绍了一个使用C++实现的职工管理系统,系统支持添加、显示、删除、修改职工信息等功能,并实现了选择排序。系统通过二级指针管理职工数组,并支持保存至文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

🎃C++动态实现职工管理系统,较全面。


一、🧥总结

提示:简单思维导图,总结我的思路:

很简单,老套路,源文件写函数实现,头文件写函数声明,头文件名字要一样。
👓关于using namespace std的使用我不解释。

1.🧶代码分布

在这里插入图片描述

2.🧵功能分布成员分布

排序我用的选择排序,注意程序运行后,输入添加成员信息的时候注意信息成员类型,不要写int型形写成字符型,这个我基本没写判断机制。

在这里插入图片描述


二、👔源码

1.👕头文件

  1. workerManager.h

代码如下(示例):

#pragma once

#include<iostream>
#include"worker.h"
#include<fstream>
#define FILENAME "empFile.txt"


class workerManager
{
public:

	//构造函数
	workerManager();

	//菜单功能
	void showMenu();

	//添加成员
	void Add_Emp();

	//退出
	void exitSystem();

	//保存文件
	void save();

	//析构函数
	~workerManager();

	//记录文件中的人数个数
	int m_EmpNum;

	//员工数组的指针
	Worker  **m_EmArray;

	//判断空文件
	bool m_FileIsEmpty;

	//统计人数
	int get_EmpNum();

	//初始化数组
	void init_Emp();

	//显示职工
	void show_Emp();

	//删除职工
	void del_Emp();

	//判断职工是否存在
	int IsExist(int id);

	//修改功能实现
	void Mod_Emp();

	//查找职工函数声明
	void Find_Emp();

	//排序算法
	void Sort_Emp();

	//清空文件
	void Clean_File();

};

2.worker.h

#pragma once
#include<iostream>
#include<string>
using namespace std;
//职工抽象类
class Worker
{
public:

	//显示个人信息
	virtual void showInfo() = 0;

	//获取岗位名称
	virtual string getDeptName() = 0;
public:
	int m_Id;//职工编号
	string m_Name;//职工姓名
	int m_DeptId;//职工所在部门名称编号
};

3.Boss.h

#pragma once
#include<iostream>
#include<string>
#include"worker.h"


class Boss : public Worker
{
public:

	//构造函数
	Boss(int id, string name, int dId);

	//显示个人信息
	virtual void showInfo();

	//获取职工岗位名称
	virtual string getDeptName();
};

4.manager.h

#pragma once
#include<iostream>
#include<string>
#include"worker.h"

using namespace std;

class Manager : public Worker
{
public:

	//构造函数
	Manager(int id, string name, int dId);

	//显示个人信息
	virtual void showInfo();

	//获取职工岗位名称
	virtual string getDeptName();
};

5.employee.h

#pragma once
#include<iostream>
#include<string>
#include"worker.h"


using namespace std;


class Employee : public Worker
{
public:

	//构造函数
	Employee(int id, string name, int dId);

	//显示个人信息
	virtual void showInfo();

	//获取职工岗位名称
	virtual string getDeptName();

};

2.🦺源文件

  1. 职工管理系统.cpp

代码如下(示例):

#define _CRT_SECURE_NO_WARNINGS 1

#include "workerManager.h"

using namespace std;

int main()
{

	while (true)
	{

		workerManager wm;

		wm.showMenu();

		cout << "请输入功能对应数字:";
		int choice = { 0 };//用户输入的选择
		cin >> choice;

		switch (choice)
		{
		case 0:
			/*退出*/
			wm.exitSystem();
			break;
		case 1:
			/*添加*/
			wm.Add_Emp();
			break;
		case 2:
			/*显示*/
			wm.show_Emp();
			break;
		case 3:
		{
			/*删除*/
			wm.del_Emp();
			break;
		}
		case 4:
			/*修改*/
			wm.Mod_Emp();
			break;
		case 5:
			/*查找*/
			wm.Find_Emp();
			break;
		case 6:
			/*排序*/
			wm.Sort_Emp();
			break;
		case 7:
			/*清空*/
			wm.Clean_File();
			break;
		default:
			system("cls");
			break;
		}
	}
	return 0;
}

2.workerManager.cpp

#define _CRT_SECURE_NO_WARNINGS 1

#include "workerManager.h"
#include "worker.h"
#include "Boss.h"
#include "employee.h"
#include "manager.h"

using namespace std;

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_EmArray = NULL;/*二级指针*/
		ifs.close();
		return;
	}
	//文件存在,但是没有记录
	char ch;
	ifs >> ch;
	if (ifs.eof())
	{
		cout << "文件为空!!" << endl;
		this->m_EmpNum = 0;/*人数*/
		this->m_FileIsEmpty = true;/*判断是否为空*/
		this->m_EmArray = NULL;/*二级指针*/
		ifs.close();
		return;
	}
	ifs.close();
	
	//文件存在记录数据
	int num = this->get_EmpNum();
	cout << "职工人数为:" << num << endl;
	this->m_EmpNum = num;

	this->m_EmArray = new Worker * [this->m_EmpNum];
	this->init_Emp();

	for (int i = 0;i < this->m_EmpNum;i++)
	{
		cout << "职工编号" << this->m_EmArray[i]->m_Id
			<< "\t职工名称" << this->m_EmArray[i]->m_Name
			<< "\t岗位" << this->m_EmArray[i]->m_DeptId << endl;
	}
}

workerManager::~workerManager()
{
	if (this->m_EmArray != NULL)
	{
		for (int i = 0;i < this->m_EmpNum;i++)
		{
			if (this->m_EmArray[i] != NULL)
			{
				delete this->m_EmArray[i];
			}
		}
		delete[] this->m_EmArray;
		this->m_EmArray = NULL;
	}
}

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;
	cout << "*********************************************" << endl;
}

void workerManager::exitSystem()
{
	cout << "欢迎下次使用" << endl;
	exit(0);
}

void workerManager::Add_Emp()
{
	cout << "请输入增加职工数量: " << endl;

	int addNum = 0;
	cin >> addNum;
	
	if (addNum > 0)
	{
		//计算新空间大小
		int newSize = this->m_EmpNum + addNum;

		//开辟新空间
		Worker** newSpace = new Worker * [newSize];

		//将原空间下内容存放到新空间下
		if (this->m_EmArray != NULL)
		{
			for (int i = 0; i < this->m_EmpNum; i++)
			{
				newSpace[i] = this->m_EmArray[i];
			}
		}

		//输入新数据
		for (int i = 0;i < addNum; i++)
		{
			int id;
			string name;
			int dSelect;

			cout << "请输入第 " << i + 1 << "个新职工编号" << endl;
			cin >> id;

			cout << "请输入第 " << i + 1 << "个新职工姓名" << endl;
			cin >> name;


			cout << "请选择该职工的岗位:" << 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:
				break;
			}

			newSpace[(this->m_EmpNum) + i] = worker;
		}

		//释放原有空间
		delete[] this->m_EmArray;

		//更新空间指向
		this->m_EmArray = newSpace;

		//更新人数
		this->m_EmpNum = newSize;

		this->m_FileIsEmpty = false;

		//提示信息
		cout << "成功添加" << addNum << "名新职工!!" << endl;
		this->save();


	}
	else
	{
		cout << "输入有误" << endl;
	}
	system("pause");
	system("cls");
}

void workerManager::save()
{
	ofstream ofs;
	ofs.open(FILENAME, ios::out);

	for (int i = 0; i < this->m_EmpNum; i++)
	{
		ofs << this->m_EmArray[i]->m_Id << " "
			<< this->m_EmArray[i]->m_Name << " "
			<< this->m_EmArray[i]->m_DeptId << endl;
	}
	ofs.close();
}

int workerManager:: get_EmpNum()
{
	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::init_Emp()
{
	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
		{
			worker = new Boss(id, name, dId);
		}
		//存放在数组
		this->m_EmArray[index] = worker;
		index++;
	}
	ifs.close();
}

void workerManager:: show_Emp()
{
	if (this->m_FileIsEmpty)
	{
		cout << "文件不存在或者记录为空" << endl;
	}
	else
	{
		for (int i = 0; i < m_EmpNum; i++)
		{
			//利用多态调用接口
			this->m_EmArray[i]->showInfo();
		}
	}
	system("pause");
	system("cls");
}

int workerManager::IsExist(int id)
{
	int index = -1;

	for (int i = 0; i < this->m_EmpNum;i++)
	{
		if (this->m_EmArray[i]->m_Id == id)
		{
			index = i;
			break;
		}
	}
	return index;
}

void workerManager::del_Emp()
{
	if (this->m_FileIsEmpty)
	{
		cout << "文件不存在或记录为空!" << endl;
	}
	else
	{
		//按照职工编号删除
		cout << "请输入要删除的职工的职工编号:";
		int id = 0;
		cin >> id;
		int index = this->IsExist(id);
		if (index != -1)
		{
			for (int i = index;i < this->m_EmpNum - 1;i++)
			{
				this->m_EmArray[i] = this->m_EmArray[i + 1];
			}
			this->m_EmpNum--;
			this->save();
			cout << "删除成功" << endl;
		}
		else
		{
			cout << "不存在啊";
		}
	}
	system("pause");
	system("cls");
}

void workerManager::Mod_Emp()
{
	if (this->m_FileIsEmpty)
	{
		cout << "文件不存在或者没数据" << endl;
	}
	else
	{
		cout << "请输入修改职工的编号" << endl;
		int id;
		cin >> id;
		int ret = this->IsExist(id);
		if (ret != -1)
		{
			//查找编号职工

			delete this->m_EmArray[ret];

			int newId = 0;
			string newName = "";
			int dSelect = 0;

			cout << "查到:" << id << "号职工,请输入新职工号:" << endl;
			cin >> newId;

			cout << "请输入新姓名:" << endl;
			cin >> newName;

			cout << "请输入新岗位:" << endl;
			cout << "1.普通员工" << endl;
			cout << "2.经理" << endl;
			cout << "3.老板" << endl;
			cin >> dSelect;


			Worker* worker = NULL;
			switch (dSelect)
			{
			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->m_EmArray[ret] = worker;
			this->save();
			cout << "修改成功" << this->m_EmArray[ret]->m_DeptId << endl;
			
		}
		else
		{
			cout << "查无此人" << endl;
		}
	}
	system("pause");
	system("cls");
}   

void workerManager::Find_Emp()
{
	if (this->m_FileIsEmpty)
	{
		cout << "文件不存在或者没数据" << endl;
	}
	else
	{
		cout << "请输入查找的方式:" << endl;
		cout << "1.按职工编号查找" << endl;
		cout << "2.按姓名查找" << endl;

		int select = 0;
		cin >> select;

		if (select == 1)
		{
			int id;
			cout << "请输入查找的职工编号:" << endl;
			cin >> id;

			int ret = this->IsExist(id);
			if (ret != -1)
			{
				cout << "查找成功!该职工信息如下:" << endl;
				this->m_EmArray[ret]->showInfo();
			}
			else
			{
				cout << "查找失败" << endl;
			}
		}
		else if(select == 2)
		{
			string name;
			cout << "请输入姓名:" << endl;
			cin >> name;

			bool flag = false;//查找标志
			for (int i = 0;i < this->m_EmpNum;i++)
			{
				if (this->m_EmArray[i]->m_Name == name)
				{
					cout << "查找成功,职工编号为:"
						<< this->m_EmArray[i]->m_Id
						<< "号的信息如下:" << endl;
					flag = true;
					this->m_EmArray[i]->showInfo();
				}
			}
			if (flag == false)
			{
				cout << "查无此人" << endl;
			}
		}
		else
		{
			cout << "输入选项有误" << endl;
		}
		system("pause");
		system("cls");

	}
}

void workerManager:: Sort_Emp()
{
	if (this->m_FileIsEmpty)
	{
		cout << "文件不存在,或者记录为空" << endl;
	}
	else
	{
		cout << "请选择排序方式:" << endl;
		cout << "1.工号升序" << endl;
		cout << "2.工号降序" << endl;

		int select = 0;
		cin >> select;

		for (int i = 0;i < m_EmpNum;i++)
		{
			int minOrMax = i;
			for (int j = i + 1;j < m_EmpNum;j++)
			{
				if (select == 1)//升序
				{
					if (m_EmArray[minOrMax]->m_Id > m_EmArray[j]->m_Id)
					{
						minOrMax = j;
					}
				}
				else//降序
				{
					if (m_EmArray[minOrMax]->m_Id < m_EmArray[j]->m_Id)
					{
						minOrMax = j;
					}
				}
				if (i != minOrMax)
				{
					Worker* temp = m_EmArray[i];
					m_EmArray[i] = m_EmArray[minOrMax];
					m_EmArray[minOrMax] = temp;
				}
			}
		}
		cout << "排序成功" << endl;
		this->save();
	}
	system("pause");
	system("cls");
}

void workerManager::Clean_File()
{
	cout << "请确认清空:" << endl;
	cout << "1.确认" << endl;
	cout << "2.返回" << endl;

	int select = 0;
	cin >> select;

	if (select == 1)
	{
		//打开模式iOS::trunc如果文件存在删除并重新创建
		ofstream ofs(FILENAME, ios::trunc);
		ofs.close();

		if (this->m_EmArray != NULL)
		{
			for (int i = 0;i < this->m_EmpNum;i++)
			{
				if (this->m_EmArray[i] != NULL)
				{
					delete this->m_EmArray[i];
				}
			}
			this->m_EmpNum = 0;
			delete[] this->m_EmArray;
			this->m_EmArray = NULL;
			this->m_FileIsEmpty = true;
		}
		cout << "清除成功" << endl;


	}
	system("pause");
	system("cls");
}

3.Boss.cpp

#define _CRT_SECURE_NO_WARNINGS 1
#include"boss.h"

Boss::Boss(int id, string name, int dId)
{
	this->m_Id = id;
	this->m_Name = name;
	this->m_DeptId = dId;
}

void Boss::showInfo()
{
	cout << "职工编号" << this->m_Id
		<< "\t职工名称" << this->m_Name
	    << "\t岗位" << this->getDeptName()
		<< "\t岗位职责:管理公司所有事务" << endl;
}

string Boss::getDeptName()
{
	return string("总裁");
}

4.manager.cpp

#define _CRT_SECURE_NO_WARNINGS 1
#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
		<< "\t职工名称" << this->m_Name
	    << "\t岗位" << this->getDeptName()
		<< "\t岗位职责:完成老板交给的任务,并下发任务给员工" << endl;
}

string Manager::getDeptName()
{
	return string("经理");
}

5.employee.cpp

#define _CRT_SECURE_NO_WARNINGS 1
#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
	    << "\t职工名称" << this->m_Name
	    << "\t岗位" << this->getDeptName()
		<< "\t岗位职责:完成经理交给的任务" << endl;
}

string Employee::getDeptName()
{
	return string("员工");
}



🎁 小结

如果对你有帮助,请给小生点点举报🥼
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值