[deque容器练习]打分案例

案例要求:

//打分案例(sort算法排序)
//创建5个选手(姓名、得分),十个评委对五个选手进行打分
//得分规则:去除最高分,去除最低分,取出平均分
//按得分对5个选手进行排名

源代码:

//打分案例(sort算法排序)
//创建5个选手(姓名、得分),十个评委对五个选手进行打分
//得分规则:去除最高分,去除最低分,取出平均分
//按得分对5个选手进行排名

#include<iostream>
#include<vector>
#include<deque>
#include<string>
#include<algorithm>
using namespace std;

//选手类
class Player 
{
public:
    Player(){}
    Player(string name,int score):mName(name),mScore(score){}
public:
    string mName;
    int mScore;
};

//创建选手
void CreatPlayer(vector<Player>& v) 
{
    string name = "ABCDE";
    for (int i = 0; i < 5; i++)
    {
        Player p;
        p.mName = "选手";
        p.mName += name[i];
        p.mScore = 0;
        v.push_back(p);
    }

}

//评委打分
void SetScore(vector<Player>& v)
{
    for (vector<Player>::iterator it = v.begin(); it != v.end(); it++)
    {
        //对选手进行打分
        deque<int> dScore;
        for (int i = 0; i < 10; i++)
        {
            int score = rand() % 41 + 60;
            dScore.push_back(score);
            //dScore[i] = rand() % 41 + 60;
        }
        //对分数进行排序
        sort(dScore.begin(), dScore.end());
        //去除最高最低分
        dScore.pop_front();
        dScore.pop_back();
        //求平均分
        int total = 0;
        for (deque<int>::iterator dit = dScore.begin(); dit != dScore.end(); dit++)
        {
            total += (*dit);
        }
        int avg = total / dScore.size();
        //保存分数
        (*it).mScore = avg;
    }
}
//排序规则(默认从小到大排序)
bool myCompare(Player& p1, Player& p2)
{
    return p1.mScore > p2.mScore;
}

//选手排名
void PrintRank(vector<Player>& v)
{
    //排序
    sort(v.begin(), v.end(), myCompare);
    //打印
    for (vector<Player>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << "姓名:" << (*it).mName << ",  成绩:" << (*it).mScore << endl;
    }
    cout << endl;
}

int main()
{
    //定义vector保存选手信息
    vector<Player> vPlist;
    CreatPlayer(vPlist);
    SetScore(vPlist);
    PrintRank(vPlist);
    return 0;
}

运行结果:

 

posted @ 2019-04-23 22:19  鲸小鱼-  阅读(244)  评论(0编辑  收藏  举报