注意:这篇文章上次更新于1833天前,文章内容可能已经过时。
This article was last updated1833 days ago, the content may be outdated.


演讲比赛程序需求
比赛规则
- 学校举行一场演讲比赛,共有12个人参加。比赛共两轮,第一轮为淘汰赛,第二轮为决赛。
- 比赛方式:分组比赛,每组6个人;选手每次要随机分组,进行比赛
- 每名选手都有对应的编号,如 10001 ~ 10012
- 第一轮分为两个小组,每组6个人。 整体按照选手编号进行抽签后顺序演讲。
- 当小组演讲完后,淘汰组内排名最后的三个选手,前三名晋级,进入下一轮的比赛。
- 第二轮为决赛,前三名胜出
- 每轮比赛过后需要显示晋级选手的信息
程序功能
- 开始演讲比赛:完成整届比赛的流程,每个比赛阶段需要给用户一个提示,用户按任意键后继续下一个阶段
- 查看往届记录:查看之前比赛前三名结果,每次比赛都会记录到文件中,文件用.csv后缀名保存
- 清空比赛记录:将文件中数据清空
- 退出比赛程序:可以退出当前程序
程序效果图:

Speech Contest Program Requirements
Competition Rules
- The school holds a speech contest with a total of 12 participants. The contest has two rounds; the first round is an elimination round, and the second round is the final.
- Contest format: group competition, 6 people per group; contestants are randomly grouped for each contest
- Each contestant has a corresponding ID, such as 10001 ~ 10012
- The first round is divided into two groups of 6 people each. The contestants speak in order after drawing lots based on their IDs.
- After each group finishes their speeches, the last three contestants in the group are eliminated, and the top three advance to the next round.
- The second round is the final, the top three win
- After each round, the information of the advancing contestants needs to be displayed
Program Functions
- Start speech contest: complete the flow of the whole contest; each contest stage prompts the user, and the user presses any key to continue to the next stage
- View past records: view the top three results of previous contests; each contest is recorded to a file, saved with a .csv extension
- Clear contest records: clear all data in the file
- Exit contest program: quit the current program
Program Screenshot:

项目创建
创建项目步骤如下:
- 创建新项目
- 添加文件
创建项目
- 打开vs2017后,点击创建新项目,创建新的C++项目
如图:

- 填写项目名称以及选取项目路径,点击确定生成项目

添加文件
- 右键源文件,进行添加文件操作

- 填写文件名称,点击添加

- 生成文件成功,效果如下图

- 至此,项目已创建完毕
Project Creation
The steps to create the project are as follows:
- Create a new project
- Add files
Creating the Project
- Open VS2017 and click Create New Project to create a new C++ project
As shown in the figure:

- Fill in the project name and select the project path, then click OK to generate the project

Adding Files
- Right-click Source Files to add a file

- Fill in the file name and click Add

- The file is generated successfully, as shown below

- At this point, the project has been created
创建管理类
功能描述:
- 提供菜单界面与用户交互
- 对演讲比赛流程进行控制
- 与文件的读写交互
创建文件
- 在头文件和源文件的文件夹下分别创建speechManager.h 和 speechManager.cpp文件

头文件实现
在speechManager.h中设计管理类
代码如下:
1 |
|
源文件实现
在speechManager.cpp中将构造和析构函数空实现补全
1 |
|
- 至此演讲管理类以创建完毕
Creating the Manager Class
Description:
- Provide a menu interface for interaction with the user
- Control the speech contest flow
- Interact with file read/write
Creating the Files
- Create speechManager.h and speechManager.cpp in the header file folder and the source file folder respectively

Header File Implementation
Design the manager class in speechManager.h
The code is as follows:
1 |
|
Source File Implementation
Complete the empty implementations of the constructor and destructor in speechManager.cpp
1 |
|
- At this point, the speech manager class has been created
菜单功能
功能描述:与用户的沟通界面
添加成员函数
在管理类speechManager.h中添加成员函数 void show_Menu();

菜单功能实现
- 在管理类speechManager.cpp中实现 show_Menu()函数
1 | void SpeechManager::show_Menu() |
测试菜单功能
- 在演讲比赛流程管理系统.cpp中测试菜单功能
代码:
1 |
|
- 运行效果如图:

- 菜单界面搭建完毕
Menu Function
Description: the interface for communicating with the user
Adding a Member Function
Add the member function void show_Menu(); to the manager class speechManager.h

Menu Function Implementation
- Implement the show_Menu() function in the manager class speechManager.cpp
1 | void SpeechManager::show_Menu() |
Testing the Menu Function
- Test the menu function in 演讲比赛流程管理系统.cpp
Code:
1 |
|
- The running result is as follows:

- The menu interface is complete
退出功能
提供功能接口
- 在main函数中提供分支选择,提供每个功能接口
代码:
1 | int main() { |
实现退出功能
在speechManager.h中提供退出系统的成员函数 void exitSystem();
在speechManager.cpp中提供具体的功能实现
1 | void SpeechManager::exitSystem() |
测试功能
在main函数分支 0 选项中,调用退出程序的接口

运行测试效果如图:

Exit Function
Providing the Function Interface
- Provide branch selection in the main function, providing an interface for each function
Code:
1 | int main() { |
Implementing the Exit Function
Provide the member function exitSystem(); to exit the system in speechManager.h
Provide the concrete implementation in speechManager.cpp
1 | void SpeechManager::exitSystem() |
Testing the Function
In branch 0 of the main function, call the interface to exit the program

The running test result is as follows:

演讲比赛功能
功能分析
比赛流程分析:
抽签 → 开始演讲比赛 → 显示第一轮比赛结果 →
抽签 → 开始演讲比赛 → 显示前三名结果 → 保存分数
创建选手类
- 选手类中的属性包含:选手姓名、分数
- 头文件中创建 speaker.h文件,并添加代码:
1 |
|
比赛
成员属性添加
- 在speechManager.h中添加属性
1 | //比赛选手 容器 12人 |
初始化属性
- 在speechManager.h中提供开始比赛的的成员函数
void initSpeech();
1 | //初始化属性 |
- 在speechManager.cpp中实现
void initSpeech();
1 | void SpeechManager::initSpeech() |
- SpeechManager构造函数中调用
void initSpeech();
1 | SpeechManager::SpeechManager() |
创建选手
- 在speechManager.h中提供开始比赛的的成员函数
void createSpeaker();
1 | //初始化创建12名选手 |
- 在speechManager.cpp中实现
void createSpeaker();
1 | void SpeechManager::createSpeaker() |
- SpeechManager类的 构造函数中调用
void createSpeaker();
1 | SpeechManager::SpeechManager() |
- 测试 在main函数中,可以在创建完管理对象后,使用下列代码测试12名选手初始状态
1 | for (map<int, Speaker>::iterator it = sm.m_Speaker.begin(); it != sm.m_Speaker.end(); it++) |

- 测试效果如图:

- 测试完毕后,可以将测试代码删除或注释。
开始比赛成员函数添加
- 在speechManager.h中提供开始比赛的的成员函数
void startSpeech(); - 该函数功能是主要控制比赛的流程
1 | //开始比赛 - 比赛流程控制 |
- 在speechManager.cpp中将startSpeech的空实现先写入
- 我们可以先将整个比赛的流程 写到函数中
1 | //开始比赛 |
抽签
功能描述:
-
正式比赛前,所有选手的比赛顺序需要打乱,我们只需要将存放选手编号的容器 打乱次序即可
-
在speechManager.h中提供抽签的的成员函数
void speechDraw();
1 | //抽签 |
- 在speechManager.cpp中实现成员函数
void speechDraw();
1 | void SpeechManager::speechDraw() |
- 在startSpeech比赛流程控制的函数中,调用抽签函数

- 在main函数中,分支1选项中,调用开始比赛的接口

- 测试

开始比赛
- 在speechManager.h中提供比赛的的成员函数
void speechContest();
1 | //比赛 |
- 在speechManager.cpp中实现成员函数
void speechContest();
1 | void SpeechManager::speechContest() |
- 在startSpeech比赛流程控制的函数中,调用比赛函数

- 再次运行代码,测试比赛

显示比赛分数
- 在speechManager.h中提供比赛的的成员函数
void showScore();
1 | //显示比赛结果 |
- 在speechManager.cpp中实现成员函数
void showScore();
1 | void SpeechManager::showScore() |
- 在startSpeech比赛流程控制的函数中,调用显示比赛分数函数

- 运行代码,测试效果

第二轮比赛
第二轮比赛流程同第一轮,只是比赛的轮是+1,其余流程不变
- 在startSpeech比赛流程控制的函数中,加入第二轮的流程

测试,将整个比赛流程都跑通

保存分数
功能描述:
- 将每次演讲比赛的得分记录到文件中
功能实现:
- 在speechManager.h中添加保存记录的成员函数
void saveRecord();
1 | //保存记录 |
- 在speechManager.cpp中实现成员函数
void saveRecord();
1 | void SpeechManager::saveRecord() |
- 在startSpeech比赛流程控制的函数中,最后调用保存记录分数函数

- 测试,整个比赛完毕后记录保存情况

利用记事本打开文件 speech.csv,里面保存了前三名选手的编号以及得分

至此,整个演讲比赛功能制作完毕!
Speech Contest Function
Function Analysis
Contest flow analysis:
Draw lots → start speech contest → display the first round results →
Draw lots → start speech contest → display the top three results → save scores
Creating the Contestant Class
- The attributes of the contestant class include: contestant name, score
- Create the speaker.h file in the header file folder and add the code:
1 |
|
The Contest
Adding Member Attributes
- Add attributes in speechManager.h
1 | //比赛选手 容器 12人 |
Initializing Attributes
- Provide the member function
void initSpeech();to start the contest in speechManager.h
1 | //初始化属性 |
- Implement
void initSpeech();in speechManager.cpp
1 | void SpeechManager::initSpeech() |
- Call
void initSpeech();in the constructor of the SpeechManager class
1 | SpeechManager::SpeechManager() |
Creating the Contestants
- Provide the member function
void createSpeaker();to start the contest in speechManager.h
1 | //初始化创建12名选手 |
- Implement
void createSpeaker();in speechManager.cpp
1 | void SpeechManager::createSpeaker() |
- Call
void createSpeaker();in the constructor of the SpeechManager class
1 | SpeechManager::SpeechManager() |
- Test: in the main function, after creating the manager object, use the following code to test the initial state of the 12 contestants
1 | for (map<int, Speaker>::iterator it = sm.m_Speaker.begin(); it != sm.m_Speaker.end(); it++) |

- The test result is as follows:

- After testing, the test code can be deleted or commented out.
Adding the Start Contest Member Function
- Provide the member function
void startSpeech();to start the contest in speechManager.h - This function mainly controls the contest flow
1 | //开始比赛 - 比赛流程控制 |
- Write the empty implementation of startSpeech in speechManager.cpp first
- We can first write the whole contest flow into the function
1 | //开始比赛 |
Drawing Lots
Description:
-
Before the official contest, the speaking order of all contestants needs to be shuffled; we only need to shuffle the container storing the contestant IDs
-
Provide the member function
void speechDraw();to draw lots in speechManager.h
1 | //抽签 |
- Implement the member function
void speechDraw();in speechManager.cpp
1 | void SpeechManager::speechDraw() |
- Call the draw lots function in the startSpeech contest flow control function

- In the main function, in branch 1, call the interface to start the contest

- Test

Starting the Contest
- Provide the member function
void speechContest();in speechManager.h
1 | //比赛 |
- Implement the member function
void speechContest();in speechManager.cpp
1 | void SpeechManager::speechContest() |
- Call the contest function in the startSpeech contest flow control function

- Run the code again to test the contest

Displaying Contest Scores
- Provide the member function
void showScore();in speechManager.h
1 | //显示比赛结果 |
- Implement the member function
void showScore();in speechManager.cpp
1 | void SpeechManager::showScore() |
- Call the display contest score function in the startSpeech contest flow control function

- Run the code and test the effect

The Second Round
The flow of the second round is the same as the first round; only the round number is +1, and the rest of the flow remains unchanged
- Add the second round flow in the startSpeech contest flow control function

Test, run through the whole contest flow

Saving Scores
Description:
- Record the scores of each speech contest to a file
Implementation:
- Add the member function
void saveRecord();to save records in speechManager.h
1 | //保存记录 |
- Implement the member function
void saveRecord();in speechManager.cpp
1 | void SpeechManager::saveRecord() |
- Finally call the save record scores function in the startSpeech contest flow control function

- Test, check the record saving after the whole contest

Open the file speech.csv with Notepad; it stores the IDs and scores of the top three contestants

At this point, the whole speech contest function is complete!
查看记录
读取记录分数
- 在speechManager.h中添加保存记录的成员函数
void loadRecord(); - 添加判断文件是否为空的标志
bool fileIsEmpty; - 添加往届记录的容器
map<int, vector<string>> m_Record;
其中m_Record 中的key代表第几届,value记录具体的信息
1 | //读取记录 |
- 在speechManager.cpp中实现成员函数
void loadRecord();
1 | void SpeechManager::loadRecord() |
- 在SpeechManager构造函数中调用获取往届记录函数

查看记录功能
- 在speechManager.h中添加保存记录的成员函数
void showRecord();
1 | //显示往届得分 |
- 在speechManager.cpp中实现成员函数
void showRecord();
1 | void SpeechManager::showRecord() |
测试功能
在main函数分支 2 选项中,调用查看记录的接口

显示效果如图:(本次测试添加了4条记录)

bug解决
目前程序中有几处bug未解决:
- 查看往届记录,若文件不存在或为空,并未提示
解决方式:在showRecord函数中,开始判断文件状态并加以判断

- 若记录为空或不存在,比完赛后依然提示记录为空
解决方式:saveRecord中更新文件为空的标志

- 比完赛后查不到本届比赛的记录,没有实时更新
解决方式:比赛完毕后,所有数据重置

- 在初始化时,没有初始化记录容器
解决方式:initSpeech中添加 初始化记录容器

- 每次记录都是一样的
解决方式:在main函数一开始 添加随机数种子
1 | srand((unsigned int)time(NULL)); |
所有bug解决后 测试:

Viewing Records
Reading Record Scores
- Add the member function
void loadRecord();to read records in speechManager.h - Add a flag to determine whether the file is empty
bool fileIsEmpty; - Add a container for past records
map<int, vector<string>> m_Record;
The key of m_Record represents the session, and the value records the specific information
1 | //读取记录 |
- Implement the member function
void loadRecord();in speechManager.cpp
1 | void SpeechManager::loadRecord() |
- Call the function to get past records in the SpeechManager constructor

Viewing Records Function
- Add the member function
void showRecord();to view records in speechManager.h
1 | //显示往届得分 |
- Implement the member function
void showRecord();in speechManager.cpp
1 | void SpeechManager::showRecord() |
Testing the Function
In branch 2 of the main function, call the interface to view records

The displayed result is as follows: (4 records were added in this test)

Bug Fixes
There are still several bugs in the program that have not been resolved:
- When viewing past records, if the file does not exist or is empty, there is no prompt
Solution: check the file status at the beginning of the showRecord function and judge accordingly

- If the record is empty or does not exist, the program still prompts that the record is empty after the contest
Solution: update the file empty flag in saveRecord

- After the contest, the record of the current contest cannot be found; it is not updated in real time
Solution: reset all data after the contest

- The record container is not initialized during initialization
Solution: add the initialization of the record container in initSpeech

- Every record is the same
Solution: add a random number seed at the beginning of the main function
1 | srand((unsigned int)time(NULL)); |
After all bugs are fixed, test:

清空记录
清空记录功能实现
- 在speechManager.h中添加保存记录的成员函数
void clearRecord();
1 | //清空记录 |
- 在speechManager.cpp中实现成员函数
void clearRecord();
1 | void SpeechManager::clearRecord() |
测试清空
在main函数分支 3 选项中,调用清空比赛记录的接口

运行程序,测试清空记录:

speech.csv中记录也为空

- 至此本案例结束!
^_^
Clearing Records
Implementing the Clear Records Function
- Add the member function
void clearRecord();to clear records in speechManager.h
1 | //清空记录 |
- Implement the member function
void clearRecord();in speechManager.cpp
1 | void SpeechManager::clearRecord() |
Testing Clearing
In branch 3 of the main function, call the interface to clear contest records

Run the program and test clearing records:

The records in speech.csv are also empty

- This is the end of this case!
^_^
我的实现
头文件
演讲者类–Speaker.h:
1 |
|
流程管理类–SpeachManager.h:
1 |
|
源文件
演讲者类–Speaker.cpp:
1 |
|
流程管理类–SpeachManager.cpp:
1 |
|
主函数文件–演讲比赛流程管理系统.cpp:
1 |
|
My Implementation
Header Files
Speaker class – Speaker.h:
1 |
|
Process management class – SpeachManager.h:
1 |
|
Source Files
Speaker class – Speaker.cpp:
1 |
|
Process management class – SpeachManager.cpp:
1 |
|
Main function file – 演讲比赛流程管理系统.cpp:
1 |
|


