1 /*
2 比赛规则:
3 学校举行一演讲比赛,共12个人参加,比赛两轮,第一轮为淘汰赛 第二轮为决赛
4 每名选手都有对应的编号:如10001~10012
5 比赛方式:分组比赛 每组6人
6 第一轮分为两小组,整体按照选手编号进行抽签后顺序演讲
7 十个评委分别个每名选手打分,去除最高分和最低分 求的平均分为本轮选手的成绩
8 当小组演讲完后 淘汰组内排名最后的三个选手 前三名晋级,进入下一轮的比赛
9 第二轮为决赛 前三名胜出
10 每轮比赛过后需要显示晋级选手的信息
11 */
12 /*基于STL的演讲比赛流程管理系统*/
13 /**
14 *项目名称:基于STL的演讲比赛流程管理系统
15 *时 间:2021.8.18
16 *作 者:Bytezero!·zhenglei
17 */
 1 /*
2 比赛规则:
3 学校举行一演讲比赛,共12个人参加,比赛两轮,第一轮为淘汰赛 第二轮为决赛
4 每名选手都有对应的编号:如10001~10012
5 比赛方式:分组比赛 每组6人
6 第一轮分为两小组,整体按照选手编号进行抽签后顺序演讲
7 十个评委分别个每名选手打分,去除最高分和最低分 求的平均分为本轮选手的成绩
8 当小组演讲完后 淘汰组内排名最后的三个选手 前三名晋级,进入下一轮的比赛
9 第二轮为决赛 前三名胜出
10 每轮比赛过后需要显示晋级选手的信息
11 */
12 /*基于STL的演讲比赛流程管理系统*/
13 /**
14 *项目名称:基于STL的演讲比赛流程管理系统
15 *时 间:2021.8.18
16 *作 者:Bytezero!·zhenglei
17 */
18
19 /*speechcontest*/
20
21 #include<iostream>
22 #include"speechManager.h"
23 #include<string>
24
25 using namespace std;
26
27 int main()
28 {
29 srand((unsigned int)time(NULL));
30 //创建管理类的对象
31 SpeechManager sm;
32
33 //测试12名选手创建
34 //for (map<int, Speaker>::iterator it = sm.m_Speaker.begin(); it != sm.m_Speaker.end(); it++)
35 //{
36 // cout << "选手编号:" << it->first << "\t\t姓名:" << it->second.m_Name <<"\t分数:"<<it->second.m_Score[0]<< endl;
37 //}
38
39
40
41 int choice = 0; //用于存储用户的输入
42 while (true)
43 {
44 //调用展示菜单
45 sm.show_Menu();
46 cout << "请输入您的选择:" << endl;
47 cin >> choice;
48
49 switch (choice)
50 {
51 case 1: //开始比赛
52 sm.startSpeech();
53 break;
54 case 2: //查看往届的比赛记录
55 sm.showRecord();
56 break;
57 case 3: //清空比赛记录
58 sm.clearRecord();
59 break;
60 case 0: //退出系统
61 sm.exitSystem();
62 break;
63
64 default: //输入有误
65 cout << "对不起,您输入有误,请重新输入!!!" << endl;
66 system("pause");
67 system("cls"); //清屏
68
69 break;
70 }
71
72 }
73
74
75 system("pause");
76 return 0;
77 }
  1 speechManager.c
2
3 /**
4 *项目名称:基于STL的演讲比赛流程管理系统
5 *时 间:2021.8.18
6 *作 者:Bytezero!·zhenglei
7 */
8 #include"speechManager.h"
9 #include<algorithm>
10
11
12 //构造函数
13 SpeechManager::SpeechManager()
14 {
15 //初始化容器和属性
16 this->initSpeech();
17
18 //创建12名选手
19 this->createSpeaker();
20
21 //加载往届记录
22 this->loadRecord();
23 }
24
25 //展示菜单
26 void SpeechManager::show_Menu()
27 {
28 cout << "**********************************************" << endl;
29 cout << "************* 欢迎参加演讲比赛 *************" << endl;
30 cout << "************** 1.开始演讲比赛 **************" << endl;
31 cout << "************** 2.查看往届记录 **************" << endl;
32 cout << "************** 3.清空比赛记录 **************" << endl;
33 cout << "************** 0.退出比赛程序 **************" << endl;
34 cout << "**********************************************" << endl;
35 cout << endl;
36
37 }
38
39 //退出系统
40 void SpeechManager::exitSystem()
41 {
42 cout << "欢迎下次使用!!!" << endl;
43 system("pause");
44 exit(0);
45 }
46
47 //初始化容器和属性
48 void SpeechManager::initSpeech()
49 {
50 //容器都置空
51 this->v1.clear();
52 this->v2.clear();
53 this->vVictory.clear();
54 this->m_Speaker.clear();
55
56 //初始化比赛轮数
57 this->m_Index = 1;
58
59 //初始化记录容器
60 this->m_Record.clear();
61
62
63 }
64
65 //创建12名选手
66 void SpeechManager::createSpeaker()
67 {
68 //12名选手的名字
69 string nameSeed = "ABCDEFGHIJKL";
70 for (int i= 0; i < nameSeed.size(); i++)
71 {
72 string name = "选手";
73 name += nameSeed[i];
74
75 //创建具体选手
76 Speaker sp;
77 sp.m_Name = name;
78
79 for (int j = 0; j < 2; j++)
80 {
81 sp.m_Score[j] = 0;
82 }
83 //创建选手编号,并且放入v1容器中
84 this->v1.push_back(i + 10001);
85
86 //选手编号以及对应选手 放入map容器中
87 this->m_Speaker.insert(make_pair(i + 10001, sp));
88
89
90 }
91 }
92 //开始比赛 比赛整个流程控制函数
93 void SpeechManager::startSpeech()
94 {
95 //第一轮开始比赛
96
97 //1.抽签
98 this->speechDraw();
99
100 //2.比赛
101 this->speechContest();
102
103 //3.显示晋级结果
104 this->showScore();
105
106 //第二轮比赛
107 this->m_Index++;
108 //1.抽签
109 this->speechDraw();
110
111 //2.比赛
112 speechContest();
113
114 //3.显示最终结果
115 this->showScore();
116
117 //4.保存分数到文件中
118 this->saveRecord();
119 //重置比赛 获取记录
120
121 //初始化容器和属性
122 this->initSpeech();
123
124 //创建12名选手
125 this->createSpeaker();
126
127 //加载往届记录
128 this->loadRecord();
129
130 cout << "本届比赛完毕!" << endl;
131 system("pause");
132 system("cls");
133
134
135 }
136 //抽签
137 void SpeechManager::speechDraw()
138 {
139 cout << "第 <<" << this->m_Index << ">> 轮比赛选手在抽签" << endl;
140 cout << "----------------------------------------------" << endl;
141 cout << "抽签后演讲顺序如下:" << endl;
142
143 if (this->m_Index == 1)
144 {
145 //第一轮比赛 //打乱
146 random_shuffle(v1.begin(), v1.end());
147 for (vector<int>::iterator it = v1.begin(); it != v1.end(); it++)
148 {
149 cout << *it << " ";
150 }
151 cout << endl;
152 }
153 else
154 {
155 //第二轮
156 random_shuffle(v2.begin(), v2.end());
157 for (vector<int>::iterator it = v2.begin(); it != v2.end(); it++)
158 {
159 cout << *it << " ";
160 }
161 cout << endl;
162 }
163
164 cout << "----------------------------------------------" << endl;
165 system("pause");
166 cout << endl;
167
168
169 }
170
171 //具体比赛
172 void SpeechManager::speechContest()
173 {
174 cout << "--------------- 第" << this->m_Index << " 轮比赛正式开始 ---------------" << endl;
175
176 //准备临时容器 存放小组成绩 给分数排序
177 multimap<double, int, greater<double>>groupScore;
178
179 int num = 0; //记录人员个数 6人一组
180
181
182 vector<int>v_Src; //比赛的人员容器
183 if (this->m_Index == 1)
184 {
185
186 v_Src = v1;
187 }
188 else
189 {
190
191 v_Src = v2;
192 }
193 //遍历所有选手进行比赛
194 for (vector<int>::iterator it = v_Src.begin(); it != v_Src.end(); it++)
195 {
196 num++;
197 //评委打分
198 deque<double>d;
199 for (int i = 0; i < 10; i++)
200 {
201 double score = (rand() % 401 + 600)/10.f; //600~1000
202 //cout << score << " ";
203 d.push_back(score);
204
205 }
206 //cout << endl;
207
208
209 //降序
210 sort(d.begin(), d.end(),greater<double>());
211
212 d.pop_front(); //去除最高分
213 d.pop_back(); //去除最低分
214
215 double sum = accumulate(d.begin(), d.end(), 0.0f); //总分
216
217 double avg = sum / (double)d.size(); //平均分
218 //打印平均分
219 //cout << "编号:" << *it << "\t姓名:" << this->m_Speaker[*it].m_Name <<"\t获取平均分:" << avg << endl;
220
221 //将平均分放入到容器中
222 this->m_Speaker[*it].m_Score[this->m_Index - 1] = avg;
223
224 //将打分的数据 放入到临时小组容器中
225
226 groupScore.insert(make_pair(avg, *it)); //key是得分,value是具体选手编号
227
228 //每6人取出前三名
229 if (num % 6 == 0)
230 {
231 cout << "第" << num / 6 << "小组比赛名次: " << endl;
232 for (multimap<double, int, greater<double>>::iterator it = groupScore.begin(); it != groupScore.end(); it++)
233 {
234 cout << "编号:" << it->second << "\t姓名:" << this->m_Speaker[it->second].m_Name << "\t成绩:"
235 << this->m_Speaker[it->second].m_Score[this->m_Index - 1] << endl;
236
237 }
238 //取走前三名
239 int count = 0;
240 for (multimap<double, int, greater<double>>::iterator it = groupScore.begin(); it != groupScore.end() && count < 3; it++, count++)
241 {
242 if (this->m_Index == 1)
243 {
244 v2.push_back((*it).second);
245 }
246 else
247 {
248 vVictory.push_back((*it).second);
249 }
250 }
251
252
253
254 groupScore.clear(); //小组容器清空
255 cout << endl;
256 }
257
258
259
260 }
261 cout << "--------------- 第" << this->m_Index << "轮比赛完毕! ---------------" << endl;
262 system("pause");
263 }
264
265 //显示得分
266 void SpeechManager::showScore()
267 {
268 cout << "--------------- 第" << this->m_Index << "轮比赛选手信息如下: ---------------" << endl;
269
270 vector<int>v;
271 if (this->m_Index == 1)
272 {
273 v = v2;
274 }
275 else
276 {
277 v = vVictory;
278 }
279
280 for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
281 {
282 cout << "选手编号:" << *it << "\t姓名:" << m_Speaker[*it].m_Name
283 << "\t得分:" << m_Speaker[*it].m_Score[this->m_Index - 1] << endl;
284
285 }
286 cout << endl;
287
288 system("pause");
289 system("cls");
290 this->show_Menu();
291
292 }
293 //保存记录
294 void SpeechManager::saveRecord()
295 {
296 ofstream ofs;
297 ofs.open("speech.csv", ios::out | ios::app);//用追加的方式写文件
298
299 //将每个人的数据写入文件中
300 for (vector<int>::iterator it = vVictory.begin(); it != vVictory.end(); it++)
301 {
302 ofs << *it << "," << this->m_Speaker[*it].m_Score[1] << ",";
303 }
304 ofs << endl;
305
306 //关闭文件
307 ofs.close();
308 cout << "记录已经保存" << endl;
309
310 //有记录了 要更新
311 this->fileIsEmpty = false;
312
313 }
314
315 //读取记录
316 void SpeechManager::loadRecord()
317 {
318 ifstream ifs("speech.csv", ios::in);//读文件
319 if (!ifs.is_open())
320 {
321 this->fileIsEmpty = true;
322 //cout << "文件不存在!" << endl;
323 ifs.close();
324 return;
325 }
326
327 //文件清空
328 char ch;
329 ifs >> ch;
330 if (ifs.eof())
331 {
332 //cout << "文件为空!" << endl;
333 this->fileIsEmpty = true;
334 ifs.close();
335 return;
336 }
337
338 //文件不为空
339 this->fileIsEmpty = false;
340 ifs.putback(ch); //将上边的读取的单个字符 再放回来
341
342 string data;
343 int index = 0;
344
345 while (ifs >> data)
346 {
347 //cout << data << endl;
348
349 vector<string>v; //为了存放6个string字符串
350
351 int pos = -1; //查到 逗号 , 位置的变量
352 int start = 0; //起始位置
353
354 while(true)
355 {
356 pos = data.find(",", start);
357
358 if (pos == -1)
359 {
360 //没有找到
361 break;
362
363 }
364 string temp = data.substr(start, pos - start);
365 //cout << temp << endl;
366 v.push_back(temp);
367 start = pos + 1;
368 }
369 this->m_Record.insert(make_pair(index, v));
370 index++;
371 }
372
373 ifs.close();
374
375 //for (map<int, vector<string>>::iterator it = m_Record.begin(); it != m_Record.end(); it++)
376 //{
377 // cout << it->first << "冠军编号:" << it->second[0] << "\t分数:"
378 // << it->second[1] << endl;
379 //}
380
381
382
383 }
384
385 //显示往届记录
386 void SpeechManager::showRecord()
387 {
388 if (this->fileIsEmpty)
389 {
390 cout << "文件为空或者文件不存在!" << endl;
391 }
392 else
393 {
394 for (int i = 0; i < this->m_Record.size(); i++)
395 {
396 cout << "第" << i + 1 << "届比赛获奖名单\n"
397 << "冠军编号:" << this->m_Record[i][0] << "\t\t冠军得分:" << this->m_Record[i][1] << "\n"
398 << "亚军编号:" << this->m_Record[i][2] << "\t\t亚军得分:" << this->m_Record[i][3] << "\n"
399 << "季军编号:" << this->m_Record[i][4] << "\t\t季军得分:" << this->m_Record[i][5] << endl;
400
401
402 }
403
404 }
405
406 system("pause");
407 system("cls");
408 }
409
410 //清空记录
411 void SpeechManager::clearRecord()
412 {
413 cout << "您是否确认清空文件?" << endl;
414 cout << "1---是" << endl;
415 cout << "2---否" << endl;
416
417 int select = 0;
418 cin >> select;
419
420 if (select == 1)
421 {
422 //确认清空
423 ofstream ofs("speech.csv", ios::trunc); //删除重新创建
424 ofs.close(); //关闭
425
426 //初始化容器和属性
427 this->initSpeech();
428
429 //创建12名选手
430 this->createSpeaker();
431
432 //加载往届记录
433 this->loadRecord();
434
435 cout << "清空成功!!" << endl;
436
437 }
438 system("pause");
439 system("cls");
440
441
442
443 }
444
445 //析构函数
446 SpeechManager::~SpeechManager()
447 {
448
449 }
 1 speechManager.h
2
3 #include<numeric>
4 #include<string>
5 #include<fstream>
6 using namespace std;
7
8 //设计演讲比赛的管理类
9 class SpeechManager
10 {
11 public:
12 //构造函数
13 SpeechManager();
14
15 //展示菜单
16 void show_Menu();
17
18 //退出系统
19 void exitSystem();
20
21
22 //析构函数
23 ~SpeechManager();
24
25 //初始化容器和属性
26 void initSpeech();
27
28 //创建12名选手
29 void createSpeaker();
30
31 //开始比赛 比赛整个流程控制函数
32 void startSpeech();
33
34 //抽签
35 void speechDraw();
36
37
38 //具体比赛
39 void speechContest();
40
41 //显示得分
42 void showScore();
43
44 //保存记录
45 void saveRecord();
46
47 //读取记录
48 void loadRecord();
49
50 //显示往届记录
51 void showRecord();
52
53 //清空记录
54 void clearRecord();
55
56
57 //判断文件是否为空
58 bool fileIsEmpty;
59
60 //存放往届记录的容器
61 map<int, vector<string>>m_Record;
62
63
64 //成员属性
65 //保存第一轮比赛选手编号容器
66 vector<int>v1;
67
68 //第一轮晋级选手编号容器
69 vector<int>v2;
70
71
72 //胜出的前三名选手编号容器
73 vector<int>vVictory;
74
75 //存放编号以及对应具体选手容器
76 map<int, Speaker>m_Speaker;
77
78 //存放比赛轮数的
79 int m_Index;
80
81
82
83
84 };
 1 /**
2 *项目名称:基于STL的演讲比赛流程管理系统
3 *时 间:2021.8.18
4 *作 者:Bytezero!·zhenglei
5 */
6
7 speaker.h
8
9 #pragma once
10 #include<iostream>
11 using namespace std;
12
13
14 //选手类
15 class Speaker
16 {
17 public:
18
19 string m_Name;
20 double m_Score[2]; //分数 最多有俩轮的得分
21
22 };

//主界面

//1.开始

(可以参加多次比赛)

//2 查看记录

//文件里的保存文件

//3.清空

//文件清空

//不参加的时候 没有数据

//2

//0 退出程序

C++ 基于STL的演讲比赛流程管理系统(sort算法+小型算法(accumulate)+内建函数对象+string字符串拼接+字符串截取+多个容器基础操作+与用户交互+文件的读写+保存+重建+整体文件数据的清空)的更多相关文章

  1. python之文件的读写和文件目录以及文件夹的操作实现代码

    这篇文章主要介绍了python之文件的读写和文件目录以及文件夹的操作实现代码,需要的朋友可以参考下 为了安全起见,最好还是给打开的文件对象指定一个名字,这样在完成操作之后可以迅速关闭文件,防止一些无用 ...

  2. C# 通过物理路径将文件以二进制保存到指定文件夹

    /// <summary> /// 通过物理路径将文件以二进制保存到指定文件夹 /// </summary> /// <param name="filePath ...

  3. springboot-用logback将日志文件按等级保存到不同文件

    springboot-用logback将日志文件按等级保存到不同文件 案例: 例如项目基本包名为com.xxx,将该包下的所有日志按debug.info.warn.error等级分别保存到D:/log ...

  4. python (11)文件的读写 按行读文件

    读文件: 读取文件 f = open('\info.txt') fil = f.read() f.close() 按行读文件: f = open("info.txt") while ...

  5. Nodejs Express下载文件,并保存成原文件

    现时需要开发一个Excel下载功能 后台有一个API,负责接收传入的JSON文件,生成带图片的Excel文件在临时目录(生成Excel使用npm exceljs库),并将文件通过Router返回 前台 ...

  6. Python之文件操作:文件的读写

    一.open函数:对文件读写之前,需要先打开文件,获取文件句柄 注意:open() file() 尽量使用open(),Python3以后不支持file()了 1.open(file_name[,ac ...

  7. Python 第三篇(上):python文件基础操作、json模块、lambda、map、filter、reduce和函数位置参数

    python一切皆对象,linux一切皆文件,python操作文件是很常见的O/I操作,其内置来open()函数可以完成文件的基本操作: 一:使用内置open()函数操作文件,基本语法如下: with ...

  8. CAD保存DWG文件,设置保存的文件版本号和密码

    主要用到函数说明: MxDrawXCustomFunction::Mx_SaveDwgEx 保存DWG文件,可以设置保存的文件版本号和密码,详细说明如下: 参数 说明 IN CString sFile ...

  9. Python中对文件的读写

    读写文件前,我们先必须了解一下,在磁盘上读写文件的功能都是由操作系统提供的,现代操作系统不允许普通的程序直接操作磁盘. 读写文件就是请求操作系统打开一个文件对象(通常称为文件描述符),然后,通过操作系 ...

随机推荐

  1. 一小时搞懂Mysql锁机制

    内容概述: 我们知道,数据也是一种供许多用户共享访问的资源.如何保证数据并发访问的一致性.有效性,是所有数据库必须解决的一个问题,锁的冲突也是影响数据库并发访问性能的一个重要因素.从这一角度来说,锁对 ...

  2. Docker安装MySQL8.0

    环境 CentOS 7.5 Docker 1.13.1 MySQL 8.0.16 安装 拉取镜像 默认拉取最新版本的镜像 $ docker pull mysql 如果要指定版本,使用下面的命令 $ d ...

  3. ctf实验吧Once More

    题目链接:http://ctf5.shiyanbar.com/web/more.php 思路分析:显然是后台逻辑代码. 1.ereg函数有漏洞,可以使用%00截断,这个就做笔记了好吧.这个函数大致意思 ...

  4. CG-CTF 480小时精通C++

    一.拖入ida,看看 和之前题,有点不一样,不一样在于,这个程序相等于将没加密的字符串,直接打印出来了,但是The Encrypted is 引起了我的注意,所以我去看看有没加密函数,结果还真有,一堆 ...

  5. [Vue入门及介绍,基础使用、MVVM架构、插值表达式、文本指令、事件指令]

    [Vue入门及介绍,基础使用.MVVM架构.插值表达式.文本指令.事件指令] 1)定义:javascript渐进式框架 ​ 渐进式:可以控制一个页面的一个标签,也可以控制一系列标签,也可以控制整个页面 ...

  6. 电脑通过WIFI连接手机ADB

    1.搜索adb wifi 2.安装并开启:根据提示 3.电脑:adb connect 192.168.1.134 a安装ADB TOOLS b安装ADB DRIVER c将ADB TOOLS复制到c: ...

  7. flex布局制作自适应网页

    网页布局是css的一个重点应用.传统的布局都是依赖display.position.float属性来实现的,但是特殊布局就不易实现,如垂直居中. 01 flex布局是什么?‍ Flex 是 Flexi ...

  8. MQTT 4 ——MQTT的Spring Mvc 配置接收字节流数据

    本篇记录一下MQTT整合Spring Mvc配置直接收发字节流数据 设备方是纯C开发,并且为了交互数据的安全,将传送的数据用了AES CBC进行了加密. 接下来正常方便做法应该是 将加密后的字节流转换 ...

  9. Java基础00-基础语法3

    1. 注释 1.1 注释概述 1.2 注释分类 1.3 示例 2. 关键字 2.1 关键字概述 2.2 关键字的特点 3. 常量 3.1 常量的概述 3.2 常量分类 以上常量除了空常量都是可以直接输 ...

  10. Long类型框架自动序列化成String失效问题排查

    目录 问题描述 猜想 1. 写错了 2. 重新使用 验证猜想 1.验证猜想 2.继续猜想 3.再次猜想 4.再次验证 5.疑惑 6.找到原因,解决疑惑 7.解决 问题描述 微服务架构下进行业务模块开发 ...