演讲比赛流程管理系统

  • 一、项目要求
  • 二、构建演讲比赛管理类
  • 三、菜单
  • 四、设置main函数执行流程
  • 五、函数实现
    • 一、构造函数
    • 二、退出函数
    • 三、InitSpeech
    • 四、CreatSpeaker
    • 五、StartSpeech
    • 六、SpeechLot
    • 七、SpeechContest
    • 八、ShowScore
    • 九、SaveRecore
    • 十、LoadRecord
    • 十一、ShowRecord
    • 十二、ClearRecord
    • 十三、析构函数
  • 六、完整函数实现源码

一、项目要求

  • 这是一场有12人参加的演讲比赛,分两轮,一轮淘汰赛,一轮决赛。
  • 每名选手都有对应编号,如10001~10012
  • 比赛方式:分六人一组比赛
  • 第一轮分两组,抽签决定演讲顺序
  • 十人打分,去除一个最高分和一个最低分,求得平均分作为选手成绩
  • 每组取前三晋级,进入第二轮
  • 第二轮前三名胜出
  • 每轮比赛都要显示晋级选手信息

二、构建演讲比赛管理类

#pragma once
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<deque>
#include<algorithm>
#include<numeric>//accumulate
#include<fstream>
#include<ctime>
#include<iomanip>//设置打印精度
#include<functional>//内建函数对象
#include"Speaker.h"
#define FILENAME "speech.csv"
using namespace std;
class SpeechContestManager
{public:vector<int>v1;vector<int>v2;vector<int>v_victory;map<int, Speaker>m_speaker;map<int, vector<string>>m_record;int m_round;bool emptyfile;SpeechContestManager();void Menu();//菜单void Exit();//退出void InitSpeech();//初始化演讲void CreatSpeaker();//设置比赛人的信息void StartSpeech();//开始演讲比赛void SpeechLot();//比赛抽签void SpeechConst();//进行比赛void ShowScore();//展示分数void SaveRecord();//保存记录void LoadRecord();//载入记录void ShowRecord();//展示记录void ClearRecord();//清空记录~SpeechContestManager();
};

为了便于理解,使用v1,v2分别存储两轮的选手,
v_victory存储赢得比赛的编号
这里还需创建一个演讲者类

#pragma once
#include<iostream>
#include<string>
using namespace std;class Speaker
{public:string m_name;double m_score[2];
};

存储姓名和两轮的比赛成绩
用map容器的对组存储编号和Speaker类的属性,用于存储选手信息
第二个map则是用于存储获奖者信息,读文件,展示文件的载体
m_round表示轮数
bool emptyfile表明文件状态
是调用展示记录函数时的判断依据

三、菜单

void SpeechContestManager::Menu()
{cout << "********************************************" << endl;cout << "*********     欢迎参加演讲比赛     *********" << endl;cout << "*********      1.开始演讲比赛      *********" << endl;cout << "*********      2.查看往期记录      *********" << endl;cout << "*********      3.清空比赛记录      *********" << endl;cout << "*********      0.退出比赛程序      *********" << endl;cout << "********************************************" << endl;cout << endl;
}

提示用户进行输入

四、设置main函数执行流程

#include"SpeechcontestManager.h"
int main()
{srand((unsigned int)time(NULL));SpeechContestManager scm;scm.InitSpeech();scm.CreatSpeaker();scm.LoadRecord();int choice;do {scm.Menu();cout << "请选择:" << endl;cin >> choice;switch (choice){case 1:scm.StartSpeech();break;case 2:scm.ShowRecord();break;case 3:scm.ClearRecord();break;case 0:scm.Exit();default:system("cls");}} while (choice);return 0;
}

要设置时间戳,在抽签和生成成绩时需要用到
实例化管理对象,先进行初始化,选手设置,载入记录。
调用菜单,根据用户输入调用不同功能函数,选择错误进行清屏即可

五、函数实现

一、构造函数

SpeechContestManager::SpeechContestManager()
{this->InitSpeech();
}

调用InitSpeech初始化,其实没必要,main函数里又手动调用了一遍

二、退出函数

void SpeechContestManager::Exit()
{cout << "欢迎下次使用!" << endl;system("pause");exit(0);
}

exit的功 能: 关闭所有文件,终止正在执行的进程。
exit(1)表示异常退出.这个1是返回给操作系统的。
exit(x)(x不为0)都表示异常退出
exit(0)表示正常退出
exit()的参数会被传递给一些操作系统,包括UNIX,Linux,和MS DOS,以供其他程序使用。

三、InitSpeech

void SpeechContestManager::InitSpeech()
{this->v1.clear();this->v2.clear();this->v_victory.clear();this->m_speaker.clear();this->m_record.clear();this->m_round =1;
}

就是对类内成员变量进行初始化操作
轮数置为1

四、CreatSpeaker

void SpeechContestManager::CreatSpeaker()
{string nameSeed[12] = { { "白羊" },{"金牛"},{"双子"},{"巨蟹"},{"狮子"},{"处女"},{"天秤"},{"天蝎"},{"射手"},{"摩羯"},{"水瓶"},{"双鱼"} };for (int i = 0; i < 12; i++){Speaker sp;sp.m_name = nameSeed[i];for (int j = 0; j < 2; j++)sp.m_score[j] = 0;this->v1.push_back(i + 1001);this->m_speaker.insert(make_pair(i + 1001, sp));}
}

这里我采用12星座作为选手名,给定编号,这里是顺序的,等下会打乱
使用make_pair插入map的对组元素

五、StartSpeech

void SpeechContestManager::StartSpeech()
{this->SpeechLot();//抽签this->SpeechContest();//比赛this->ShowScore();//展示成绩this->m_round++;//进入下一轮this->SpeechLot();//抽签this->SpeechContest();//比赛this->ShowScore();//展示成绩this->SaveRecord();//保存三甲this->InitSpeech();//初始化,为下一届做准备this->CreatSpeaker();this->LoadRecord();//载入保存记录cout << "本届比赛结束!" << endl;system("pause");system("cls");
}

这里是比赛流程函数的调用

六、SpeechLot

void SpeechContestManager::SpeechLot()
{cout << "第 << " << this->m_round << " >> 轮比赛选手正在抽签" << endl;cout << "----------------------------" << endl;cout << "抽签后演讲顺序如下" << endl;if (this->m_round == 1){random_shuffle(v1.begin(), v1.end());for (auto it : this->v1)cout << it << " ";cout << endl;}else{random_shuffle(v2.begin(), v2.end());for (auto it : this->v2)cout << it << " ";cout << endl;}cout << "----------------------------" << endl;system("pause");cout << endl;
}

利用random_shuffle随机打乱容器数据顺序,里面存的是选手编号

七、SpeechContest

void SpeechContestManager::SpeechContest()
{cout << "::::::::::第" << this->m_round << "轮比赛正式开始:" << "::::::::::"  << endl;multimap<double, int, greater<double>>groupScore;//设置组容器,用内置函数对象进行降序排序,用于取前三int num = 0;vector<int>vc;if (this->m_round == 1)//第一轮vc = v1;//将v1的随机编号利用重载=赋给vcelse//反之vc = v2;for (auto it : vc){num++;deque<double>d;//利用deque容器存分数for (int i = 0; i < 10; i++){double score = (rand() % 400 + 601) / 10.f;//设置随机分数d.push_back(score);}sort(d.begin(), d.end());//进行有序排序d.pop_back();//去掉最高分d.pop_front();//去掉最低分double sum = accumulate(d.begin(), d.end(), 0.0f);//累加函数double avg= sum / (double)d.size();this->m_speaker[it].m_score[this->m_round - 1] = avg;//将品均分存入参赛人员信息中groupScore.insert(make_pair(avg, it));//插入数据if (num % 6 == 0)//满六人后展示六人成绩{cout << "第" << num / 6 << "小组比赛名次:" << endl;for (auto it : groupScore)cout << "编号:" << it.second << "\t姓名:" << this->m_speaker[it.second].m_name << "\t成绩" <<fixed<<setprecision(2)<< this->m_speaker[it.second].m_score[this->m_round - 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_round == 1)//为第一轮v2.push_back(it->second);//存入第二轮elsev_victory.push_back(it->second);//为第二轮,存入胜利者中}//这里是将前三的人相应容器中groupScore.clear();清空cout << endl;}}cout << "::::::::::第" << this->m_round << "轮比赛结束:" << ":::::::::::" << endl;system("pause");
}

进行比赛要分第一轮和第二轮进行不同存储

八、ShowScore

void SpeechContestManager::ShowScore()
{cout << "::::::::::第" << this->m_round << "轮晋级选手信息如下:" << "::::::::::" << endl;vector<int>v;if (this->m_round == 1)v = v2;elsev = v_victory;for (auto it : v)cout << "选手编号:" << it << "\t姓名:" << m_speaker[it].m_name << "\t得分:" << fixed << setprecision(2) << m_speaker[it].m_score[this->m_round - 1] << endl;cout << endl;system("pause");system("cls");this->Menu();
}

展示的都是晋级成功的或获胜的人的信息,打印比赛后所存储的信息

九、SaveRecore

void SpeechContestManager::SaveRecord()
{ofstream out;out.open(FILENAME, ios::out | ios::app);for (auto it : v_victory)out << m_speaker[it].m_name << "," <<fixed<<setprecision(2)<< m_speaker[it].m_score[1] << it << ",";out << endl;cout << "记录保存成功" << endl;this->emptyfile = false;
}

保存本届获胜者信息,用’,'分隔,ios::app追加写入
这里有个问题,无法以两位精度保存数据,不知如何解决

十、LoadRecord

void SpeechContestManager::LoadRecord()
{ifstream in(FILENAME, ios::in);if (!in.is_open()){this->emptyfile = true;cout << "文件不存在!" << endl;in.close();return;}char ch;in >> ch;if (in.eof()){this->emptyfile = true;cout << "文件为空!" << endl;in.close();return;}this->emptyfile = false;in.putback(ch);string data;int index = 0;while (in >> 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(index, v));index++;}in.close();
}

is_open()判断文件是否打开
在读出最后一个字符时,in.eof()还是为假的,只当读过最后一个字符再读(读不成功)时in.eof()才变为真,用来判断文件是否为空,读完后用putback()再放回去,如果文件存在且不为空,那么就将数据存入map容器中。
这里先找到’,‘位置,再用子串截取从start到’,'所在下标的子串。更新start获取新数据。
这里是为了应用find()查找函数,实际可以用几个临时变量存取,采用空格隔开。

十一、ShowRecord

void SpeechContestManager::ShowRecord()
{if (this->emptyfile)cout << "文件不存在或记录为空!" << endl;else{for (int i = 0; i < this->m_record.size(); i++){cout << "第" << i + 1 << "届" <<"冠军:" << this->m_record[i][0]  << "\t得分:" << fixed << setprecision(2) << this->m_record[i][1] << "\t""亚军:" << this->m_record[i][2] << "\t得分:" << fixed << setprecision(2) << this->m_record[i][3] << "\t""季军:" << this->m_record[i][4] <<"\t得分:" << fixed << setprecision(2) << this->m_record[i][5] << endl;}}system("pause");system("cls");
}

使用了map容器得[]访问方法,因数据是以greater<>排序过的,所以按顺序输出三甲即可,这里用emptyfile判断文件是否为空。

十二、ClearRecord

void SpeechContestManager::ClearRecord()
{cout << "确认清空?(Y/N)" << endl;char choice;cin >> choice;if (choice == 'Y'){ofstream out(FILENAME, ios::trunc);out.close();this->InitSpeech();this->CreatSpeaker();cout << "清空成功!" << endl;}system("pause");system("cls");
}

使用ios::trunc清除文件数据

十三、析构函数

通过之前的学习,我们知道,容器内部会自己析构释放内存,这里无需实现任何功能SpeechContestManager::~SpeechContestManager() {};

六、完整函数实现源码

#include "SpeechcontestManager.h"SpeechContestManager::SpeechContestManager()
{this->InitSpeech();
}void SpeechContestManager::Menu()
{cout << "********************************************" << endl;cout << "*********     欢迎参加演讲比赛     *********" << endl;cout << "*********      1.开始演讲比赛      *********" << endl;cout << "*********      2.查看往期记录      *********" << endl;cout << "*********      3.清空比赛记录      *********" << endl;cout << "*********      0.退出比赛程序      *********" << endl;cout << "********************************************" << endl;cout << endl;
}void SpeechContestManager::Exit()
{cout << "欢迎下次使用!" << endl;system("pause");exit(0);
}void SpeechContestManager::InitSpeech()
{this->v1.clear();this->v2.clear();this->v_victory.clear();this->m_speaker.clear();this->m_record.clear();this->m_round =1;
}void SpeechContestManager::CreatSpeaker()
{string nameSeed[12] = { { "白羊" },{"金牛"},{"双子"},{"巨蟹"},{"狮子"},{"处女"},{"天秤"},{"天蝎"},{"射手"},{"摩羯"},{"水瓶"},{"双鱼"} };for (int i = 0; i < 12; i++){Speaker sp;sp.m_name = nameSeed[i];for (int j = 0; j < 2; j++)sp.m_score[j] = 0;this->v1.push_back(i + 1001);this->m_speaker.insert(make_pair(i + 1001, sp));}
}void SpeechContestManager::StartSpeech()
{this->SpeechLot();this->SpeechContest();this->ShowScore();this->m_round++;this->SpeechLot();this->SpeechContest();this->ShowScore();this->SaveRecord();this->InitSpeech();this->CreatSpeaker();this->LoadRecord();cout << "本届比赛结束!" << endl;system("pause");system("cls");
}void SpeechContestManager::SpeechLot()
{cout << "第 << " << this->m_round << " >> 轮比赛选手正在抽签" << endl;cout << "----------------------------" << endl;cout << "抽签后演讲顺序如下" << endl;if (this->m_round == 1){random_shuffle(v1.begin(), v1.end());for (auto it : this->v1)cout << it << " ";cout << endl;}else{random_shuffle(v2.begin(), v2.end());for (auto it : this->v2)cout << it << " ";cout << endl;}cout << "----------------------------" << endl;system("pause");cout << endl;
}void SpeechContestManager::SpeechContest()
{cout << "::::::::::第" << this->m_round << "轮比赛正式开始:" << "::::::::::"  << endl;multimap<double, int, greater<double>>groupScore;int num = 0;vector<int>vc;if (this->m_round == 1)vc = v1;elsevc = v2;for (auto it : vc){num++;deque<double>d;for (int i = 0; i < 10; i++){double score = (rand() % 400 + 601) / 10.f;d.push_back(score);}sort(d.begin(), d.end());d.pop_back();d.pop_front();double sum = accumulate(d.begin(), d.end(), 0.0f);double avg= sum / (double)d.size();this->m_speaker[it].m_score[this->m_round - 1] = avg;groupScore.insert(make_pair(avg, it));if (num % 6 == 0){cout << "第" << num / 6 << "小组比赛名次:" << endl;for (auto it : groupScore)cout << "编号:" << it.second << "\t姓名:" << this->m_speaker[it.second].m_name << "\t成绩" <<fixed<<setprecision(2)<< this->m_speaker[it.second].m_score[this->m_round - 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_round == 1)v2.push_back(it->second);elsev_victory.push_back(it->second);}groupScore.clear();cout << endl;}}cout << "::::::::::第" << this->m_round << "轮比赛结束:" << ":::::::::::" << endl;system("pause");
}void SpeechContestManager::ShowScore()
{cout << "::::::::::第" << this->m_round << "轮晋级选手信息如下:" << "::::::::::" << endl;vector<int>v;if (this->m_round == 1)v = v2;elsev = v_victory;for (auto it : v)cout << "选手编号:" << it << "\t姓名:" << m_speaker[it].m_name << "\t得分:" << fixed << setprecision(2) << m_speaker[it].m_score[this->m_round - 1] << endl;cout << endl;system("pause");system("cls");this->Menu();
}void SpeechContestManager::SaveRecord()
{ofstream out;out.open(FILENAME, ios::out | ios::app);for (auto it : v_victory)out << m_speaker[it].m_name << "," <<fixed<<setprecision(2)<< m_speaker[it].m_score[1] << it << ",";out << endl;cout << "记录保存成功" << endl;this->emptyfile = false;
}void SpeechContestManager::LoadRecord()
{ifstream in(FILENAME, ios::in);if (!in.is_open()){this->emptyfile = true;cout << "文件不存在!" << endl;in.close();return;}char ch;in >> ch;if (in.eof()){this->emptyfile = true;cout << "文件为空!" << endl;in.close();return;}this->emptyfile = false;in.putback(ch);string data;int index = 0;while (in >> 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(index, v));index++;}in.close();
}void SpeechContestManager::ShowRecord()
{if (this->emptyfile)cout << "文件不存在或记录为空!" << endl;else{for (int i = 0; i < this->m_record.size(); i++){cout << "第" << i + 1 << "届" <<"冠军:" << this->m_record[i][0]  << "\t得分:" << fixed << setprecision(2) << this->m_record[i][1] << "\t""亚军:" << this->m_record[i][2] << "\t得分:" << fixed << setprecision(2) << this->m_record[i][3] << "\t""季军:" << this->m_record[i][4] <<"\t得分:" << fixed << setprecision(2) << this->m_record[i][5] << endl;}}system("pause");system("cls");
}void SpeechContestManager::ClearRecord()
{cout << "确认清空?(Y/N)" << endl;char choice;cin >> choice;if (choice == 'Y'){ofstream out(FILENAME, ios::trunc);out.close();this->InitSpeech();this->CreatSpeaker();cout << "清空成功!" << endl;}system("pause");system("cls");
}SpeechContestManager::~SpeechContestManager() {};

演讲比赛流程管理系统(看看你的星座会赢吗)相关推荐

  1. C++基于STL的演讲比赛流程管理系统

    目录 介绍 speaker.h speechManager.h speechManager.cpp 演讲比赛流程管理系统.cpp 介绍 学校举行一场演讲比赛,共有12个人参加.比宴共两轮,第一轮为淘汰 ...

  2. 演讲比赛流程管理系统C++

    参考B站黑马视频 演讲比赛流程管理系统 1.1.比赛规则 学校举行演讲比赛,共有12人参加.比赛共两轮,第一轮为淘汰赛,第二轮为决赛 每名选手都有对应的编号,如10001 ~ 10012 比赛方式:分 ...

  3. C++入门——演讲比赛流程管理系统

    参考链接 https://www.bilibili.com/video/BV1et411b73Z?p=264 演讲比赛流程管理系统 演讲比赛程序需求 比赛规则 学校举行一场演讲比赛,共有12个人参加. ...

  4. 【带你敲】演讲比赛流程管理系统

    CSDN话题挑战赛第2期 参赛话题:学习笔记 1. 演讲比赛程序需求 1.1 比赛规则 学校举行一场演讲比赛,共有12个人参加.比赛共两轮,第一轮为淘汰赛,第二轮为决赛. 比赛方式:分组比赛,每组6个 ...

  5. C++ 演讲比赛流程管理系统

    文章目录 演讲比赛流程管理系统 一. 程序需求 1. 比赛规则 2. 程序功能 二. 创建管理类 1. 创建文件 2. 头文件实现 3. 源文件实现 三. 菜单功能 1. 添加成员函数 2. 实现函数 ...

  6. C++阶段06笔记01【基于STL的演讲比赛流程管理系统】

    C++匠心之作-从0到1入门学编程[视频+课件+笔记+源码] 目录 1.演讲比赛程序需求 1.1.比赛规则 1.2.程序功能 1.3.程序效果图 2.项目创建 2.1.创建项目 2.2.添加文件 3. ...

  7. C++项目实战 —— 演讲比赛流程管理系统

    演讲比赛流程管理系统 文章目录 演讲比赛流程管理系统 1.演讲比赛程序需求 1.1 比赛规则 1.2 程序功能 2.项目创建 3.创建管理类 3.1 创建文件 3.2 头文件实现 3.3 源文件实现 ...

  8. 演讲比赛流程管理系统(C++)

    各位读者朋友,大家好!本篇文章主要内容是将黑马程序员(C++)的演讲比赛流程管理系统的源代码展现给大家.原码的大部分与黑马程序员老师写的代码一致,我只是对其中的一小部分做了优化,比如用户输入自己的选择 ...

  9. 【49C++项目案例:演讲比赛流程管理系统】

    文章目录 演讲比赛流程管理系统 1.演讲比赛程序需求 1.1 比赛规则 1.2 程序功能 1.3 程序效果图 2.项目创建 2.1 创建项目 2.2 添加文件 3.创建管理类 3.1 创建文件 3.2 ...

最新文章

  1. java的内存管理机制
  2. 使用PropertyPlaceholderConfigurer读取属性文件
  3. VC++2005项目的目录结构设置
  4. 这样讲 Netty 中的心跳机制,还有谁不会?
  5. HDU 2552 A simple problem
  6. 国内985副教授与行政人员一年能够拿到多少工资?
  7. 马斯克:特斯拉将发布结合太阳能、电池存储技术的新产品
  8. cropbox php,jQuery用户头像裁剪插件cropbox.js使用详解
  9. matlab电气应用,基于MATLAB/Simulink的高压直流输电系统的仿真研究.pdf
  10. 自然辩证法与计算机科学与技术,自然辩证法与计算机科学技术.docx
  11. 暑期计算机数学培训心得体会,实用的暑期培训心得体会3篇
  12. 禁用win10无用服务,提高Win10系统游戏性能!
  13. POE交换机和普通交换机的区别介绍
  14. 计算机网络知识点总结(第四章 网络层)
  15. 【人工智能】人类大脑中的神经元群体是如何相互作用,进而产生感知和行为的?
  16. 初识——雷达通信一体化技术
  17. 推荐一个朋友做的资源网站
  18. 用友nc登陆提示java_用友NC软件无法进入登陆界面,提示“网页上有错误”等现象   问题现象...
  19. 预防山体滑坡,泥石流监测智能预警系统
  20. 线刷宝智能刷机使用教程

热门文章

  1. Radeon Vii 系统分析 001记——工具
  2. java 身份证地址提取籍贯_java从地址串中解析提取省市区-完美匹配中国所有地址|自动解析地址...
  3. 8500WN12核顶配 极速稳定上网享受数字生活
  4. MPEG2-TS流深入解析
  5. 51nod - 1289 大鱼吃小鱼(栈)
  6. What is important ?
  7. 最小4k计算机组成,计算机组成原理第4章第三讲.ppt
  8. 拍摄VR全景的价格差异
  9. yml配置oracle驱动,Springboot+mybatis-plus+oracle 配置多数据源
  10. jd-Gui for myEclipse