Person.h
#pragma once
#include<iostream>
using namespace std;
#include<vector>
#include<string>
class Person
{
public:Person() {};// 构造函数,初始化Person(string name, int id);// 获取姓名string& getName();// 给姓名赋值void setName(const string& name);// 获取idint& getId();// 赋值idvoid setId(const int& id);// 获取评委的评分容器vector<double>& getVScore();// 赋值评委的评分容器void setVScore(const vector<double>& v);// 获取最终分数double* getScore();// 赋值最终分数void setScore(double* score);// 获取最终名次int& getRank();// 赋值最终名次void setRank(const int& rank);// 打印评委打分分数void showVScore();// 析构函数~Person();
private:string name; // 姓名int id; // 选手号double score[2]; // 记录两轮比赛最终分数,也就是去除最高分与最低分的平均值vector<double> vScore; // 用来存放评委打的分数int rank;//本次比赛排名
};
Person.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include"Person.h"// 构造函数,初始化
Person::Person(string name,int id)
{this->id = id;this->name = name;this->rank = 0;for (int i = 0; i < 2; i++){this->score[i] = 0;}this->vScore.clear();
}// 获取姓名
string& Person::getName()
{return this->name;
}// 给姓名赋值
void Person::setName(const string& name)
{this->name = name;
}// 获取id
int& Person::getId()
{return this->id;
}// 赋值id
void Person::setId(const int& id)
{this->id = id;
}// 获取评委的评分容器
vector<double>& Person::getVScore()
{return this->vScore;
}// 赋值评委的评分容器
void Person::setVScore(const vector<double>& v)
{this->vScore = v;
}// 获取最终分数
double* Person::getScore()
{return this->score;
}// 打印评委打分分数
void Person::showVScore()
{for (int i = 0; i < this->vScore.size(); i++){cout << this->vScore[i] << " ";}
}// 赋值最终分数
void Person::setScore(double* score)
{for (int i = 0; i < 2; i++){this->score[i] = score[i];}
}// 获取最终名次
int& Person::getRank()
{return this->rank;
}// 赋值最终名次
void Person::setRank(const int& rank)
{this->rank = rank;
}// 析构函数
Person::~Person()
{}
SpeakManager.h
#pragma once
#include<stdio.h>
using namespace std;
#include<vector>
#include"Person.h"
#include<map>
class SpeakManager
{
public:// 构造函数SpeakManager();// 显示菜单void showManu();// 退出程序void exitSystem();// 开始比赛void speakContest();// 获取第一轮容器vector<Person>& getVPersonOne();// 赋值第一轮容器void setVPersonOne(const vector<Person>& v);// 获取第二轮容器vector<Person>& getVPersonTwo();// 赋值第二轮容器void setVPersonTwo(const vector<Person>& v);// 获取胜利容器vector<Person>& getVVictor();// 赋值胜利容器void settVVictor(const vector<Person>& v);// 获取文件状态bool getIsFileEmpty();// 更改文件状态void setIsFileEmpty(bool isFileEmpty);// 参数初始化void inti();// 获取比赛场数int& getSession();// 更改比赛场数void setSession();// 创建12名选手void createrPerson();// 打印void printVectorPerson(vector<Person>& v);// 将比赛结果记录到文件中void recordToFile();// 2.查询往届记录void searchRecord();// 3.清空比赛记录void clearAll();//析构函数~SpeakManager();private:vector<Person> vPersonOne; // 用来存放第一轮比赛选手vector<Person> vPersonTwo; // 用来存放第二轮比赛选手vector<Person> vVictor; // 用来存放最终获胜的三名选手bool isFileEmpty; // 用来记录文件状态int session; // 记录比赛场数map<int, vector<string>> m; // 存放往届记录
};
SpeakManager.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include"SpeakManager.h"
#include<algorithm>
#include<numeric>
#include<functional>
#include<map>
#include<fstream>
// 构造函数
SpeakManager::SpeakManager()
{// 参数初始化this->inti();this->m.clear();
}// 参数初始化
void SpeakManager::inti()
{this->vPersonOne.clear();this->vPersonTwo.clear();this->vVictor.clear();this->isFileEmpty = false; // 文件默认状态为空this->session = 0;
}// 显示菜单
void SpeakManager::showManu()
{cout << "********************************************" << endl;cout << "************* 欢迎参加演讲比赛 ************" << endl;cout << "************* 1.开始演讲比赛 *************" << endl;cout << "************* 2.查看往届记录 *************" << endl;cout << "************* 3.清空比赛记录 *************" << endl;cout << "************* 0.退出比赛程序 *************" << endl;cout << "********************************************" << endl;cout << endl;
}// 退出程序
void SpeakManager::exitSystem()
{int choose;cout << "是否确认退出程序" << endl<< "1.确认" << endl<< "其它:取消" << endl;cin >> choose;if (choose == 1){cout << "退出成功,欢迎下次使用!" << endl;system("pause");system("cls");exit(0);}else{system("pause");system("cls");return;}
}// 获取第一轮容器
vector<Person>& SpeakManager::getVPersonOne()
{return this->vPersonOne;
}// 赋值第一轮容器
void SpeakManager::setVPersonOne(const vector<Person>& v)
{this->vPersonOne = v;
}// 获取第二轮容器
vector<Person>& SpeakManager::getVPersonTwo()
{return this->vPersonTwo;
}// 赋值第二轮容器
void SpeakManager::setVPersonTwo(const vector<Person>& v)
{this->vPersonTwo = v;
}// 获取胜利容器
vector<Person>& SpeakManager::getVVictor()
{return this->vVictor;
}// 赋值胜利容器
void SpeakManager::settVVictor(const vector<Person>& v)
{this->vVictor = v;
}// 获取文件状态
bool SpeakManager::getIsFileEmpty()
{return this->isFileEmpty;
}// 更改文件状态
void SpeakManager::setIsFileEmpty(bool isFileEmpty)
{this->isFileEmpty = isFileEmpty;
}// 获取比赛场数
int& SpeakManager::getSession()
{return this->session;
}// 更改比赛场数
void SpeakManager::setSession()
{this->session = session;
}// 创建12名选手
void SpeakManager::createrPerson()
{string name = "ABCDEFGHIJKL";for (int i = 0; i < name.size(); i++){string str = "选手";this->vPersonOne.push_back(Person(str + name[i], 10001 + i));}
}void SpeakManager::printVectorPerson(vector<Person>& v)
{for (vector<Person>::iterator it = v.begin(); it != v.end(); it++){cout << "选手:" << it->getName() << " "<< "id号:" << it->getId() << endl;}system("pause");
}void print(Person p)
{cout << "选手:" << p.getName() << " "<< "id号:" << p.getId() << endl;
}
void print1(Person p)
{cout << "选手:" << p.getName() << " "<< "id号:" << p.getId() << " ";p.showVScore();cout << endl;
}void print2(Person p)
{cout << "选手:" << p.getName() << " "<< "id号:" << p.getId() << " "<< "最终分数为:" << p.getScore()[0]<< " "<< "评委打分结果为:";p.showVScore();cout << endl;
}
void print3(Person p)
{cout << "选手:" << p.getName() << " "<< "id号:" << p.getId() << " "<< "最终分数为:" << p.getScore()[1] << " "<< "评委打分结果为:";p.showVScore();cout << endl;
}class MyGreater
{
public:bool operator()(Person p1,Person p2){return p1.getScore()[0] > p2.getScore()[0];}
};
class MyGreater1
{
public:bool operator()(Person p1, Person p2){return p1.getScore()[1] > p2.getScore()[1];}
};// 开始比赛
void SpeakManager::speakContest()
{// 用于记录选手两轮的分数double score[2] = { 0 };// 用于存放第一轮比赛vector<Person> v1;vector<Person> v2;// 用于存放第二轮比赛,自动按照double排序multimap<double, Person> m;if (this->session == 0){cout << "第" << session+1 << "轮淘汰赛正式开始,欢迎各位参赛选手!" << endl;// 创建12名选手并存储在vPersonOne中this->createrPerson();// 显示12名选手this->printVectorPerson(this->vPersonOne);cout << endl;cout << "下面将打乱顺序开始比赛!" << endl;// 打乱顺序random_shuffle(this->vPersonOne.begin(),this->vPersonOne.end());system("pause");cout << endl;cout << "打乱后的顺序为:" << endl;for_each(this->vPersonOne.begin(), this->vPersonOne.end(), print);cout << endl;system("pause");cout << "下面将选手分为两组,结果如下:" << endl;// 将12个人分为两个组v1.resize(this->vPersonOne.size() / 2);v2.resize(this->vPersonOne.size() / 2);//copy(this->vPersonOne.begin(), this->vPersonOne.begin() + 6,v1.begin());//copy(this->vPersonOne.begin()+6, this->vPersonOne.end(), v2.begin());v1.assign(this->vPersonOne.begin(), this->vPersonOne.begin() + 6);v2.assign(this->vPersonOne.begin()+6, this->vPersonOne.end());cout << "第一组选手为:" << endl;for_each(v1.begin(),v1.end(),print);cout << "第二组选手为:" << endl;for_each(v2.begin(), v2.end(), print);cout << endl;system("pause");cout << "下面两组进行比赛,请10位评委老师打分!" << endl;// 第一层循环遍历选手for (int i = 0; i < v1.size(); i++){// 第二层循环遍历评委老师打分for (int j = 0; j < 10; j++){// (rand()%60+40)/10.f 0-9.9分v1[i].getVScore().push_back((rand() % 60 + 40) / 10.f);v2[i].getVScore().push_back((rand() % 60 + 40) / 10.f);}// 计算最终得分score[this->session] = accumulate(v1[i].getVScore().begin(), v1[i].getVScore().end(), 0)/ (double)(v1[i].getVScore().size() - 2);v1[i].setScore(score);score[this->session] = accumulate(v2[i].getVScore().begin(), v2[i].getVScore().end(), 0)/ (double)(v2[i].getVScore().size() - 2);v2[i].setScore(score);}// 在这里还可以将选手信息更新到this->vPersonOne中// 不弄了system("pause");cout << endl;cout << "打完分之后未排序的选手信息:" << endl;cout << endl;cout << "第一组信息为:" << endl;for_each(v1.begin(), v1.end(), print1);cout << endl;cout << "第二组信息为:" << endl;for_each(v2.begin(), v2.end(), print1);system("pause");cout << endl;cout << "打完分之后有得分未排序后的选手信息:" << endl;cout << endl;cout << "第一组信息为:" << endl;for_each(v1.begin(), v1.end(), print2);cout << endl;cout << "第二组信息为:" << endl;for_each(v2.begin(), v2.end(), print2);// 对最终得分进行排序sort(v1.begin(), v1.end(), MyGreater());sort(v2.begin(), v2.end(), MyGreater());system("pause");cout << endl;cout << "打完分之后有得分排序后的选手信息:" << endl;cout << endl;cout << "第一组信息为:" << endl;for_each(v1.begin(), v1.end(), print2);cout << endl;cout << "第二组信息为:" << endl;for_each(v2.begin(), v2.end(), print2);system("pause");cout << endl;cout << "淘汰后三名后的选手信息:" << endl;cout << endl;v1.resize(3);v2.resize(3);cout << "第一组信息为:" << endl;for_each(v1.begin(), v1.end(), print2);cout << endl;cout << "第二组信息为:" << endl;for_each(v2.begin(), v2.end(), print2);// 将晋级的选手放入this->vPersonTwo中system("pause");cout << "=======================================" << endl;for (int i = 0; i < 3; i++){this->vPersonTwo.push_back(v1[i]);this->vPersonTwo.push_back(v2[i]);}for_each(this->vPersonTwo.begin(), this->vPersonTwo.end(), print2);this->session++;}if (this->session == 1)// 决赛{system("cls");cout << "下面进行第" << this->session + 1 << "部分,决赛" << endl;// 第一层遍历选手for (int i = 0; i < this->vPersonTwo.size(); i++){// 第二层评委打分// 第二层循环遍历评委老师打分for (int j = 0; j < 10; j++){// (rand()%60+40)/10.f 0-9.9分this->vPersonTwo[i].getVScore().push_back((rand() % 60 + 40) / 10.f);}// 计算最终得分score[this->session] = accumulate(this->vPersonTwo[i].getVScore().begin(), this->vPersonTwo[i].getVScore().end(), 0)/ (double)(this->vPersonTwo[i].getVScore().size() - 2);this->vPersonTwo[i].setScore(score);}system("pause");cout << endl;cout << "打完分之后未排序的选手信息:" << endl;cout << endl;for_each(this->vPersonTwo.begin(), this->vPersonTwo.end(), print1);cout << endl;system("pause");cout << endl;cout << "打完分之后有得分未排序后的选手信息:" << endl;cout << endl;for_each(this->vPersonTwo.begin(), this->vPersonTwo.end(), print3);cout << endl;// 对最终得分进行排序sort(this->vPersonTwo.begin(), this->vPersonTwo.end(), MyGreater1());system("pause");cout << endl;cout << "打完分之后有得分排序后的选手信息:" << endl;cout << endl;for_each(this->vPersonTwo.begin(), this->vPersonTwo.end(), print3);cout << endl;system("pause");cout << endl;cout << "淘汰后三名后的选手信息:" << endl;cout << endl;this->vPersonTwo.resize(3);for_each(this->vPersonTwo.begin(), this->vPersonTwo.end(), print3);cout << endl;// 将晋级的选手放入this->vPersonTwo中/*system("pause");cout << "=======================================" << endl;for (int i = 0; i < 3; i++){this->vPersonTwo.push_back(v1[i]);this->vPersonTwo.push_back(v2[i]);}for_each(this->vPersonTwo.begin(), this->vPersonTwo.end(), print2);*/this->vVictor = this->vPersonTwo;cout << "=======================================" << endl;for_each(this->vVictor.begin(), this->vVictor.end(), print3);this->session = 0; // 决赛打完清零}// 将比赛结果记录到文件中recordToFile();// 清空容器this->inti();
}// 将比赛结果记录到文件中
void SpeakManager::recordToFile()
{ofstream ofs;ofs.open("test.csv", ios::out | ios::app);for (int i = 0; i < this->vVictor.size(); i++){ofs << this->vVictor[i].getName() << "," << this->vVictor[i].getScore()[1] << ",";}ofs << endl;ofs.close();cout << "存储完毕" << endl;this->isFileEmpty = false;
}// 2.查询往届记录
void SpeakManager::searchRecord()
{if (this->isFileEmpty == true){cout << "文件为空或不存在" << endl;}else {ifstream ifs;ifs.open("test.csv", ios::in);char ch;ifs >> ch;if (ifs.eof()){this->isFileEmpty = true;cout << "文件为空或不存在" << endl;}else{this->isFileEmpty = false;ifs.putback(ch);// 将文件内容读取出来string date;int index = 0;while (ifs >> date){int start = 0;vector<string> v;while (true){int pos = -1;pos = date.find(",", start);if (pos == -1){break;}string str = date.substr(start, pos - start);start = pos + 1;v.push_back(str);}m.insert(make_pair(index,v));index++;}}ifs.close();}// 打印往届记录for (int i = 0; i < m.size(); i++){cout << "冠军: " << m[i][0] << " 分数:" << m[i][1]<< " 亚军:" << m[i][2] << " 分数:" << m[i][3]<< " 季军:" << m[i][4] << " 分数:" << m[i][5] << endl;}cout << endl;
}void SpeakManager::clearAll()
{if (this->isFileEmpty){cout << "文件为空或不存在" << endl;}else{ofstream ofs;ofs.open("test.csv", ios::trunc);ofs.close();cout << "清空成功" << endl;this->inti();this->vVictor.clear();this->m.clear();}
}//析构函数
SpeakManager::~SpeakManager()
{}
演讲比赛演讲流程.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include"SpeakManager.h"
#include"Person.h"
#include<algorithm>
#include<string>
#include<ctime>
class Print
{
public:void operator()(Person p){cout << "选手:" << p.getName() << " "<< "id:" << p.getId() << endl;}};
class MyGreat
{
public:bool operator()(Person p1, Person p2){return p1.getId() > p2.getId();}};
void test01()
{vector<Person> v;v.push_back(Person("111",11));v.push_back(Person("222", 22));v.push_back(Person("333", 33));v.push_back(Person("444", 44));v.push_back(Person("555", 55));v.push_back(Person("666", 66));random_shuffle(v.begin(), v.end());for_each(v.begin(), v.end(), Print());cout << endl;sort(v.begin(), v.end(), MyGreat());for_each(v.begin(), v.end(), Print());
}
int main()
{// test01();srand((unsigned int)time(NULL));SpeakManager s;int input;while (true){// 显示菜单s.showManu();cout << "请选择您需要的选项:" << endl;cin >> input;switch (input){case 1:// 1.开始演讲比赛s.speakContest();break; case 2:// 2.查询往届记录s.searchRecord();break;case 3:// 3.清空比赛记录s.clearAll();break;case 0:// 4.退出程序s.exitSystem();break;default:cout << "输入不合法,请重新输入:" << endl;system("pause");system("cls");}}return 0;
}