C++ 利用容器创建部门员工信息并显示

C++ 利用容器创建部门员工信息并显示,第1张

/*
公司招聘n个员工,(ABCDEFGH),员工进入公司后需要指派到各部门工作
员工信息有:姓名,年龄,工资;部门分为算法,运营,设计,产品
随机给员工分配部门和工资
通过multimap进行信息的插入,key(部门编号),value(员工)
分部门显示员工信息
*/
/*实现步骤
1.创建N名员工,放入到vector中
2.遍历vector容器,取出每个员工,进行随机分组
3.分组后将员工部门编号作为key,员工作为value,放入到multimap中
4.分部门显示员工信息
*/

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define staff_num 12
#define Algorithm_id 1
#define Operation_id 2
#define Design_id 3
#define Product_id 4
class staff
{
public:
	staff(string name, int age, int salary)
	{
		this->m_name = name;
		this->m_age = age;
		this->m_salary = salary;
	}
	string m_name;
	int m_age;
	int m_salary;
};
void creatstaff(vector &v)
{
	string name_seed[staff_num];
	string name0 = "Staff_";
	char name1 = 'A';
	for (int i = 0; i < staff_num; i++)
	{
		name_seed[i] = name0 + name1;
		name1++;
	}
	srand((unsigned int)time(NULL));
	for (int i = 0; i < staff_num; i++)
	{
		v.push_back(staff(name_seed[i], rand() % 21 + 30, rand() % 41 + 20));
	}
}
void printstaff(const vector &v)
{
	cout << "Staff info: " << endl;
	for (vector::const_iterator it = v.begin(); it != v.end(); it++)
	{
		cout << "Name: " << it->m_name << "\tAge: " << it->m_age << "\t\tSalary: " << it->m_salary <<"W" << endl;
	}
	cout << endl;
}
void printstaff(const multimap& m)
{
	cout << "Staff info: (multimap) " << endl;
	for (multimap::const_iterator it=m.begin(); it != m.end(); it++)
	{
		cout << "Department: " << it->first << "\t" << it->second.m_name << "\t\tAge: "
			<< it->second.m_age << "\t\tSalary: " << it->second.m_salary << "W" << endl;
	}
	cout << endl;
}
void showstaff(multimap& m)
{
	cout << "=========================Algorithm=========================" << endl;
	multimap::iterator pos = m.find(Algorithm_id);
	int count = m.count(Algorithm_id);
	int index = 0;
	for (; pos != m.end() && index < count; pos++, index++)
	{
		cout << "Deptid: " << pos->first << "\t" << pos->second.m_name << "\t\tAge: "
			<< pos->second.m_age << "\t\tSalary: " << pos->second.m_salary << "W" << endl;
	}
	cout << endl;
	cout << "=========================Operation=========================" << endl;
	pos = m.find(Operation_id);
	count = m.count(Operation_id);
	index = 0;
	for (; pos != m.end() && index < count; pos++, index++)
	{
		cout << "Deptid: " << pos->first << "\t" << pos->second.m_name << "\t\tAge: "
			<< pos->second.m_age << "\t\tSalary: " << pos->second.m_salary << "W" << endl;
	}
	cout << endl;
	cout << "==========================Design===========================" << endl;
	pos = m.find(Design_id);
	count = m.count(Design_id);
	index = 0;
	for (; pos != m.end() && index < count; pos++, index++)
	{
		cout << "Deptid: " << pos->first << "\t" << pos->second.m_name << "\t\tAge: "
			<< pos->second.m_age << "\t\tSalary: " << pos->second.m_salary << "W" << endl;
	}
	cout << endl;
	cout << "==========================Product==========================" << endl;
	pos = m.find(Product_id);
	count = m.count(Product_id);
	index = 0;
	for (; pos != m.end() && index < count; pos++, index++)
	{
		cout << "Deptid: " << pos->first << "\t" << pos->second.m_name << "\t\tAge: "
			<< pos->second.m_age << "\t\tSalary: " << pos->second.m_salary << "W" << endl;
	}
}
void setgroupstaff(vector &v, multimap &m)
{
	srand((unsigned int)time(NULL));
	for (vector::const_iterator it = v.begin(); it != v.end(); it++)
	{
		int deptid = rand()%4 + 1;
		m.insert(make_pair(deptid, *it));
	}
}
int main()
{
	vector v_staff;
	creatstaff(v_staff); 
	multimap m_staff;
	setgroupstaff(v_staff,m_staff);

	//printstaff(m_staff);
	showstaff(m_staff);
	system("pause");
	return 0;
}

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

原文地址: https://outofmemory.cn/langs/563325.html

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

发表评论

登录后才能评论

评论列表(0条)

保存