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

 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 退出程序

 

posted on 2021-08-19 10:35  Bytezero!  阅读(185)  评论(0编辑  收藏  举报