栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

Vector第一次练习-选手得分

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Vector第一次练习-选手得分

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

public:
	string mName;
	int mScore;
};

void CretePerInfor(vector& v);

void GetTeachScore(vector& v);

void ShowResult(vector& v);

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

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

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

GPersonInfor::~GPersonInfor()
{

}

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

		int avg = sum / d.size();

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

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

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

//main函数

#include 
#include "ginforn.h"

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

	vector v;
	CretePerInfor(v);
	GetTeachScore(v);
	ShowResult(v);

	system("pause");
	return 0;
}

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

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1008599.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号