C++结课大作业——职工管理系统

Posted #呆呆

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++结课大作业——职工管理系统相关的知识,希望对你有一定的参考价值。

这个项目分为头文件和CPP文件和一个.txt文件

实现功能如图所示

我一一列出来(头文件,cpp文件,txt文件)
按着这三个复制粘贴就可以用

boss.h

#pragma once
//普通员工头文件子类继承父类
#include"worker.h"
#include<iostream>
#include<string>
using namespace std;
class boss :public worker {
public:
	//构造函数
	boss(int id, string name, int did);
	//显示个人信息
	virtual void showinfo() ;

	//获取岗位名称
	virtual string getdepainfo() ;
};

employee.h

#pragma once
//普通员工头文件子类继承父类
#include"worker.h"
#include<iostream>
#include<string>
using namespace std;
class Employee:public worker 
{
public://构造函数
	Employee(int id, string name,int did);

	//显示个人信息
	 void showinfo();//虚函数关键字可删可不删

	//获取岗位名称
	 string getdepainfo();


};

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);
	//显示个人信息
    void showinfo() ;

	//获取岗位名称
	 string getdepainfo() ;
};

worker.h

#pragma once
#include<iostream>
#include<string>
using namespace std;
//抽象员工子类
class worker {
public:
	//显示个人信息
	virtual void showinfo() = 0;

	//获取岗位名称
	virtual string getdepainfo() = 0;

	int m_id;//职工编号
	string m_name;//职工姓名
	int m_depaid;//职工所在部门编号
};

workerManager.h

#pragma once//防止头文件重复包含
#include<iostream>//包含输入输出流头文件

#include"worker.h"
using namespace std;//使用标准命名空间
class workermanager {
public:
	workermanager();//构造函数
	void showmenu();//菜单函数
	void exitsystem();//退出系统函数
	//增加职工信息,员工,经理,老板
	//职工的属性有:职工编号。,姓名,所在部门编号
	//职工行为有:岗位职责信息描述,获取岗位名称
	int m_empnum;//记录文件中的人数
	worker ** m_emparray;//职工数组指针
	//添加员工
	void addemp();
	//保存文件
	void save();
	void show();//打印职工信息
	bool m_fileisemp;//判断文件是否为空
	//统计文件中人数
	int getnum();

	void init();
	void delemp();//删除职工信息
	int isexist(int id);
	void modify();
	void search();
	void sort();
	void clean();
	~workermanager();//析构函数

};

接下来是.cpp文件

boss.h

#pragma once
//普通员工头文件子类继承父类
#include"worker.h"
#include<iostream>
#include<string>
using namespace std;
#include"boss.h"
boss::boss(int id, string name, int did)
{
	this->m_id = id;
	this->m_name = name;
	this->m_depaid = did;
}
void boss::showinfo()
{ 
	cout << "职工编号为:" << this->m_id
		<< "\\t职工姓名:" << this->m_name
		<< "\\t职工岗位为:" << this->getdepainfo()
		<< "\\t岗位职责:管理公司" << endl;
}
string boss::getdepainfo()
{
	return "老板";
}

employee.cpp

#pragma once;
#include<iostream>
#include<string>
using namespace std;
#include"employee.h"
//构造函数
Employee::Employee(int id, string name,int did) 
{
    this->m_id = id;
	this->m_name = name;
	this->m_depaid = did;
}

//显示个人信息
void Employee::showinfo()
{
	cout << "职工编号为:" << this->m_id
		<< "\\t职工姓名:" << this->m_name
		<< "\\t职工岗位为:" << this->getdepainfo()
		<< "\\t岗位职责:完成经理分配的任务" << endl;

}

//获取岗位名称
string Employee::getdepainfo() 
{
	return "员工";

 }

manager.cpp

#include<iostream>
#pragma once
#include<string>
#include"worker.h"
#include"manager.h"
 manager::manager(int id,string name,int did)
{
	 this->m_id = id;
	 this->m_name = name;
	 this->m_depaid = did;

}
 void manager::showinfo()
 {
	 cout << "职工编号为:" << this->m_id
		 << "\\t职工姓名:" << this->m_name
		 << "\\t职工岗位为:" << this->getdepainfo()
		 << "\\t岗位职责:完成老板分配的任务" << endl;
 }
 string manager::getdepainfo()
 {
	 return "经理";
 }

workerManager.cpp

#include"workerManger.h"
#include"boss.h"
#include"employee.h"
#include"manager.h"
#include<fstream>
#define FILENAME "EMPFILE.txt"
workermanager::workermanager()//构造函数类外实现
{
	//文件不存在
	ifstream ifs;
	ifs.open(FILENAME, ios::in);
	if (!ifs.is_open())
	{
		cout << "文件不存在" << endl;
		this->m_empnum = 0;
		this->m_emparray = NULL;
		this->m_fileisemp = true;
		ifs.close();
		return;
	}
	//文件存在,但是没有信息
	char ch;
	ifs >> ch;
	if (ifs.eof())
	{
		cout << "文件夹为空" << endl;
		this->m_empnum = 0;
		this->m_emparray = NULL;
		this->m_fileisemp = true;
		ifs.close();
		return;

	}
	//当文件存在并且有数据
	int num = this->getnum();
	cout << "职工人数为:" << num << endl;
	this->m_empnum = num;
	this->m_emparray = new worker * [this->m_empnum];
	this->init();
	//先开辟空间将文件中的数据存到数组中
	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_depaid << endl;
	}

}
	void workermanager::delemp()
	{
		if (this->m_fileisemp)
		{
			cout << "文件不存在或者为空" << endl;
		}
		else {
			cout << "请输入要删除的职工编号" << endl;
			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_emparray[i] = this->m_emparray[i + 1];
				}
				this->m_empnum--;
				this->save();
				cout << "删除成功" << endl;

			}
			else
				cout << "未找到该职工" << endl;
		}
		system("pause");
		system("cls");
		 
	}
	void workermanager::modify()
	{
		if (this->m_fileisemp)
		{
			cout << "文件不存在或者为空" << endl;
		}
		cout << "请输入要修改职工的编号" << endl;
		int id;
		cin >> id;
		int t = this->isexist(id);
		if (t != -1)
		{
			delete this->m_emparray[t];
			int newid = 0;
			string newname = "";
			int ds = 0;
			cout << "查询到id为" << t << "号职工,请输入新职工号:" << endl;
			cin >> newid;
			cout << "请输入新姓名" << endl;
			cin >> newname;
			cout << "请输入岗位:" << endl << "1,普通员工\\n2.经理\\n3.老板\\n";
			cin >> ds;
			worker* worker = NULL;
			switch (ds)
			{
			case 1:worker = new Employee(newid, newname, ds);
				break;
			case 2:
				worker = new manager(newid, newname, ds);
				break;
			case 3:worker = new boss(newid, newname, ds);
				break;
			default:
				break;
			}
			this->m_emparray[t] = worker;
			cout << "修改成功" << endl;
			this->save();
		}
		else
			cout << "查无此人" << endl;
		system("pause");
		system("cls");
	}

	int workermanager::isexist(int id)
	{
		int index = -1;
		for (int i = 0; i < this->m_empnum; i++)
		{
			if (this->m_emparray[i]->m_id == id)
			{
				index = i;
				break;
			}
		}
		return index;
	}
void  workermanager::show()
{
	if (this->m_fileisemp)
	{
		cout << "文件不存在或者记录为空" << endl;
	}
	else
	{
		for (int i = 0; i < m_empnum; i++)
		{
			//利用多态调用接口
			this->m_emparray[i]->showinfo();
		}
	}
	system("pause");
	system("cls");


}
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++;
	}
	return num;
}
void workermanager::showmenu()//菜单函数类外实现
{
	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;
	system("pause");
	exit(0);
}
void workermanager::addemp()
{
	cout << "请输入添加职工人数:\\n";
	int num;
	cin >> num;
	if (num > 0)
	{
		//计算添加新空间大小
		int newsize = this->m_empnum + num;//原来的加上新的
		//开辟新空间
		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 < num; i++)
		{
			int id;
			string  name;
			int ds;
			cout << "请输入第" << i + 1 << "个新职工编号:\\n";
			cin >> id;
			cout << "请输入第" << i + 1 << "个新职工姓名:\\n";
			cin >> name;
			cout << "请选择该职工岗位:\\n";
			cout << "1,普通员工" << endl;
			cout << "2,经理" << endl;
			cout << "3,老板" << endl;
			cin >> ds;
			worker* worker = NULL;
			switch (ds)
			{
			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戴口罩的人脸识别

matlab的作业,,,求大神解

c++ 职工管理系统 代码汇总 分开简易讲解全文思路

c++初学者动手实践项目:职工系统管理(上)

260行python结课作业|学生管理系统

C++C++实现职工管理系统