C++ //案列-员工分组 ( 容器存放,查找,打印,统计,宏定义 ,随机)

//案列-员工分组
//描述:公司招聘10个员工(ABCDEFGHIJ),10名指派员工进入公司,需要指派那个员工在那个部门工作
//员工信息有:姓名 工资组成; 部门分为:策划 美术 研发
//随机给10名员工分配工资和部门
//通过multimap进行信息的插入,key(部门编号) value员工
//分部门显示员工信息

  1 //案列-员工分组
  2 //描述:公司招聘10个员工(ABCDEFGHIJ),10名指派员工进入公司,需要指派那个员工在那个部门工作
  3 //员工信息有:姓名 工资组成;   部门分为:策划 美术  研发
  4 //随机给10名员工分配工资和部门
  5 //通过multimap进行信息的插入,key(部门编号) value员工
  6 //分部门显示员工信息
  7 
  8 #include<iostream>
  9 #include<string>
 10 #include<map>
 11 #include<vector>
 12 
 13 
 14 using namespace std;
 15 #define CEHUA  0
 16 #define MEISHU 1
 17 #define YANFA  2
 18 
 19 class Worker
 20 {
 21 public:
 22 
 23     string m_Name;
 24     int m_Salary;
 25 };
 26 
 27 void createWorker(vector<Worker>& v)
 28 {
 29     string nameSeed = "ABCDEFGHIJ";
 30     for (int i = 0; i < 10; i++)
 31     {
 32         Worker worker;
 33         worker.m_Name = "员工";
 34         worker.m_Name += nameSeed[i];
 35 
 36         worker.m_Salary = rand() % 10000 + 10000; //10000-19999
 37 
 38         //将员工放入容器中
 39         v.push_back(worker);
 40 
 41 
 42     }
 43 }
 44 //员工分组
 45 void setGroup(vector<Worker>& v, multimap<int, Worker>& m)
 46 {
 47     for (vector<Worker>::iterator it = v.begin(); it != v.end(); it++)
 48     {
 49         //产生随机部门编号
 50         int deptId = rand() % 3; //0 1 2
 51         //将员工插入到分组中
 52         //key代表部门标号 value代表员工
 53         m.insert(make_pair(deptId, *it));
 54     }
 55 }
 56 
 57 void showWorkerByGroup(multimap<int, Worker>& m)
 58 {
 59     
 60     cout << "策划部门:" << endl;
 61 
 62     multimap<int, Worker>::iterator pos = m.find(CEHUA);
 63     int count = m.count(CEHUA);   //统计具体人数
 64     int index = 0;
 65     for (; pos != m.end()&& index<count; pos++,index++)
 66     {
 67         cout << "姓名:" << pos->second.m_Name << "\t工资:" << pos->second.m_Salary << endl;
 68 
 69     }
 70 
 71     cout << "------------------------------" << endl;
 72     cout << "美术部门:" << endl;
 73      pos = m.find(MEISHU);
 74      count = m.count(MEISHU);   //统计具体人数
 75      index = 0;
 76     for (; pos != m.end() && index < count; pos++, index++)
 77     {
 78         cout << "姓名:" << pos->second.m_Name << "\t工资:" << pos->second.m_Salary << endl;
 79 
 80     }
 81 
 82     cout << "------------------------------" << endl;
 83     cout << "研发部门:" << endl;
 84     pos = m.find(YANFA);
 85     count = m.count(YANFA);   //统计具体人数
 86     index = 0;
 87     for (; pos != m.end() && index < count; pos++, index++)
 88     {
 89         cout << "姓名:" << pos->second.m_Name << "\t工资:" << pos->second.m_Salary << endl;
 90 
 91     }
 92 
 93 
 94 
 95 }
 96 int main()
 97 {
 98     srand((unsigned int)time(NULL));
 99     //1.创建员工
100     vector<Worker>vWorker;
101     createWorker(vWorker);
102 
103     //2.员工分组
104     multimap<int, Worker>mWorker;
105     setGroup(vWorker, mWorker);
106 
107     //3.分组显示员工
108     showWorkerByGroup(mWorker);
109 
110     //测试
111     //for (vector<Worker>::iterator it = vWorker.begin(); it != vWorker.end(); it++)
112     //{
113     //    cout << "姓名:" << it->m_Name << "\t工资:" << (*it).m_Salary << endl;
114     //}
115 
116     system("pause");
117     return 0;
118 }

 

 

每次打印结果都不一样 随机分配

 

posted on 2021-08-16 16:22  Bytezero!  阅读(116)  评论(0编辑  收藏  举报