運用c++實現(xiàn)一個基于STL的演講比賽流程管理系統(tǒng)。
成都創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),東陽企業(yè)網(wǎng)站建設(shè),東陽品牌網(wǎng)站建設(shè),網(wǎng)站定制,東陽網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,東陽網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。比賽方式演講比賽流程管理系統(tǒng).cpp
#include#include"SpeechManager.h"
#includeusing namespace std;
int main(){srand((unsigned int)time(NULL));
SpeechManager s1;
int input;
while (1){s1.Show_menu();
cout<< " 請輸入你的選項: "<< endl;
cin >>input;
switch (input){case 0:
//退出比賽程序
s1.Exit_system();
break;
case 1:
//開始演講比賽
s1.Start_speech();
break;
case 2:
//查看往屆記錄
s1.Show_record();
break;
case 3:
//清空比賽記錄
s1.Clear_record();
break;
default:
system("cls");//清屏
break;
}
}
system("pause");
return 0;
}
該段為項目的雛形,上面也是接下來我們將要實現(xiàn)的功能。
2. 管理類的實現(xiàn)首先在SpeechManager.h中設(shè)計管理類
#pragma once
#includeusing namespace std;
class SpeechManager
{public:
SpeechManager();
~SpeechManager();
};
然后在SpeechManager.cpp中將構(gòu)造和析構(gòu)函數(shù)空實現(xiàn)補全
#include "speechManager.h"
SpeechManager::SpeechManager(){}
SpeechManager::~SpeechManager(){}
3. 實現(xiàn)菜單功能首先在管理類SpeechManager類中添加成員函數(shù)void show_Menu()
然后在SpeechManager.cpp中實現(xiàn)
void SpeechManager::Show_menu(){cout<< " "<< endl;
cout<< " 歡迎參加演講比賽! "<< endl;
cout<< " 1.開始演講比賽 "<< endl;
cout<< " 2.查看往屆記錄 "<< endl;
cout<< " 3.清空比賽記錄 "<< endl;
cout<< " 0.退出比賽程序 "<< endl;
cout<< " "<< endl;
}
實現(xiàn)效果:
4. 實現(xiàn)退出功能首先在在SpeechManager類中添加成員函數(shù)void exitSystem()
然后在SpeechManager.cpp中實現(xiàn)
void SpeechManager::Exit_system(){cout<< " 歡迎下次使用 "<< endl;
system("pause");
exit(0);
}
實現(xiàn)效果:
5. 實現(xiàn)比賽功能 5.1 選手類的實現(xiàn)在Speaker.h設(shè)計選手類
#pragma once;
#include#includeusing namespace std;
class Speaker{public:
string Name;
double Score[2];//最多有兩輪成績
};
5.2添加成員變量在SpeechManager類中添加成員變量
int rounds; //比賽輪次
vectorv1; //第一輪參與選手(12位
vectorv2;//晉級第二輪的選手(6位
vectorthird;//獲得前三名的選手
mapspeaker;//存放編號以及對應(yīng)具體選手
5.3 初始化成員變量首先在SpeechManager類中添加成員函數(shù)void Init_speaker()
然后在SpeechManager.cpp中實現(xiàn)
void SpeechManager::Init_speaker(){//初始化選手數(shù)據(jù)
this->v1.clear();
this->v2.clear();
this->third.clear();
this->speaker.clear();
this->rounds = 1;//初始化比賽輪次
}
接著在SpeechManager.cpp中的SpeechManager構(gòu)造函數(shù)中調(diào)用void Init_speaker()
SpeechManager::SpeechManager(){this->Init_speaker();
}
5.4 創(chuàng)建選手首先在SpeechManager類中添加成員函數(shù)void Create_speaker()
然后在SpeechManager.cpp中實現(xiàn)
void SpeechManager::Create_speaker(){string nameSeed = "ABCDEFGHIJKL";
for (int i = 0; i< nameSeed.size(); i++){string name = "選手";
name += nameSeed[i];
Speaker s;
s.Name = name;
for (int j = 0; j< 2; j++) s.Score[j] = 0;
this->v1.push_back(10001 + i);
this->speaker.insert(make_pair(10001 + i, s));
}
}
接著在SpeechManager.cpp中的SpeechManager構(gòu)造函數(shù)中調(diào)用void Create_speaker()
SpeechManager::SpeechManager(){this->Init_speaker();
this->Create_speaker();
}
5.5 比賽功能實現(xiàn)首先在SpeechManager類中添加成員函數(shù)void Start_speech()
然后在SpeechManager.cpp中實現(xiàn)
void SpeechManager::Start_speech(){//第一輪比賽
//抽簽
this->Drawing();
//比賽
this->Completion();
//顯示晉級成果
this->Show_score();
//第二輪比賽
this->rounds++;
//抽簽
this->Drawing();
//比賽
this->Completion();
//顯示最終結(jié)果
this->Show_score();
//保存分數(shù)
this->Record();
cout<< "本屆比賽順利結(jié)束"<< endl; //這一條與上面的顯示會閃爍過去,原因不明
system("pasue");
system("cls");
}
5.5.1 抽簽功能實現(xiàn)首先在SpeechManager類中添加成員函數(shù)void Drawing()
然后在SpeechManager.cpp中實現(xiàn)
void SpeechManager::Drawing(){if (this->rounds == 1) cout<< " 第一輪比賽選手正在抽簽"<< endl;
else cout<< " 第二輪比賽選手正在抽簽"<< endl;
cout<< "-------------------------------------"<< endl;
cout<< " 演講順序如下"<< endl;
if (this->rounds == 1){random_shuffle(v1.begin(), v1.end());
for (vector::iterator it = v1.begin(); it != v1.end(); it++) cout<< *it<< " ";
cout<< endl;
}
else{random_shuffle(v2.begin(), v2.end());
for (vector::iterator it = v2.begin(); it != v2.end(); it++) cout<< *it<< " ";
cout<< endl;
}
cout<< "-------------------------------------"<< endl;
system("pause");
cout<< endl;
}
實現(xiàn)效果:
5.5.2 比賽功能實現(xiàn)首先在SpeechManager類中添加成員函數(shù)void Completion()
然后在SpeechManager.cpp中實現(xiàn)
void SpeechManager::Completion(){if (this->rounds == 1) cout<< " 第一輪比賽正式開始"<< endl;
else cout<< " 第二輪比賽正式開始"<< endl;
multimap>group;//記錄每組選手數(shù)據(jù)
int num = 0;//6人為一組
vectorv;
if (this->rounds == 1) v = v1;
else v = v2;
for (vector::iterator it = v.begin(); it != v.end(); it++){num++;
dequed;//存放分數(shù)
for (int i = 0; i< 10; i++){ double score = (rand() % 401 + 600) / 10.f; //600~1000
d.push_back(score);
}
sort(d.begin(), d.end(), greater());
d.pop_front();//去除最高分
d.pop_back();//去除最低分
double sum = accumulate(d.begin(), d.end(), 0.0f);//計算總分
double avg = sum / (double)d.size();//計算平均分
this->speaker[*it].Score[this->rounds - 1] = avg;
group.insert(make_pair(avg, *it));
//6人一組
if (num % 6 == 0){ if (num / 6 == 1) cout<< "第一小組比賽名次如下:"<< endl;
else cout<< "第二小組比賽名次如下:"<< endl;
for (multimap>::iterator it = group.begin();
it != group.end(); it++)
cout<< "編號:"<< it->second<< " 姓名:"
<< this->speaker[it->second].Name
<< " 成績:"<< this->speaker[it->second].Score[this->rounds - 1]
<< endl;
//取各組前三名
int count = 0;
for (multimap>::iterator it = group.begin();
it != group.end() && count< 3; it++, count++){ if (this->rounds == 1) v2.push_back((*it).second);
else third.push_back((*it).second);
}
group.clear();
cout<< endl;
}
}
if (this->rounds == 1) cout<< " 第一輪比賽完畢"<< endl;
else cout<< " 第二輪比賽完畢"<< endl;
system("pause");
}
實現(xiàn)效果:
5.5.3 顯示功能實現(xiàn)首先在SpeechManager類中添加成員函數(shù)void Show_score()
然后在SpeechManager.cpp中實現(xiàn)
void SpeechManager::Show_score(){if (this->rounds == 1) cout<< " 第一輪比賽晉級選手信息如下:"<< endl;
else cout<< " 第二輪比賽晉級選手信息如下:"<< endl;
vectorv;
if (this->rounds == 1) v = v2;
else v = third;
for (vector::iterator it = v.begin(); it != v.end(); it++)
cout<< "編號:"<< *it<< " 姓名:"<speaker[*it].Name
<< " 成績:"<< this->speaker[*it].Score[this->rounds - 1]<< endl;
cout<< endl;
system("pause");
system("cls");
this->Show_menu();
}
實現(xiàn)效果:
5.5.4 第二輪比賽實現(xiàn)第二輪比賽僅需將比賽輪次+1,后續(xù)流程與第一輪一樣
實現(xiàn)效果:
首先在SpeechManager類中添加成員函數(shù)void Record()
然后在SpeechManager.cpp中實現(xiàn)
void SpeechManager::Record(){ofstream ofs;
ofs.open("speech.csv", ios::out | ios::app);// 用輸出的方式打開文件
//存入數(shù)據(jù)
for (vector::iterator it = third.begin(); it != third.end(); it++)
ofs<< *it<< ","<< speaker[*it].Score[1]<< ",";
ofs<
利用記事本打開文件speech.csv,里面保存了前三名選手的編號以及得分
6. 實現(xiàn)讀取功能首先在SpeechManager類中添加成員函數(shù)和成員變量
void Load_record();//讀取往屆記錄
bool File_is_empty;//判斷文件是否為空
map>record;//往屆記錄
然后在SpeechManager.cpp中實現(xiàn)
void SpeechManager::Load_record(){ifstream ifs("speech.csv", ios::in);
if (!ifs.is_open()){this->File_is_empty = true;
cout<< "文件不存在!"<< endl;
ifs.close();
return;
}
char ch;
ifs >>ch;
if (ifs.eof()){cout<< "文件為空!"<< endl;
this->File_is_empty = true;
ifs.close();
return;
}
this->File_is_empty = false;
ifs.putback(ch);//把讀取的單個字符放回去
string data;
int index = 0;
while (ifs >>data){vectorv;
int pos = -1;
int start = 0;
while (true){ pos = data.find(",", start);
if (pos == -1) break;
string tmp = data.substr(start, pos - start);
//第一個是起始位置,第二個是到逗號的截取長度
v.push_back(tmp);
start = pos + 1;
}
this->record.insert(make_pair(index, v));
index++;
}
ifs.close();
}
接著在void Record()
利用File_is_empty
更新文件狀態(tài)
//比賽完后文件不為空
this->File_is_empty = false;
在SpeechManager.cpp中的SpeechManager構(gòu)造函數(shù)中調(diào)用void Record()
SpeechManager::SpeechManager(){this->Init_speaker();
this->Create_speaker();
this->Load_record();
}
在SpeechManager.cpp中的void Init_speaker()
函數(shù)中初始化記錄容器
this->record.clear();
在SpeechManager.cpp中的void Start_speech()
函數(shù)中重置比賽
//重置比賽
this->Init_speaker();
this->Create_speaker();
this->Load_record();
7. 實現(xiàn)查看功能首先在SpeechManager類中添加成員函數(shù)void Show_record()
然后在SpeechManager.cpp中實現(xiàn)
void SpeechManager::Show_record(){if (this->File_is_empty) cout<< "文件不存在,或記錄為空!"<< endl;
else{for (map>::iterator it = this->record.begin();
it != this->record.end(); it++){ cout<< "第"<< it->first+1<< "屆"<<
"冠軍編號:"<< it->second[0]<< " 得分:"<< it->second[1]<< " "
"亞軍編號:"<< it->second[2]<< " 得分:"<< it->second[3]<< " "
"季軍編號:"<< it->second[4]<< " 得分:"<< it->second[5]<< endl;
}
}
system("pause");
system("cls");
}
實現(xiàn)效果:
8. 實現(xiàn)清空功能首先在SpeechManager類中添加成員函數(shù)void Clear_record()
然后在SpeechManager.cpp中實現(xiàn)
void SpeechManager::Clear_record()
{cout<< "確認清空?"<< endl
<< "1. Yes"<< endl
<< "2. No"<< endl;
int select;
cin >>select;
if (select == 1){ofstream ofs("speech.csv", ios::trunc);
ofs.close();
this->Init_speaker();
this->Create_speaker();
this->Load_record();
cout<< "清除成功"<< endl;
}
else if (select == 2) return;
else cout<< "輸入有誤!"<< endl;
system("pause");
system("cls");
}
實現(xiàn)效果:
#pragma once;
#include#includeusing namespace std;
class Speaker{public:
string Name;
double Score[2];
};
“SpeechManage.h”#pragma once
#include#include#include
2. 源文件
“SpeechManage.cpp”#include "SpeechManager.h"
SpeechManager::SpeechManager(){this->Init_speaker();
this->Create_speaker();
this->Load_record();
}
SpeechManager::~SpeechManager(){}
void SpeechManager::Show_menu(){cout<< " "<< endl;
cout<< " 歡迎參加演講比賽! "<< endl;
cout<< " 1.開始演講比賽 "<< endl;
cout<< " 2.查看往屆記錄 "<< endl;
cout<< " 3.清空比賽記錄 "<< endl;
cout<< " 0.退出比賽程序 "<< endl;
cout<< " "<< endl;
}
void SpeechManager::Exit_system(){cout<< " 歡迎下次使用 "<< endl;
system("pause");
exit(0);
}
void SpeechManager::Init_speaker(){this->v1.clear();
this->v2.clear();
this->third.clear();
this->speaker.clear();
this->record.clear();
this->rounds = 1;
}
void SpeechManager::Create_speaker(){string nameSeed = "ABCDEFGHIJKL";
for (int i = 0; i< nameSeed.size(); i++){string name = "選手";
name += nameSeed[i];
Speaker s;
s.Name = name;
for (int j = 0; j< 2; j++) s.Score[j] = 0;
this->v1.push_back(10001 + i);
this->speaker.insert(make_pair(10001 + i, s));
}
}
void SpeechManager::Start_speech(){this->Drawing();
this->Completion();
this->Show_score();
this->rounds++;
this->Drawing();
this->Completion();
this->Show_score();
this->Record();
this->Init_speaker();
this->Create_speaker();
this->Load_record();
cout<< "本屆比賽順利結(jié)束"<< endl;
system("pasue");
system("cls");
}
void SpeechManager::Drawing(){if (this->rounds == 1) cout<< " 第一輪比賽選手正在抽簽"<< endl;
else cout<< " 第二輪比賽選手正在抽簽"<< endl;
cout<< "-------------------------------------"<< endl;
cout<< " 演講順序如下"<< endl;
if (this->rounds == 1){random_shuffle(v1.begin(), v1.end());
for (vector::iterator it = v1.begin(); it != v1.end(); it++) cout<< *it<< " ";
cout<< endl;
}
else{random_shuffle(v2.begin(), v2.end());
for (vector::iterator it = v2.begin(); it != v2.end(); it++) cout<< *it<< " ";
cout<< endl;
}
cout<< "-------------------------------------"<< endl;
system("pause");
cout<< endl;
}
void SpeechManager::Completion(){if (this->rounds == 1) cout<< " 第一輪比賽正式開始"<< endl;
else cout<< " 第二輪比賽正式開始"<< endl;
multimap>group;
int num = 0;
vectorv;
if (this->rounds == 1) v = v1;
else v = v2;
for (vector::iterator it = v.begin(); it != v.end(); it++){num++;
dequed;
for (int i = 0; i< 10; i++){ double score = (rand() % 401 + 600) / 10.f;
d.push_back(score);
}
sort(d.begin(), d.end(), greater());
d.pop_front();
d.pop_back();
double sum = accumulate(d.begin(), d.end(), 0.0f);
double avg = sum / (double)d.size();
this->speaker[*it].Score[this->rounds - 1] = avg;
group.insert(make_pair(avg, *it));
if (num % 6 == 0){ if (num / 6 == 1) cout<< "第一小組比賽名次如下:"<< endl;
else cout<< "第二小組比賽名次如下:"<< endl;
for (multimap>::iterator it = group.begin(); it != group.end(); it++)
cout<< "編號:"<< it->second<< " 姓名:"<< this->speaker[it->second].Name
<< " 成績:"<< this->speaker[it->second].Score[this->rounds - 1]<< endl;
int count = 0;
for (multimap>::iterator it = group.begin(); it != group.end() && count< 3; it++, count++){ if (this->rounds == 1) v2.push_back((*it).second);
else third.push_back((*it).second);
}
group.clear();
cout<< endl;
}
}
if (this->rounds == 1) cout<< " 第一輪比賽完畢"<< endl;
else cout<< " 第二輪比賽完畢"<< endl;
system("pause");
}
void SpeechManager::Show_score(){if (this->rounds == 1) cout<< " 第一輪比賽晉級選手信息如下:"<< endl;
else cout<< " 第二輪比賽晉級選手信息如下:"<< endl;
vectorv;
if (this->rounds == 1) v = v2;
else v = third;
for (vector::iterator it = v.begin(); it != v.end(); it++)
cout<< "編號:"<< *it<< " 姓名:"<speaker[*it].Name
<< " 成績:"<< this->speaker[*it].Score[this->rounds - 1]<< endl;
cout<< endl;
system("pause");
system("cls");
this->Show_menu();
}
void SpeechManager::Record(){ofstream ofs;
ofs.open("speech.csv", ios::out | ios::app);
for (vector::iterator it = third.begin(); it != third.end(); it++)
ofs<< *it<< ","<< speaker[*it].Score[1]<< ",";
ofs<File_is_empty = false;
}
void SpeechManager::Load_record(){ifstream ifs("speech.csv", ios::in);
if (!ifs.is_open()){this->File_is_empty = true;
cout<< "文件不存在!"<< endl;
ifs.close();
return;
}
char ch;
ifs >>ch;
if (ifs.eof()){cout<< "文件為空!"<< endl;
this->File_is_empty = true;
ifs.close();
return;
}
this->File_is_empty = false;
ifs.putback(ch);
string data;
int index = 0;
while (ifs >>data){vectorv;
int pos = -1;
int start = 0;
while (true){ pos = data.find(",", start);
if (pos == -1) break;
string tmp = data.substr(start, pos - start);
v.push_back(tmp);
start = pos + 1;
}
this->record.insert(make_pair(index, v));
index++;
}
ifs.close();
}
void SpeechManager::Show_record(){if (this->File_is_empty) cout<< "文件不存在,或記錄為空!"<< endl;
else{for (map>::iterator it = this->record.begin(); it != this->record.end(); it++){ cout<< "第"<< it->first+1<< "屆"<<
"冠軍編號:"<< it->second[0]<< " 得分:"<< it->second[1]<< " "
"亞軍編號:"<< it->second[2]<< " 得分:"<< it->second[3]<< " "
"季軍編號:"<< it->second[4]<< " 得分:"<< it->second[5]<< endl;
}
}
system("pause");
system("cls");
}
void SpeechManager::Clear_record()
{cout<< "確認清空?"<< endl
<< "1. Yes"<< endl
<< "2. No"<< endl;
int select;
cin >>select;
if (select == 1){ofstream ofs("speech.csv", ios::trunc);
ofs.close();
this->Init_speaker();
this->Create_speaker();
this->Load_record();
cout<< "清除成功"<< endl;
}
else if (select == 2) return;
else cout<< "輸入有誤!"<< endl;
system("pause");
system("cls");
}
“演講比賽流程管理系統(tǒng).cpp”#include#include"SpeechManager.h"
#includeusing namespace std;
int main(){srand((unsigned int)time(NULL));
SpeechManager s1;
int input;
while (1){s1.Show_menu();
cout<< " 請輸入你的選項: "<< endl;
cin >>input;
switch (input){case 0:
s1.Exit_system();
break;
case 1:
s1.Start_speech();
break;
case 2:
s1.Show_record();
break;
case 3:
s1.Clear_record();
break;
default:
system("cls");
break;
}
}
system("pause");
return 0;
}
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧