大橙子网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

c++小项目:基于STL的演讲比赛流程管理系统-创新互联

一、项目目的

运用c++实现一个基于STL的演讲比赛流程管理系统。

成都创新互联公司是一家集网站建设,东阳企业网站建设,东阳品牌网站建设,网站定制,东阳网站建设报价,网络营销,网络优化,东阳网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。比赛方式
  • 共两轮,第一轮为分组淘汰赛,第二轮为决赛,共有十名评委,打分方式为去掉最高分和最低分的平均分为基准
  • 第一轮共两组,每组六人,为随机分组和抽签决定演讲顺序,每组取前三名进入下一轮 ;
  • 第二轮为淘汰赛,六名选手同台竞技,决出冠亚季军;
  • 每轮比赛结束后都会公布胜出选手分数及排名;
程序功能
  • 开始演讲比赛:完成整届比赛的流程,每个比赛阶段需要给用户一个提示,用户按任意键后继续下一个阶段;该阶段还具有初始化比赛进程、抽签、进行比赛、显示晋级成果、保存分数的功能。
  • 查看往届记录:查看往届比赛结冠亚军信息,每次比赛都会记录到文件(格式为csv)中。
  • 清空比赛记录:将文件中往届比赛结冠亚军信息清空。
  • 退出比赛程序:退出当前程序。
二、项目实现 1. 项目雏形

演讲比赛流程管理系统.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;
}

该段为项目的雏形,上面也是接下来我们将要实现的功能。

2. 管理类的实现

首先在SpeechManager.h中设计管理类

#pragma once
#includeusing namespace std;

class SpeechManager
{public:
	SpeechManager();

	~SpeechManager();
};

然后在SpeechManager.cpp中将构造和析构函数空实现补全

#include "speechManager.h"

SpeechManager::SpeechManager(){}

SpeechManager::~SpeechManager(){}
3. 实现菜单功能

首先在管理类SpeechManager类中添加成员函数void show_Menu()
然后在SpeechManager.cpp中实现

void SpeechManager::Show_menu(){cout<< "                                      "<< endl;
	cout<< "            欢迎参加演讲比赛!         "<< endl;
	cout<< "            1.开始演讲比赛            "<< endl;
	cout<< "            2.查看往届记录            "<< endl;
	cout<< "            3.清空比赛记录            "<< endl;
	cout<< "            0.退出比赛程序            "<< endl;
	cout<< "                                      "<< endl;
}

实现效果:

image.png

4. 实现退出功能

首先在在SpeechManager类中添加成员函数void exitSystem()
然后在SpeechManager.cpp中实现

void SpeechManager::Exit_system(){cout<< "            欢迎下次使用            "<< endl;
	system("pause");
	exit(0);
}

实现效果:

image.png

5. 实现比赛功能 5.1 选手类的实现

在Speaker.h设计选手类

#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;//存放编号以及对应具体选手
5.3 初始化成员变量

首先在SpeechManager类中添加成员函数void Init_speaker()
然后在SpeechManager.cpp中实现

void SpeechManager::Init_speaker(){//初始化选手数据
	this->v1.clear();
	this->v2.clear();
	this->third.clear();
	this->speaker.clear();

	this->rounds = 1;//初始化比赛轮次
}

接着在SpeechManager.cpp中的SpeechManager构造函数中调用void Init_speaker()

SpeechManager::SpeechManager(){this->Init_speaker();
}
5.4 创建选手

首先在SpeechManager类中添加成员函数void Create_speaker()
然后在SpeechManager.cpp中实现

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构造函数中调用void Create_speaker()

SpeechManager::SpeechManager(){this->Init_speaker();
	this->Create_speaker();
}
5.5 比赛功能实现

首先在SpeechManager类中添加成员函数void Start_speech()
然后在SpeechManager.cpp中实现

void SpeechManager::Start_speech(){//第一轮比赛
	//抽签
	this->Drawing();
	//比赛
	this->Completion();
	//显示晋级成果
	this->Show_score();
	//第二轮比赛
	this->rounds++;
	//抽签
	this->Drawing();
	//比赛
	this->Completion();
	//显示最终结果
	this->Show_score();
	//保存分数
	this->Record();
        
        cout<< "本届比赛顺利结束"<< endl; //这一条与上面的显示会闪烁过去,原因不明
	system("pasue");
	system("cls");
}
5.5.1 抽签功能实现

首先在SpeechManager类中添加成员函数void Drawing()
然后在SpeechManager.cpp中实现

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;
}

实现效果:

image.png

5.5.2 比赛功能实现

首先在SpeechManager类中添加成员函数void Completion()
然后在SpeechManager.cpp中实现

void SpeechManager::Completion(){if (this->rounds == 1) cout<< "            第一轮比赛正式开始"<< endl;
	else cout<< "            第二轮比赛正式开始"<< endl;

	multimap>group;//记录每组选手数据
	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;//存放分数
		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");
}

实现效果:

image.png

5.5.3 显示功能实现

首先在SpeechManager类中添加成员函数void Show_score()
然后在SpeechManager.cpp中实现

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();
}

实现效果:

image.png

5.5.4 第二轮比赛实现

第二轮比赛仅需将比赛轮次+1,后续流程与第一轮一样
实现效果:

image.png

5.5.5 保存功能实现

首先在SpeechManager类中添加成员函数void Record()
然后在SpeechManager.cpp中实现

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<

利用记事本打开文件speech.csv,里面保存了前三名选手的编号以及得分

image.png

6. 实现读取功能

首先在SpeechManager类中添加成员函数和成员变量

void Load_record();//读取往届记录
        
	bool File_is_empty;//判断文件是否为空
        
	map>record;//往届记录

然后在SpeechManager.cpp中实现

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更新文件状态

//比赛完后文件不为空
	this->File_is_empty = false;

在SpeechManager.cpp中的SpeechManager构造函数中调用void Record()

SpeechManager::SpeechManager(){this->Init_speaker();
	this->Create_speaker();
	this->Load_record();
}

在SpeechManager.cpp中的void Init_speaker()函数中初始化记录容器

this->record.clear();

在SpeechManager.cpp中的void Start_speech()函数中重置比赛

//重置比赛
	this->Init_speaker();
	this->Create_speaker();
	this->Load_record();
7. 实现查看功能

首先在SpeechManager类中添加成员函数void Show_record()
然后在SpeechManager.cpp中实现

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");
}

实现效果:

image.png

8. 实现清空功能

首先在SpeechManager类中添加成员函数void Clear_record()
然后在SpeechManager.cpp中实现

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");
}

实现效果:

image.png

image.png

  • 至此本项目结束
三、源代码显示 1. 头文件 “Speaker.h”
#pragma once;
#include#includeusing namespace std;

class Speaker{public:
	string Name;
	double Score[2];
};
“SpeechManage.h”
#pragma once
#include#include#include#include"Speaker.h"
#include#include#include#include#includeusing namespace std;

class SpeechManager{public:
	SpeechManager();

	void Show_menu();

	void Exit_system();

	void Init_speaker();

	int rounds; 
	vectorv1; 
	vectorv2;
	vectorthird;
	mapspeaker;

	void Create_speaker();

	void Start_speech();

	void Drawing();

	void Show_score();

	void Record();

	void Completion();

	void Load_record();

	bool File_is_empty;

	map>record;

	void Show_record();

	void Clear_record();

	~SpeechManager();
};
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<< "本届比赛顺利结束"<< 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");
}
“演讲比赛流程管理系统.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;
}

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


网站题目:c++小项目:基于STL的演讲比赛流程管理系统-创新互联
新闻来源:http://dzwzjz.com/article/dcjjjh.html
在线咨询
服务热线
服务热线:028-86922220
TOP