话不多说上代码:
自己的:
#include<iostream>
#include<string>
#include<deque>
#include<algorithm>
#include<vector>
using namespace std;
class Person
{
public:
Person(string name,int score)
{
this->m_name = name;
this->m_score = score;
}
int calAve()
{
deque<int>d1;
cout << "请输入评委对此选手的打分情况:" << endl;
for (int i = 0; i < 10; i++)
{
cout << "请输入第" << i + 1 << "位评委的打分:" << endl;
int s = 0;
cin >> s;
d1.push_back(s);
}
sort(d1.begin(), d1.end());
d1.pop_front();
d1.pop_back();
int sum = 0;
for (int i = 0; i < 8; i++)
{
sum += d1[i];
}
this->m_score= sum / 8;
return this->m_score;
}
string m_name;
int m_score;
};
void test01()
{
Person p1("Tom", 0);
cout<<p1.m_name<<"的最终得分为:"<<p1.calAve()<<endl;
}
int main()
{
test01();
showScore(v);
system("pause");
return 0;
}
老师的代码:
#include<iostream>
#include<string>
#include<deque>
#include<algorithm>
#include<vector>
#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 createPerson(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);
//将创建的Person对象的放入容器中
v.push_back(p);
}
}
void setScore(vector<Person>& v)
{
for (vector<Person>::iterator it=v.begin();it!=v.end();it++)
{
deque<int>d;
for (int i = 0; i < 10; i++)
{
int score = rand() % 41 + 60;//60~100的随机数
d.push_back(score);
}
/*cout << " 选手:" << (*it).m_name << "打分:" << endl;
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;
}
int ave = sum/d.size();
it->m_score = ave;
}
}
void showScore(const vector<Person>& v)
{
for (vector<Person>::const_iterator it = v.begin(); it != v.end(); it++)
{
cout << it->m_name << "的得分为:" << it->m_score << endl;
}
}
int main()
{
//随机种子
srand((unsigned int)time(NULL));
//1.创建5名选手
vector<Person>v;;//存放选手类
createPerson(v);
//测试
//cout << "选手的信息:" << endl;
//for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
//{
// cout << "姓名:" << it->m_name << "\t得分:" << it->m_score << endl;
//}
//2.给5名选手打分
setScore(v);
//3.最后显示得分
showScore(v);
system("pause");
return 0;
}```