> 文章列表 > 【C++】演讲比赛流程管理系统

【C++】演讲比赛流程管理系统

【C++】演讲比赛流程管理系统

1、比赛需求

  • 共12个人参加,比赛共两轮,第一轮为淘汰赛、第二轮为决赛
  • 每个人都有相对应的编号,如1001
  • 比赛分为两组,每组6个人
  • 第一轮分为两个小组,整体按照选手编号进行抽签后顺序演讲
  • 十个评委分别给每个选手打分,去除最高分和最低分,求的平均分为本轮选手成绩
  • 当小组演讲完后,淘汰组内排名最后的三名选手,前三名晋级,进入下阶段比赛
  • 第二轮为决赛,前三名胜出
  • 每轮比赛过后需要显示晋级选手的信息

2、程序功能

  • 开始演讲比赛:完成整届比赛的流程,每个比赛阶段需要给用户一个提示,用户按任意键后继续下一个阶段
  • 查看往届记录:查看之前比赛前三名结果,每次比赛都会记录到文件中,文件用.csv后缀名保存。
  • 清空比赛记录:将文件中数据清空
  • 退出比赛程序:退出当前程序

3、实现
还有很多需要改进的地方,下次再改吧。

#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <numeric>
#include <algorithm>
#include <fstream>
using namespace std;class Person {
public:int id;string name;float score;Person(string name, int id) {this->id = id;this->name = name;}
};
class GreaterScore {
public:bool operator()(const Person& p1, const Person& p2) {return p1.score > p2.score;}
};
class PrintPlayer {
public:void operator()(const Person& p) {cout << p.id << " " << p.name << " " << p.score << endl;}
};
void printPerson(const Person& p) {cout << p.id << " " << p.name<<"\\t";
}void printVal(int val) {cout << val<<" ";
}
void saveRecord(list<Person>& l) {ofstream ofs;ofs.open("record.csv",ios::out|ios::binary|ios::app);for (list<Person>::const_iterator it = l.begin(); it != l.end(); it++) {ofs << it->id<<",";ofs << it->name<<",";ofs << it->score<<",";ofs << endl;}ofs.close();
}
void readRecord() {ifstream ifs;ifs.open("record.csv",ios::in|ios::binary);string buf;int i = 0;while (getline(ifs, buf)) {if (i % 3 == 0) {printf("\\n第%d届比赛结果。", (i / 3)+1);buf.pop_back();cout << "冠军: "<<buf;}else if (i % 3 == 1) {buf.pop_back();cout << " 亚军: " << buf;}else if (i % 3 == 2) {buf.pop_back();cout << " 季军: " << buf;}i++;}ifs.close();
}
void clearRecord() {ofstream ofs;ofs.open("record.csv",ios::out);ofs << "";ofs.close();
}
list<Person>& match(list<Person>& p) {srand(time(0));multiset<int>s;for (list<Person>::iterator it = p.begin(); it != p.end(); it++) {for (int i = 0; i < 10; i++) {int r = rand() % 100;s.insert(r);}cout << it->name << "得分分别为:" << endl;for_each(s.begin(), s.end(), printVal);s.erase(s.begin());s.erase(--s.end());float average = accumulate(s.begin(), s.end(), 0) / s.size(); // first last baseit->score = average;cout << "\\n去除最高分和最低分后";cout << it->name;cout << "的平均得分为:" << average << endl;s.clear();}p.sort(GreaterScore());for (int i = 0; i < 3; i++) {p.pop_back();}//for_each(p.begin(), p.end(),PrintPlayer());return p;
}
void createMatch() {cout << "第一轮比赛开始。" << endl;int player_num = 12;printf("共有%d名选手参加。\\n", player_num);string player_name = "ABCDEFGHIJKL";vector<Person>v;int id = 1001;for (int i = 0; i < player_num; i++) {string name = "player";name += player_name[i];v.push_back(Person(name, id + i));}random_shuffle(v.begin(), v.end());for_each(v.begin(), v.end(), printPerson);cout << "\\n分组情况如下:" << endl;int i = 0;list<Person>l1;list<Person>l2;for (vector<Person>::iterator it = v.begin(); it != v.end(); it++ ,i++) {if (i < 6) {l1.push_back(*it);}else {l2.push_back(*it);}}cout << "第一组:";for_each(l1.begin(), l1.end(),printPerson);cout << "\\n第二组:";for_each(l2.begin(), l2.end(),printPerson);cout << endl;list<Person>&l = match(l1);list<Person>&l3 = match(l2);for (list<Person>::iterator it = l3.begin(); it != l3.end(); it++) {l.push_back(*it);}for_each(l.begin(), l.end(), PrintPlayer());l3 = match(l);for_each(l3.begin(), l3.end(), PrintPlayer());saveRecord(l3);
}void menu() {cout << "演讲比赛开始" << endl;createMatch();readRecord();
}int main() {menu();return 1;
}