#include<iostream>
#include<string>
#include <fstream>
#include <vector>
#include <iomanip>
using namespace std;
typedef bool Status;
#define OK 1
#define ERROR 0
#define NULL 0
#define OVERFLOW -2
const int MAX_SIZE=20;
typedef struct StudentNode{
string name; //名字
int score; // 得分
int range; // 排名
int item; // 得分项目
char sex; // 性别
string SchoolName; // 所在学校 名字
struct StudentNode * nextstudent;
}StudentNode;
typedef struct EventNode{
int item; //项目编号
string eventname; // 项目名字
int count; //该项目得分人数
StudentNode * next; //获奖学生
struct EventNode * nextevent; //下一个项目
}EventNode,EventList[MAX_SIZE];
typedef struct SNode{
int serial; //学校编号
string SchoolName; //学校名字
int malescore; //男子总分
int femalescore; //女子总分
int totalscore; //学校总分
struct EventNode * project;// 项目
vector<int> x; //获奖项目编号向量
}SNode,SchoolNode[MAX_SIZE];//
typedef struct Games{
EventList XML;
SchoolNode SCL;
int n; //学校数
int m; //男子项目数
int w; //女子项目数
}Games;
//=====================================//
// //
//====================================//
Status IsEqual(string const A,string const B)
{
if(A.length() != B.length()) return ERROR;
int i=0;
while(A[i] != '\0'){
if(A[i] != B[i]) return ERROR;
i++;
}
return OK;
}//IsEqual
Status Convert(string & A,bool a)
{
for (int i=0;i<A.length();i++)
{
if(a==1) {if(A[i]>='a'&&A[i]<='z') A[i]-=32;}
if(a==0) {if(A[i]>='A'&&A[i]<='Z') A[i]+=32;}
}
return OK;
}//Convert
int NumSchool(Games G,string ch)
{
for(int i=1;i<=G.n;i++)
if(IsEqual(G.SCL[i].SchoolName,ch))
return i;
return ERROR;
}//NumShool
Status InitData(Games &GD)
{
ifstream in("DATA.dat");
in>>GD.n>>GD.m>>GD.w;
for(int i=1;i<=GD.n;i++)
{
in>>GD.SCL[i].serial;
in>>GD.SCL[i].SchoolName;
GD.SCL[i].femalescore=GD.SCL[i].malescore=GD.SCL[i].totalscore=0;
}
int xm;
while(in>>xm != NULL)
{
GD.XML[xm].item = xm;
in>>GD.XML[xm].count;
in>>GD.XML[xm].eventname;
int k; //计数用
do{
StudentNode *p=new StudentNode;
StudentNode *r=new StudentNode;
EventNode *q=new EventNode;
in>>p->range; k=p->range;
in>>p->SchoolName>>p->name;
in>>p->sex;
in>>p->score;
p->item=xm;
int counter=NumSchool(GD,p->SchoolName);
q->eventname=GD.XML[xm].eventname;
q->count=GD.XML[xm].count;
r->name=p->name;
r->SchoolName=p->SchoolName;
r->range=p->range;
r->score=p->score;r->sex=p->sex;
q->nextevent=GD.SCL[counter].project;
GD.SCL[counter].project=q;
r->nextstudent=GD.SCL[counter].project->next;
GD.SCL[counter].project->next=r;
GD.SCL[counter].x.push_back(xm);
GD.SCL[counter].totalscore+=p->score;
if(p->sex=='M')
GD.SCL[counter].malescore+=p->score;
else
GD.SCL[counter].femalescore+=p->score;
p->nextstudent = GD.XML[xm].next;
GD.XML[xm].next = p;
}while(k<GD.XML[xm].count);
}
return OK;
}// InitData
Status StatisticSchoolScore(Games &GD)
{
cout<<"\n\t┌──────┬──────┬──────┬──────┐"<<endl;
cout<<"\t│ 学校名字 │ 男子总分 │ 女子总分 │ 学校总分 │"<<endl;
for(int i=1;i<=GD.n;i++)
{
cout<<"\t├──────┼──────┼──────┼──────┤"<<endl;
cout<<"\t│"<<setw(8)<<GD.SCL[i].SchoolName<<" │"<<setw(8)<<GD.SCL[i].malescore
<<" │"<<setw(8)<<GD.SCL[i].femalescore<<" │"<<setw(8)<<GD.SCL[i].totalscore<<" │"<<endl;
}
cout<<"\t└──────┴──────┴──────┴──────┘"<<endl;
return OK;
}//StatisticSchoolScore
Status SchoolTotalScoreOrder(Games GD)
{
SNode temp;
for(int i=1;i<GD.n;i++)
{
for(int j=1;j<=GD.n-i;j++)
{
if(GD.SCL[j+1].totalscore > GD.SCL[j].totalscore){
temp=GD.SCL[j+1];
GD.SCL[j+1]=GD.SCL[j];
GD.SCL[j]=temp;
}// if
}//for
}//for
cout<<endl<<setw(25)<<' '<<" 按学校总分排序后如下表\n";
StatisticSchoolScore(GD);
return OK;
}//SchoolTotalScoreOrder
Status SchoolFemaleScoreOrder(Games GD)
{
SNode temp;
for(int i=1;i<GD.n;i++)
{
for(int j=1;j<=GD.n-i;j++)
{
if(GD.SCL[j+1].femalescore > GD.SCL[j].femalescore){
temp=GD.SCL[j+1];
GD.SCL[j+1]=GD.SCL[j];
GD.SCL[j]=temp;
}// if
}//for
}//for
cout<<endl<<setw(25)<<' '<<" 按女子总分排序后如下表\n";
StatisticSchoolScore(GD);
return OK;
}//SchoolTotalScoreOrder
Status SchoolMaleScoreOrder(Games GD)
{
SNode temp;
for(int i=1;i<GD.n;i++)
{
for(int j=1;j<=GD.n-i;j++)
{
if(GD.SCL[j+1].malescore > GD.SCL[j].malescore){
temp=GD.SCL[j+1];
GD.SCL[j+1]=GD.SCL[j];
GD.SCL[j]=temp;
}// if
}//for
}//for
cout<<endl<<setw(25)<<' '<<" 按男子总分排序后如下表\n";
StatisticSchoolScore(GD);
return OK;
}//SchoolScoreOrder
int StrLen(string S)
{
int i=0;
while (S[i]!='\0')i++;
return i;
}//StrLen
int Input(int num)
{
string ch;
int back;
do {
cin>>ch;
int len=StrLen(ch);
if(len>2) goto leap;
else if(!(len-2))
{
int a=int(ch[0]-48);
int b=int(ch[1]-48);
back=a*10+b;
}
else back=int(ch[0])-48;
if(back<=num && back>=1)
return back;
leap: cout<<"\n\t输错啦!!重新输入(1-12):";
}while(1);
}// Input
Status FindFromEvents(Games & GD)
{
char ch;
do {
cout<<"\n\n\t请输入你想查寻项目的编号(1-18): ";
int num=Input(GD.m+GD.w);
cout<<"\n\t┌──────────┬──────┬───┬─────┬───┬────┐"<<endl;
cout<<"\t│ 项目名称 │ 获奖者 │ 名次 │ 来自学校 │ 得分 │ 性别 │"<<endl;
StudentNode *p=GD.XML[num].next;//****
StudentNode *q=p;
for(int i=1;i<=GD.XML[num].count;i++)
{
p=GD.XML[num].next;
cout<<"\t├──────────┼──────┼───┼─────┼───┼────┤"<<endl;
cout<<"\t│"<<setw(19)<<GD.XML[num].eventname<<" │"<<setw(11)<<GD.XML[num].next->name<<" │"
<<setw(4)<<GD.XML[num].next->range<<" │"<<setw(8)<<GD.XML[num].next->SchoolName<<" │"
<<setw(4)<<GD.XML[num].next->score<<" │"<<setw(4)<<GD.XML[num].next->sex<<" │"<<endl;
GD.XML[num].next=p->nextstudent;
}
cout<<"\t└──────────┴──────┴───┴─────┴───┴────┘"<<endl;
GD.XML[num].next=q;
cout<<"\n\n\t是否继续查看(输y,表示继续查看,输n,表示不查了): ";
do
{
cin>>ch; if(ch=='Y' || ch=='y' || ch=='N' || ch=='n') break;
cout<<"\n\n\t输入错误,请重新输入(y/n):";
} while (1);
}while(ch=='Y' || ch=='y');
return OK;
}//FindFromEvents
Status FindThoughSchool(Games & GD)
{
char ch;
do {
cout<<"\n\t输入你想查询的学校的编号(1-12):";
int num=Input(GD.n);
cout<<"\n\n"<<setw(25)<<' '<<"学校名字是:"<<GD.SCL[num].SchoolName<<endl;
cout<<"\n\t┌──────────┬──────┬───┬───┬────┐"<<endl;
cout<<"\t│ 项目名称 │ 获奖者 │ 名次 │ 得分 │ 性别 │"<<endl;
for(int i=0;i < GD.SCL[num].x.size();i++)
{
StudentNode *p=GD.XML[GD.SCL[num].x[i]].next;
StudentNode *q=p;
for(int j=0;j<GD.XML[GD.SCL[num].x[i]].count;j++)
{
if(IsEqual(GD.SCL[num].SchoolName,p->SchoolName))
{
cout<<"\t├──────────┼──────┼───┼───┼────┤"<<endl;
cout<<"\t│"<<setw(19)<<GD.XML[GD.SCL[num].x[i]].eventname<<" │"<<setw(11)<<p->name<<" │"
<<setw(4)<<p->range<<" │"
<<setw(4)<<p->score<<" │"<<setw(4)<<p->sex<<" │"<<endl;
}
GD.XML[GD.SCL[num].x[i]].next=p->nextstudent;
p=GD.XML[GD.SCL[num].x[i]].next;
}//for
GD.XML[GD.SCL[num].x[i]].next=q;
}//for
cout<<"\t└──────────┴──────┴───┴───┴────┘"<<endl;
cout<<"\n\n\t是否继续查看(输y,表示继续查看,输n,表示不查了): ";
do
{
cin>>ch; if(ch=='Y' || ch=='y' || ch=='N' || ch=='n') break;
cout<<"\n