C++歌手大赛系统

只愿长相守 提交于 2019-12-28 16:13:34

项目结构

       选手类(Player)
       操作类(System)  // 老师说最好别用这个名字,以后一定要注意这个问题

在这里插入图片描述

Player类

这一部分代码是写在头文件里的

#define TheJudgesNum 10
class Player
{
private:
public:
 char num[20];
 char name[20];
 int score[TheJudgesNum];
 float sum;
 float ave;
 Player *next;
 Player();
 ~Player();
 int getMaxScore();
 int getMinScore();
};

Player.cpp立马跟上

#include "Player.h"
#include <iostream>
using namespace std;
int Player::getMaxScore() {
 int Maxscore = score[0];
 for (int i = 1; i < TheJudgesNum; i++) {
  if (score[i] > Maxscore) {
   Maxscore = score[i];
  }
 }
 return Maxscore;
}
int Player::getMinScore() {
 int Minscore = score[0];
 for (int i = 1; i < TheJudgesNum; i++) {
  if (score[i] < Minscore) {
   Minscore = score[i];
  }
 }
 return Minscore;
}
Player::Player()
{
 for (int i = 0; i < TheJudgesNum; i++) {
  score[i] = 0;
 }
 sum = 0;
 ave = 0;
}
Player::~Player()
{
}

emmm怎么不是4个字符缩进,看起来好奇怪

System类

#pragma once
#include"Player.h"
class System
{
private:
 int PlayerNum;
 Player *Head;
 Player *End;
public:
 void Interface();
 void Run();
 void InputPlayerInformation();
 void OutputPlayerInformation();
 void GiveScore();
 void Sort();
 void Swap(Player *A,Player *B);
 bool Search(char ch[20],int i);
 void WriteDataFile();
 void AddPlayerInformation();
 void ReadDataFile();
 System();
 ~System();
};

System.cpp

#include "Player.h"
#include "System.h"
#include <string> 
#include <fstream> 
#include <iostream>
using namespace std;
void System::Interface() {
 cout << endl;
 cout << "                                       ***************菜单****************" << endl;
 cout << "                                       ***    1——录入选手信息        ***" << endl;
 cout << "                                       ***    2——信息输出            ***" << endl;
 cout << "                                       ***    3——评委打分            ***" << endl;
 cout << "                                       ***    4——成绩排序(平均分)  ***" << endl;
 cout << "                                       ***    5——数据查询            ***" << endl;
 cout << "                                       ***    6——追加选手信息        ***" << endl;
 cout << "                                       ***    7——写入数据文件        ***" << endl;
 cout << "                                       ***    0——退出系统            ***" << endl;
 cout << "                                       ***********************************" << endl;
 cout << endl;
 cout << "                                       ***********************************" << endl;
 cout << "                                       ***    请选择想要进行的操作     ***" << endl;
 cout << "                                       ***********************************" << endl;
}
void System::Run() {
 int choice = 1;
 while (choice) {
  Interface();
  cin >> choice;
  if (choice > 7 || choice < 0) {
   cout << "                                       ***********************************" << endl;
   cout << "                                       ***   选择操作有误,请重新输入  ***" << endl;
   cout << "                                       ***********************************" << endl;
   continue;
  }
  switch (choice)
  {
  case 1:
   int k;
   cout << "                                       ***********************************" << endl;
   cout << "                                       ***      1——输入选手信息      ***" << endl;
   cout << "                                       ***      2——读取选手信息     ***" << endl;
   cout << "                                       ***    请选择想要进行的操作     ***" << endl;
   cout << "                                       ***********************************" << endl;
   cin >> k;
   if (k == 1) {
    InputPlayerInformation();
   }else if(k == 2){
    ReadDataFile();
   }
   break;
  case 2:
   OutputPlayerInformation();
   break;
  case 3:
   GiveScore();
   break;
  case 4:
   Sort();
   break;
  case 5:
   cout << "                                       ***********************************" << endl;
   cout << "                                       ***    请选择想要查找的方式     ***" << endl;
   cout << "                                       ***        1——编号查找        ***" << endl;
   cout << "                                       ***        2——姓名查找        ***" << endl;
   cout << "                                       ***********************************" << endl;
   int i;
   cin >> i;
   if (i == 1) {
    char num[20];
    cout << "                                       ***********************************" << endl;
    cout << "                                       ***  请输入想要查找的选手编号   ***" << endl;
    cout << "                                       ***********************************" << endl;
    cin >> num;
    if (!Search(num, i)) {
     cout << "                                       ***********************************" << endl;
     cout << "                                       ***        无此选手信息         ***" << endl;
     cout << "                                       ***********************************" << endl;
    }
   }
   else if (i == 2) {
    char name[20];
    cout << "                                       ***********************************" << endl;
    cout << "                                       ***  请输入想要查找的选手姓名   ***" << endl;
    cout << "                                       ***********************************" << endl;
    cin >> name;
    if (!Search(name, i)) {
     cout << "                                       ***********************************" << endl;
     cout << "                                       ***        无此选手信息         ***" << endl;
     cout << "                                       ***********************************" << endl;
    }
   }
   break;
  case 6:
   AddPlayerInformation();
   break;
  case 7:
   WriteDataFile();
   break;
  }
 }
}
void System::InputPlayerInformation() {
 cout << endl;
 cout << "                                       ***********************************" << endl;
 cout << "                                       ***       请输入选手个数        ***" << endl;
 cout << "                                       ***********************************" << endl;
 cin >> PlayerNum;
 for (int i = 0; i < PlayerNum; i++) {
  cout << endl;
  cout << "                                       ***********************************" << endl;
  cout << "                                       ***   请输入第" << i + 1 << "选手编号、姓名   ***" << endl;
  cout << "                                       ***********************************" << endl;
  cin >> End->num >> End->name;
  while (Search(End->num, 1) || Search(End->name, 2)) {
   cout << "                                       ***********************************" << endl;
   cout << "                                              已有此编号,请重新输入      " << endl;
   cout << "                                       ***********************************" << endl;
   cin >> End->num >> End->name;
  }
  End->next = new Player;
  End = End->next;
 }
 cout << endl;
 cout << "                                       ***********************************" << endl;
 cout << "                                       ***          录入完成           ***" << endl;
 cout << "                                       ***********************************" << endl;
}
void System::OutputPlayerInformation() {
 if (Head->next == End) {
  cout << endl;
  cout << "                                       ***********************************" << endl;
  cout << "                                       ***    对不起,没有选手信息     ***" << endl;
  cout << "                                       ***********************************" << endl;
  return;
 }
 Player *player;
 player = Head->next;
 cout << "************************************" << endl;
 cout << "     编号     姓名    总分    平均分" << endl;
 for (int i = 0; i < PlayerNum; i++) {
  cout << "" << player->num <<"   " << player->name <<"    " << player->sum << "       " << player->ave << "       " << endl;
  player = player->next;
 }
}
void System::GiveScore() {
 Player *player;
 player = Head->next;
 if (PlayerNum == 0) {
  cout << "                                       ***********************************" << endl;
  cout << "                                       ***      请先录入选手信息       ***" << endl;
  cout << "                                       ***********************************" << endl;
 }
 for (int i = 0; i < PlayerNum; i++) {
  cout << "                                       ***********************************" << endl;
  cout << "                                            请评委为选手" << player->name << "打分    " << endl;
  cout << "                                       ***********************************" << endl;
  cout << endl;
  cout << endl;
  for (int j = 0; j < TheJudgesNum; j++) {
   cout << "                                       ***********************************" << endl;
   cout << "                                               请输入第" << j + 1 << "个评委给分      " << endl;
   cout << "                                       ***********************************" << endl;
   cin >> player->score[j];
   cout << endl;
   player->sum += player->score[j];
  }
  player->sum = player->sum - player->getMaxScore() - player->getMinScore();
  player->ave = player->sum / (TheJudgesNum - 2);
  cout << "                                       ***********************************" << endl;
  player = player->next;
 }
}
void System::Sort() {
 //以平均分为关键字进行降序排列
 Player *player;
 if (PlayerNum == 0) {
  cout << "                                       ***********************************" << endl;
  cout << "                                       ***      请先录入选手信息       ***" << endl;
  cout << "                                       ***********************************" << endl;
  return;
 }
 for (int i = 0; i < PlayerNum; i++) {
  player = Head->next;
  for (int j = 0; j < PlayerNum - 1 - i; j++) {
   if (player->ave < player->next->ave)
   {
    Swap(player, player->next);
   }
   player = player->next;
  }
 }
 cout << endl;
 cout << "                                       ***********************************" << endl;
 cout << "                                       ***    成绩排序成功,请查看     ***" << endl;
 cout << "                                       ***********************************" << endl;
}
void System::Swap(Player *A, Player *B) {
 Player *Temp = End;
 strcpy_s(Temp->num, A->num);
 strcpy_s(Temp->name, A->name);
 //
 strcpy_s(A->num, B->num);
 strcpy_s(A->name, B->name);
 //
 strcpy_s(B->num, Temp->num);
 strcpy_s(B->name, Temp->name);
 //
 Temp->sum = A->sum;
 Temp->ave = A->ave;
 //
 A->sum = B->sum;
 A->ave = B->ave;
 //
 B->sum = Temp->sum;
 B->ave = Temp->ave;
 for (int i = 0; i < TheJudgesNum; i++) {
  Temp->score[i] = A->score[i];
  A->score[i] = B->score[i];
  B->score[i] = Temp->score[i];
 }
}
bool System::Search(char ch[20], int i) {
 Player *player = Head->next;
 if (i == 1) {
  while (player != End && strcmp(ch, player->num) != 0)
  {
   player = player->next;
  }
 }
 else if (i == 2) {
  while (player != End && strcmp(ch, player->name) != 0)
  {
   player = player->next;
  }
 }
 if (player != End) {
  int choice;
  cout << "                                       ***********************************" << endl;
  cout << "                                       ***         找到该选手          ***" << endl;
  cout << "                                       ***     1——输出选手信息       ***" << endl;
  cout << "                                       ***     2——不输出选手信息     ***" << endl;
  cout << "                                       ***********************************" << endl;
  cin >> choice;
  if (choice == 1) {
   cout << "                                       ***********************************" << endl;
   cout << "                                       ****编号***姓名***总分***平均分****" << endl;
   cout << "                                           " << player->num << "   " << player->name << "    " << player->sum << "       " << player->ave << "       " << endl;
   cout << "                                       ***********************************" << endl;
  }
  return true;
 }
 else
 {
  return false;
 }
}
void System::WriteDataFile() {
 //定义文件输出流 
 ofstream oFile;
 Player *player = Head->next;
 if (PlayerNum == 0) {
  cout << "                                       ***********************************" << endl;
  cout << "                                       ***      数据文件写入失败       ***" << endl;
  cout << "                                       ***********************************" << endl;
  return;
 }
 //打开要输出的文件 
 oFile.open("歌手大赛选手得分信息统计表.csv", ios::out | ios::trunc);    // 这样就很容易的输出到一个excel文件里
 oFile << "编号" << "," << "姓名" << ",";
 for (int i = 0; i < TheJudgesNum; i++) {
  oFile << "评委" << i + 1 << ",";
 }
 oFile << "总分" << "," << "平均分" << endl;
 for (int i = 0; i < PlayerNum; i++) {
  oFile << player->num << "," << player->name << ",";
  for (int j = 0; j < TheJudgesNum; j++) {
   oFile << player->score[j] << ",";
  }
  oFile << player->sum << "," << player->ave << endl;
  player = player->next;
 }
 oFile.close();
 cout << "                                       ***********************************" << endl;
 cout << "                                       ***      数据文件写入完成       ***" << endl;
 cout << "                                       ***********************************" << endl;
 //打开要输出的文件  用输入的方式输出
 //ifstream iFile("scoresheet.csv"); 
 //string   readStr((std::istreambuf_iterator<char>(iFile)),  std::istreambuf_iterator<char>());   
 //cout <<  readStr.c_str();
}
void System::AddPlayerInformation() {
 cout << "                                       ***********************************" << endl;
 cout << "                                       *** 请输入追加选手的编号、姓名  ***" << endl;
 cout << "                                       ***********************************" << endl;
 cin >> End->num >> End->name;
 while (Search(End->num, 1)) {
  cout << "                                       ***********************************" << endl;
  cout << "                                              已有此编号,请重新输入      " << endl;
  cout << "                                       ***********************************" << endl;
  cin >> End->num >> End->name;
 }
 cout << "                                       ***********************************" << endl;
 cout << "                                       *** 请输入追加选手的评委打分情况***" << endl;
 cout << "                                       ***********************************" << endl;
 for (int i = 0; i < TheJudgesNum; i++) {
  cout << "                                       ***********************************" << endl;
  cout << "                                               请输入第" << i + 1 << "个评委给分      " << endl;
  cout << "                                       ***********************************" << endl;
  cin >> End->score[i];
  End->sum += End->score[i];
 }
 End->sum = End->sum - End->getMaxScore() - End->getMinScore();
 End->ave = End->sum / (TheJudgesNum - 2);
 PlayerNum++;
 End->next = new Player;
 End = End->next;
 cout << "                                       ***********************************" << endl;
 cout << "                                       ***           追加成功          ***" << endl;
 cout << "                                       ***********************************" << endl;
}
void System::ReadDataFile() {
 ifstream inFile("歌手大赛选手得分信息统计表.csv", ios::in);
 string str;
 if (inFile.fail()) {
  cout << "                                       ***********************************" << endl;
  cout << "                                       ***         找不到文件          ***" << endl;
  cout << "                                       ***********************************" << endl;
  return;
 }
 //getline(inFile,str);
 while (getline(inFile, str) && inFile.good()) {
  //.csv文件用","作为分隔符
  PlayerNum++;
  getline(inFile, str, ',');
  strcpy_s(End->num, str.c_str());
  getline(inFile, str, ',');
  strcpy_s(End->name, str.c_str());
  End->next = new Player;
  End = End->next;
 }
 inFile.close();
}
System::System()
{
 PlayerNum = 0;
 Head = new Player;
 Head->next = new Player;
 End = Head->next;
}
System::~System()
{
 delete Head, End;
}

以上就是主要的代码

最后在Main.cpp里进行运行

Main.cpp

#include "System.h"
int main()
{
 System s;
 s.Run();
 return 0;
}

大概就是这些,都是自己一步一步做出来的,学习在于积累,加油,永远别说不知道,不知道就去学!!!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!