• C++学习笔记(三十三)


    在完成对C语言的学习后,我最近开始了对C++和Java的学习,目前跟着视频学习了一些语法,也跟着敲了一些代码,有了一定的掌握程度。现在将跟着视频做的笔记进行整理。本篇博客是整理C++知识点的第三十三篇博客。

    本篇博客用C++实现了演讲比赛流程管理系统,本文是下半部分。

    本系列博客所有C++代码都在Visual Studio 2022环境下编译运行。程序为64位。

    目录

    演讲比赛流程管理系统

    抽签功能实现

    比赛功能实现

    显示晋级选手信息

    保存分数

    获取往届分数

    清空记录功能实现


    演讲比赛流程管理系统

    抽签功能实现

    1. void speechmanager::draw_speaker()
    2. {
    3. if (index == 1) {
    4. random_shuffle(v1.begin(), v1.end());
    5. cout << "The people in first round is " << endl;
    6. for (vector<int>::iterator it = v1.begin(); it != v1.end(); ++it) {
    7. cout << "The number is " << *it << " " << " and the name is " << m.find(*it)->second.name << endl;
    8. }
    9. cout << endl;
    10. }
    11. else if (index == 2)
    12. {
    13. cout << "The people in second round is " << endl;
    14. random_shuffle(v2.begin(), v2.end());
    15. for (vector<int>::iterator it = v2.begin(); it != v2.end(); ++it) {
    16. cout << "The number is " << *it << " " << " and the name is " << m.find(*it)->second.name << endl;
    17. }
    18. cout << endl;
    19. }
    20. }

    draw_speaker成员函数实现抽签。如果是第一轮(index代表轮数),就随机对v1进行洗牌,输出洗牌后的结果。实际上没有创建新容器,而是将洗牌后前六个作为第一组,后六个作为第二组。如果是第二轮,就对v2进行洗牌,输出洗牌后的结果。

    比赛功能实现

    1. void speechmanager::speech_contest()
    2. {
    3. if (index == 1) {
    4. cout << "The first round" << endl;
    5. int num = 1;
    6. multimap<double, int, greater<double>> mm;
    7. for (vector<int>::iterator it = v1.begin(); it != v1.end(); ++it) {
    8. deque<double> score;
    9. int i;
    10. for (i = 0; i < 10; i += 1) {
    11. int rscore = rand() % 401 + 600;
    12. double result = rscore % 10;
    13. score.push_back(rscore);
    14. }
    15. sort(score.begin(), score.end(), greater<double>());
    16. score.pop_back();
    17. score.pop_front();
    18. double average = accumulate(score.begin(), score.end(), 0) / 8.0;
    19. mm.insert(make_pair(average, *it));
    20. if (num == 6) {
    21. cout << "The first group" << endl;
    22. int n = 1;
    23. for (multimap<double, int, greater<double>>::iterator ite = mm.begin(); ite != mm.end(); ++ite) {
    24. cout << "The number is " << ite->second << " and the name is " << m.find(ite->second)->second.name << " and the score is " << ite->first << endl;
    25. m.find(ite->second)->second.scorea = ite->first;
    26. if (n <= 3) {
    27. v2.push_back(ite->second);
    28. }
    29. n += 1;
    30. }
    31. mm.clear();
    32. }
    33. if (num == 12) {
    34. cout << "The second group" << endl;
    35. int n = 1;
    36. for (multimap<double, int, greater<double>>::iterator ite = mm.begin(); ite != mm.end(); ++ite) {
    37. cout << "The number is " << ite->second << " and the name is " << m.find(ite->second)->second.name << " and the score is " << ite->first << endl;
    38. m.find(ite->second)->second.scorea = ite->first;
    39. if (n <= 3) {
    40. v2.push_back(ite->second);
    41. }
    42. n += 1;
    43. }
    44. mm.clear();
    45. }
    46. num += 1;
    47. }
    48. }
    49. else if (index == 2)
    50. {
    51. cout << "The second round" << endl;
    52. multimap<double, int, greater<double>> mm;
    53. for (vector<int>::iterator it = v2.begin(); it != v2.end(); ++it) {
    54. deque<double> score;
    55. int i;
    56. for (i = 0; i < 10; i += 1) {
    57. int rscore = rand() % 401 + 600;
    58. double result = rscore % 10;
    59. score.push_back(rscore);
    60. }
    61. sort(score.begin(), score.end(), greater<double>());
    62. score.pop_back();
    63. score.pop_front();
    64. double average = accumulate(score.begin(), score.end(), 0) / 8.0;
    65. mm.insert(make_pair(average, *it));
    66. }
    67. int n = 1;
    68. for (multimap<double, int, greater<double>>::iterator ite = mm.begin(); ite != mm.end(); ++ite) {
    69. cout << "The number is " << ite->second << " and the name is " << m.find(ite->second)->second.name << " and the score is " << ite->first << endl;
    70. m.find(ite->second)->second.scoreb = ite->first;
    71. if (n <= 3) {
    72. v3.push_back(ite->second);
    73. }
    74. n += 1;
    75. }
    76. mm.clear();
    77. }
    78. }

    在第一轮,先输出提示信息,然后创建一个multimap容器mm,mm的键表示成绩,值表示编号,排序按降序排序。然后创建一个循环,遍历v1,对于每个成员,都按照前面的去掉最高分和最低分后取剩下的平均分的规则,得到平均分,然后和编号一起作为键值对加入mm。每次循环后num加一。(num在循环开始前初始化为1)。在num加一前,对num的值进行判断,如果是6或12(此时代表已经遍历了6或12个)就进行特殊操作。

    当num值为6时,输出表明是第一组成绩,随后遍历输出mm的元素,并将前三个的编号加入v2。随后清空mm。当num值为12时,输出表明是第二组成绩,随后遍历输出mm的元素,将前三个的编号加入v2,并清空mm。

    在第二轮,先输出提示信息,然后创建一个multimap容器mm,mm的键表示成绩,值表示编号,排序按降序排序。然后创建一个循环,遍历v2,对于每个成员,都按照前面的去掉最高分和最低分后取剩下的平均分的规则,得到平均分,然后和编号一起作为键值对加入mm。然后遍历mm,输出相关信息,前三个加入v3后清空mm。

    显示晋级选手信息

    1. void speechmanager::start_contest()
    2. {
    3. index = 1;
    4. draw_speaker();
    5. system("pause");
    6. speech_contest();
    7. system("pause");
    8. cout << "The people in second round is " << endl;
    9. for (vector<int>::iterator it = v2.begin(); it != v2.end(); ++it) {
    10. cout << "The number is " << *it << " " << " and the name is " << m.find(*it)->second.name << endl;
    11. }
    12. cout << endl;
    13. system("pause");
    14. system("cls");
    15. index = 2;
    16. draw_speaker();
    17. system("pause");
    18. speech_contest();
    19. system("pause");
    20. cout << "The people of victorious is " << endl;
    21. for (vector<int>::iterator it = v3.begin(); it != v3.end(); ++it) {
    22. cout << "The number is " << *it << " " << " and the name is " << m.find(*it)->second.name << endl;
    23. }
    24. cout << endl;
    25. system("pause");
    26. save_result();
    27. cout << "The contest has ended" << endl;
    28. system("pause");
    29. system("cls");
    30. }

    start_contest成员函数实现了流程功能和晋级选手信息输出。首先将index置为1,然后首先调用draw_speaker函数,再调用speech_contest成员函数,再输出第一轮选手晋级信息。随后首先将index置为2,然后首先调用draw_speaker函数,再调用speech_contest成员函数。然后输出获胜的选手信息,存入文件中,提示比赛结束。

    保存分数

    1. void speechmanager::save_result()
    2. {
    3. ofstream ofs;
    4. ofs.open("speechresult.csv", ios::out | ios::app);
    5. for (vector<int>::iterator it = v3.begin(); it != v3.end(); ++it) {
    6. ofs << *it << "," << m.find(*it)->second.name << "," << m.find(*it)->second.scoreb << endl;
    7. }
    8. ofs.close();
    9. }

    save_result成员函数将v3的信息按照一定格式存入文件中。(前面在比赛流程中已经修改了所有对象scorea和scoreb成员变量)

    获取往届分数

    1. void speechmanager::read_result()
    2. {
    3. ifstream ifs;
    4. ifs.open("speechresult.csv", ios::in);
    5. if (ifs.is_open() == false) {
    6. cout << "The file does not exist" << endl;
    7. return;
    8. }
    9. char ch;
    10. ifs >> ch;
    11. if (ifs.eof() == true) {
    12. cout << "It is blank" << endl;
    13. return;
    14. }
    15. ifs.putback(ch);
    16. int number = 1;
    17. while (true) {
    18. bool flag = true;
    19. for (int i = 0; i < 3; i += 1) {
    20. vector vs;
    21. string s;
    22. getline(ifs, s);
    23. int pos = -1;
    24. int start = 0;
    25. pos = s.find(",", start);
    26. if (pos != -1) {
    27. string strtemp1 = s.substr(start, pos - start);
    28. vs.push_back(strtemp1);
    29. }
    30. else {
    31. flag = false;
    32. break;
    33. }
    34. start = pos + 1;
    35. pos = s.find(",", start);
    36. if (pos != -1) {
    37. string strtemp2 = s.substr(start, pos - start);
    38. vs.push_back(strtemp2);
    39. }
    40. string strtemp3 = s.substr(pos + 1, s.size() - 1);
    41. vs.push_back(strtemp3);
    42. if (i == 0) {
    43. cout << "The " << number << " contest result" << endl;
    44. }
    45. cout << "The " << i + 1 << " place:" << endl;
    46. cout << "number: " << vs[0] << " name: " << vs[1] << " score: " << vs[2] << endl;
    47. }
    48. if (flag == false) {
    49. break;
    50. }
    51. number += 1;
    52. }
    53. ifs.close();
    54. system("pause");
    55. system("cls");
    56. }

    程序首先检查文件是否存在,不存在提示文件为空并结束函数。然后检查是否有内容,无内容就提示为空并结束函数。然后进入大循环,条件是true,大循环内定义了bool类型变量flag,默认为true,为false就退出循环。小循环会执行三次,代表一届比赛的前三名,每次读取一行内容。将内容按照格式碎开,随后输出。每次小循环结束将number(循环外定义,初始值为1)加一,代表记录的届数加一。读到内容结束时就将flag置为false并退出小循环。

    清空记录功能实现

    1. void speechmanager::clear_result()
    2. {
    3. int select;
    4. cout << "Do you want to clear all of the results?";
    5. cout << "Please press 0 to confirm" << endl;
    6. cin >> select;
    7. if (select == 0) {
    8. ofstream ofs("speechresult.csv", ios::trunc);
    9. cout << "Done" << endl;
    10. init_speaker();
    11. }
    12. system("pause");
    13. system("cls");
    14. }

    clear_result函数实现清空功能。首先提示是否清空,按0表示清空。然后输入不是0就结束函数。否则,将文件清空,提示已清空。

    另外,speechmanager类的析构函数是空实现。

    演讲比赛流程管理系统已经结束了,主要是理清流程,注意容器的选用。

  • 相关阅读:
    【kafka】 | 01 | kafka和zookeeper的部署及使用方法
    HomeAssistant——Intrgration开发
    VMware虚拟机更改静态IP报错Unit network.service entered failed state解决方案
    Spring Security 中的 BCryptPasswordEncoder
    代码随想录刷题 Day28
    解决 Django 开发中的环境配置问题:Windows 系统下的实战指南20231113
    G1D15-fraud-APT-汇报-基础模型与LR相关内容总结-KG-cs224w colab1-ctf rce41-44
    聊聊Mybatis的SqlSession
    wps excel js编程
    [激光器原理与应用-14]:激光器企业组织内部结构解剖
  • 原文地址:https://blog.csdn.net/m0_71007572/article/details/126447772