找回密码
 注册

QQ登录

只需一步,快速开始

搜索

C++演讲比赛程序

[复制链接]
bluesky1 发表于 2023-9-29 01:06:55 | 显示全部楼层 |阅读模式
  1. #include<iostream>
  2. using namespace std;
  3. #include<vector>;
  4. #include<map>
  5. #include<string>
  6. #include<algorithm>
  7. #include<ctime>
  8. #include<functional>
  9. #include<deque>
  10. #include<numeric>
  11. #include<fstream>
  12. #include<ctime>

  13. void showMenu()
  14. {
  15.         cout << "**********************************" << endl;
  16.         cout << "*******  欢迎参加演讲比赛  *******" << endl;
  17.         cout << "*******   1.开始演讲比赛   *******" << endl;
  18.         cout << "*******   2.查看往届记录   *******" << endl;
  19.         cout << "*******   3.清空比赛记录   *******" << endl;
  20.         cout << "*******   0.退出比赛记录   *******" << endl;

  21. }

  22. class Player
  23. {
  24. public:
  25.         Player()
  26.         {

  27.         }
  28.         Player(string name, double score)
  29.         {
  30.                 this->name = name;
  31.                 this->score = score;
  32.         }

  33.         string name;
  34.         double score;
  35. };

  36. void print(int a)
  37. {
  38.         cout << a << "  ";
  39. }

  40. class Jiang
  41. {
  42. public:
  43.         bool operator()(double dou1, double dou2)const
  44.         {
  45.                 return dou1 > dou2;

  46.         }



  47. };

  48. class SpeechContest
  49. {
  50. public:
  51.         map<int, string>players;
  52.         vector<int>draw;
  53.         bool is_empty=true;
  54.         map<int, vector<string>>his_rec;


  55.         //创建比赛选手容器
  56.         void creatPlayers()
  57.         {
  58.                 string st = "ABCDEFJHIJKL";
  59.                 int number = 10001;
  60.                 for (int i = 0; i < 12; i++)
  61.                 {
  62.                         string name = "选手";
  63.                         name += st[i];
  64.                         players.insert(make_pair(number + i, name));
  65.                         draw.push_back(number + i);

  66.                 }

  67.         }

  68.         //进行抽签
  69.         void drawLots()
  70.         {

  71.                 random_shuffle(draw.begin(), draw.end());

  72.         }

  73.         //打印抽签后的演讲顺序
  74.         void print_DrawLots()
  75.         {
  76.                 cout << "抽签后演讲顺序如下:" << endl;
  77.                 for_each(draw.begin(), draw.end(), print);
  78.                 cout << endl;
  79.                 system("pause");

  80.         }

  81.         void  print_group(multimap<double, int, Jiang>&group1)
  82.         {
  83.                 for (multimap<double, int>::iterator it = group1.begin(); it != group1.end(); it++)
  84.                 {
  85.                         cout << it->second << "  " << it->first << "  " << players.find(it->second)->second << endl;
  86.                 }
  87.         }

  88.         void  delete_End(multimap<double, int, Jiang>& group1)
  89.         {
  90.                 int i = 0;
  91.                 do
  92.                 {
  93.                         multimap<double, int>::iterator it = group1.end();
  94.                         --it;
  95.                         group1.erase(it);
  96.                         i++;

  97.                 }while (i < 3);

  98.       
  99.         }

  100.         void add_Final_Player(multimap<double, int, Jiang>& group1)
  101.         {
  102.                 for (multimap<double, int>::iterator it = group1.begin(); it != group1.end(); it++)
  103.                 {
  104.                        
  105.                         draw.push_back(it->second);

  106.                        
  107.                 }

  108.         }

  109.         void competition(multimap<double, int, Jiang>&group1,int a)
  110.         {
  111.                 deque<double>de;
  112.                 double score;

  113.                 for (int i = a; i < 6+a; i++)
  114.                 {
  115.                         de.clear();
  116.                         for(int j=0;j<10;j++)
  117.                         {
  118.                               
  119.                                 score = rand() % 21 + 79 + rand() % 100 / 100.00;
  120.                                 de.push_back(score);
  121.                         }

  122.                         sort(de.begin(),de.end());
  123.                         de.pop_back();
  124.                         de.pop_front();
  125.                         score=accumulate(de.begin(), de.end(),0)/8.00;
  126.                        
  127.                         group1.insert(make_pair(score, draw[i]));

  128.                 }
  129.       
  130.         }
  131.       
  132.         void first_Round()
  133.         {
  134.                 multimap<double, int, Jiang>group1;
  135.                 multimap<double, int, Jiang>group2;
  136.                
  137.                 drawLots();
  138.                 print_DrawLots();
  139.                 competition(group1,0);
  140.                 cout << "第1小组比赛名次:" << endl;
  141.                 print_group(group1);

  142.                 competition(group2,6);
  143.                 cout << "第2小组比赛名次:" << endl;
  144.                 print_group(group2);

  145.                 delete_End(group1);
  146.                 delete_End(group2);


  147.                 cout << "晋级人员:" << endl;
  148.                 print_group(group1);
  149.                 print_group(group2);

  150.                 draw.clear();
  151.                 add_Final_Player(group1);
  152.                 add_Final_Player(group2);

  153.                 cout << "第一轮比赛完毕" << endl;


  154.         }

  155.         void read()
  156.         {
  157.                 ifstream ifs;
  158.                 ifs.open("speech_result.csv", ios::in);

  159.                 if (!ifs.is_open())
  160.                 {
  161.                         cout << "该文件不存在" << endl;
  162.                         is_empty = true;
  163.                         ifs.close();
  164.                         return;
  165.                 }
  166.                


  167.                 char ch;
  168.                 ifs >> ch;
  169.                 if (ifs.eof())
  170.                 {
  171.                         cout << "记录为空" << endl;
  172.                         is_empty = true;
  173.                         ifs.close();
  174.                         return;

  175.                 }

  176.                 is_empty = false;
  177.                 ifs.putback(ch);
  178.                 vector<string>ve;
  179.                 string date;
  180.                 int        index = 1;
  181.                 while (ifs>>date)
  182.                 {
  183.                         ve.clear();
  184.                         int start = 0;
  185.                         int pos = -1;
  186.                         while (true)
  187.                         {
  188.                               
  189.                                 pos = date.find(',', start);
  190.                                 if (pos==-1)
  191.                                 {
  192.                                         break;

  193.                                 }

  194.                                 string st = date.substr(start, pos - start);
  195.                                 ve.push_back(st);
  196.                                 start = pos + 1;
  197.                         }
  198.                        
  199.                         his_rec.insert(make_pair(index, ve));
  200.                         index++;

  201.                 }
  202.                

  203.                


  204.         }

  205.         void store(multimap<double, int, Jiang>&group)
  206.         {
  207.                 ofstream ofs;
  208.                 ofs.open("speech_result.csv",ios::out|ios::app);
  209.                 for(multimap<double, int, Jiang>::iterator it=group.begin();it!=group.end();it++)
  210.                 {
  211.                         ofs << it->second << "," << it->first << ",";

  212.                 }
  213.                 ofs << endl;
  214.                 ofs.close();
  215.                 cout << "记录已保存" << endl;
  216.                 is_empty = false;
  217.       
  218.         }

  219.         void second_Round()
  220.         {
  221.                 multimap<double, int, Jiang>group;
  222.                 cout << "第二轮比赛开始" << endl;
  223.                 drawLots();
  224.                 print_DrawLots();
  225.                 competition(group, 0);
  226.                 cout << "决赛名次:" << endl;
  227.             print_group(group);
  228.                 delete_End(group);
  229.                 cout << "冠亚季军:" << endl;
  230.                 print_group(group);
  231.                 store(group);
  232.       
  233.         }

  234.         void del_rec()
  235.         {
  236.                 fstream fs;
  237.                 fs.open("speech_result.csv", ios::out);
  238.                 fs.clear();
  239.                 is_empty = true;
  240.                 cout << "记录已清空" << endl;
  241.       
  242.       
  243.         }


  244. };

  245. void contest(SpeechContest &con)
  246. {
  247.         con.creatPlayers();
  248.         con.first_Round();
  249.         con.second_Round();
  250. }

  251. void showTop(string st)
  252. {
  253.         cout << st << "\t";

  254. }
  255. void showHis(const pair<int,vector<string>>&pa)
  256. {
  257.         cout << "第" << pa.first << "届:";

  258.         for_each(pa.second.begin(), pa.second.end(), showTop);
  259.         cout << endl;

  260. }
  261. void hisRec(SpeechContest &con)
  262. {
  263.         con.read();
  264.         if (con.is_empty)
  265.         {
  266.                 return;
  267.         }
  268.       
  269.         for_each(con.his_rec.begin(), con.his_rec.end(),showHis);


  270. }

  271. int main()
  272. {
  273.         srand((unsigned int)time(NULL));
  274.         SpeechContest con;

  275.         while (true)
  276.         {
  277.                 showMenu();
  278.                 cout << "请输入您的选择:" << endl;
  279.                 int sel;
  280.                 cin >> sel;
  281.                 switch (sel)
  282.                 {
  283.                 case 1:
  284.                         contest(con);
  285.                         system("pause");
  286.                         system("cls");
  287.                         break;

  288.                 case 2:
  289.                         hisRec(con);
  290.                         system("pause");
  291.                         system("cls");
  292.                         break;

  293.                 case 3:
  294.                         con.del_rec();
  295.                         system("pause");
  296.                         system("cls");
  297.                         break;

  298.                 case 0:
  299.                         cout << "欢迎下次使用" << endl;
  300.                         system("pause");
  301.                         return 0;
  302.                        
  303.                         break;
  304.                 default:
  305.                         cout << "您输入有误!" << endl;
  306.                         system("pause");
  307.                         system("cls");






  308.                 }




  309.         }

  310.         system("pause");
  311.         return 0;
  312. }
复制代码


您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|手机版|小黑屋|ELEOK |网站地图

GMT+8, 2024-4-29 21:54

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表