运动会分数统计系统——史上最完善的运动会分数统计系统
题目三:运动会分数统计
参加运动会有 n
个学校,学校编号为 1……n。比赛分成 m
个男子项目和 w 个女子项目。项目编号为男子 1……m,女子m+1……m+w。不同的项目取前五名或前三名积分;前五名的积分
为:7、5、3、2、1,前三名的积分为:5、3、2;哪些取前五名或前三名由学生自己设定。(m<=20,n<=20)
1)
可以输入各个项目的前三名或前五名的成绩;
2)
能统计各学校总分;
3)
可以按学校编号或名称、学校总分、男女团体总分排序输出;
4)
可以按学校编号查询学校某个项目的情况;可以按项目编号查询取得前三或前五名的学校;
5)
数据存入文件并能随时查询;
6)
其他要求:
(1)
输入数据:可以输入学校的名称,运动项目的名称;
(2)
输出形式:有中文提示,各学校分数为整型;
(3)
界面要求:有合理的提示,使用者可根据提示,完成相关的功能要求;
(4)
存储结构:根据系统功能要求自行设计,但是要求运动会的相关数据要存储在数据文件中,在实验报告中指明你用到的存储结构;
(5)
测试数据:分别使用合法数据、非法数据进行程序测试,以保证程序的稳定,测试数据及测试结果请在实验报告中写明;
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define MAX 40
//数据结构
class school
{
public:
school()
{
for(int i=0; i { grade[i]=0; point[i]=0; } grade_b=0; grade_g=0; grade_totle=0; } school(string name) { name_school=name; for(int i=0; i { grade[i]=0; point[i]=0; } grade_b=0; grade_g=0; grade_totle=0; } static string name_sport[MAX];//项目名称 static bool point_way[MAX];//0表示取前五名,1表示取前3名; static bool sex_sport[MAX];//1表示男,0表示女 static int n,m;//n学校数量,m项目数量 string name_school;//学校名称 int num;//学校编号 int grade[MAX];//成绩 int point[MAX];//积分 int grade_totle;//总成绩 int grade_b;//男子团体 int grade_g;//女子团体 }; int school::n=0; int school::m=0; string school::name_sport[MAX]= {}; bool school::point_way[MAX]= {0}; bool school::sex_sport[MAX]= {0}; //工具栏 class toolbar { public: toolbar(int num,string too[],string too_en[]) { n=num; for(int i=0; i { tool[i]=too[i]; tool_en[i]=too_en[i]; } } void toolbar_show() { for(int i=0; i<80; i++) cout<<"-"; for(int i=0; i cout< cout< for(int i=0; i<80; i++) cout<<"-"; } void set_tool(int num,string too[],string too_en[]) { n=num; for(int i=0; i { tool[i]=too[i]; tool_en[i]=too_en[i]; } } int get_tool_en(string eng) { for(int i=0; i if(tool_en[i][0]==eng[0]||tool_en[i][0]==(char)(eng[0]-32)) return i+1; return 0; } private: int n; string tool[6]; string tool_en[6]; }; string t0[5]= {"文件","编辑","查看","帮助","退出"}; string t_e0[5]= {"F","E","V","H","Q"}; toolbar tool_0(5,t0,t_e0); string t1[5]= {"新建","打开","保存","另存为","返回"}; string t_e1[5]= {"N","O","S","A","B"}; toolbar tool_1(5,t1,t_e1); string t2[4]= {"添加","修改","删除","返回"}; string t_e2[4]= {"I","C","D","B"}; toolbar tool_2(4,t2,t_e2); string t3[4]= {"查看学校","查看项目","查看所有项目","返回"}; string t_e3[4]= {"S","P","A","B"}; toolbar tool_3(4,t3,t_e3); //返回当前工具栏是几号工具栏 toolbar *toolbar_now(int num) { switch (num) { case 0: return &tool_0; case 1: return &tool_1; case 2: return &tool_2; default: return &tool_3; }; } //显示区域 class display { public: display() { g=1; pre=3;//设置默认排序 bit=0; name[0]="编号<1>"; name[1]="校名<2>"; name[2]="总分<3>"; name[3]="男总<4>"; name[4]="女总<5>"; name[5]="排名"; } void _show() { cout< < < < < < cout< if(g) for(int i=0; i { cout< < < < < < } else for(int i=school::n-1; i>=0; i--) { cout< < < < < < } } void show() { if(school::n>0) { if(bit==1) g=!g; else _sort(pre); _show(); } } bool get_str(string str) { char ch=str[0]; if(ch>='1'&&ch<'6') { if(pre==ch-'1'+1) bit=1; else { bit=0; pre=ch-'1'+1; } return 1; } return 0; } void set_sch(vector { this->sch=sch; } private: void _sort(int tem) { for(vector for(vector if((tem==1&&j->num>i->num)||(tem==2&&j->name_school>i->name_school)||(tem==3&&j->grade_totle>i->grade_totle) ||(tem==4&&j->grade_b>i->grade_b)||(tem==5&&j->grade_g>i->grade_g)) { school tem; tem=*i; *i=*j; *j=tem; } } int pre; bool g; bool bit; string name[6]; vector }; display dis; string path_now="";//当前路径 bool change_if=0;//记录内容是否改变 //判断路径格式是否正确 bool path_right(string path) { string name=path.substr(0,3); if(GetDriveType(name.c_str())!=DRIVE_REMOVABLE&&GetDriveType(name.c_str())!=DRIVE_FIXED) { printf("%c",7); cout<<"不是有效盘符或盘符格式不正确!(c:)"< Sleep(2000); return 0; } else { for(size_t i=2; i if(path[path.length()-1]=='\\' ||path[path.length()-1]=='/' ||((path[2]!='/')&&(path[2]!='\\')) ||!(('A'<=path[i]&&'Z'>=path[i]) ||('a'<=path[i]&&'z'>=path[i]) ||('0'<=path[i]&&'9'>=path[i]) ||path[i]=='\\' ||path[i]=='/' ||path[i]=='.')) { printf("%c",7); cout<<"路径格式不正确!(只能输入英语字母数字和'\\','/','.')"< Sleep(2000); return 0; } } return 1; } //创建多级文件夹 bool creat_path(string path) { int i = 0; int i_ret; int i_len = path.length(); for (i = 0; i { if (path[i] == '\\' || path[i] == '/') { path[i] = '\0'; //如果不存在,创建 i_ret = _access(path.c_str(),0); if (i_ret != 0) { i_ret = _mkdir(path.c_str()); if (i_ret != 0) { return 0; } } path[i] = '/'; } } return 1; } //写入文件 void _store(string path ,vector { fstream out; out.open(path.c_str(),ios::out); out< for(int i=0; i out< out< out< for(int i=0; i { out< for(int j=0; j out< out< } out.close(); } //从文件读取 bool _read(string path,vector { fstream in; in.open(path.c_str(),ios::in); in>>school::n; for(int i=0; i { string name; in>>name; school sch_tem(name); sch.push_back(sch_tem); } in>>school::m; for(int i=0; i { in>>school::name_sport[i]>>school::point_way[i]; for(int j=0; j in>>sch[j].grade[i]; } in.close(); if(in.rdstate()) return 0; else return 1; } bool store_file(string path,vector { fstream out; out.open(path.c_str(),ios::in); if(force==0&&out)//文件已存在 { out.close(); return 0; } else if(force==1&&out)//强制写入 { out.close(); _store(path,sch); return 1; } else { out.open(path.c_str(),ios::out); if(!out)//路径中有未创建文件夹 { creat_path(path.c_str());//创建多级目录 _store(path,sch); return 1; } else { out.close(); _store(path,sch); return 1; } } } //新建,从控制台读取数据 void new_build(vector { while(1) { l1: system("cls"); for(vector cout< cout<<"请输入学校名称,以#结束"< string name; cin>>name; if(name=="#") break; for(vector if(i->name_school==name) { printf("%c",7); cout<<"学校已存在,请重新输入"< cin.sync(); Sleep(2000); goto l1; } school sch_tem(name); sch.push_back(sch_tem); sch.back().num=sch.size(); school::n=sch.size(); } } void new_build_(vector { int t=school::m; while(1) { l2: system("cls"); for(vector cout< int i=t-1; if(i>=school::m) { cout< for(int j=0; j cout< cout< } cout<<"请依次输入项目名称、积分方式(0表示取前5名,1表示取前3名)和各学校成绩:\n#结束,$撤销上次输入"< cin>>school::name_sport[t]; if(school::name_sport[t]=="#") break; if(school::name_sport[t]=="$") { if(t>school::m) t--; else printf("%c",7); goto l2; } for(int i=0; i if(school::name_sport[i]==school::name_sport[t]) { printf("%c",7); cout<<"项目已存在,请重新输入"< cin.sync(); Sleep(2000); goto l2; } cin>>school::point_way[t]; if(cin.rdstate()) { printf("%c",7); cout<<"积分方式输入有误(0表示取前5名,1表示取前3名),请重新输入"< Sleep(2000); cin.clear(); cin.sync(); goto l2; } for(int i=0; i cin>>sch[i].grade[t]; if(cin.rdstate()) { printf("%c",7); cout<<"输入数据有误,请重新输入"< Sleep(2000); cin.clear(); cin.sync(); goto l2; } else { cout<<"录入成功"< cin.sync(); Sleep(1000); } t++; } school::m=t; } //处理数据 void computer(vector { //存储学校编号 for(vector i->num=i-sch.begin()+1; //项目是男子项目还是女子项目 for(int i=0; i if(school::name_sport[i].find("女")) school::sex_sport[i]=1; else school::sex_sport[i]=0; //成绩转换为积分 for(int i=0; i for(int j=0; j sch[j].point[i]=0; int p_5[5]= {7,5,3,2,1}; int p_3[3]= {5,3,2}; for(int i=0; i if(school::point_way[i]==0)//取前5名 { int n=school::n; for(int p=0; p { int max=-1; for(int j=0; j if(sch[j].grade[i]>max&&sch[j].point[i]==0) max=sch[j].grade[i]; n++; for(int j=0; j if(sch[j].grade[i]==max) { sch[j].point[i]=p_5[p]; n--; } } } else//取前3名 { int n=school::n; for(int p=0; p { int max=-1; for(int j=0; j if(sch[j].grade[i]>max&&sch[j].point[i]==0) max=sch[j].grade[i]; n++; for(int j=0; j if(sch[j].grade[i]==max) { sch[j].point[i]=p_3[p]; n--; } } } //计算学校总分,男子团体分,女子团体分 for(int i=0; i { sch[i].grade_totle=0; sch[i].grade_b=0; sch[i].grade_g=0; } for(int i=0; i for(int j=0; j { sch[i].grade_totle+=sch[i].point[j]; if(school::sex_sport[j]==1) sch[i].grade_b+=sch[i].point[j]; else sch[i].grade_g+=sch[i].point[j]; } } vector { system("cls"); vector new_build(sch); new_build_(sch); computer(sch); dis.set_sch(sch); path_now=""; change_if=1; return sch; } vector { vector l3: system("cls"); cout<<"请输入路径:"< string path; cin>>path; if(!path_right(path)) goto l3; fstream in; in.open(path.c_str(),ios::in); if(!in) { printf("%c",7); cout<<"文件不存在!"< Sleep(1000); cout<<"重新输入 string str; l9: cin>>str; if(str[0]=='r'||str[0]=='R') goto l3; else if(str[0]=='n'||str[0]=='N') sch=new_(); else { printf("%c",7); goto l9; } } else { in.close(); if(_read(path,sch)) { path_now=path; computer(sch); dis.set_sch(sch); } else { printf("%c",7); cout<<"不是运动会分数统计数据文件!"< Sleep(1000); cout<<"重新输入 string str; l10: cin>>str; if(str[0]=='r'||str[0]=='R') goto l3; else if(str[0]=='n'||str[0]=='N') sch=new_(); else { printf("%c",7); goto l10; } } } return sch; } void save_(vector { if(school::n==0) { printf("%c",7); return ; } system("cls"); if(path_now!="") { store_file(path_now,sch,1); cout<<"保存成功!"< Sleep(1000); } else { string path; l5: cout<<"请输入路径:"< cin>>path; if(!path_right(path)) goto l5; if(store_file(path,sch)==0) { cout<<"文件已存在!强制写入或重新输入路径?"< printf("%c",7); Sleep(1000); cout<<"强制写入 string str; l6: cin>>str; if(str[0]=='r'||str[0]=='R') goto l5; else if(str[0]=='f'||str[0]=='F') store_file(path,sch,1); else { printf("%c",7); goto l6; } } cout<<"保存成功!"< Sleep(1000); path_now=path; } change_if=0; } void save_as(vector { if(school::n==0) { printf("%c",7); return ; } system("cls"); string path; l7: cout<<"请输入路径:"< cin>>path; if(!path_right(path)) goto l7; if(store_file(path,sch)==0) { cout<<"文件已存在!强制写入或重新输入路径?"< printf("%c",7); Sleep(1000); cout<<"强制写入 string str; l8: cin>>str; if(str[0]=='r'||str[0]=='R') goto l7; else if(str[0]=='f'||str[0]=='F') store_file(path,sch,1); else { printf("%c",7); goto l8; } } cout<<"保存成功!"< Sleep(1000); path_now=path; change_if=0; } void find_p_all_(vector void insert_(vector { if(school::n==0) { printf("%c",7); return ; } system("cls"); cout<<"添加学校 "< string str; l11: cin>>str; if(str[0]=='s'||str[0]=='S') { while(1) { system("cls"); find_p_all_(sch); cout<<"输入学校名称,再依次输入各项成绩,#结束添加"< string name; l12: cin>>name; if(name=="#") break; for(vector if(i->name_school==name) { printf("%c",7); cout<<"学校已存在,请重新输入"< cin.sync(); Sleep(2000); goto l12; } school sch_tem(name); for(int i=0; i cin>>sch_tem.grade[i]; if(cin.rdstate()) { printf("%c",7); cout<<"输入数据有误,请重新输入"< Sleep(2000); cin.clear(); cin.sync(); goto l12; } else { cout<<"录入成功"< cin.sync(); Sleep(1000); } sch.push_back(sch_tem); sch.back().num=sch.size(); school::n=sch.size(); } } else if(str[0]=='p'||str[0]=='P') { new_build_(sch); } else { printf("%c",7); goto l11; } computer(sch); dis.set_sch(sch); change_if=1; } void change_s(vector { l16: system("cls"); cout<<"请输入新的学校名称:"< string name; cin>>name; for(vector if(j->name_school==name) { printf("%c",7); cout<<"学校已存在,请重新输入"< cin.sync(); Sleep(2000); goto l16; } sch[i].name_school=name; } void change_p(vector { l17: system("cls"); cout< cout< < < for(int j=0; j cout< < < cout<<"修改项目名称 string str; cin>>str; if(str[0]=='n'||str[0]=='N') { l18: system("cls"); cout<<"请输入新名称:"< string name; cin>>name; for(int j=0; j if(str==school::name_sport[j]) { printf("%c",7); cout<<"项目已存在,请重新输入"< Sleep(1500); goto l18; } school::name_sport[i]=name; } else if(str[0]=='w'||str[0]=='W') { l19: system("cls"); cout<<"请输入新的积分方式,0取前5名,1取前3名"< string way; cin>>way; if(way[0]=='0'||way[0]=='1') { school::point_way[i]=way[0]-'0'; } else { printf("%c",7); cout<<"请输入0或者1!,请重新输入"< Sleep(1500); goto l19; } } else { int tem=-1; bool done=0; if('1'<=str[0]&&'9'>=str[0]&&(str[0]-'0'-1 { tem=str[0]-'0'-1; done=1; } if('0'<=str[1]&&'9'>=str[1]&&((str[0]-'0')*10+str[1]-'0'-1 { tem=(str[0]-'0')*10+str[1]-'0'-1; done=1; } if(done==1) { l20: system("cls"); cout<<"您将要修改 "< cout<<"原成绩为:"< cout<<"请输入新成绩:"< cin>>sch[tem].grade[i]; if(cin.rdstate()) { printf("%c",7); cout<<"输入数据有误,请重新输入"< Sleep(2000); cin.clear(); cin.sync(); goto l20; } } else { printf("%c",7); goto l17; } } } void change_(vector { if(school::n==0) { printf("%c",7); return ; } l14: system("cls"); cout<<"修改学校名称 "< string str; cin>>str; if(str[0]=='s'||str[0]=='S') { while(1) { int tem=-1; bool done=0; system("cls"); cout<<"请输入要修改的学校名称或编号,#结束"< string str; cin>>str; if(str=="#") break; if('1'<=str[0]&&'9'>=str[0]&&(str[0]-'0'-1 { tem=str[0]-'0'-1; done=1; } if('0'<=str[1]&&'9'>=str[1]&&((str[0]-'0')*10+str[1]-'0'-1 { tem=(str[0]-'0')*10+str[1]-'0'-1; done=1; } for(int i=0; i 添加项目 修改项目内容