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

c++ 基础知识-类与对象-实践-员工管理系统(记录)2

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

c++ 基础知识-类与对象-实践-员工管理系统(记录)2

c++ 基础知识-类与对象-实践-员工管理系统(记录)2 1.员工管理系统操作类
#pragma once//防止头文件重复包含
#ifndef _WORKMANAGER_
#define _WORKMANAGER_
#include 
#include "worker.h"
#include "employee.h"
#include "manager.h"
#include "boss.h"
#include 
#include 
using namespace std;

#define FILENAME "E:\c++_workspace\code_practice\empFile.txt"


//class Employee;
class workManager
{
public:
	//无参数构造函数
	workManager();

	//菜单展示函数
	void Show_Menu();
	
	//选项
	void Choice_Putton();

	//退出系统
	void Exit_System();

	//添加员工
	void Add_Worker();

	//保存数据到文本文档
	void Save_Data();

	//获取当前文件中员工人数
	int Get_EmpNum();

	//初始化信息,将文件中内容保存到指定数组
	void Init_Worker();

	//显示信息
	void   Display_Information();

	//删除员工
	void Remove_Worker();

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

	//查找指定员工信息
	void Find_Worker();

	//按照编号排序
	void Sort_Worker();

	//清空数据
	void Clear_Worker();

	//析构函数
   ~workManager();

public:
	//记录职工人数
	 int m_EmpNum;

	 //职工数组指针
	 //注意指针类型需要释放
	Worker ** m_EmpArray;

	//标志文件是否为空
	bool m_FileIsEmpty;
};


#endif
#include "workManager.h"

workManager::workManager()
{
	ifstream ifs;
	ifs.open(FILENAME,ios::in);
	
	//1.文件不存在情况
	if(!ifs.is_open())
	{
		cout<<"文件不存在"<m_EmpNum = 0;
		this->m_FileIsEmpty = true;
		this->m_EmpArray = NULL;
		ifs.close();
		return;
	}

	//2.文件存在,内容为空
	char ch;
	ifs >> ch;
	if (ifs.eof())
	{
		//文件为空
		cout<<"文件为空!"<m_EmpNum = 0;
		//初始化数组指针
		this->m_EmpArray = NULL;
		//初始化文件标识
		this->m_FileIsEmpty = true;
		ifs.close();
		return;
	}

	//3.存在文件,文件中有内容,输出人员个数
	int num = this->Get_EmpNum();
	cout<<"职工人数为: "<m_EmpNum = num;
	//初始化信息
	this->Init_Worker();
}

void workManager::Show_Menu()
{
	cout<<"*******************************"<
	int choice = 0;//存储用户选项
	while(true)
	{
		this->Show_Menu();
		cout<<"请输入您的选择!!!"<> choice;//接受选项
		//m_choice = choice;
		switch(choice)
		{
		case 0://退出系统
			this->Exit_System();
			break;
		case 1://增加职工
			this->Add_Worker();
			break;
		case 2://显示职工
			this->Display_Information();
			break;
		case 3://删除职工
			this->Remove_Worker();
			break;
		case 4://修改职工
			this->Mod_Worker();
			break;
		case 5://查找职工
			this->Find_Worker();
			break;
		case 6://排序职工
			this->Sort_Worker();
			break;
		case 7://清空文件
			this->Clear_Worker();
			break;
		default:
			system("cls");
			break;
		}
	}
}

void workManager::Exit_System()
{
	cout<<"您选择退出系统!!!"<
	cout<<"请输入增加职工数量:  "<>addNum;

	if(addNum > 0)
	{
		//计算新的空间大小
		int newSize = 0;
		if (this->m_EmpNum > 0 )
		{
			newSize = this->m_EmpNum + addNum;
		}else{
			newSize = 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<<"请输入第 "<>id;

			//查重
			for (int j = 0; j < (this->m_EmpNum+i); j++)
			{
				if(id == newSpace[j]->m_Id)
				{
					cout<<"该员工编号重复"<>id;
				}
			}

			cout<<"请输入第 "<>name;
			
			//查重
			for (int j = 0; j < (this->m_EmpNum+i); j++)
			{
				if(name == newSpace[j]->m_Name)
				{
					cout<<"该员工姓名重复"<>name;
				}
			}

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

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

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

		//更改新空间的指向
		this->m_EmpArray = newSpace;

		//更改新空间的大小
		this->m_EmpNum = newSize;

		//文件是否存在
		this->m_FileIsEmpty = false;

		//保存信息
		this->Save_Data();

		//提示信息
		cout<<"成功添加"<
		cout<<"输入有误"<
	ofstream ofs;
	ofs.open(FILENAME,ios::out);//写文件

	//将每个人数据写入到文件中
	for (int i = 0; i < this->m_EmpNum;i++)
	{
		ofs<m_EmpArray[i]->m_Id << " "
			<m_EmpArray[i]->m_Name<< " "
			<m_EmpArray[i]->m_DeptId<
	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 workManager::Init_Worker()
{
	ifstream ifs;
	ifs.open(FILENAME,ios::in);
	int id;
	string name;
	int dId;
	int num = 0;
	Worker ** newSpace = new Worker*[this->m_EmpNum];
	while(ifs >> id && ifs >> name && ifs >> dId)
	{
		Worker * worker = NULL;
		switch(dId)
		{
		case 1://普通员工
			worker = new Employee(id,name,dId);
			break;
		case 2://经理
			worker = new Manager(id,name,dId);
			break;
		case 3://老板
			worker = new Boss(id,name,dId);
			break;
		}

		newSpace[num++] = worker;
	}

	//更改新空间的指向
	this->m_EmpArray = newSpace;

}

void   workManager::Display_Information()
{
	if (this->m_FileIsEmpty == true)
	{
		cout<<"文件不存在或文件为空!!!"<
		cout<<"显示员工信息"<m_EmpNum;i++)
		{
			cout<<"员工编号:"<m_EmpArray[i]->m_Id << " "
				<<"员工姓名:"<m_EmpArray[i]->m_Name<< " "
				<<"员工职位等级:"<m_EmpArray[i]->m_DeptId<
	if (this->m_FileIsEmpty == true)
	{
		cout<<"文件不存在或文件为空!!!"<
		cout<<"请输入待删除员工数量:  "<>removeNum;

		//保证删除个数不能大于原有数据个数
		if(removeNum > 0 && this->m_EmpNum >= removeNum)
		{
			//删除数据
			for(int i = 0; i < removeNum;i++)
			{
				cout<<"请输入第 "<>choice;
				switch(choice)
				{
				case 1://员工编号
					cout<<"请输入待删除员工编号"<>id;
					for (int i = 0; i < this->m_EmpNum;i++)
					{
						if(id == this->m_EmpArray[i]->m_Id)
						{
							while (i < this->m_EmpNum-1)
							{
								this->m_EmpArray[i] = this->m_EmpArray[i+1];//依次用后续一个元素覆盖前一个元素,直到数组尾。
								i++;
							}
							flag = true;
							this->m_EmpNum--;
						}
					}
					if (flag == false)
					{
						cout<<"没有该员工编号,请核对后重新输入!!!"<>name;
					for (int i = 0; i < this->m_EmpNum; i++)
					{
						if(name == this->m_EmpArray[i]->m_Name)
						{
							while (i < this->m_EmpNum-1)
							{
								this->m_EmpArray[i] = this->m_EmpArray[i+1];//依次用后续一个元素覆盖前一个元素,直到数组尾。
								i++;
							}
							flag = true;
							this->m_EmpNum--;
						}
					}
					if (flag == false)
					{
						cout<<"没有该员工姓名,请核对后重新输入!!!"<Save_Data();

			//提示信息
			cout<<"成功删除"<
			cout<<"输入有误"<
	if (this->m_FileIsEmpty == true)
	{
		cout<<"文件不存在或文件为空!!!"<
		cout<<"请输入待修改员工数量:  "<>modNum;

		//保证修改个数不能大于原有数据个数
		if(modNum > 0 && this->m_EmpNum >= modNum)
		{
			//修改数据
			for(int i = 0; i < modNum;i++)
			{
				int id;
				string name;
				int dId;
				bool flag = false;
				int choice;
				cout<<"请输入第 "<>choice;
				switch(choice)
				{
				case 1://员工编号
					{
						cout<<"请输入待修改员工编号"<>id;
						for (int i = 0; i < this->m_EmpNum;i++)
						{
							if(id == this->m_EmpArray[i]->m_Id)
							{

								cout<<"请输入待修改员工信息"<>id;
								cout<<"请输入修改后员工姓名:"<>name;
								cout<<"请输入修改后员工职位:"<>dId;

								//判断编号姓名是否重复
								for (int j = 0; j < this->m_EmpNum; ++j)
								{
									if(id != this->m_EmpArray[j]->m_Id || name != this->m_EmpArray[j]->m_Name)
									{
										this->m_EmpArray[i]->m_Id = id;
										this->m_EmpArray[i]->m_Name = name;
										this->m_EmpArray[i]->m_DeptId = dId;
										flag = true;
									}else{
										if(name == this->m_EmpArray[j]->m_Name)
										{
											cout<<"该员工姓名重复"<>name;
										}
										else if(id == this->m_EmpArray[j]->m_Id)
										{
											cout<<"该员工编号重复"<>id;
										}else{
											cout<<"该员工编号姓名均重复"<>id;
											cout<<"请重新输入该员工姓名"<>name;
										}
									}
								}
							}
						}
						if (flag == false)
						{
							cout<<"没有该员工编号,请核对后重新输入!!!"<
						cout<<"请输入待修改员工姓名"<>name;
						for (int i = 0; i < this->m_EmpNum; i++)
						{
							if(name == this->m_EmpArray[i]->m_Name)
							{
								cout<<"请输入待修改员工信息"<>id;
								cout<<"请输入修改后员工姓名:"<>name;
								cout<<"请输入修改后员工职位:"<>dId;

								//判断编号姓名是否重复
								for (int j = 0; j < this->m_EmpNum; ++j)
								{
									if(id != this->m_EmpArray[j]->m_Id || name != this->m_EmpArray[j]->m_Name)
									{
										this->m_EmpArray[i]->m_Id = id;
										this->m_EmpArray[i]->m_Name = name;
										this->m_EmpArray[i]->m_DeptId = dId;
										flag = true;
									}else{
										if(name == this->m_EmpArray[j]->m_Name)
										{
											cout<<"该员工姓名重复"<>name;
										}
										else if(id == this->m_EmpArray[j]->m_Id)
										{
											cout<<"该员工编号重复"<>id;
										}else{
											cout<<"该员工编号姓名均重复"<>id;
											cout<<"请重新输入该员工姓名"<>name;
										}
									}
								}
							}
						}
						if (flag == false)
						{
							cout<<"没有该员工姓名,请核对后重新输入!!!"<Save_Data();

			//提示信息
			cout<<"成功修改"<
	if (this->m_FileIsEmpty == true)
	{
		cout<<"文件不存在或文件为空!!!"<>choice;
	switch(choice)
	{
	case 1://员工编号
		cout<<"请输入查找员工编号"<>id;
		for (int i = 0; i < this->m_EmpNum;i++)
		{
			if(id == this->m_EmpArray[i]->m_Id)
			{
				cout<<"员工编号:"<m_EmpArray[i]->m_Id << " "
					<<"员工姓名:"<m_EmpArray[i]->m_Name<< " "
					<<"员工职位等级:"<m_EmpArray[i]->m_DeptId<
			cout<<"没有该员工编号,请核对后重新输入!!!"<>name;
		for (int i = 0; i < this->m_EmpNum; i++)
		{
			if(name == this->m_EmpArray[i]->m_Name)
			{
				cout<<"员工编号:"<m_EmpArray[i]->m_Id << " "
					<<"员工姓名:"<m_EmpArray[i]->m_Name<< " "
					<<"员工职位等级:"<m_EmpArray[i]->m_DeptId<
			cout<<"没有该员工姓名,请核对后重新输入!!!"<
	if (this->m_FileIsEmpty == true)
	{
		cout<<"文件不存在或文件为空!!!"<
		cout<<"请选择排序方式:"<>choice;
		//选择排序
		for (int i = 0; i < this->m_EmpNum; i++)
		{
			int minOriMax = i;
			for (int j = i + 1; j < this->m_EmpNum; j++)
			{
				if (choice == 1)
				{
					if(this->m_EmpArray[minOriMax]->m_Id > this->m_EmpArray[j]->m_Id)
					{
						minOriMax = j;
					}
				}
				else//降序
				{
					if(this->m_EmpArray[minOriMax]->m_Id < this->m_EmpArray[j]->m_Id)
					{
						minOriMax = j;
					}
				}
			}

			if (i != minOriMax)
			{
				Worker * temp = this->m_EmpArray[i];
				this->m_EmpArray[i] = this->m_EmpArray[minOriMax];
				this->m_EmpArray[minOriMax] = temp;
			}
		}
		cout<<"排序成功!!!"<Save_Data();
		this->Display_Information();
	}
}

void workManager::Clear_Worker()
{
	if (this->m_FileIsEmpty == true)
	{
		cout<<"文件不存在或文件为空!!!"<
		
		cout<<"确认将员工数据清空!!!"<>choice;
		if (choice == 1)
		{
			for (int i = 0; i < this->m_EmpNum; i++)
			{
				Worker * temp = NULL;
				this->m_EmpArray[i] = temp;
			}
			this->m_EmpNum = 0;
			this->m_FileIsEmpty = true;
			cout<<"员工数据已清空!!!"<Save_Data();
			system("pause");
			system("cls");
		}
		else
		{
			system("pause");
			system("cls");
		}
	}
}

workManager::~workManager()
{
	//释放内存
	if (this->m_EmpArray != NULL)
	{
		delete [] this->m_EmpArray;
		this->m_EmpArray = NULL;
	}
}
2.调用主函数
#include "workManager.h"
#include "employee.h"
#include "boss.h"
#include "manager.h"
using namespace std;

void test_fun()
{
	workManager wm;
	wm.Choice_Putton();
}

int main()
{
	test_fun();
	return 0;
}  
暑期编程PK赛 得CSDN机械键盘等精美礼品!
转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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