拿cion写的
目录结构如下
配置文件CMakeLists.txt
cmake_minimum_required(VERSION 3.25)
project(speech)
set(CMAKE_CXX_STANDARD 17)
add_executable(speech main.cpp speechManager speechManager.cpp)
main.cpp
#include <iostream>
#include <ctime>
#include "speechManager.h"
using namespace std;
int main() {
srand((unsigned int)time(NULL));
SpeechManager sm;
int choice = 0; // 记录用户选项
// 测试代码
// for (auto &t : sm.m_Speaker) {
// cout << "选手编号: " << t.first
// << " 姓名: " << t.second.name
// << " 第一轮初始成绩: " << t.second.score[0] << endl;
// }
while (true) {
sm.showMenu();
cout << "请输入您要使用的功能编号: " << endl;
cin >> choice;
switch (choice) {
case 0: // 退出系统
sm.exitSystem();
break;
case 1: // 开始比赛
sm.startSpeech();
break;
case 2: // 查看记录
sm.showRecord();
break;
case 3: // 清空记录
sm.clearRecord();
break;
default:
system("cls"); // 清屏
break;
}
}
system("pause");
return 0;
}
speaker.h
//
// Created by Cauchyshy on 2023/5/6.
//
#ifndef SPEECH_SPEAKER_H
#define SPEECH_SPEAKER_H
#endif //SPEECH_SPEAKER_H
/*
* 演讲人员类
* */
#pragma once
#include <iostream>
using namespace std;
class Speaker {
public:
string name; // 姓名
double score[2]; // 分数 有两轮 因此开个数组
};
speechManager.cpp
//
// Created by Cauchyshy on 2023/5/5.
//
/*
* 功能描述
* 提供菜单界面与用户交互
* 对比赛演讲流程进行控制
* 文件交互
* */
#include "speechManager.h"
// 构造函数实现
SpeechManager::SpeechManager() {
// 初始化属性
this->initSpeech();
// 创建选手
this->createSpeaker();
// 获取往届记录
this->loadRecord();
}
// 菜单展示
void SpeechManager::showMenu() {
cout << "********************************************" << endl;
cout << "************ 欢迎参加演讲比赛 ************" << endl;
cout << "************ 1.开始演讲比赛 **************" << endl;
cout << "************ 2.查看往届记录 **************" << endl;
cout << "************ 3.清空比赛记录 **************" << endl;
cout << "************ 0.退出比赛系统 **************" << endl;
cout << "********************************************" << endl;
}
// 退出系统
void SpeechManager::exitSystem() {
cout << "欢迎下次使用!" << endl;
system("pause");
exit(0);
}
// 比赛开始 并且在构造函数中调用
void SpeechManager::initSpeech() {
// 容器初始为空
this->v1.clear();
this->v2.clear();
this->vVictory.clear();
this->m_Speaker.clear();
// 初始化比赛轮数 1
this->m_Index = 1;
// 初始化记录容器
this->m_Record.clear();
}
// 创建选手 12
void SpeechManager::createSpeaker() {
string naemSeed = "ABCDEFGHIJKL";
for (int i = 0; i < naemSeed.size(); ++i) {
string name = "选手";
name += naemSeed[i];
Speaker sp;
sp.name = name;
for (int j = 0; j < 2; ++j) {
sp.score[j] = 0;
}
// 给选手编号
this->v1.push_back(i + 10001);
// 放到map容器中
this->m_Speaker.insert(make_pair(i + 10001, sp));
}
}
// 抽签
void SpeechManager::speechDraw() {
cout << "第 << " << this->m_Index << " >>轮比赛选手正在抽签" << endl;
cout << "loading.................." << endl;
cout << "抽签后演讲顺序如下: " << endl;
if (this->m_Index == 1) {
// 第一轮
random_shuffle(v1.begin(), v1.end());
for (auto &t : v1) {
cout << t << " ";
}
cout << endl;
} else {
random_shuffle(v2.begin(), v2.end());
for (auto &t : v2) {
cout << t << " ";
}
cout << endl;
}
cout << "-------------------------------" << endl;
system("pause");
}
// 比赛的逻辑实现
void SpeechManager::speechContest() {
cout << "---------------第" << this->m_Index << "轮比赛正式开始---------------" << endl;
multimap<double, int, greater<double>> groupScore; // 临时容器 保存key分数 value选手编号
int num = 0; // 记录人员数 6个人为一组
vector<int> v_Src; // 比赛的人员容器
if (this->m_Index == 1) {
v_Src = v1;
} else {
v_Src = v2;
}
// 遍历所有参赛选手
for (auto it : v_Src) {
num++;
// 评委打分
deque<double> d;
for (int i = 0; i < 10; ++i) {
double score = (rand() % 401 + 600) / 10.0; // 600-1000 / 10.0
d.push_back(score);
}
// 排序
sort(d.begin(), d.end(), greater<double>());
// 去掉最高分和最低分
d.pop_front();
d.pop_back();
// 算平均分
double sum = accumulate(d.begin(), d.end(), 0.0f);
double avg = sum * 1.0 / (double)d.size();
this->m_Speaker[it].score[this->m_Index - 1] = avg;
// 6个人一组 临时容器保存
groupScore.insert(make_pair(avg, it));
if (num % 6 == 0) {
cout << "第" << num / 6 << "小组比赛名次如下:" << endl;
for (auto it : groupScore) {
cout << "编号: " << it.second << " 姓名: " << this->m_Speaker[it.second].name << " 成绩: " << this->m_Speaker[it.second].score[this->m_Index - 1] << endl;
}
int cnt = 0;
// 获取前三名
for (multimap<double, int, greater<double>>::iterator it = groupScore.begin(); it != groupScore.end() && cnt < 3; it++, cnt++) {
if (this->m_Index == 1) {
v2.push_back((*it).second);
} else {
vVictory.push_back((*it).second);
}
}
groupScore.clear();
cout << endl;
}
}
cout << "--------------------第" << this->m_Index << "轮比赛完毕--------------------" << endl;
system("pause");
}
// 显示分数
void SpeechManager::showScore() {
cout << "--------------------第" << this->m_Index << "轮晋级选手信息如下--------------------" << endl;
vector<int> v;
if (this->m_Index == 1) {
v = v2;
} else {
v = vVictory;
}
for (auto it : v) {
cout << "选手编号: " << it << " 姓名: " << this->m_Speaker[it].name << " 分数: " << this->m_Speaker[it].score[this->m_Index - 1] << endl;
}
cout << endl;
system("pause");
system("cls");
this->showMenu();
}
// 保存分数到文件中并记录
void SpeechManager::saveRecord() {
ofstream ofs;
ofs.open("../speech.csv", ios::out | ios::app); // 用输出的方式打开文件 写文件 追加方式写
// 将每个人的数据写入文件中
for (auto it : vVictory) {
ofs << it << "," << this->m_Speaker[it].score[1] << ",";
}
ofs << endl;
// 关闭文件
ofs.close();
cout << "文件已经保存" << endl;
this->fileIsEmpty = false; // 有记录了 文件不为空
}
// 开始比赛 比赛流程控制
void SpeechManager::startSpeech() {
// 第一轮比赛
// 1.抽签
this->speechDraw();
// 2.比赛
this->speechContest();
// 3.显示晋级结果
this->showScore();
this->m_Index++; // 记得进入下一轮
// 第二轮比赛
// 1.抽签
this->speechDraw();
// 2.比赛
this->speechContest();
// 3.显示最终结果
this->showScore();
// 4.保存分数
saveRecord();
// 重置比赛
this->initSpeech();
// 创建选手
createSpeaker();
// 获取往届记录
this->loadRecord();
cout << "本届比赛完毕!" << endl;
system("pause");
system("cls");
}
// 读取往届记录
void SpeechManager::loadRecord() {
ifstream ifs("../speech.csv", ios::in); // 输入流 读文件
if (!ifs.is_open()) {
this->fileIsEmpty = true; // 文件为空
cout << "文件不存在" << endl;
ifs.close();
return ;
}
char ch;
ifs >> ch;
if (ifs.eof()) {
cout << "文件为空!" << endl;
this->fileIsEmpty = true;
ifs.close();
return ;
}
// 否则文件不为空
this->fileIsEmpty = false;
ifs.putback(ch); // 读取的单个字符放回去 读走的字符要放回去刚才读了一个 否则就少了一个
// 利用,分割
string data;
int idx = 0;
while (ifs >> data) {
vector<string> v;
int pos = -1;
int start = 0;
while (true) {
pos = data.find(",", start);
if (pos == -1) break;
string tmp = data.substr(start, pos - start);
v.push_back(tmp);
start = pos + 1;
}
this->m_Record.insert(make_pair(idx, v));
idx++;
// cout << data << endl;
}
ifs.close();
// for (auto it : m_Record) {
// cout << it.first << "冠军编号为 " << it.second[0] << " 分数为 " << it.second[1] << endl;
// }
}
// 查看往届得分
void SpeechManager::showRecord() {
if (this->fileIsEmpty) {
cout << "文件不存在或记录为空!" << endl;
return ;
}
for (int i = 0; i < this->m_Record.size(); ++i) {
cout << "第" << i + 1 << "届 "
<< " 冠军编号 " << this->m_Record[i][0] << " 得分 " << this->m_Record[i][1] << " "
<< " 亚军编号 " << this->m_Record[i][2] << " 得分 " << this->m_Record[i][3] << " "
<< " 季军编号 " << this->m_Record[i][4] << " 得分 " << this->m_Record[i][5] << endl;
}
system("pause");
system("cls");
}
// 清空文件记录
void SpeechManager::clearRecord() {
cout << "确认清空?" << endl;
cout << "1.确认" << endl;
cout << "2.返回" << endl;
int select = 0;
cin >> select;
if (select == 1) {
// 打开模式 ios::trunc 如果文件存在先删除再创建
ofstream ofs("../speech.csv", ios::trunc);
ofs.close();
// 初始化属性
this->initSpeech();
// 创建选手
this->createSpeaker();
// 获取往届记录
this->loadRecord();
// 文件状态改为true 因为清空了再创建文件为空了
this->fileIsEmpty = true;
cout << "清空成功!" << endl;
}
system("pause");
system("cls");
}
// 析构函数实现
SpeechManager::~SpeechManager() {
// 没开辟堆区 不实现也是可以的
}
speechManager.h
//
// Created by Cauchyshy on 2023/5/5.
//
#ifndef SPEECH_SPEECHMANAGER_H
#define SPEECH_SPEECHMANAGER_H
#endif //SPEECH_SPEECHMANAGER_H
#pragma once
#include <iostream>
#include <vector>
#include <map>
#include <deque>
#include <string>
#include <algorithm>
#include <numeric>
#include <functional>
#include <fstream>
#include "speaker.h"
using namespace std;
// 演讲比赛管理类
class SpeechManager {
public:
// 属性
// 比赛选手 12人 放的是选手编号 下同
vector<int> v1;
// 第一轮晋级 6人
vector<int> v2;
// 胜利前三名 3人
vector<int> vVictory;
// 存放编号 以及具体选手信息 key->value
map<int, Speaker> m_Speaker;
// 比赛轮数
int m_Index;
// 文件为空的标志
bool fileIsEmpty;
// 往届记录
map<int, vector<string>> m_Record;
// 构造函数
SpeechManager();
// 菜单展示
void showMenu();
// 退出系统
void exitSystem();
// 初始化属性 比赛开始
void initSpeech();
// 创建选手
void createSpeaker();
// 抽签
void speechDraw();
// 比赛逻辑实现
void speechContest();
// 显示分数
void showScore();
// 保存分数记录
void saveRecord();
// 开始比赛 对比赛流程进行控制
void startSpeech();
// 读取往届记录
void loadRecord();
// 查看往届得分
void showRecord();
// 清空文件记录
void clearRecord();
// 析构函数
~SpeechManager();
};