【C++第二阶段】案例-职工管理系统

以下内容仅为当前认识,可能有不足之处,欢迎讨论!



案例=>职工管理系统

首先写一个workmanager的类,里面有各种各样的方法。展示职工信息,对职工信息进行增删改查。

接着,写成员函数——菜单功能。一个普通的展示界面。

0.退出功能

退出功能需要重刷新屏幕,然后调用退出函数。

完整的流程,应该是调用某个方法后刷新一遍屏幕,以此规整。

1.增加职工功能

将职工变为抽象类,分为3种,普通职工,经理职工,老板职工。

抽象类的打工人对象,有展示信息函数,也有获得其职能函数。同时也有属性。

然后分别对普通职工、经理职工、老板职工重写其父类纯虚函数。

最后,先测试添加职工功能是否完善。然后再尝试创建时即写入文件操作。

2.显示职工信息

判断文件是否存在,如果存在才显示职工信息,此时我重写了左移运算符。

3.删除职工信息

按照编号删除职工。首先看职工号是否存在,删除之后,职工数量-1,并保存至文件中。

4.修改职工信息

先判断编号职工/姓名在不在,如果在,获取其索引,更改内容,将其写入。

5.查找职工信息

查找职工信息有两种,按照编号查找,按照姓名查找。

6.排序职工

排序是普通的选择排序。

7.清空所有文档

删除所有内容。

以下是各个文件中的内容:

头文件:

cattle.h

#pragma once 
#include<iostream>
#include<string>
using namespace std;

//牛马类
//牛马的属性为牛马编号,牛马代号,牛马所在部门编号
//牛马的行为:岗位职责信息描述,获取岗位名称

class Cattle {

public:
	//属性值
	int cattle_ID;
	string cattle_name;
	int cattle_part;


public:
	//打印自身信息
	//获取职能信息字符
	virtual void printSelf()=0 ;
	virtual string getDuty()=0 ;
	//virtual ostream& operator<<(ostream& out) = 0;
};

boss_cattle.h

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

using namespace std;

class BossCattle : public Cattle {
public:
	//构造函数&析构函数
	BossCattle(int boss_ID, string boss_name, int boss_part);
	~BossCattle();

public:
	void printSelf();
	string getDuty();
	ostream& operator<<(ostream& out);

	//ostream& operator<<(ostream& out, Cattle& cattle) {
	//	//for (int i = 0; i < wm.numCattles; i++) {
	//	cout << "职工编号:" << cattle.cattle_ID << "\t";
	//	cout << "职工姓名:" << cattle.cattle_name << "\t";
	//	cout << "职工职位:" << cattle.getDuty() << endl;
	//	//}
	//	return out;
	//}
};


manager_cattle.h

#pragma once
#include<iostream>
#include<string>
#include"cattle.h"
using namespace std;



//经理牛马
//继承牛马类,同时重写对应方法

class ManagerCattle :public Cattle {

public:
	//构造函数与析构函数
	ManagerCattle(int manager_ID, string manager_name, int manager_part);
	~ManagerCattle();

public:
	//重写父类函数
	void printSelf();
	string getDuty();
	//ostream& operator<<(ostream& out);
};

common_cattle.h

#pragma once
#include<iostream>
#include<string>
#include"cattle.h"
using namespace std;

class CommonCattle : public Cattle {
	//构造函数与析构函数
public:
	CommonCattle(int common_ID, string common_name, int common_part);
	~CommonCattle();

	//普通牛马
public:
	void printSelf();
	string getDuty();

public:
	//ostream& operator<<(ostream& out);
	
};

workermanager.h

#pragma once  //防止头文件重复包含
#include<iostream>  //包含输入输出流的头文件
using namespace std;  //使用标准命名空间
#include"cattle.h"
#include"boss_cattle.h"
#include"common_cattle.h"
#include"manager_cattle.h"
//包含文件流操作的头文件
#include<fstream>
#include<string>
#include<cstdlib>
//#include<stdtio.h>

#define FILENAME "temFile.txt"


class WorkerManager {
public:
	int numCattles;
	//这里的数组是什么类型的?该怎么写?
	Cattle** cattle_array_ptr;
	//定义是否有文件存在
	bool FileIsExist;

public:
	//构造函数&析构函数
	WorkerManager();

	~WorkerManager();

public:

	//展示菜单功能
	void ShowMenu();

	//退出程序功能
	void ExitMenu();

	//添加新员工功能
	void addCattles();

	//展示员工功能
	void showCattles();

	//写入文件功能
	void save();

	//得到已有的职员个数
	int get_numCattles();


	//初始化数组
	void initCattle_array_ptr(ifstream &file , int cattles_number);
	
	//重写左移运算符
	//ostream& operator<<(ostream& cout, Cattle** cattles_ptr);
	//ostream& operator<<(ostream& out);
	
	//删除职工操作
	//删除职工,就是将职工对应的指针指向空值,但是为了程序的鲁棒性,需要先判断是否存在要删除的职工。
	void deleteCattles();

	int existCattle(int &cattle_ID);

	void alterCattle();

	Cattle* getCattle();

	void searchCattle();

	//按照编号对职工排序,选择排序
	void sortedCattle();

	//清空文件
	void cleanCattle();


};

源文件:

cattle.cpp

#include<iostream>
#include<string>
#include"cattle.h"

using namespace std;


boss_cattle.cpp

#include<iostream>
#include<string>
#include"cattle.h"
#include"boss_cattle.h"

BossCattle::BossCattle(int boss_ID, string boss_name, int boss_part) {
	this->cattle_ID = boss_ID; 
	this->cattle_name = boss_name;
	this->cattle_part = boss_part;

}

BossCattle::~BossCattle() {

}

void BossCattle::printSelf() {
	cout << "老板姓名为:" << this->cattle_name << ".\t";
	cout << "老板编号为:" << this->cattle_ID << ".\t";
	cout << "老板部门为:" << this->getDuty() << ".\t"<<endl;
}

string BossCattle::getDuty() {
	return "老板";
}
//
//ostream& BossCattle::operator<<(ostream& out) {
//	cout << "老板姓名为:" << this->cattle_name << ".\t";
//	cout << "老板编号为:" << this->cattle_ID << ".\t";
//	cout << "老板部门为:" << this->getDuty() << ".\t" << endl;
//	return out;
//}

manager_cattle.cpp

#include<iostream>
#include<string>
#include"cattle.h"
#include"manager_cattle.h"

using namespace std;

ManagerCattle::ManagerCattle(int manager_ID, string manager_name, int manager_part) {
	this->cattle_ID = manager_ID; 
	this->cattle_name = manager_name;
	this->cattle_part = manager_part; 
}

ManagerCattle::~ManagerCattle() {

}

void ManagerCattle::printSelf() {
	cout << "经理姓名为:" << this->cattle_name << ".\t";
	cout << "经理编号为:" << this->cattle_ID << ".\t";
	cout << "经理部门为:" << this->getDuty() << "." << endl;
}

string ManagerCattle::getDuty() {
	return "经理";
}
//ostream& ManagerCattle::operator<<(ostream& out) {
//
//	cout << "经理姓名为:" << this->cattle_name << ".\t";
//	cout << "经理编号为:" << this->cattle_ID << ".\t";
//	cout << "经理部门为:" << this->getDuty() << "." << endl;
//	return out;
//}

common_cattle.cpp

#include<iostream>
#include<string>
#include"cattle.h"
#include"common_cattle.h"

using namespace std;

CommonCattle::CommonCattle(int common_ID, string common_name, int common_part){
	this->cattle_ID = common_ID;
	this->cattle_name = common_name;
	this->cattle_part = common_part;
}

CommonCattle::~CommonCattle() {
	cout << "over common cattle." << endl;
}

void CommonCattle::printSelf() {
	cout << "职工姓名为:" << this->cattle_name <<".\t";
	cout << "职工编号为:" << this->cattle_ID << ".\t" ;
	cout << "职工部门在:" << this->getDuty() << ".\t" << endl;
}

string CommonCattle::getDuty() {
	return "员工";
}
//ostream& CommonCattle::operator<<(ostream& out) {
//
//	cout << "职工姓名为:" << this->cattle_name << ".\t";
//	cout << "职工编号为:" << this->cattle_ID << ".\t";
//	cout << "职工部门在:" << this->getDuty() << ".\t" << endl;
//	return out;
//}

workmanager.cpp

#include"workmanager.h"
//#include <boost/type_index.hpp>


//构造函数
WorkerManager::WorkerManager() {

	ifstream file;
	file.open(FILENAME, ios::in);
	//如果文件打不开,说明文件不存在,那么就说明文件不存在。
	if (!file.is_open()) {
		//如果文件不存在,那么初始化成员属性
		cout << "文件不存在。。。" << endl;
		this->numCattles = 0;
		this->FileIsExist = true;
		this->cattle_array_ptr = NULL;

		//构造函数中初始化成员属性
		//this->numCattles = 0;
		//this->cattle_array_ptr = NULL;
	}
	else{
		char first_char;
		file >> first_char;
		//如果文件读取为eof,则说明文件内容为空。
		if (file.eof()) {
			cout << "文件内容为空." << endl;
			this->numCattles = 0;
			this->FileIsExist = true;
			this->cattle_array_ptr = NULL;
		}
		else {
			//让文件回到开头重新开始读取
			file.seekg(ios::beg);
			int number;
			number = this->get_numCattles();
			this->numCattles = number;
			cout << "职工人数为" << number << endl;

			//初始化数组指针。
			initCattle_array_ptr(file , number);
		}
	}
	file.close();
	for (int i = 0; i < this->numCattles; i++) {
		//this->cattle_array_ptr[i]->printSelf();
		//cout << this->cattle_array_ptr[i] << endl;
	}
}

//析构函数
WorkerManager::~WorkerManager() {

	if (this->cattle_array_ptr != NULL) {

		for (int i = 0; i < this->numCattles; i++) {
			if (this->cattle_array_ptr[i] == NULL) {
				delete this->cattle_array_ptr[i];
				this->cattle_array_ptr[i] = NULL;
			}
		}

		delete this->cattle_array_ptr;
		this->cattle_array_ptr = NULL;
		this->numCattles = 0;
	}

}

//展示菜单功能
void WorkerManager::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;
}


Cattle* WorkerManager::getCattle() {
	int cattle_ID;
	string cattle_name;
	int cattle_partID;
	Cattle* little_cattle = NULL;

	cout << "请输入职工编号:";
	cin >> cattle_ID;
	//cattle_ptr[i]->cattle_ID = cattle_ID;

	cout << "请输入职工姓名:";
	cin >> cattle_name;
	//cattle_ptr[i]->cattle_name = cattle_name;

	cout << "请输入职工部门(1=普通员工;2=经理;3=老板):";
	cin >> cattle_partID;
	cout << endl;
	switch (cattle_partID) {
	case 1:
		little_cattle = new CommonCattle(cattle_ID, cattle_name, 1);
		break;
	case 2:
		little_cattle = new ManagerCattle(cattle_ID, cattle_name, 2);
		break;

	case 3:
		little_cattle = new BossCattle(cattle_ID, cattle_name, 3);
		break;

	default:
	{}
	}
	return little_cattle;
}

void WorkerManager::ExitMenu() {
	system("pause");
	exit(0);
}

void WorkerManager::addCattles() {
	int after_number;
	cout << "请输入添加职工数量:";
	cin >> after_number;
	//这里是为了新输入的职工个数。


	//判断是否确实是正数。
	if (after_number > 0) {
		//添加职工,创建职工数组,开辟新空间
		int total_number = this->numCattles + after_number;
		Cattle** cattle_ptr = new Cattle * [total_number];

		if (this->cattle_array_ptr != NULL) {

			//如果之前有职工,那么就对其读取。
			for (int i = 0; i < this->numCattles; i++) {
				//这个目的是什么呢?读取原来的职工信息
				cattle_ptr[i] = this->cattle_array_ptr[i];
			}
		}

		for (int i = this->numCattles; i < total_number; i++) {

			//int cattle_ID;
			//string cattle_name;
			//int cattle_partID;

			//cout << "请输入职工编号:";
			//cin >> cattle_ID;
			cattle_ptr[i]->cattle_ID = cattle_ID;

			//cout << "请输入职工姓名:";
			//cin >> cattle_name;
			cattle_ptr[i]->cattle_name = cattle_name;

			//cout << "请输入职工部门(1=普通员工;2=经理;3=老板):";
			//cin >> cattle_partID;
			//cout << endl;
			
			Cattle* little_cattle = getCattle();
			/*switch (cattle_partID) {
			case 1:
				little_cattle = new CommonCattle(cattle_ID, cattle_name, 1);
				break;
			case 2:
				little_cattle = new ManagerCattle(cattle_ID, cattle_name, 2);
				break;

			case 3:
				little_cattle = new BossCattle(cattle_ID, cattle_name, 3);
				break;

			default :
				continue;
			}*/
			cattle_ptr[i] = little_cattle;
		}
		//删除原有空间
		delete[] this->cattle_array_ptr;

		//更改原空间指向
		this->cattle_array_ptr = cattle_ptr;

		//更改职工人数
		this->numCattles = total_number;

		//打印添加成功
		cout << "您已添加成功." << endl;
		system("pause");
		this->save();
	}
	else {
		cout << "请输入合适的数字." << endl;
		system("pause");
		system("cls");
	}
}

void WorkerManager::save() {
	ofstream fileManager;
	//fileManager.open(FILENAME, ios::out && ios::app);
	fileManager.open(FILENAME, ios::out);

	for (int i = 0; i < this->numCattles; i++) {
		fileManager << this->cattle_array_ptr[i]->cattle_ID << "\t"
			<< this->cattle_array_ptr[i]->cattle_name << "\t"
			<< this->cattle_array_ptr[i]->getDuty() << endl;
	}
	fileManager.close();

}

int WorkerManager::get_numCattles() {
	ifstream file;
	file.open(FILENAME, ios::in);
	int temp_ID;
	string temp_name;
	string temp_partID;
	int temp_cattles_number = 0;
	while (file>>temp_ID && file>>temp_name && file>>temp_partID) {
		temp_cattles_number++;
		//cout << "此时为" << temp_cattles_number << endl;
		//这里我写入的文件是字符串,所以应该要读取字符串……
	}
	file.close();
	return temp_cattles_number;
	
}

void WorkerManager::initCattle_array_ptr(ifstream &file , int cattles_number) {
	//读取文件中的职工数量之后,初始化数组个数为职工数量
	//然后逐行读取到数组中。
	this->cattle_array_ptr = new Cattle *[cattles_number];
	int read_ID;
	string read_name;
	string read_partID;
	int order = 0;
	while (!file.eof() && cattles_number--) {
		file >> read_ID;
		file >> read_name;
		file >> read_partID;
		if (read_partID == "员工") {
			this->cattle_array_ptr[order] = new CommonCattle(read_ID, read_name, 1);
		}
		else if (read_partID == "经理") {
			this->cattle_array_ptr[order] = new ManagerCattle(read_ID, read_name, 2);
		}
		else {
			this->cattle_array_ptr[order] = new BossCattle(read_ID, read_name, 3);
		}
		order += 1;
		//cout << "此时order= " << order << endl;
		//cout << "此时cattles_number = " << cattles_number  << endl;
	}
	/*
	for (int i = 0; i < order; i++) {
		cout << "====================" << endl;
		cout << this->cattle_array_ptr[i]->cattle_ID;
		cout << this->cattle_array_ptr[i]->cattle_name;
		cout << this->cattle_array_ptr[i]->cattle_part;
		this->cattle_array_ptr[i]->printSelf();
		cout << "====================" << endl;

	}*/


}


ostream& operator<<(ostream& out, Cattle* cattle) {
	cout << "职工姓名:\t" << cattle->cattle_name << "\t\t";
	cout << "职工编号:\t" << cattle->cattle_ID << "\t\t";
	cout << "职工等级:\t" << cattle->getDuty() << "\t\t";
	return out;
}
void WorkerManager::showCattles() {
	if (!this->numCattles) {
		cout << "无职工。" << endl;
	}
	else {
		for (int i = 0; i < this->numCattles; i++) {
			cout << this->cattle_array_ptr[i] <<endl;
			//this->cattle_array_ptr[i]->printSelf();
		}
	}
	//cout << this << endl;
	system("pause");
	system("cls");
}

void WorkerManager::deleteCattles() {
	int index;
	int cattle_ID;
	bool unfinished_state = true;
	while (true && unfinished_state) {
		cout << "请输入要删除的职工编号:";
		cin >> cattle_ID;
		index = existCattle(cattle_ID);
		if (index!=-1) {
			//如果存在要删除的职工编号,就将该指针指向空
			for (int i = index; i < this->numCattles; i++) {

				this->cattle_array_ptr[i] = this->cattle_array_ptr[i + 1];
				/*this->cattle_array_ptr[this->numCattles-1]->cattle_ID = NULL;
				this->cattle_array_ptr[this->numCattles-1]->cattle_name = "";
				this->cattle_array_ptr[this->numCattles-1]->cattle_part = NULL;*/

				//index是索引,this->numCattles是数量
				//如果index是最后一个呢?
				/*if (index == this->numCattles - 1) {
					cout << "您要删除的是最后一个元素。" << endl;
					this->cattle_array_ptr[i]->cattle_ID = NULL;
					this->cattle_array_ptr[i]->cattle_name = "";
					this->cattle_array_ptr[i]->cattle_part = NULL;
				}
				else {
					this->cattle_array_ptr[i] = this->cattle_array_ptr[i + 1];
				}*/
			}
			this->numCattles -= 1;
			unfinished_state = false;
		}
		else {
			cout << "请输入正确的职工编号。" << endl;
		}
	}
	this->save();
	system("pause");
	system("cls");
	

}

int WorkerManager::existCattle(int &cattle_ID) {
	int index = -1;
	for (int i = 0; i < this->numCattles; i++) {
		if (this->cattle_array_ptr[i]->cattle_ID == cattle_ID) {
			index = i;
			return index;
		}
	}
	return index;

}

void WorkerManager::alterCattle() {
	//先判断输入的这个编号职工在不在
	//如果在的话,就获取其索引,然后删除指针,重新新建一类对象,将其写入。
	int search_index;
	int cattle_index;
	cout << "请输入要寻找的职工编号:";
	cin >> search_index;
	cattle_index = existCattle(search_index);
	if (cattle_index != -1) {
		//该员工找到了,执行以下操作。
		int alter_cattle_ID;
		string alter_cattle_name;
		int alter_cattle_IDPart;

		int alter_choice;

		cout << "请输入要更改的内容,1=职工编号,2=职工姓名,3=职工部门,4=全部。" << endl;
		cin >> alter_choice;

		switch (alter_choice) {
		case 1:
		{
			int temp_cattle_ID = this->cattle_array_ptr[cattle_index]->cattle_ID;
			cout << "请输入要更改的职工编号:";
			cin >> alter_cattle_ID;
			cout << endl;
			this->cattle_array_ptr[cattle_index]->cattle_ID = alter_cattle_ID;
			cout << "原来的职工信息为:";
			cout << "职工姓名:\t" << this->cattle_array_ptr[cattle_index]->cattle_name << "\t\t职工编号:\t" << temp_cattle_ID  << "\t\t职工部门:\t" << this->cattle_array_ptr[cattle_index]->getDuty() << "." << endl;
			//cout<<this->cattle_array_ptr[cattle_index]
			cout << "现在的职工信息为:";
			cout <<this->cattle_array_ptr[cattle_index] << endl;
			cout << "修改成功!" << endl;
		//是否有更简单的方式实现?如何根据内容新建一个临时的职工?然后打印?
		}
		break;
		case 2:
		{
			string temp_name = this->cattle_array_ptr[cattle_index]->cattle_name;
			cout << "请输入要更改的职工姓名:";
			cin >> alter_cattle_name;
			cout << endl;
			this->cattle_array_ptr[cattle_index]->cattle_name = alter_cattle_name;
			cout << "原来的职工信息为:";
			cout << "职工姓名:\t" << temp_name << "\t\t职工编号:\t" << this->cattle_array_ptr[cattle_index]->cattle_ID  << "\t\t职工部门:\t" << this->cattle_array_ptr[cattle_index]->getDuty() << "." << endl;
			//cout<<this->cattle_array_ptr[cattle_index]
			cout << "现在的职工信息为:";
			cout << this->cattle_array_ptr[cattle_index] << endl;
			cout << "修改成功!" << endl;
		}
		break;
		case 3:
		{
			
			bool right = true;
			string temp_part = this->cattle_array_ptr[cattle_index]->getDuty();
			while (right) {				
				cout << "请输入要更改的职工所在部门(1=普通员工;2=经理;3=老板):";
				cin >> alter_cattle_IDPart;
				cout << endl;
				if (alter_cattle_IDPart > 3 || alter_cattle_IDPart < 0) {
					cout << "请输入正确的职工部门." << endl;
					continue;
				}
				else {
					this->cattle_array_ptr[cattle_index]->cattle_part = alter_cattle_IDPart;
					right = false;
				}//else
			}//while
			cout << "原来的职工信息为:";
			cout << "职工姓名:\t" << this->cattle_array_ptr[cattle_index]->cattle_name << "\t\t职工编号:\t" << this->cattle_array_ptr[cattle_index]->cattle_ID << "\t\t职工部门:\t" << temp_part << "." << endl;
			//cout<<this->cattle_array_ptr[cattle_index]
			cout << "现在的职工信息为:";
			cout << this->cattle_array_ptr[cattle_index] << endl;
			cout << "修改成功!" << endl;
		}//case

			break;
		case 4:
		{
				Cattle* temp_cattle = this->cattle_array_ptr[cattle_index];
				cout << "请输入要更改的职工编号:";
				cin >> alter_cattle_ID;
				cout << endl;

				cout << "请输入要更改的职工姓名:";
				cin >> alter_cattle_name;
				cout << endl;

				bool right = true;
				while (right) {
					cout << "请输入要更改的职工所在部门(1=普通员工;2=经理;3=老板):";
					cin >> alter_cattle_IDPart;
					if (alter_cattle_IDPart > 3 || alter_cattle_IDPart < 0) {
						cout << "请输入正确的职工部门." << endl;
						continue;
					}
					else {
						right = false;
						}//else
					}
				cout << endl;

				delete this->cattle_array_ptr[cattle_index];

				//Cattle* new_cattle = NULL;
				this->cattle_array_ptr[cattle_index] = getCattle();

				cout << "原来的职工信息为:";
				cout << temp_cattle << endl;
				//cout<<this->cattle_array_ptr[cattle_index]
				cout << "现在的职工信息为:";
				cout << this->cattle_array_ptr[cattle_index] << endl;
				/*switch (alter_cattle_IDPart) {
				case 1:
					new_cattle = new CommonCattle(alter_cattle_ID, alter_cattle_name, 1);
					break;
				case 2:
					new_cattle = new ManagerCattle(alter_cattle_ID, alter_cattle_name, 2);
					break;
				case 3:
					new_cattle = new BossCattle(alter_cattle_ID, alter_cattle_name, 3);
					break;
				default:
					break;
				}*/
				//this->cattle_array_ptr[cattle_index] = new_cattle;
		}
			break;
		}
		
	}
	else {
		//员工没有找到,执行以下输出。
		cout << "该员工不存在..." << endl;
	}
	system("pause");
	system("cls");
	this->save();
}


//查找员工,按照编号查找,按照姓名查找。
void WorkerManager::searchCattle() {
	/*输入是按照哪种方式查找,编号或者姓名。
	* 1.先判断文件是否存在,如果不存在,则直接说明文件不存在或者为空。
	* 在构造函数中已完成,所以直接用就行。。。
	* 2.输入按照哪种方式查找。并用switch语句进行选择。
	* 
	
	*/
	//
	bool flag = true;
	int search_method;
	while (flag) {
		cout << "请输入查找方式 1=>按照编号查找 | 2=>按照姓名查找 :";
		cin >> search_method;
		if (search_method == 1 || search_method == 2) {
			flag = false;
		}
		else {
			cout << "请输入正确的查找方式代码 1=>按照编号查找 | 2=>按照姓名查找 " << endl;
		}
	}
	switch (search_method) {
	case 1://按照编号查找
	{
		int search_ByID;
		cout << "请输入要查找的职工编号:";
		cin >> search_ByID;
		int search_IDresult = existCattle(search_ByID);
		if (search_IDresult != -1) {//-1也会被传进来,因为非0
			cout << this->cattle_array_ptr[search_IDresult] << endl;
		}
		else {
			cout << "查无此人。。。" << endl;
		}
	}
		break;
	case 2://按照姓名查找
	{

		string search_name_result;
		cout << "请输入查找人姓名:";
		cin >> search_name_result;
		bool flag = false;
		int search_index;
		for (int i = 0; i < this->numCattles; i++) {
			string temp_cattle_name = this->cattle_array_ptr[i]->cattle_name;
			if (temp_cattle_name == search_name_result) {
				search_index = i;
				flag = true;
				break;
			}
			else {
				continue;
				//cout << "查无此人。。。" << endl;
			}
		}
		if (flag) {
			cout << this->cattle_array_ptr[search_index] << endl;
		}
		else {
			cout << "查无此人。。。" << endl;
		}
	}
		break;
	}
	system("pause");
	system("cls");
}

void WorkerManager::sortedCattle() {
	/*选择排序
	随机选取一个锚点,将各个点与其比较。将最小值放在最前面。
	锚点 = 随机选的
	临时职工=新建一个指针。但是指向null。
	两个for循环
	for 对列表中每个员工编号进行遍历
		如果 锚点对应的编号>当前编号
			临时职工 = 当前编号的职工
	列表的第i个元素 = 当前编号
	*/
	//int random;
	//bool flag = false;
	//int num=0;
	//生成1-numcattles的随机数
	//random = (rand() % (this->numCattles - 1)) + 0 + 1;
	Cattle* cattle = NULL;
	for (int i = 0; i < this->numCattles; i++) {
		for (int j = i; j < this->numCattles; j++) {
			if (this->cattle_array_ptr[i]->cattle_ID     >      this->cattle_array_ptr[j]->cattle_ID) {
				cattle = this->cattle_array_ptr[i];
				this->cattle_array_ptr[i] = this->cattle_array_ptr[j];
				this->cattle_array_ptr[j] = cattle;
				//num++;
			}
		}
	}

	for (int i = 0; i < this->numCattles; i++) {
		cout << this->cattle_array_ptr[i] << endl;
	}
	system("pause");
	system("cls");
}

void WorkerManager::cleanCattle() {

	if (this->cattle_array_ptr != NULL) {

		for (int i = 0; i < this->numCattles; i++) {
			if (this->cattle_array_ptr[i] == NULL) {
				delete this->cattle_array_ptr[i];
				this->cattle_array_ptr[i] = NULL;
			}
		}

		delete this->cattle_array_ptr;
		this->cattle_array_ptr = NULL;
		this->numCattles = 0;
	}
	system("pause");
	system("cls");

}

测试运行文件.cpp

#include<iostream>
using namespace std;
#include<string>
#include<fstream>
#include"workmanager.h"
#include"cattle.h"
#include"boss_cattle.h"
#include"manager_cattle.h"
#include"common_cattle.h"


void test0305_0() {
	int choice = 0;
	WorkerManager *wm = new WorkerManager;
	while (true) {

		wm->ShowMenu();
		cout << "请输入对应功能:";
		cin >> choice;
		switch (choice) {
		case 0://退出程序
			wm->ExitMenu();
			break;
		case 1://增加职工信息
			wm->addCattles();
			
			break;
		case 2://显示职工信息
			//wm.showCattles();
			//cout << wm << endl;
			wm->showCattles();
			break;
		case 3://删除离职职工
			wm->deleteCattles();
			break;
		case 4://修改职工信息
			wm->alterCattle();
			break;
		case 5://查找职工信息
			wm->searchCattle();
			break;
		case 6://按照编号排序
			wm->sortedCattle();
			break;
		case 7://清空所有文档
			wm->cleanCattle();
			break;
		default:
			system("cls");
			break;
		}
		system("cls");
	}
}

void test0307() {
	Cattle* ca = NULL;
	ca = new CommonCattle(1, "张三", 2);
	ca->printSelf();
	delete ca;

	Cattle* tt = NULL;
	tt = new ManagerCattle(2, "李四", 3);
	tt->printSelf();
	delete tt;

	Cattle* le = NULL;
	le = new BossCattle(3, "王老五", 4);
	le->printSelf();
	delete le;
}


int main() {
	cout << "hello ! world ! " << endl;
	test0305_0();
	

	system("pause");
	return 0;

}


以上是我的学习笔记,希望对你有所帮助!
如有不当之处欢迎指出!谢谢!

学吧,学无止境,太深了

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HelpFireCode

随缘惜缘不攀缘。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值