C++演讲比赛管理系统代码实现

可能代码有些瑕疵,还请大家谅解,毕竟我码了大半天代码了~

#include<iostream>
#include<fstream>
#include<ctime>
#include<vector>
#include<map>
#include<cstdlib>
#include<numeric>
#include<set>//一大堆头文件。 
using namespace std;
class person{
	public:
		char name;
		int score;
};//选手类。 
person a[12];//选手数组。 
int main(){
	srand((unsigned int) time(NULL));//随机数种子。 
	fstream f;
	f.open("演讲比赛成绩单.txt",ios::app | ios::in);//打开文件。 
	cout << "欢迎来到演讲比赛管理系统!" << endl;
	while(1){
		char ans = 65;//为选手名做准备 
		for (int i = 0; i < 12; i++){
			a[i].name = ans;
			ans++; 
		}//赋值好名字。 
		map<int,person> m, xm1, xm2;//设定多轮的map容器 
		set<int> s1,s2;//为排序做准备 
		int r, p1 = 1, choice;
		int b[12]; 
		cout << "请选择您的操作:" << endl;
		cout << "1.开始新一轮比赛。" << endl;
		cout << "2.读取往期比赛成绩。" << endl;
		cout << "3.退出系统。" << endl;
		cin >> choice;//三种操作 
		if (choice == 3){
			cout << "下次再见"  << endl;
			return 0; 
		}
		else if (choice == 1){
			f.close();//不知道大家有没有这个bug,作者本人每次操作不重开文件就会失败 
			f.open("演讲比赛成绩单.txt",ios::app | ios::in);//打开文件 
			for (int i = 0; i < 12; i++) b[i] = 0;//为编号数组做初始化 
			for (int i = 0; i < 12; i++){//随机分配编号系统 
				while (1){//无限循环 
					r = rand()%12 + 1;//随机数
					for (int j = 0; j < i;j++){//判断有没有重复 
						if (b[j] == r){
							p1 = 0; //判断条件,若是p1为0就重新抽随机数 
							break;
						}
					}
					if (p1 == 0){
						p1 = 1;
						continue;
					}
					m.insert(make_pair(r, a[i]));//插入选手信息 
					b[i] = r;
					break;
				}
			}
			cout << "备战中:" << endl; 
			for (map<int,person>::iterator it = m.begin(); it != m.end();it++){
				cout << "选手姓名:" << (*it).second.name << " 选手编号:" << (*it).first << endl;
			}//开战前打印一波 
			cout << endl << "第一轮分组中(随机分组):" << endl << endl;
			for(map<int,person>::iterator it = m.begin(); it != m.end(); it++){
				(*it).second.score = rand()%60 + 41;//随机设定分数 
			}
			int p2 = 1;
			int y[12] ;
			for(int i = 0; i < 12; i++)y[i] = -1;
			for (int i = 0; i < 6; i++){//随机分组开始 
				while(1){
					r = rand()%12;//随机抽数 
					for(int j = 0; j < i; j++){
						if (y[j] == r){//循环遍历,判断是否出现过 
							p2 = 0;
							break;
						}
					} 
					if(p2 == 0){
						p2 = 1;//重置p2 
						continue;//条件判断 
					}
					y[i] = r;//放入数组 
					map<int,person>::iterator it = m.find(r);//查找位置 
					xm1.insert((*it));//放入新map 
					s1.insert((*it).second.score);//放入set排序 
					break;
				}
			}
			int p3 = 1;
			for (int i = 0; i < 12; i++){
				for (int j = 0; j < 6; j++){
					if(y[j] == i){//根据上文数组判断第二组 
						p3 = 0;
						break;
					}
					
				}
				if(p3 == 0){
					p3 = 1;
					continue;
				}
				
				map<int,person>::iterator io = m.find(i);//不断加入 
				xm2.insert((*io));
				s2.insert((*io).second.score);
			}
			map<int,person> sm1,sm2,zm;
			for (map<int,person>::iterator it = xm1.begin(); it != xm1.end(); it++){
				for (set<int>::iterator io = ++(++(++s1.begin())); io != s1.end(); io++){
					if((*it).second.score == *io){//筛选前三(set升序排序,后三位即是前三名,因此上面三个++ 
						sm1.insert(*it);
						zm.insert(*it);
					}
				}
			}
			for (map<int,person>::iterator it = xm2.begin(); it != xm2.end(); it++){
				for (set<int>::iterator io = ++(++(++s2.begin())); io != s2.end(); io++){
					if((*it).second.score == *io){//筛选第二组前三 
						sm2.insert(*it);
						zm.insert(*it);
					}
				}
			}
			cout << "第一轮比赛结果:" << endl << "第一组比赛前三名:" << endl; 
			for (map<int,person>::iterator it = sm1.begin(); it != sm1.end();it++){
				cout << "选手姓名:" << (*it).second.name << " 选手编号:" << (*it).first << " 选手成绩:" << (*it).second.score << endl;//打印第一组 
			}
			cout << endl << "第二组比赛前三名:" << endl;
			for (map<int,person>::iterator it = sm2.begin(); it != sm2.end();it++){
				cout << "选手姓名:" << (*it).second.name << " 选手编号:" << (*it).first << " 选手成绩:" << (*it).second.score << endl;//打印第二组 
			}
			
			set<int>z_score;
			for (map<int,person>::iterator it = zm.begin(); it != zm.end();it++){
				(*it).second.score = rand()%60 + 41;//随机决赛成绩 
				z_score.insert((*it).second.score);//放入set排序 
			}
			cout << "决赛成绩:" << endl; 
			int an = 0;
			map<int,pair<int,person> > r_m;
			for (set<int>::iterator it = z_score.begin(); it != z_score.end();it++){
				for (map<int,person>::iterator io = zm.begin(); io != zm.end();io++){
					if ((*io).second.score == *it){
						cout << "第" << 6 - an << "名是选手" <<(*io).second.name << ",编号是" << (*io).first << " 成绩是" << (*io).second.score << endl;//打印前六名成绩 
						if (6 - an <= 3){//若是前三名就放入容器,输入文件 
							r_m.insert(make_pair(6 - an,(*io)));
						}
						an++;
						
					}
				}
			}
			string date;
			cout << "请输入比赛日期:";//用比赛日期区别比赛 
			cin >> date;
			
			f << "日期:" << date << endl;
			for (map<int,pair<int,person> >::iterator ip = r_m.begin(); ip != r_m.end();ip++){
				f << "第" << (*ip).first << "名是选手" <<(*ip).second.second.name << ",编号是" << (*ip).second.first << " 成绩是" << (*ip).second.second.score << endl;//输入 
			}
			f << endl;
		}
		else{
			string k;
			f.close();
			f.open("演讲比赛成绩单.txt",ios::app | ios::in);//重开文件 
			while(getline(f,k)){//不断输进string以打印 
				cout << k << endl;
			}
		}
	}	
	f.close();//关闭文件 
	return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值