Vector第一次练习-选手得分

//选手信息头文件,ginforn.h
#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <algorithm>
using namespace std;
class GPersonInfor
{
public:
	GPersonInfor(string name, int score);
	~GPersonInfor();

public:
	string mName;
	int mScore;
};

void CretePerInfor(vector<GPersonInfor>& v);

void GetTeachScore(vector<GPersonInfor>& v);

void ShowResult(vector<GPersonInfor>& v);

------------------------------------------------------分割线------------------------------------------------------------------

//选手信息的实现,ginforn.cpp
#include "ginforn.h"

GPersonInfor::GPersonInfor(string name, int score)
{
	mName = name;
	mScore = score;
}

GPersonInfor::~GPersonInfor()
{

}

void CretePerInfor(vector<GPersonInfor>& v)
{
	string namespeed = "abcde";
	int score = 0;
	for (int i = 0; i < 5; i++)
	{
		string name = "选手";
		name += namespeed[i];
		GPersonInfor person(name, score);
		v.push_back(person);
		cout << "参赛选手姓名: " << name << endl;
	}
	cout << "--------------------" << endl;
}

void GetTeachScore(vector<GPersonInfor>& v)
{
	for (vector<GPersonInfor> ::iterator it = v.begin(); it != v.end(); it++)
	{
		deque<int> d;
		for (int i = 0; i < 10; i++)
		{
			int score = rand() % 41 + 60;
			d.push_back(score);
		}
		cout << "姓名:" << it->mName << " 得分如下: " << endl;
		for (deque<int> ::iterator dit = d.begin(); dit != d.end(); dit++)
		{
			cout << *dit << " ";
		}
		cout << endl;

		sort(d.begin(), d.end());
		d.pop_back();
		d.pop_front();

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

		int avg = sum / d.size();

		//如何将分数传到score中
		it->mScore = avg;
	}
	cout << "--------------------" << endl;
}

void ShowResult(vector<GPersonInfor>& v)
{
	for (vector<GPersonInfor> ::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << "获奖姓名: " << it->mName << " 平均分: " << it->mScore << endl;
	}
}

------------------------------------------------------分割线------------------------------------------------------------------

//main函数

#include <iostream>
#include "ginforn.h"

int main()
{
	//随机数种子
	srand((unsigned int)time(NULL));

	vector<GPersonInfor> v;
	CretePerInfor(v);
	GetTeachScore(v);
	ShowResult(v);

	system("pause");
	return 0;
}

------------------------------------------------------分割线------------------------------------------------------------------

2022.7.9 ,第一次记录自己写过的代码,小白,刚看完C++视频,开始记录的第一天!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值