C++

C++,第1张

简单的项目构成

 头文件 职工抽象类
#pragma once
#include
#include
using namespace std;
class Worker {
public:
	//显示个人信息
	virtual void showInfo() = 0;
	//显示岗位名称
	virtual string getDName() = 0;
	int m_id;
	string m_name;
	int m_dId;
};
Boss类(继承自职工抽象类)
#pragma once
#include
#include
#include"Worker.h"
using namespace std;
class Boss :public Worker {
public:
	Boss(int id, string name, int dId);
	void showInfo();
	string getDName();
};
经理类(继承自职工抽象类)
#pragma once
#include
#include
#include"Worker.h"
using namespace std;
class Manger :public Worker {
public:
	Manger(int id, string name, int dId);
	void showInfo();
	string getDName();
};
普通员工类(继承自职工抽象类)
#pragma once
#include
#include
#include"Worker.h"
using namespace std;
class Employee :public Worker {
public:
	Employee(int id, string name, int dId);
	void showInfo();
	string getDName();
};

管理系统类(实现信息的增删查改,排序等等)
#pragma once //防止头文件重复包含
#include
#include"Worker.h"
using namespace std;
//只做声明, 不做实现
class WorkerManger {
public:
	WorkerManger();

	//展示菜单的函数
	void showMenu();
	
	//退出系统函数
	void exitSystem();

	//添加新员工
	void addEmp();

	//保存到txt文本中
	void save();
	//统计人数
	int getEmNum();

	//初始化员工
	void initEmp();

	//显示员工信息
	void showWorker();

	//删除职工
	void delEmp();

	//判断编号员工是否存在
	int isExist(int num);

	//修改员工信息
	void modEmp();

	//查找员工
	void findEmp();

	//排序
	void sortEmp();

	//清空文件
	void cleanFile();


	~WorkerManger();
	
	
	//判断文件是否为空的标志,后面删除时需要用到
	bool m_isEmpty;

	int m_emNum;
	Worker** m_emArray;
	
};
源文件 职工抽象类
#include"Worker.h"

void Worker::showInfo() {

}

string Worker::getDName() {
	return string("None");
}
Boss类(继承自职工抽象类)
#include"Boss.h"

Boss::Boss(int id, string name, int dId) {
	m_id = id;
	m_name = name;
	m_dId = dId;
}
void Boss::showInfo() {
	cout << "职工编号:" << m_id;
	cout << "\n职工姓名:" << m_name;
	cout << "\n岗位:" << getDName();
	cout << "\n岗位职责:啥也不用干,诶!就是玩儿" << endl;
	cout << endl;

}
string Boss::getDName() {
	return string("公司老板");
}
经理类(继承自职工抽象类)
#include"Manger.h"
Manger::Manger(int id, string name, int dId) {
	m_id = id;
	m_name = name;
	m_dId = dId;
}
void Manger::showInfo() {
	cout << "职工编号:" << m_id;
	cout << "\n职工姓名:" << m_name;
	cout << "\n岗位:" << getDName();
	cout << "\n岗位职责:压榨底层打工人,帮老板端茶倒水" << endl;
	cout << endl;
}
string Manger::getDName() {
	return string("部门经理");
}
普通员工类(继承自职工抽象类)
#include"Employee.h"


Employee::Employee(int id, string name, int dId ) {
	m_id = id;
	m_name = name;
	m_dId = dId;
}
void Employee::showInfo() {
	cout << "职工编号:" << m_id;
	cout << "\n职工姓名:" << m_name;
	cout << "\n岗位:" << getDName();
	cout << "\n岗位职责:帮经理端茶倒水" << endl;
	cout << endl;

}
string Employee::getDName() {
	return string("底层打工人");
}
管理系统类(实现信息的增删查改,排序等等)
#include
#include"WorkManger.h"
#include"Worker.h"
#include"Employee.h"
#include"Manger.h"
#include"Boss.h"

#define FILENAME "empFile.txt"
WorkerManger::WorkerManger() {
	ifstream ifs;
	ifs.open(FILENAME, ios::in);
	//如果文件不存在则初始化各个参数(第一次使用系统)
	if (!ifs.is_open()) {
		cout << "文件不存在" << endl;
		this->m_emNum = 0;
		this->m_emArray = NULL;
		this->m_isEmpty = true;
		ifs.close();
		return;
	}
	//如果文件存在,但是数据被清理过
	char ch;
	ifs >> ch;
	if (ifs.eof()) {
		cout << "数据被清理了" << endl;
		this->m_emNum = 0;
		this->m_emArray = NULL;
		this->m_isEmpty = true;
		ifs.close();
		return;
	}
	//获取文件中的员工数量
	getEmNum();
	this->m_emArray = new Worker * [this->m_emNum];
	initEmp();
	this->m_isEmpty = false;
	
}
WorkerManger::~WorkerManger() {
	if (m_emArray != NULL) {
		delete[] m_emArray;
		m_emArray = NULL;
	}

}

void WorkerManger::save() {
	ofstream ofs;
	ofs.open(FILENAME, ios::out);
	for (int i = 0; i < m_emNum; ++i) {
		ofs << m_emArray[i]->m_id << " "
			<< m_emArray[i]->m_name << " "
			<< m_emArray[i]->m_dId << " ";
	}
}

int WorkerManger::getEmNum() {
	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();
	this->m_emNum = num;
	return num;
}

//如果文件存在且有数据的初始化函数
void WorkerManger::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 Manger(id, name, dId);
		else
			worker = new Boss(id, name, dId);
		this->m_emArray[index] = worker;
		index++;
	}

}
//显示员工信息
void WorkerManger::showWorker() {
	//如果不为空
	system("cls");
	if (!m_isEmpty) {
		string dName;
		cout << "员工信息如下:" << endl;
		for (int i = 0; i < this->m_emNum; ++i) {
			this->m_emArray[i]->showInfo();
		}
	}
	else {
		cout << "还未录入员工信息!!!" << endl;
	}
	system("pause");
	

}


int WorkerManger::isExist(int num) {
	for (int i = 0; i < this->m_emNum; ++i) {
		//找到了员工
		if (this->m_emArray[i]->m_id == num)
			return i;
	}
	//否则返回-1
	return -1;
}

//删除员工
void WorkerManger::delEmp() {
	int index = -1;
	int id;
	if (getEmNum() == 0) {
		cout << "还未录入员工信息!!!" << endl;
		system("pause");
		return;
	}
	cout << "请输入要删除员工的编号:";
	cin >> id;
	system("cls");
	index = isExist(id);
	if (index == -1) {
		cout << "未找到该员工!!!" << endl;
	}
	else {
		for (int i = index; i < this->m_emNum - 1; ++i) {
			this->m_emArray[i] = this->m_emArray[i + 1];
		}
		this->m_emNum--;
		this->save();
		cout << "删除成功!!!" << endl;
	}
	system("pause");
}
void WorkerManger::modEmp() {
	if (getEmNum() == 0)
	{
		cout << "还未录入员工信息!!" << endl;
		system("pause");
		return;
	}
	else {
		int index = -1;
		int id;
		cout << "请输入要修改信息的员工的编号:" << endl;
		cin >> id;
		index = isExist(id);
		if (index == -1) {
			cout << "未找到该员工,请输入有效编号" << endl;
		}
		else {
			int newId;
			string newName;
			int newDid;
			Worker* newEm = NULL;
			cout << "请输入员工的新编号:" << endl;
			cin >> newId;
			cout << "请输入员工的新姓名:" << endl;
			cin >> newName;
			cout << "1、底层打工人   2、部门经理   3、老板" << endl;
			cout << "请输入员工的新职位(输入编号):" << endl;
			cin >> newDid;

			delete this->m_emArray[index];
			if (newDid == 1) {
				newEm = new Employee(newId, newName, newDid);
			}
			else if (newDid == 2) {
				newEm = new Manger(newId, newName, newDid);
			}
			else {
				newEm = new Boss(newId, newName, newDid);
			}
			this->m_emArray[index] = newEm;
			this->save();
			cout << "员工信息修改完成!!!" << endl;
		}
		system("pause");;
	}
}

void WorkerManger::findEmp() {
	if (getEmNum() == 0)
	{
		cout << "还未录入员工信息!!!" << endl;
		system("pause");
		return;
	}
	int id;
	int doId;
	int index = -1;
	string name;
	cout << "请输入查找方式(1、编号  2、姓名):" << endl;
	cin >> doId;
	if (doId == 1) {
		
		cout << "请输入员工编号:" << endl;
		cin >> id;
		index = this->isExist(id);
		if (index == -1) {
			cout << "输入编号有误" << endl;
		}
		else
		{
			this->m_emArray[index]->showInfo();
		}
		
	}
	else if (doId == 2) {
		cout << "请输入员工姓名:" << endl;
		cin >> name;
		int flag = 1;
		for (int i = 0; i < m_emNum; ++i) {
			if (name == m_emArray[i]->m_name) {
				flag = 0;
				this->m_emArray[i]->showInfo();
				cout << endl;
			}
		}
		if (flag)
			cout << "未找到该员工!!!!" << endl;
	}
	else {
		cout << "输入有误!!!!" << endl;
		return;
	}
	system("pause");
}


//按编号排序
void WorkerManger::sortEmp() {
	if (getEmNum() == 0) {
		cout << "还未录入员工信息" << endl;
		system("pause");
		return;
	}
	int opt = 1;
	cout << "请输入排序方式(1、升序  2、降序):";
	cin >> opt;
	if (opt == 1) {
		for (int i = 0; i < this->m_emNum - 1; ++i) {
			for (int j = i + 1; j < this->m_emNum - 1; ++j) {
				if (m_emArray[i]->m_id > m_emArray[j]->m_id) {
					Worker* t = m_emArray[i];
					m_emArray[i] = m_emArray[j];
					m_emArray[j] = t;
				}

			}
		}
	}
	else {
		for (int i = 0; i < this->m_emNum - 1; ++i) {
			for (int j = i + 1; j < this->m_emNum; ++j) {
				if (m_emArray[i]->m_id < m_emArray[j]->m_id) {
					Worker* t = m_emArray[i];
					m_emArray[i] = m_emArray[j];
					m_emArray[j] = t;
				}

			}
		}
	}
	this->save();
	cout << "排序完毕" << endl;
	system("pause");
}


void WorkerManger::cleanFile() {
	int opt = 2;
	cout << "确认清空文件!!!!" << endl;
	cout << "1、确认" << endl;
	cout << "2、返回" << endl;
	cin >> opt;
	if (opt == 2)
		return;
	//ios::trunc如果文件存在,则删除重新创建
	ofstream ofs(FILENAME, ios::trunc);
	ofs.close();
	if (this->m_emArray != NULL) {
		for (int i = 0; i < this->m_emNum; ++i) {
			if (this->m_emArray[i] != NULL)
				delete this->m_emArray[i];
		}
		this->m_emNum = 0;
		delete[] this->m_emArray;
		this->m_emArray = NULL;
	}
	cout << "清除成功!!!" << endl;
	system("pause");
}

void WorkerManger::showMenu() {
	cout << "****************************************************" << endl;
	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;
	cout << endl;
}

void WorkerManger::exitSystem() {
	cout << "欢迎您下次使用!!!" << endl;
	system("pause");
	exit(0);
}

//添加新员工
void WorkerManger::addEmp() {
	int addNum = 0;

	cout << "请输入要添加的员工的数量:" << endl;
	cin >> addNum;

	if(addNum > 0){
		int newSize = addNum + m_emNum;
		Worker** newArray = new Worker * [newSize];

		if (m_emNum != 0) {
			//先将原本的数据存入newArray里
			for (int j = 0; j < m_emNum; ++j) {
				newArray[j] = m_emArray[j];
			}
			//释放之前的空间
	//		delete[] this->m_emArray;
			
		}


		//输入员工信息
		for (int i = 0; i < addNum; ++i) {
		
			int t_id;
			string t_name;
			int t_dId;
			Worker* t_emp = NULL;
			cout << "请输入第" << i + 1 << "位员工的信息!!" << endl;
			cout << "请输入编号:";
			cin >> t_id;
			cout << "请输入姓名:";
			cin >> t_name;
			cout << "1、底层打工人  2、经理  3、老板" << endl;
			cout << "请选择岗位(输入数字):";
			cin >> t_dId;
			if (isExist(t_id) != -1) {
				cout << "该员工编号重复,请重新录入" << endl;
				system("pause");
				--i;
				continue;
			}
			//根据岗位new不同的worker类,
			if (t_dId == 1)
				t_emp = new Employee(t_id, t_name, t_dId);
			else if (t_dId == 2)
				t_emp = new Manger(t_id, t_name, t_dId);
			else if (t_dId == 3)
				t_emp = new Boss(t_id, t_name, t_dId);
			else 
			{
				cout << "非法输入!!!" << endl;
				break;
			}
			//暂时存入newArray里
			newArray[i + this->m_emNum] = t_emp;
		}
		delete[] this->m_emArray;
		//得到新的指向
		m_emArray = newArray;
	
		
		//更新员工数量
		m_emNum = newSize;
		m_isEmpty = false;
		cout << "员工信息录入完成!!!" << endl;
		save();
		system("pause");
		
	}
	else {
		cout << "输入有误!!!" << endl;
		system("pause");
		
	}
}
主程序
#include
#include"WorkManger.h"

#include"Worker.h"
#include"Manger.h"
#include"Employee.h"
#include"Boss.h"


using namespace std;




int main() {
	
	//实例化管理者对象
	WorkerManger wm;
	
	int choice = 0;
	while (true) {
		//调用展示菜单的成员函数
		system("cls");
		wm.showMenu();
		cout << "请输入您的选择:" << endl;
		cin >> choice;
		switch (choice)
		{
			case 0: //退出系统
				wm.exitSystem();
				break;
			case 1: //增加职工
				wm.addEmp();
				break;
			case 2: //显示职工
				wm.showWorker();
				break;
			case 3: //删除职工
				wm.delEmp();
				break;
			case 4: //修改职工
				wm.modEmp();
				break;
			case 5: //查找职工
				wm.findEmp();
				break;
			case 6: //编号排序
				wm.sortEmp();
				break;
			case 7: //清空数据
				wm.cleanFile();
				break;
			default:
				system("cls");
				break;
		}
	}
	system("pause");
	return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/569358.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-09
下一篇 2022-04-09

发表评论

登录后才能评论

评论列表(0条)

保存