1. 演讲比赛程序需求
1.2程序功能
2. 项目创建
创建名为speech_contest的目录名称
3. 创建管理类
功能描述:
提供菜单界面与用户交互
对演讲比赛流程进行控制
与文件的读写交互
3.1 创建文件
在头文件和源文件的文件夹下分别创建speechManager.h和speechManager.cpp文件
speechManager.h
#ifndef SPEECHMANAGER_H
#define SPEECHMANAGER_H
#include <iostream>
#pragma once
//设计演讲管理类
class SpeechManager
{
public:
SpeechManager();
~SpeechManager();
};
#endif // SPEECHMANAGER_H
speechManager.cpp
#include "speechManager.h"
SpeechManager::SpeechManager()
{
}
SpeechManager::~SpeechManager()
{
}
4. 菜单功能
功能描述:与用户的沟通界面
4.1 添加成员函数
在管理类中speechManager.h中添加成员函数void show_Menu();
5. 显示菜单
//菜单功能
void SpeechManager::show_Menu()
{
while (true) {
cout << "=================" << endl;
cout << "欢迎参加演讲比赛" << endl;
cout << "1.开始演讲比赛" << endl;
cout << "2.查看往届记录" << endl;
cout << "3.清空比赛记录" << endl;
cout << "0.退出比赛程序" << endl;
cout << "=================" << endl;
cout << "请输入你的选择: " << endl;
int select;
cin >> select;
switch (select) {
case 1:
cout << "开始" << endl;
break;
case 0:
cout << "欢迎下次使用" << endl;
exit(0);
default:
system("cls");
break;
}
}
}
6. 演讲比赛功能
6.1 功能分析
比赛流程分析:
抽签 -》开始演讲比赛 -》显示第一轮比赛结果 -》
抽签 -》开始演讲比赛 -》显示前三名结果 -》保存分数
6.2 创建选手类
选手类中的属性包括:选手姓名、分数
头文件中创建speaker.h文件,并添加代码:
#ifndef SPEAKER_H
#define SPEAKER_H
#include <iostream>
using namespace std;
class Speaker
{
public:
Speaker();
string m_Name; //姓名
double m_Score[2]; //分数 最多有两轮得分
};
#endif // SPEAKER_H
6.3 比赛
6.3.1 成员属性添加
//设计演讲管理类
class SpeechManager {
public:
SpeechManager();
~SpeechManager();
void show_Menu();
//初始化属性和容器
void initSpeech();
//添加成员属性
//保存第一轮比赛的选手编号的容器
vector<int> v1;
//第一轮晋级选手编号容器
vector<int> v2;
//胜出前三名选手编号容器
vector<int> vVictory;
//存放编号以及对应选手的容器
map<int, Speaker> m_Speaker;
//存放比赛轮数
int m_Index;
};
//初始化
void SpeechManager::initSpeech()
{
//容器为空
this->v1.clear();
this->v2.clear();
this->vVictory.clear();
this->m_Speaker.clear();
//初始化比赛轮数
this->m_Index = 1;
}
SpeechManager::SpeechManager()
{
this->initSpeech();
}
6.3.3 创建选手
提供比赛的成员函数void createSpeaker();
void SpeechManager::createSpeaker()
{
string nameSeed = "ABCDEFGHIJKL";
for (int i = 0; i < (int)nameSeed.size(); i++) {
string name = "选手";
name += nameSeed[i];
Speaker sp;
sp.m_Name = name;
for (int j = 0; j < 2; j++) {
sp.m_Score[j] = 0;
}
// 12名选手编号
this->v1.push_back(i + 10001);
//选手编号 以及对应的选手,放到map容器中
this->m_Speaker.insert(make_pair(i + 10001, sp));
}
}
6.3.4 开始比赛成员函数添加
编写startSpeech();
函数作用是控制比赛的流程
//控制比赛流程
void SpeechManager::startSpeech()
{
//第一轮比赛
// 1.抽签
// 2.比赛
// 3.显示晋级结果
//第二轮比赛
// 1.抽签
// 2.比赛
// 3.显示最终结果
// 4.保存分数
}
6.3.5 抽签
void SpeechManager::speechDraw()
{
cout << "第<<" << this->m_Index << ">>轮选手正在抽签" << endl;
cout << "==================" << endl;
cout << "抽签后演讲顺序如下:" << endl;
if (this->m_Index == 1) {
random_shuffle(v1.begin(), v1.end());
//查看是否创建成功选手
for (vector<int>::iterator it = v1.begin(); it != v1.end(); it++) {
cout << *it << " ";
}
cout << endl;
} else {
random_shuffle(v2.begin(), v2.end());
for (vector<int>::iterator it = v2.begin(); it != v2.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
system("pause");
system("cls");
}
6.3.3 开始比赛
在头文件中添加比赛的成员函数void speechContest();
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 = this->v1;
} else {
v_Src = this->v2;
}
//遍历所有的参赛选手进行比赛
for (vector<int>::iterator it = v_Src.begin(); it != v_Src.end(); it++) {
num++;
//进行打分
deque<double> d;
for (int i = 0; i < 10; i++) {
double score = (rand() % 401 + 600) / 10.f; // 600~1000
// cout << "分数" << score << " ";
d.push_back(score);
}
// cout << endl;
sort(d.begin(), d.end(), greater<double>()); //排序
d.pop_back(); //去除尾
d.pop_front(); //去除头
double sum = accumulate(d.begin(), d.end(), 0.0f); //获取总分
double avg = sum / (double)d.size(); //平均分 size应该是容器的大小,这里应该为8
//打印平均分
// cout << "编号" << *it << " "
// << "姓名:" << this->m_Speaker[*it].m_Name << "平均分:" << avg << endl;
//将平均分放入map容器中
this->m_Speaker[*it].m_Score[this->m_Index - 1] = avg; //根据键获取到值,在给属性赋值
//将打分的数据放入临时小组容器
groupScore.insert(make_pair(avg, *it)); //平均分 和编号 设置了greater 插入数据会自动排序
//每6人取出前三名
if (num % 6 == 0) {
cout << "第" << num / 6 << "小组名次如下:" << endl;
for (multimap<double, int, greater<double>>::iterator it = groupScore.begin(); it != groupScore.end(); it++) {
cout << "编号" << it->second << "姓名" << this->m_Speaker[it->second].m_Name
<< "成绩:" << this->m_Speaker[it->second].m_Score[this->m_Index - 1] << endl;
}
//取走前三名
int count = 0;
for (multimap<double, int, greater<double>>::iterator it = groupScore.begin(); it != groupScore.end() && count < 3; it++, count++) {
if (this->m_Index == 1) {
v2.push_back((*it).second);
} else {
this->vVictory.push_back((*it).second);
}
}
//清空
groupScore.clear();
cout << endl;
}
}
cout << "第" << this->m_Index << "轮比赛完毕" << endl;
system("pause");
system("cls");
}
6.3.7 显示比赛分数
void show_Score();实现
void SpeechManager::show_Score()
{
cout << "-------第" << this->m_Index << "轮晋级选手信息如下:--------" << endl;
vector<int> v;
if (this->m_Index == 1) {
v = v2;
} else {
v = this->vVictory;
}
for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
cout << "选手编号:" << *it << "姓名:" << m_Speaker[*it].m_Name << "分数:" << m_Speaker[this->m_Index - 1].m_Score << endl;
}
cout << endl;
system("pause");
system("cls");
this->show_Menu();
}
6.3.8 第二轮比赛
第二轮比赛流程同第一轮,只是比赛的轮+1,其余流程不变
void SpeechManager::startSpeech()
{
//第一轮比赛
// 1.抽签
this->speechDraw();
// 2.比赛
this->speechContest();
// 3.显示晋级结果
this->show_Score();
//第二轮比赛
this->m_Index++;
// 1.抽签
this->speechDraw();
// 2.比赛
this->speechContest();
// 3.显示最终结果
this->show_Score();
// 4.保存分数
}
6.4 保存分数
将每次演讲比赛的得分记录到文件中
void saveRecord();
void SpeechManager::save_Record()
{
ofstream ofs;
ofs.open("speech.csv", ios::out | ios::app); // out是出来,写操作 in是进去,读操作 ,追加模式
//将每个人数据写入
for (vector<int>::iterator it = this->vVictory.begin(); it != this->vVictory.end(); it++) {
ofs << *it << "," //编号3
<< m_Speaker[*it].m_Score[1] << ",";
}
ofs << endl;
ofs.close();
cout << "记录已经保存" << endl;
}
7 查看记录
7.1 读取记录分数
void load_Record();
判断文件是否为空bool fileIsEmpty;
添加往届记录的容器map<int,vector<string>>m_Record;
其中m_Record中key代表第几届,value代表具体的信息。
void SpeechManager::load_Record()
{
ifstream ifs;
ifs.open("speech.csv", ios::in); //读取文件
if (!ifs.is_open()) {
this->fileIsEmpty = true;
cout << "文件不存在" << endl;
ifs.close();
return;
}
//文件清空
char ch;
ifs >> ch;
if (ifs.eof()) {
this->fileIsEmpty = true;
cout << "文件为空" << endl;
return;
}
//文件不为空
else {
this->fileIsEmpty = false;
ifs.putback(ch); //将上面读取的单个字符,放回来
string data;
int index = 0;
while (ifs >> data) {
cout << "数据为" << data << endl;
vector<string> v; //存放6个string字符串
int pos = -1; //查到逗号位置的变量
int start = 0;
while (true) {
pos = data.find(",", start); //查找逗号,从start开始位置,pos返回逗号的位置
if (pos == -1) { //没有找到
break;
}
string temp = data.substr(start, pos - start); //获取字串
// cout << temp << endl;
v.push_back(temp); //放入
start = pos + 1;
}
this->m_Record.insert(make_pair(index, v));
index++;
}
ifs.close();
for (map<int, vector<string>>::iterator it = this->m_Record.begin(); it != this->m_Record.end(); it++) {
cout << "第" << it->first + 1 << "届冠军编号" << it->second[0] << "分数" << it->second[1] << endl;
}
}
system("pause");
system("cls");
}
7.2 查看记录流程
void show_Record();
void SpeechManager::show_Record()
{
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");
}
7.3 BUG解决
查看往届记录时,若文件不存在或为空,并未提示
解决方式:在showRecord函数中,开始判断文件状态
if (this->fileIsEmpty) {
cout << "文件为空,或者文件不存在" << endl;
}
若记录为空或不存在,比完赛依然提示记录为空
解决方式:saveRecord中更新文件为空的标志
this->fileIsEmpty = false;
添加随机数种子
srand((unsigned int)time(NULL));
8 清空记录
8.1 清空记录功能实现
void clear_Record();
void SpeechManager::clear_Record()
{
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();
cout << "清空成功" << endl;
}
system("pause");
system("cls");
}
9 全部的代码
9.1项目结构如下
speaker.h文件内容
#ifndef SPEAKER_H
#define SPEAKER_H
#include <iostream>
using namespace std;
class Speaker {
public:
Speaker();
string m_Name; //姓名
double m_Score[2]; //分数 最多有两轮得分
};
#endif // SPEAKER_H
speaker.cpp文件内容
#include "speaker.h"
Speaker::Speaker()
{
}
speechManager.h文件内容
#ifndef SPEECHMANAGER_H
#define SPEECHMANAGER_H
#include <iostream>
#pragma once //防止头文件重复包含
#include <algorithm>
#include <deque>
#include <fstream>
#include <functional>
#include <map>
#include <numeric>
#include <speaker.h>
#include <vector>
using namespace std;
//设计演讲管理类
class SpeechManager {
public:
SpeechManager();
~SpeechManager();
void show_Menu();
//初始化属性和容器
void initSpeech();
//添加成员属性
//保存第一轮比赛的选手编号的容器
vector<int> v1;
//第一轮晋级选手编号容器
vector<int> v2;
//胜出前三名选手编号容器
vector<int> vVictory;
//存放编号以及对应选手的容器
map<int, Speaker> m_Speaker;
//存放比赛轮数
int m_Index;
//创建演讲人
void createSpeaker();
//控制比赛流程
void startSpeech();
//打乱顺序
void speechDraw();
//比赛成员函数
void speechContest();
//显示比赛结果
void show_Score();
//保存
void save_Record();
//获取数据
void load_Record();
bool fileIsEmpty;
//存放往届记录的容器
map<int, vector<string>> m_Record;
//查看记录功能
void show_Record();
//清空记录
void clear_Record();
};
#endif // SPEECHMANAGER_H
speechManager.cpp文件内容
#include "speechManager.h"
SpeechManager::SpeechManager()
{
this->initSpeech();
this->createSpeaker();
//加载往届记录
// this->load_Record();
}
SpeechManager::~SpeechManager()
{
}
//菜单功能
void SpeechManager::show_Menu()
{
int select;
while (true) {
cout << "=================" << endl;
cout << "欢迎参加演讲比赛" << endl;
cout << "1.开始演讲比赛" << endl;
cout << "2.查看往届记录" << endl;
cout << "3.清空比赛记录" << endl;
cout << "0.退出比赛程序" << endl;
cout << "=================" << endl;
cout << "请输入你的选择: " << endl;
cin >> select;
switch (select) {
case 1:
this->startSpeech();
break;
case 2:
// cout << "查看比赛记录" << endl;
this->load_Record();
this->show_Record();
break;
case 3:
this->clear_Record();
break;
case 0:
cout << "欢迎下次使用" << endl;
exit(0);
default:
system("cls");
break;
}
}
}
//初始化
void SpeechManager::initSpeech()
{
//容器为空
this->v1.clear();
this->v2.clear();
this->vVictory.clear();
this->m_Speaker.clear();
//初始化比赛轮数
this->m_Index = 1;
//记录数据也需要清空
this->m_Record.clear();
}
//创建人物
void SpeechManager::createSpeaker()
{
string nameSeed = "ABCDEFGHIJKL";
for (int i = 0; i < (int)nameSeed.size(); i++) {
string name = "选手";
name += nameSeed[i];
Speaker sp;
sp.m_Name = name;
for (int j = 0; j < 2; j++) {
sp.m_Score[j] = 0;
}
// 12名选手编号
this->v1.push_back(i + 10001);
//选手编号 以及对应的选手,放到map容器中
this->m_Speaker.insert(make_pair(i + 10001, sp));
}
}
//控制比赛流程
void SpeechManager::startSpeech()
{
//第一轮比赛
// 1.抽签
this->speechDraw();
// 2.比赛
this->speechContest();
// 3.显示晋级结果
this->show_Score();
//第二轮比赛
this->m_Index++;
// 1.抽签
this->speechDraw();
// 2.比赛
this->speechContest();
// 3.显示最终结果
this->show_Score();
// 4.保存分数
this->save_Record();
//进行数据重置
this->initSpeech();
this->createSpeaker();
//加载往届记录
// this->load_Record();
cout << "本届比赛完毕!" << endl;
system("pause");
system("cls");
}
void SpeechManager::speechDraw()
{
cout << "第<<" << this->m_Index << ">>轮选手正在抽签" << endl;
cout << "==================" << endl;
cout << "抽签后演讲顺序如下:" << endl;
if (this->m_Index == 1) {
random_shuffle(v1.begin(), v1.end());
//查看是否创建成功选手
for (vector<int>::iterator it = v1.begin(); it != v1.end(); it++) {
cout << *it << " ";
}
cout << endl;
} else {
random_shuffle(v2.begin(), v2.end());
for (vector<int>::iterator it = v2.begin(); it != v2.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
system("pause");
cout << "==================" << endl;
}
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 = this->v1;
} else {
v_Src = this->v2;
}
//遍历所有的参赛选手进行比赛
for (vector<int>::iterator it = v_Src.begin(); it != v_Src.end(); it++) {
num++;
//进行打分
deque<double> d;
for (int i = 0; i < 10; i++) {
double score = (rand() % 401 + 600) / 10.f; // 600~1000
// cout << "分数" << score << " ";
d.push_back(score);
}
// cout << endl;
sort(d.begin(), d.end(), greater<double>()); //排序
d.pop_back(); //去除尾
d.pop_front(); //去除头
double sum = accumulate(d.begin(), d.end(), 0.0f); //获取总分
double avg = sum / (double)d.size(); //平均分 size应该是容器的大小,这里应该为8
//打印平均分
// cout << "编号" << *it << " "
// << "姓名:" << this->m_Speaker[*it].m_Name << "平均分:" << avg << endl;
//将平均分放入map容器中
this->m_Speaker[*it].m_Score[this->m_Index - 1] = avg; //根据键获取到值,在给属性赋值
//将打分的数据放入临时小组容器
groupScore.insert(make_pair(avg, *it)); //平均分 和编号 设置了greater 插入数据会自动排序
//每6人取出前三名
if (num % 6 == 0) {
cout << "第" << num / 6 << "小组名次如下:" << endl;
for (multimap<double, int, greater<double>>::iterator it = groupScore.begin(); it != groupScore.end(); it++) {
cout << "编号" << it->second << "姓名" << this->m_Speaker[it->second].m_Name
<< "成绩:" << this->m_Speaker[it->second].m_Score[this->m_Index - 1] << endl;
}
//取走前三名
int count = 0;
for (multimap<double, int, greater<double>>::iterator it = groupScore.begin(); it != groupScore.end() && count < 3; it++, count++) {
if (this->m_Index == 1) {
v2.push_back((*it).second);
} else {
this->vVictory.push_back((*it).second);
}
}
//清空
groupScore.clear();
cout << endl;
}
}
cout << "第" << this->m_Index << "轮比赛完毕" << endl;
system("pause");
// system("cls");
}
void SpeechManager::show_Score()
{
cout << "-------第" << this->m_Index << "轮晋级选手信息如下:--------" << endl;
vector<int> v;
if (this->m_Index == 1) {
v = v2;
} else {
v = this->vVictory;
}
for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
cout << "选手编号:" << *it << "姓名:" << m_Speaker[*it].m_Name << "分数:" << m_Speaker[this->m_Index - 1].m_Score << endl;
}
cout << endl;
system("pause");
system("cls");
// this->show_Menu();
}
void SpeechManager::save_Record()
{
ofstream ofs;
ofs.open("speech.csv", ios::out | ios::app); // out是出来,写操作 in是进去,读操作 ,追加模式
//将每个人数据写入
for (vector<int>::iterator it = this->vVictory.begin(); it != this->vVictory.end(); it++) {
ofs << *it << "," //编号3
<< m_Speaker[*it].m_Score[1] << ",";
}
ofs << endl;
ofs.close();
cout << "记录已经保存" << endl;
this->fileIsEmpty = false;
}
void SpeechManager::load_Record()
{
ifstream ifs;
ifs.open("speech.csv", ios::in); //读取文件
if (!ifs.is_open()) {
this->fileIsEmpty = true;
// cout << "文件不存在" << endl;
ifs.close();
return;
}
//文件清空
char ch;
ifs >> ch;
if (ifs.eof()) {
this->fileIsEmpty = true;
// cout << "文件为空" << endl;
return;
}
//文件不为空
else {
this->fileIsEmpty = false;
ifs.putback(ch); //将上面读取的单个字符,放回来
string data;
int index = 0;
while (ifs >> data) {
// cout << "数据为" << data << endl;
vector<string> v; //存放6个string字符串
int pos = -1; //查到逗号位置的变量
int start = 0;
while (true) {
pos = data.find(",", start); //查找逗号,从start开始位置,pos返回逗号的位置
if (pos == -1) { //没有找到
break;
}
string temp = data.substr(start, pos - start); //获取字串
// cout << temp << endl;
v.push_back(temp); //放入
start = pos + 1;
}
this->m_Record.insert(make_pair(index, v));
index++;
}
ifs.close();
// for (map<int, vector<string>>::iterator it = this->m_Record.begin(); it != this->m_Record.end(); it++) {
// cout << "第" << it->first + 1 << "届冠军编号" << it->second[0] << "分数" << it->second[1] << endl;
// }
}
// system("pause");
// system("cls");
}
void SpeechManager::show_Record()
{
if (this->fileIsEmpty) {
cout << "文件为空,或者文件不存在" << endl;
} else {
for (int i = 0; i < (int)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::clear_Record()
{
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();
cout << "清空成功" << endl;
}
system("pause");
system("cls");
}
main.cpp文件内容
#include "speechManager.h"
#include <iostream>
using namespace std;
#include <algorithm>
#include <ctime>
int main()
{
//添加随机数种子
srand((unsigned int)time(NULL));
SpeechManager sm;
sm.show_Menu();
//查看是否创建成功选手
// for (map<int, Speaker>::iterator it = sm.m_Speaker.begin(); it != sm.m_Speaker.end(); it++) {
// cout << it->first << endl;
// cout << it->second.m_Name << it->second.m_Score[0] << endl;
// }
return 0;
}