在完成对C语言的学习后,我最近开始了对C++和Java的学习,目前跟着视频学习了一些语法,也跟着敲了一些代码,有了一定的掌握程度。现在将跟着视频做的笔记进行整理。本篇博客是整理C++知识点的第三十二篇博客。
本篇博客用C++实现了演讲比赛流程管理系统,本文是上半部分。
本系列博客所有C++代码都在Visual Studio 2022环境下编译运行。程序为64位。
目录
接下来是一个演讲比赛流程管理系统,这部分分为两篇博客来完成。
学校举行了一场演讲比赛,有12个人参加,比赛分为两轮,第一轮是淘汰赛,第二轮是决赛。每位选手有自己对应的编号。
比赛第一轮分组比赛,每组6人,按照选手抽签顺序演讲。有十位评委打分,去掉最高分和最低分后取剩下的平均分作为本轮成绩。每组的前三名晋级。
第二轮是决赛,评分规则同第一轮,前三名胜出。
要求程序在每轮比赛过后显示晋级选手的信息。
程序需要有以下四个功能:
开始演讲比赛,完成整场比赛的流程,每个比赛阶段给用户一个提示,按任意键进入下一个阶段。
查看往届记录,查看之前每届比赛前三名结果,每次比赛结果记录到文件中,文件后缀csv。
清空比赛记录,删除文件中记录。
退出比赛程序:退出当前程序。
本程序用了四个文件。speaker.h有speaker类,存放选手信息。speechmanager.h和speechmanager.cpp存放了关于比赛流程的speechmanager类的所有内容。speechcontest.cpp有程序的main函数。
- #pragma once
- #include
- #include
- #include"speaker.h"
- class speechmanager
- {
- public:
- speechmanager();
- void show_menu();
- void exit_system();
- ~speechmanager();
-
- void init_speaker();
- void make_speaker();
-
- void start_contest();
- void draw_speaker();
- void speech_contest();
-
- void save_result();
- void read_result();
- void clear_result();
-
- vector<int> v1;
- vector<int> v2;
- vector<int> v3;
- map<int, speaker> m;
- int index;
- };
这是speechmanager.h的所有内容,里面的成员函数以及成员变量的作用在下文会解释。
- #include
- #include"speechmanager.h"
- using namespace std;
- int main(void)
- {
- speechmanager sm;
- while (true)
- {
- sm.show_menu();
- int choice;
- cout << "Please enter the choice: ";
- cin >> choice;
-
- switch (choice)
- {
- case 0:
- sm.exit_system();
- break;
- case 1:
- sm.start_contest();
- break;
- case 2:
- sm.read_result();
- break;
- case 3:
- sm.clear_result();
- break;
- default:
- cout << "Please enter 0,1,2 or 3" << endl;
- system("pause");
- system("cls");
- break;
- }
-
- if (choice == 0) {
- break;
- }
- }
- return 0;
- }
这是speechcontest.cpp的内容,创建一个speechmanager类对象sm,随后使用用死循环。每次循环先给出菜单,然后要求输入选项,不同选项进入sm的不同成员函数。输入0退出循环并结束程序,输入其他选项要求重新输入。
- void speechmanager::show_menu()
- {
- cout << "****************************************" << endl;
- cout << "********** 1. begin the contest ********" << endl;
- cout << "********** 2. watch the result *********" << endl;
- cout << "********** 3. clear the result *********" << endl;
- cout << "********** 0. exit the program *********" << endl;
- cout << "****************************************" << endl;
- }
这是speechmanager类的show_menu成员函数,输出菜单。
- void speechmanager::exit_system()
- {
- cout << "Welcome to use again" << endl;
- system("pause");
- system("cls");
- }
这是speechmanager类的exit_system成员函数,输出Welcome to use again。
- #pragma once
- #include
- #include
- using namespace std;
- class speaker
- {
- public:
- string name;
- double scorea;
- double scoreb;
- };
这是speaker.h的内容,创建了一个speaker类,是选手类。成员变量name表示姓名,成员变量scorea表示第一轮成绩,成员变量scoreb表示第二轮成绩。
v1 v2 v3存放的是选手编号,v1是所有选手,v2是第一轮晋级选手,v3是胜出选手。m的键是选手编号,值是编号对应的学生类。
- speechmanager::speechmanager()
- {
- srand((unsigned int)time(NULL));
- init_speaker();
- make_speaker();
- }
这是speechmanager类的构造函数。调用了init_speaker成员函数和make_speaker类成员函数。初始化随机种子是因为本程序的分数是利用了随机数。
- void speechmanager::init_speaker()
- {
- v1.clear();
- v2.clear();
- v3.clear();
- m.clear();
- }
init_speaker成员函数清空v1 v2 v3和m的内容,此函数结束后这四个容器全部为空置状态。
- void speechmanager::make_speaker()
- {
- speaker sp1;
- sp1.name = "Guillermo";
- sp1.scorea = 0;
- sp1.scoreb = 0;
- v1.push_back(2022072701);
- m.insert(make_pair(2022072701, sp1));
-
- speaker sp2;
- sp2.name = "Grace";
- sp2.scorea = 0;
- sp2.scoreb = 0;
- v1.push_back(2022072702);
- m.insert(make_pair(2022072702, sp2));
-
- speaker sp3;
- sp3.name = "Georgette";
- sp3.scorea = 0;
- sp3.scoreb = 0;
- v1.push_back(2022072703);
- m.insert(make_pair(2022072703, sp3));
-
- speaker sp4;
- sp4.name = "Gaston";
- sp4.scorea = 0;
- sp4.scoreb = 0;
- v1.push_back(2022072704);
- m.insert(make_pair(2022072704, sp4));
-
- speaker sp5;
- sp5.name = "Greg";
- sp5.scorea = 0;
- sp5.scoreb = 0;
- v1.push_back(2022072705);
- m.insert(make_pair(2022072705, sp5));
- speaker sp6;
- sp6.name = "Gert";
- sp6.scorea = 0;
- sp6.scoreb = 0;
- v1.push_back(2022072706);
- m.insert(make_pair(2022072706, sp6));
-
- speaker sp7;
- sp7.name = "Gilma";
- sp7.scorea = 0;
- sp7.scoreb = 0;
- v1.push_back(2022072707);
- m.insert(make_pair(2022072707, sp7));
-
- speaker sp8;
- sp8.name = "Gordon";
- sp8.scorea = 0;
- sp8.scoreb = 0;
- v1.push_back(2022072708);
- m.insert(make_pair(2022072708, sp8));
-
- speaker sp9;
- sp9.name = "Gil";
- sp9.scorea = 0;
- sp9.scoreb = 0;
- v1.push_back(2022072709);
- m.insert(make_pair(2022072709, sp9));
-
- speaker sp10;
- sp10.name = "Gabrielle";
- sp10.scorea = 0;
- sp10.scoreb = 0;
- v1.push_back(2022072710);
- m.insert(make_pair(2022072710, sp10));
-
- speaker sp11;
- sp11.name = "Genevieve";
- sp11.scorea = 0;
- sp11.scoreb = 0;
- v1.push_back(2022072711);
- m.insert(make_pair(2022072711, sp11));
-
- speaker sp12;
- sp12.name = "Gonzalo";
- sp12.scorea = 0;
- sp12.scoreb = 0;
- v1.push_back(2022072712);
- m.insert(make_pair(2022072712, sp12));
- }
make_speaker成员函数创建了十二个speaker类对象,每个对象的name都是单独给出,而scorea和scoreb都置为0。随后将每个选手的编号加入v1容器,编号为键对象为值加入m容器。