有五個學生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;
}