栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

2022-07-02

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2022-07-02

C++之基于多态的管理系统
案例来源于:
https://www.bilibili.com/video/BV1et411b73Z?p=167&vd_source=0475e9436337406639ae97cc3cfcc136
实现功能:
增、删、改、查、排序,清空、退出系统

代码如下:
1.主函数

#include
#include"employee.h"
#include"manager.h"
#include"boss.h"
#include "workerManager.h"
using namespace std;

int main()
{
	WorkerManager wm;
	int choice = 0;
	while (true)
	{
		wm.Show_Menu();
		cout << "请输入你的选择" << endl;
		cin >> choice;
		switch (choice)
		{
		case 0: //退出系统
			wm.exitSystem();
			break;
		case 1: //添加
			wm.Add_Emp();
			break;
		case 2: //显示
			wm.Show_Emp();
			break;
		case 3: //删除
		{
			int ret = wm.isExist(1);
			if (ret != -1)
			{
				cout << "职工存在" << endl;
			}
			else { cout << "不存在" << endl; }
			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:
			cout << "不是有效指令,请重新输入:" << endl;
			system("cls");
				break;
		}

	}
	system("pause");
	return 0;
}

2.boss.cpp文件

#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->m_DeptId
		<< "t制定公司规划,并向经理交接任务" << endl;
}
string Boss::getDepName()
{
	return string("Boss");
}

boss.h文件

#pragma once
#include
#include"worker.h"
#include
using namespace std;

class Boss : public Worker
{
public:
	Boss(int id, string name, int dId);
	virtual void showInfo();
	virtual string getDepName();
};

3.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
		<< "t职工姓名" << this->m_name
		<< "t岗位" << this->m_DeptId
		<< "t完成管理任务,并辅助完成生产任务" << endl;
}
string Manager::getDepName()
{
	return string("经理");
}

manager.h文件

#pragma once
#include
#include"worker.h"
#include
using namespace std;

class Manager : public Worker 
{public:
	Manager(int id, string name, int dId);
	virtual void showInfo();
	virtual string getDepName();
};

4.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
		<< "t职工姓名" << this->m_name
		<< "t岗位" << this->m_DeptId
		<< "t完成生产任务" << endl;
}
string Employee::getDepName()
{
	return string("employee");
}

employee.h文件

#pragma once
#include
#include"worker.h"
#include
using namespace std;

class Employee :public Worker
{
public:
	Employee(int id, string name, int dId);
	virtual void showInfo();
	virtual string getDepName();
};

5.worker.h 头文件

#pragma once
#include
#include
using namespace std;

class Worker
{
public:
	virtual void showInfo() = 0;
	virtual string getDepName() = 0;
	int m_Id;
	string m_name;
	int m_DeptId;
};

6.wokerManager.cpp文件

/1.菜单界面
//2.增删改查
//3.与文件进行交互
//使用父类指针指向子类,在堆区开辟空间保存父类指针。
#include "workerManager.h";

WorkerManager::WorkerManager()
{
	this->m_EmpNum = 0;
	this->m_EmpArray = NULL;
	int num = this->get_EmpNum();
	cout << "已有数量为:" <m_EmpNum = num;

	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;
	}
	this->m_EmpArray = new Worker *[this->m_EmpNum];
	init_Emp();
	for (int i = 0; i < m_EmpNum; i++)
	{
		cout << "职工号: " << this->m_EmpArray[i]->m_Id
			<< "职工姓名:" << this->m_EmpArray[i]->m_name
			<< "部门编号: " << this->m_EmpArray[i]->m_DeptId << endl;

	}

}
void WorkerManager::Show_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::exitSystem()
{
	cout << " *已成功退出* " << endl;
	cout << "**欢迎下次使用**" << endl;
	system("pause");
	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_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 << " 个新职工编号: " << endl;
				cin >> id;
				cout << "请输入第 " << i + 1 << " 个新职工姓名:" << endl;
				cin >> name;


				cout << "请输入该职工岗位: " << endl;
				cout << "1.普通员工" << endl;
				cout << "2.管理人员" << endl;
				cout << "3.Big Boss" << endl;
				cout << "根据岗位,请输入岗位编号:" << 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_EmpArray;
			//更改新空间的指向
			this->m_EmpArray = newSpace;
			//更新新的个数
			this->m_EmpNum = newSize;
			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_EmpArray[i]->m_Id << "  "
			<< this->m_EmpArray[i]->m_name << "  "
			<< this->m_EmpArray[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_EmpArray[index] = worker;
		index++;

	}
}

void WorkerManager::Show_Emp()
{
	if (this->m_FileIsEmpty)
	{
		cout << "文件记录不存在或者记录为空" << endl;

	}
	else
	{
		for (int i = 0; i < this->m_EmpNum; i++)
		{
			this->m_EmpArray[i]->showInfo();
		}
	}

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

}
void WorkerManager::Del_Emp()
{
	if (this->m_FileIsEmpty)
	{
		cout << "文件不存在" << endl;

	}
	else 
	{
		cout << "请输入需要删除的职工编号:" << endl;
		int id = 0;
		cin >> id;
		int index=this->isExist(id);
		if (index!=-1)
		{
			for (int i = 0; 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");
	}
	//isExit(id);


}
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::Mod_Emp()
{
	if (this->m_FileIsEmpty)
	{
		cout << "当前文件不存在!!!" << endl;
	}
	else 
	{
		cout << "请输入需要修改的职工的编号信息:" << endl;
		int id = 0;
		cin >> id;
		int ret=this->isExist(id);
		if (ret!=-1)
		{
			delete this->m_EmpArray[ret];
			int newId = 0;
			string newName = "";
			int dSelect = 0;
			cout << "查到:" << id<<" 号职工,请输入新职工号"<> newId;
			cout << "请输入名字:" << endl;
			cin >> newName;
			cout << "请输入该职工岗位: " << endl;
			cout << "1.普通员工" << endl;
			cout << "2.管理人员" << endl;
			cout << "3.Big Boss" << endl;
			cout << "根据岗位,请输入岗位编号:" << 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_EmpArray[ret] = worker;
			cout << "修改成功!!!" << endl;
			cout << this->m_EmpArray[ret]->m_DeptId << endl;
			this->save();

		}
		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;
		cout << "请输入你选择的查询方式:" << 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_EmpArray[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 (m_EmpArray[i]->m_name==name)
				{
					cout << "查询成功!!!" << endl;
					cout << "职工编号为:" << " "
						<< m_EmpArray[i]->m_Id << " "
						<< "的信息如下:" << endl;
					flag = true;
					this->m_EmpArray[i]->showInfo();
				}

			}
			if (flag = false)
			{
				cout << "查询无果..." << endl;

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

void WorkerManager::Sort_Emp()
{
	if (this->m_FileIsEmpty)
	{
		cout << "当前文件不存在!!!" << endl;
		system("pause");
		system("cls");
	}
	else
	{
		cout << "请输入排序的方式:" << endl;
		cout << "1.升序" << endl;
		cout << "2.降序" << endl;
		cout << "请输入你选择的排序方式:" << endl;
		int select = 0;
		cin >> select;
		for (int i = 0; i < this->m_EmpNum; i++)
		{
			int minOrMax = i;
			for (int j = i; j < this->m_EmpNum; j++)
			{
				if (select==1)
				{
					if (m_EmpArray[i]->m_Id>m_EmpArray[j]->m_Id)
					{
						minOrMax = j;
					}
				}
				else 
				{
					if (m_EmpArray[i]->m_Id < m_EmpArray[j]->m_Id)
					{
						minOrMax = j;
					}
				}

			}
			if (i!=minOrMax)
			{
				Worker *temp = m_EmpArray[i];
				m_EmpArray[i] = m_EmpArray[minOrMax];
				m_EmpArray[minOrMax] = temp;

			}

		}

		cout << "排序成功!" << endl;
		this->save();
		this->Show_Emp();

	}

}
void WorkerManager::Clean_File()
{
	cout << "正在执行清空操作:" << endl;
	cout << "1.继续执行" << endl;
	cout << "2.取消执行" << endl;
	int select = 0;
	cout<<"请选择你的操作:"<> select;
	if (select=1)
	{
		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;

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

}

//释放空间
WorkerManager::~WorkerManager()
{
	if (this->m_EmpArray!=NULL)
	{
		delete[] this->m_EmpArray;
		this->m_EmpArray = NULL;
	}
}

wokerManager.h文件

#pragma once//防止头文件重复包含
#include
#include
#include"worker.h"
#include"employee.h"
#include"manager.h"
#include"boss.h"
#include
using namespace std;
#define FILENAME "empFile.txt"

class WorkerManager 
{
public:

	//构造函数
	WorkerManager();
	void Show_Menu();
	void save();
	void exitSystem();
	//析构函数
	~WorkerManager();
	int m_EmpNum;
	bool m_FileIsEmpty;

	int get_EmpNum();

	Worker ** m_EmpArray;
	void Add_Emp();

	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();

};

功能0:退出系统

功能1:

功能2

功能3:

功能4:

功能5:按照编号,按照姓名


功能6:1.升序,2降序

功能7:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1004961.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号