STL案例 --评委打分

有五个学生ABCDE--每个学生有10个评委打分 去掉最高最低求他们的平均分数

 

class Person
{
public:
	Person(string name, int score)
	{
		this->m_Name = name;
		this->m_Score = score;
	}


	string m_Name; //姓名
	int m_Score; //平均分
};

创建学生的类 有5个学生

 

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);
	}

}

初始化vector容器  内部装有<Person>变量的成员

void ShowVector(vector<Person>& v)
{
	for (vector<Person>::iterator it = v.begin();it != v.end();it++)
	{
		cout << "选手姓名为 : " << (*it).m_Name << endl;
		cout << "选手的分数为:" << (*it).m_Score << endl;
		cout << endl;

	}
}

展示成员----(*it)指向每一个成员 即person单元

// 打分
void SetScore(vector<Person> &v)
{
	for (vector<Person> ::iterator it = v.begin();it != v.end();it++)
	{
		deque<int> d;
		cout << "请给选手 " << (*it).m_Name << " 打分" << endl;
		for (int i = 0;i < 10;i++)
		{
			int score;
			cin >> score;
			d.push_back(score);
		}
		//分数输入完毕
		cout<<(*it).m_Name << " 打分完毕" << endl;

		sort(d.begin(), d.end()); //排序
		d.pop_back();d.pop_front(); //去除最低分去除最高分

		int ScoreSum = 0;
		for (deque <int>::iterator dit = d.begin();dit != d.end();dit++)
		{
			ScoreSum += (*dit);
		}
		ScoreSum /= 8;

		
		(*it).m_Score = ScoreSum;

		system("cls");
	}
}
int main()
{
	//1.创建五名选手
	vector<Person> v;//存放选手容器
	CreatPerson(v);  //初始化容器
	ShowVector(v);
	 
	SetScore(v); //评委打分
	ShowVector(v);

	system("pause");
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章