?? ydh.h
字號(hào):
#include<iostream.h>
#include<iomanip.h>
#include<string.h>
#include<fstream.h>
#include<stdlib.h>
#include<ctype.h>
#include<stdio.h>
#include<conio.h>
typedef struct School //學(xué)校結(jié)構(gòu)
{
char name[20]; //學(xué)校名稱
int number; //學(xué)校編號(hào)
int boy; //男子團(tuán)體總分
int girl; //女子團(tuán)體總分
School *next;
}School;
typedef struct Sport //運(yùn)動(dòng)項(xiàng)目結(jié)構(gòu)
{
char name[20]; //運(yùn)動(dòng)項(xiàng)目名稱
int isboy; //0為女項(xiàng)目,1為男項(xiàng)目
int is3; //0為取前五名,1為取前五名
int number; //項(xiàng)目編號(hào)
int first; //第一名學(xué)校編號(hào)
int second; //第二名學(xué)校編號(hào)
int third; //第三名學(xué)校編號(hào)
int fourth; //第四名學(xué)校編號(hào)
int fifth; //第五名學(xué)校編號(hào)
Sport *next;
}Sport;
int getint(int a) //字符轉(zhuǎn)換成數(shù)字
{
return (int)(a-'0');
}
School * head1;
void school_add() //添加學(xué)校
{
School * p;
int mark=0;
p=new School;
cout<<"請(qǐng)輸入學(xué)校名稱:";
cin>>p->name;
char c;
while (mark!=1)
{
cout<<"請(qǐng)輸入學(xué)校編號(hào):";
cin>>c;
if (!isdigit(c))//是否為數(shù)字
{
cout<<"數(shù)據(jù)非法"<<endl;
}
else
{
mark=1;
p->number=c;
}
}
p->boy=0;
p->girl=0;
p->next=head1->next;
head1->next=p;
cout<<"成功添加了一個(gè)學(xué)校"<<endl;
}
int school_getlong(School *first)//得到鏈表長度
{
int i=0;
while (first->next!=NULL)
{
i++;
first=first->next;
}
return i;
}
void school_write()//將學(xué)校數(shù)據(jù)寫入文本
{
School * p;
p=head1;
p=p->next;
ofstream outfile("School.txt",ios::out);
outfile<<school_getlong(p)+1<<" ";
while (p!=NULL)
{
outfile<<p->name<<" "<<p->number<<" "<<p->boy<<" "<<p->girl<<" ";
p=p->next;
}
outfile.close();
cout<<"Write Success!"<<endl;
}
void school_read()//從文本讀入學(xué)校數(shù)據(jù)
{
int i;
ifstream infile ("School.txt",ios::in);
infile>>i;
while(i>0)
{
School * p;
p=new School;
infile>>p->name>>p->number>>p->boy>>p->girl;
p->next=head1->next;
head1->next=p;
i--;
}
cout<<"School Data Read Success!"<<endl;
}
void school_output(School *p)//輸出學(xué)校
{
cout<<" 校名 編號(hào) 男團(tuán)總分 女團(tuán)總分 總分\t\n";
while(p)
{
cout<<p->name<<"\t"<<getint(p->number)<<"\t"<<p->boy<<"\t"<<p->girl<<"\t "<<(p->girl+p->boy)<<endl;
p=p->next;
}
}
int school_isexist(int a)//檢驗(yàn)學(xué)校是否存在
{
int b=0;
School *p;
p=head1;
p=p->next;
while(p)
{
if(p->number==a)
{
return 1;
}
p=p->next;
}
return 0;
}
void school_show(int a)//輸出所有學(xué)校
{
School *p;
p=head1;
p=p->next;
while(p)
{
if(p->number==a)
{
cout<<p->name<<" ";
return;
}
p=p->next;
}
cout<<" 無 ";
}
void school_search(int a)//按編號(hào)搜索學(xué)校
{
School *p;
p=head1;
p=p->next;
while(p)
{
if(p->number==a)
{
cout<<"校名:"<<p->name<<" "<<"男子團(tuán)體總分:"<<p->boy<<" "<<"女子團(tuán)體總分:"<<p->girl<<" "<<"總分:"<<(p->boy+p->girl)<<" ";
return;
}
p=p->next;
}
cout<<"無此編號(hào)";
}
void school_addmark(int a,int b,int c)//a為分?jǐn)?shù),b為學(xué)校編號(hào),c=1表示男,c=0表示女
{
School *p;
p=head1;
p=p->next;
while(p)
{
if(p->number==b)
{
if(c=='1')
{
p->boy=p->boy+a;
}
else
{
p->girl=p->girl+a;
}
}
p=p->next;
}
}
void school_order(School *temp,int type) //type=0按總分,type=1按男總分,type=2按女總分,
{
School *p,*q,*small,*temp1;
temp1=new School;
temp1->next=NULL;
p=temp;
while(p)
{
small=p;
q=p->next;
while(q)
{
switch(type)
{
case 0:
if((q->boy+q->girl)<(small->girl+small->boy))
{
small=q;
}
break;
case 1:
if(q->boy<small->boy)
{
small=q;
}
break;
case 2:
if(q->girl<small->girl)
{
small=q;
}
break;
default:
cout<<"error"<<endl;
}
if(small!=p)
{
temp1->boy=p->boy;
p->boy=small->boy;
small->boy=temp1->boy;
temp1->girl=p->girl;
p->girl=small->girl;
small->girl=temp1->girl;
strcpy(temp1->name,p->name);
strcpy(p->name,small->name);
strcpy(small->name,temp1->name);
temp1->number=p->number;
p->number=small->number;
small->number=temp1->number;
}
q=q->next;
}
p=p->next;
}
}
Sport * head2;
int sport_isexist(int a) //檢查運(yùn)動(dòng)項(xiàng)目(編號(hào))是否已經(jīng)存在
{
int b=0;
Sport *p;
p=head2;
p=p->next;
while(p)
{
if(p->number==a)
{
return 1;
}
p=p->next;
}
return 0;
}
void sport_add() //添加項(xiàng)目
{
Sport * p;
int mark=0;
p=new Sport;
cout<<"請(qǐng)輸入項(xiàng)目名稱:";
cin>>p->name;
char c;
while (mark!=1)
{
cout<<"請(qǐng)輸入項(xiàng)目編號(hào):";
cin>>c;
if (!isdigit(c))
{
cout<<"數(shù)據(jù)非法"<<endl;
}
else
{
if(sport_isexist(c))
{
cout<<"該編號(hào)已存在"<<endl;
}
else
{
mark=1;
p->number=c;
}
}
}
mark=0;
while (mark!=1)
{
cout<<"請(qǐng)輸入項(xiàng)目類型(0為女子項(xiàng)目,1為男子項(xiàng)目):";
cin>>c;
p->isboy=(int)(c-'0');//字符轉(zhuǎn)換成數(shù)字
if (!isdigit(c))
{
cout<<"數(shù)據(jù)非法"<<endl;
}
else if(p->isboy<0||p->isboy>1)
{
cout<<"數(shù)據(jù)非法"<<endl;
}
else
{
mark=1;
p->isboy=c;
}
}
mark=0;
while (mark!=1)
{
cout<<"請(qǐng)輸入項(xiàng)目名次情況(0為取前3名,1為取前5名):";
cin>>c;
p->is3=(int)(c-'0');
if (!isdigit(c))
{
cout<<"數(shù)據(jù)非法"<<endl;
}
else if(p->is3<0||p->is3>1)
{
cout<<"數(shù)據(jù)非法"<<endl;
}
else
{
mark=1;
p->is3=c;
}
}
mark=0;
while (mark!=1)
{
cout<<"請(qǐng)輸入第一名的學(xué)校編號(hào):";
cin>>c;
if (!isdigit(c))
{
cout<<"數(shù)據(jù)非法"<<endl;
}
else
{
if(!school_isexist(c))
{
cout<<"該學(xué)校不存在,請(qǐng)先添加";
}
else
{
mark=1;
p->first=c;
if(p->is3=='0')
school_addmark(5,c,p->isboy);
else
school_addmark(7,c,p->isboy);
}
}
}
mark=0;
while (mark!=1)
{
cout<<"請(qǐng)輸入第二名的學(xué)校編號(hào):";
cin>>c;
if (!isdigit(c))
{
cout<<"數(shù)據(jù)非法"<<endl;
}
else
{
if(!school_isexist(c))
{
cout<<"該學(xué)校不存在,請(qǐng)先添加";
}
else
{
mark=1;
p->second=c;
if(p->is3=='0')
school_addmark(3,c,p->isboy);
else
school_addmark(5,c,p->isboy);
}
}
}
mark=0;
while (mark!=1)
{
cout<<"請(qǐng)輸入第三名的學(xué)校編號(hào):";
cin>>c;
if (!isdigit(c))
{
cout<<"數(shù)據(jù)非法"<<endl;
}
else
{
if(!school_isexist(c))
{
cout<<"該學(xué)校不存在,請(qǐng)先添加";
}
else
{
mark=1;
p->third=c;
if(p->is3=='0')
school_addmark(2,c,p->isboy);
else
school_addmark(3,c,p->isboy);
}
}
}
mark=0;
if(p->is3=='1')
{
while (mark!=1)
{
cout<<"請(qǐng)輸入第四名的學(xué)校編號(hào):";
cin>>c;
if (!isdigit(c))
{
cout<<"數(shù)據(jù)非法"<<endl;
}
else
{
if(!school_isexist(c))
{
cout<<"該學(xué)校不存在,請(qǐng)先添加";
}
else
{
mark=1;
p->fourth=c;
school_addmark(2,c,p->isboy);
}
}
}
mark=0;
while (mark!=1)
{
cout<<"請(qǐng)輸入第五名的學(xué)校編號(hào):";
cin>>c;
if (!isdigit(c))
{
cout<<"數(shù)據(jù)非法"<<endl;
}
else
{
if(!school_isexist(c))
{
cout<<"該學(xué)校不存在,請(qǐng)先添加"<<endl;
}
else
{
mark=1;
p->fifth=c;
school_addmark(1,c,p->isboy);
}
}
}
}
else
{
p->fourth='0';
p->fifth='0';
}
p->next=head2->next;
head2->next=p;
cout<<"成功添加了一個(gè)運(yùn)動(dòng)項(xiàng)目"<<endl;
}
int sport_getlong(Sport *first) //得到項(xiàng)目鏈表長度
{
int i=0;
while (first->next!=NULL)
{
i++;
first=first->next;
}
return i;
}
void sport_write() //將項(xiàng)目數(shù)據(jù)寫入文本文檔
{
Sport * p;
p=head2;
p=p->next;
ofstream outfile("Sport.txt",ios::out);
outfile<<sport_getlong(p)+1<<" ";
while (p!=NULL)
{
outfile<<p->name<<" "<<p->number<<" "<<p->isboy<<" "<<p->is3<<" "<<p->first<<" "<<p->second<<" "<<p->third<<" "<<p->fourth<<" "<<p->fifth<<" ";
p=p->next;
}
outfile.close();
cout<<"Write Success!"<<endl;
}
void sport_read() //從文本讀取項(xiàng)目數(shù)據(jù)
{
int i;
ifstream infile ("Sport.txt",ios::in);
infile>>i;
while(i>0)
{
Sport * p;
p=new Sport;
infile>>p->name>>p->number>>p->isboy>>p->is3>>p->first>>p->second>>p->third>>p->fourth>>p->fifth;
p->next=head2->next;
head2->next=p;
i--;
}
cout<<"Sport Data Read Success!"<<endl;
}
void sport_output(Sport *p) //輸出項(xiàng)目的情況
{
cout<<" name "<<"\t"<<"Num"<<" "<<"B/G"<<" "<<"3/5"<<" "<<"first"<<" "<<"second"<<" "<<"third"<<" "<<"fourth"<<" "<<"fifth"<<" "<<endl;
while(p)
{
cout<<p->name<<"\t"<<" "<<getint(p->number)<<" "<<getint(p->isboy)<<" "<<getint(p->is3)<<" ";
school_show(p->first);
school_show(p->second);
school_show(p->third);
school_show(p->fourth);
school_show(p->fifth);
p=p->next;
}
cout<<endl;
}
void sport_search(int a) //搜索項(xiàng)目
{
Sport *p;
p=head2;
p=p->next;
while(p)
{
if(p->number==a)
{
cout<<"項(xiàng)目名:"<<p->name<<endl<<"項(xiàng)目類型:";
if(p->isboy==1)
{
cout<<"男子項(xiàng)目";
}
else
{
cout<<"女子項(xiàng)目";
}
cout<<endl<<"第一名:";
school_show(p->first);
cout<<endl<<"第二名:";
school_show(p->second);
cout<<endl<<"第三名:";
school_show(p->third);
cout<<endl<<"第四名:";
school_show(p->fourth);
cout<<endl<<"第五名:";
school_show(p->fifth);
return;
}
p=p->next;
}
cout<<"無此編號(hào)";
}
void ydh_main() //運(yùn)動(dòng)會(huì)程序主函數(shù)
{
head1=new School;
head1->next=NULL;
head2=new Sport;
head2->next=NULL;
//school_add();
sport_read();
school_read();
//sport_add();
School * p1;
Sport * p2;
p1=head1;
p1=p1->next;
p2=head2;
p2=p2->next;
char choose;
char temp;
//string ch=" ";
int a=1;
while(a!=0)
{
cout<<endl;
cout<<" 實(shí)驗(yàn)一 "<<endl;
cout<<" **********************************************************"<<endl;
cout<<" * *"<<endl;
cout<<" * 1.輸入運(yùn)動(dòng)項(xiàng)目; 2.輸入學(xué)校; *"<<endl;
cout<<" * *"<<endl;
cout<<" * 3.按學(xué)校編號(hào)輸出總分; 4.按總分排序; *"<<endl;
cout<<" * *"<<endl;
cout<<" * 5.按男團(tuán)體總分排序; 6.按女團(tuán)體總分排序; *"<<endl;
cout<<" * *"<<endl;
cout<<" * 7.按項(xiàng)目編號(hào)查詢; 8.按學(xué)校編號(hào)查詢; *"<<endl;
cout<<" * *"<<endl;
cout<<" * 0.返回 *"<<endl;
cout<<" * *"<<endl;
cout<<" * 提示:需先輸入學(xué)校后才能輸入運(yùn)動(dòng)項(xiàng)目 *"<<endl;
cout<<" * *"<<endl;
cout<<" **********************************************************"<<endl;
cout<<" 請(qǐng)選擇:";
//cin>>ch;
//choose=int(ch[0])+int(ch[1])-'0'; //處理異常狀態(tài)
cin>>choose;
if (!isdigit(choose))
{
system("cls");
cout<<"操作非法1"<<endl;
}
else
{
switch(getint(choose))
{
case 1:
system("cls");
cout<<"當(dāng)前項(xiàng)目:"<<endl;
sport_output(p2);
cout<<"當(dāng)前學(xué)校:"<<endl;
school_output(p1);
sport_add();
break;
case 2:
system("cls");
school_add();
break;
case 3:
system("cls");
school_output(p1);
break;
case 4:
system("cls");
school_order(p1,0);
school_output(p1);
break;
case 5:
system("cls");
school_order(p1,1);
school_output(p1);
break;
case 6:
system("cls");
school_order(p1,2);
school_output(p1);
break;
case 7:
system("cls");
cout<<"請(qǐng)輸入項(xiàng)目編號(hào):";
cin>>temp;
sport_search(temp);
break;
case 8:
system("cls");
cout<<"請(qǐng)輸入學(xué)校編號(hào):";
cin>>temp;
school_search(temp);
break;
case 0:
system("cls");
a=0;
break;
default:
system("cls");
cout<<"操作非法\n";
}
}
}
school_write();
sport_write();
system("exit");
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -