您的位置:首页 > 编程语言 > C语言/C++

C++:足球联赛积分系统

2017-11-13 22:41 239 查看
#include<iostream>
#include<iomanip>
#include<string>
#include<fstream>
#include<stdlib.h>
using namespace std;
class Team
{
public:
Team(char n[15],int s,int w,int d,int l,int g,int lo,int sc):sum(s),win(w),draw(d),lost(l),get(g),lose(lo),score(sc)
{
int i;
for(i=0;i<15;i++)
name[i]=n[i];
};
Team(){};
int sum,win,draw,lost,get,lose,score;
char name[15];
Team operator +(Team &a)
{
sum=sum+1;
get=get+a.get;
lose=lose+a.lose;
if(a.get>a.lose)win=win+1;
else if(a.get==a.lose)draw=draw+1;
else lost=lost+1;
score=win*3+draw;
return *this;
};
void Dispaly()
{
cout<<setw(15)<<name<<setw(10)<<sum<<setw(10)<<win<<setw(10)<<draw<<setw(10)<<lost<<setw(10)<<get<<setw(10)<<lose<<setw(10)<<score<<endl;
};
};
void Change(Team &a,Team &b)
{
Team c;
c=a;
a=b;
b=c;
};
void main()
{
cout<<endl<<endl<<endl;
cout<<"□□□□□  □□□   □□□  □□□□□ □□□□    □   □□□   □□□   "<<endl;
cout<<" □  □ □   □ □   □ □ □ □  □  □   □    □     □    "<<endl;
cout<<" □ □  □   □ □   □   □    □  □   □□   □     □    "<<endl;
cout<<" □□□  □   □ □   □   □    □□□   □ □   □     □    "<<endl;
cout<<" □ □  □   □ □   □   □    □  □  □ □   □     □    "<<endl;
cout<<" □    □   □ □   □   □    □  □  □□□□  □ 
b7e6
    □    "<<endl;
cout<<" □    □   □ □   □   □    □  □  □  □  □   □ □   □"<<endl;
cout<<"□□□    □□□   □□□   □□□  □□□□  □□  □□□□□□□□□□□□□□"<<endl;
cout<<endl<<"目前比赛的排名为:"<<endl<<endl;;
cout<<setw(5)<<"名次"<<setw(15)<<"队名"<<setw(10)<<"比赛场数"<<setw(10)<<"胜场"<<setw(10)<<"平局"<<setw(10)<<"败场"<<setw(10)<<"进球"<<setw(10)<<"输球"<<setw(10)<<"总分"<<endl;
ifstream in("score.txt");
if(!in)
{
cout<<"Unable to open in";
exit(1);
}
char name[15],a[3];
int sum,win,draw,lost,get,lose,score;
int i,j;
Team team[10];
for(i=0;i<10;i++)
{
in.get(a,2,' ');
in.getline(name,15);
in.clear();
in.get(a,4);sum=atoi(a);
in.get(a,4);win=atoi(a);
in.get(a,4);draw=atoi(a);
in.get(a,4);lost=atoi(a);
in.get(a,4);get=atoi(a);
in.get(a,4);lose=atoi(a);
in.get(a,4);score=atoi(a);
Team t(name,sum,win,draw,lost,get,lose,score);
team[i]=t;
}
in.close;
for(i=0;i<9;i++)
for(j=i+1;j<10;j++)
if(team[i].score<team[j].score)
Change(team[i],team[j]);
for(i=0;i<10;i++)
{
cout<<setw(5)<<i+1;
team[i].Dispaly();
}
while(1)
{   cout<<endl<<"请输入新一轮的比赛结果:"<<endl<<endl;
char name1[15],name2[15];
int get1,get2;
cin>>name1>>name2>>get1>>get2;
Team team1(name1,0,0,0,0,get1,get2,0);
Team team2(name2,0,0,0,0,get2,get1,0);
int t1=0,t2=0;
int longth1=strlen(name1),longth2=strlen(name2);
for(i=0;i<10;i++)
{
if(team[i].name[13]==team1.name[longth1-1]&&team[i].name[12]==team1.name[longth1-2]&&team[i].name[11]==team1.name[longth1-3]) t1=i;
if(team[i].name[13]==team2.name[longth2-1]&&team[i].name[12]==team2.name[longth2-2]&&team[i].name[11]==team2.name[longth2-3]) t2=i;
}
team[t1]=team[t1]+team1;
team[t2]=team[t2]+team2;
for(i=0;i<9;i++)
for(j=i+1;j<10;j++)
if(team[i].score<team[j].score)
Change(team[i],team[j]);
cout<<endl<<"目前比赛的排名为:"<<endl<<endl;;
cout<<setw(5)<<"名次"<<setw(15)<<"队名"<<setw(10)<<"比赛场数"<<setw(10)<<"胜场"<<setw(10)<<"平局"<<setw(10)<<"败场"<<setw(10)<<"进球"<<setw(10)<<"输球"<<setw(10)<<"总分"<<endl;
for(i=0;i<10;i++)
{
cout<<setw(5)<<i+1;
team[i].Dispaly();
}
ofstream out("score.txt");
if(!out)
{
cout<<"Unable to open out";
exit(1);
}
for(i=0;i<10;i++)
{
out<<setw(15)<<team[i].name<<setw(10)<<team[i].sum<<setw(10)<<team[i].win<<setw(10)<<team[i].draw<<setw(10)<<team[i].lost<<setw(10)<<team[i].get<<setw(10)<<team[i].lose<<setw(10)<<team[i].score<<endl;
}
out.close;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: