评委打分
#include<iostream>
#include<string>
#include<vector>
#include<deque>
#include<algorithm>
#include<ctime>
using namespace std;
//创建选手类
class Person
{
public:
Person(string name, int score)
{
this->m_Name = name;
this->m_Score = score;
}
string m_Name;
int m_Score;
};
void CreatPerson(vector<Person>& v)
{
string nameSeed = "ABCDE";
for (int i = 0; i < 5; i++)
{
string name = "选手";
name += nameSeed[i];//给五个选手取名字
int score = 0;//初始化分数
Person p(name, score);//创建选手实例
v.push_back(p);//把创建的五个选手实例放到容器中
}
}
//在deque容器中加入10个评委分数,求出平均分数之后,将数据传入victor容器中
void setScore(vector<Person>& v)//参数为一个存放Person类的容器
{
for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)//遍历容器中的数据
{
//将评委的分放到deque容器中
deque<int >d;
for (int i = 0; i < 10; i++)
{
int score = rand() % 41 + 60;//产生随机的分数
d.push_back(score);
}
cout << it->m_Name << "的初始分数是" << it->m_Score << endl;
cout << it->m_Name << "评委打分是";
for (deque<int>::iterator dit = d.begin(); dit != d.end(); dit++)//遍历容器
{
cout << *dit << " ";//不换行输出
}
cout << endl;//换行
//容器中的数据排序
sort(d.begin(), d.end());
//去除容器中的最低分和最高分
d.pop_front();
d.pop_back();
//容器中的数据取平均分
int sum = 0;
for (deque<int>::iterator dit = d.begin(); dit != d.end(); dit++)//遍历容器数据
{
sum += *dit;//*dit就是容器中的数据int
}
int avg = sum / d.size();//求平均
it->m_Score = avg;//平均分作为评委的最终结果,放到victor容器中
}
}
//显示选手的最终信息
void showScore(vector<Person>& v)
{
for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)//遍历vector容器
{
cout << "姓名:" << it->m_Name << "得分:" << it->m_Score << endl;
}
}
int main(void)
{
//随机数种子
srand((unsigned int)time(NULL));
//创建五名选手
vector<Person>v;//创建vector容器中存放persol类数据
CreatPerson(v);//产生五名选手在容器中
//给五名选手打分
setScore(v);
//显示选手的信息结果
showScore(v);
system("pause");
return 0;
}
员工分类
#include<iostream>
#include<algorithm>
#include<map>
#include<vector>
#include<string>
#include<ctime>
using namespace std;
#define CEHUA 0;//宏CEHUA代表的字符串是0
#define MEISHU 1;
#define YANFA 2;
//创建工人类
class Worker
{
public:
string m_Name;
int m_Salary;
};
//创建出工人的具体信息,然后把他们放到vector容器中去
void CreatWorker(vector<Worker>& v)
{
string NameSeed = "ABCDEFHGHIJ";
for (int i = 0; i < 10; i++)//给我们的工人起名字,和工资
{
Worker worker;//创建工人实例
worker.m_Name = "员工";
worker.m_Name += NameSeed[i];
worker.m_Salary = rand() % 10000 + 10000;
//把员工的信息放到容器中去
v.push_back(worker);
}
}
//分组
void SetGroup(vector<Worker>& v, multimap<int, Worker>& m)//传入两个容器
{
for (vector<Worker>::iterator it = v.begin(); it != v.end(); it++)//将vector容器中的数据传入mult容器中
{
//产生随机的编号
int deptId = rand() % 3;//0,1,2
m.insert(make_pair(deptId, *it));
}
}
//分组显示
void ShowWorkerByGroup(multimap<int, Worker>&m)
{
cout << "策划部门" << endl;
multimap<int, Worker>::iterator pos = m.find(0);//找到key为0元素是否存在,存在就返回他在容器中的第一个地址
int count = m.count(0);//计算元素个数
int index = 0;
for (; pos != m.end() && index < count; pos++, index++)
{
cout << "姓名:" << pos->second.m_Name << "工资:" << pos->second.m_Salary << endl;
}
cout << "美术部门" << endl;
pos = m.find(1);//找到0元素是否存在,存在就返回他在容器中的地址
count = m.count(1);//计算元素个数
index = 0;
for (; pos != m.end() && index < count; pos++, index++)
{
cout << "姓名:" << pos->second.m_Name << "工资:" << pos->second.m_Salary << endl;
}
cout << "研发部门" << endl;
pos = m.find(2);//找到0元素是否存在,存在就返回他在容器中的地址
count = m.count(2);//计算元素个数
index = 0;
for (; pos != m.end() && index < count; pos++, index++)
{
cout << "姓名:" << pos->second.m_Name << "工资:" << pos->second.m_Salary << endl;
}
}
int main(void)
{
srand((unsigned int)time(NULL));
//创建员工
vector<Worker>vWorker;//创建实例
CreatWorker(vWorker);
//分组
multimap<int, Worker>mWorker;
SetGroup(vWorker, mWorker);
//分组显示员工
ShowWorkerByGroup(mWorker);
system("pause");
return 0;
}