C++ Programming Practice - Speech Contest Process Management System

This article is a programming example in C++: speech contest process management system.

Speech Contest Process Management System

1 Speech Contest Procedure Requirements

1.1 Competition rules

  • The school held a speech contest, with a total of 12 people participating. The competition had two rounds , the first round was the elimination round, and the second round was the finals.
  • Each player has a corresponding number , such as 10001 ~ 10012
  • Competition method: Group competition, 6 people in each group
  • The first round is divided into two groups, and the overall draw will be based on the player number , and the speech will be given in the order of the draw.
  • The ten judges will score each contestant separately, remove the highest score and the lowest score, and find the average score of the contestants in this round.
  • After the group's speech, the three lowest ranked contestants in the group will be eliminated, and the top three will advance to the next round of competition.
  • The second round is the final, the top three will win
  • After each round, the information of the qualified players needs to be displayed

1.2 Program functions

  • Start the speech contest: Complete the entire competition process. Each competition stage needs to give the user a prompt. The user presses any key to continue to the next stage.
  • View previous records: View the top three results of previous competitions. Each competition will be recorded in a file, and the file will be saved with a .csv suffix.
  • Clear competition records: clear the data in the file
  • Exit the competition program: You can exit the current program

1.3 Program renderings

image-20220313155347334

2 Project creation

The steps to create a project are as follows :

  • Create new project
  • add files

2.1 Create project

The steps to create a project are as follows:

  • Create new project
  • add files

2.1 Create project

After opening Visual studio, click Create New Project to create a new C++ project

image-20220123124340461 image-20220123125134404 image-20220123125917692

2.2 Create files

Right-click the source file and select Add->New Item

image-20220123130442955

Give the C++ file a name and click Add.

image-20220123130813668

3 Create management class

Function description :

  • Provide menu interface to interact with users
  • Control the speech contest process
  • Reading and writing interactions with files

3.1 Create files

  • speechManager.hCreate and speechManager.cppfiles respectively in the folders of the header file and source file

image-20220312141057724

3.2 Header file implementation

speechManager.hDesign management class in

code show as below:

#pragma once
#include<iostream>
using namespace std;

//设计演讲比赛的管理类
class SpeechManager {
   
    
    
public:
	//构造函数
	SpeechManager();

	//析构函数
	~SpeechManager();
};

3.3 Source file implementation

speechManager.cppImplement the constructor and destructor in

code show as below:

#include"speechManager.h"

//构造函数
SpeechManager::SpeechManager() {
   
    
    
}

//析构函数
SpeechManager::~SpeechManager() {
   
    
    
}

4 Menu functions

Function description: Communication interface with users

4.1 Add member functions

speechManager.hAdd member functions to the management classvoid show_Menu();

//显示菜单
void show_Menu();

4.2 Menu function implementation

  • speechManager.cppImplement show_Menu()functions in the management class
//显示菜单
void SpeechManager::show_Menu() {
   
    
    
	cout << "************************************" << endl;
	cout << "********  欢迎参加演讲比赛  ********" << endl;
	cout << "********   1.开始演讲比赛   ********" << endl;
	cout << "********   2.查看往届记录   ********" << endl;
	cout << "********   3.清空比赛记录   ********" << endl;
	cout << "********   0.退出比赛记录   ********" << endl;
	cout << "************************************" << endl;
	cout << endl;
}

4.3 Test menu function

  • 演讲比赛流程管理系统.cppTest menu functionality in

Code:

	SpeechManager sm;
	sm.show_Menu();

Operation rendering:

image-20220312144514775

5 Exit function

Function description: realize the exit program

5.1 Provide functional interfaces

  • Provide branch selection in the main function and provide each functional interface

Code:

#include<iostream>
using namespace std;
#include"speechManager.h"

int main() {
   
    
    
	SpeechManager sm;
	int choice = 0; //用来存储用户的选项

	while (true) {
   
    
    
		sm.show_Menu();
		cout << "请输入您的选择:" << endl;
		cin >> choice;

		switch (choice)
		{
   
    
    
		case 1: //开始比赛
			break;
		case 2: //查看记录
			break;
		case 3: //清空记录
			break;
		case 0: //退出系统
			break;
		default:
			system("cls"); //清屏
			break;
		}
	}

	system("pause");
	return 0;
}

5.2 Implement exit function

speechManager.hProvide member functions to exit the system invoid exitSystem();

speechManager.hProvide specific function implementation in

//退出系统
void SpeechManager::exitSystem() {
   
    
    
	cout << "欢迎下次使用" << endl;
	system("pause");
	exit(0);
}

5.3 Test function

In the main function branch 0 option, call the interface to exit the program

case 0: //退出系统
	sm.exitSystem();
	break;

The effect is as shown in the figure:

image-20220312150528020

6 Speech Contest Function

6.1 Functional analysis

Competition process:

Draw lots→Start the speech contest→Display the results of the first round→

Draw lots→Start the speech contest→Display the top three results→Save the score

6.2 Create player class

The attributes in the player class include: player name, score

Create speaker.ha file in the header file and add the code:

#pragma once
#include<iostream>
using namespace std;

//选手类
class Speaker {
   
    
    
public:
	string m_Name; //姓名
	double m_Score[2]; //分数,最多有两轮得分
};

6.3 Competition

6.3.1 Adding member attributes

speechManager.hAdd properties in

	//成员属性
	//保存第一轮比赛选手的编号
	vector<int>v1;
	//第一轮晋级选手编号容器
	vector<int>v2;
	//胜出的前三名选手编号容器
	vector<int>vVictory;
	//存放编号以及对应具体选手的容器
	map<int, Speaker>m_Speaker;
	//存放比赛的轮数
	int m_Index;

6.3.2 Initialization properties

speechManager.hProvide a member function that starts the race invoid initSpeech();

	//初始化容器和属性
	void initSpeech();

Implemented in speechManager.cppvoid initSpeech();

//初始化容器和属性
void SpeechManager::initSpeech() {
   
    
    
	//容器都置空
	this->v1.clear();
	this->v2.clear();
	this->vVictory.clear();
	this->m_Speaker.clear();

	//初始化比赛轮数
	this->m_Index = 1;
}

Called in the SpeechManager constructorvoid initSpeech();

//构造函数
SpeechManager::SpeechManager() {
   
    
    
	//初始化容器和属性
	this->initSpeech();
}

6.3.3 Create players

speechManager.hProvide a member function that starts the race invoid createSpeaker();

	//创建12名选手
	void createSpeaker();

speechManager.cppimplemented invoid createSpeaker();

void SpeechManager::createSpeaker() {
   
    
    
	string nameSeed = "ABCDEFGHIJKL";
	for (int i = 0; i < 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;
		}

		//创建选手编号,并且放入到v1容器中
		this->v1.push_back(i + 1001);
		//选手编号以及对应的选手 放入到map容器中
		this->m_Speaker.insert(make_pair(i + 1001, sp));
	}
}

speechManager.cppCalled in SpeechManagerthe classvoid createSpeaker();

//构造函数
SpeechManager::SpeechManager() {
   
    
    
	//初始化容器和属性
	this->initSpeech();
	//创建12名选手
	this->createSpeaker();
}

For testing, in the main function, after creating the management object, you can use the following code to test the initial status of the 12 players.

//测试代码:
//测试12名选手的创建
for (map<int, Speaker>::iterator it = sm.m_Speaker.begin(); it != sm.m_Speaker.end(); it++) {
   
    
    
	cout << "选手编号:" << it->first << " 姓名:" << it->second.m_Name << " 分数:" << it->second.m_Score[0] << endl;
}

Test renderings:

image-20220312183901650

After the test is completed, the test code can be deleted or commented.

6.3.4 Adding start match member function

speechManager.hProvide a member function that starts the race invoid startSpeech();

This function mainly controls the flow of the game

	//开始比赛 - 比赛流程
	void startSpeech();

startSpeechWrite the empty implementation in speechManager.cpp first

//开始比赛
void SpeechManager::startSpeech() {
   
    
    
	//第一轮比赛
	//1.抽签
    speechDraw();
	//2.比赛
	//3.显示晋级结果
	//第二轮比赛
	//1.抽签
	//2.比赛
	//3.显示最终结果
	//4.保存分数
}

6.3.5 Drawing lots

Function description :

Before the official competition, the order of all players needs to be shuffled. You only need to shuffle the containers storing player numbers.

speechManager.hA member function that provides lottery invoid speechManager();

	//抽签
	void speechDraw();

Implement member functions in speechManager.cppvoid speechDraw();

//抽签
void SpeechManager::speechDraw() {
   
    
    
	cout << "第 " << this->m_Index << " 轮比赛选手正在抽签" << endl;
	cout << "--------------------------------------" << endl;
	cout << "抽签后演讲顺序如下:" << endl;

	if (this->m_Index == 1) {
   
    
    
		//第一轮比赛
		random_shuffle(v1.begin(

Supongo que te gusta

Origin blog.csdn.net/m0_66238629/article/details/131549757
Recomendado
Clasificación