源程序代码分析
#include<iostream>#include<conio.h>
#include<windows.h>
#include<fstream>
#include<ctime>
using namespace std;
class qiudui ; //对类的声明
void paixu(qiudui c[]); //对友元函数的声明
class qiudui
{
private:
char* name; //球队名称
int sheng; //胜的场数
int fu; //负的场数
int ping; //平的场数
public:
friend void paixu(qiudui c[]); //将联赛排序定义为友元函数
char* getname();
void compare(qiudui& c); //经行胜负平的判断
int score; //比分
int jifen; //积分
int getsheng(); //获得胜的场数
int getfu(); //获得负的场数
int getping(); //获得平的场数
int jingsheng; //净胜球
int getjingsheng();
void set(char* n,int sheng1,int fu1,int ping1); //设置球队信息
int changshu; //比赛的场数
int getchangshu();//获得比赛的场数
int jinqiu; //进球数
int getjifen(); //获得积分
int getjinqiu(); //获得进球数
int getshiqiu(); //获得失球数
int getscore(); //获得分数
int shiqiu; //失球数
void show(); //输出信息
void jieguo(); //
};
char* qiudui::getname()
{
return name;
}
void qiudui::compare(qiudui & c) //计算两个队的胜负平的场数,用引用做参数
{
if(score>c.score)
{
sheng++;
c.fu++;
}
else if(score==c.score)
{
ping++;
c.ping++;
}
else
{
fu++;
c.sheng++;
}
}
void qiudui::jieguo()
{
changshu=sheng+fu+ping;
jifen=sheng*3+ping;
jingsheng=jinqiu-shiqiu;
}
void qiudui::set(char* n,int sheng1,int fu1,int ping1)
{
name=new char[];
strcpy(name,n);
sheng=sheng1;
fu=fu1;
ping=ping1;
}
int qiudui::getjinqiu()
{
return score;
}
int qiudui::getscore()
{
return score;
}
void qiudui::show()
{
cout<<name<<"\t"<<changshu<<" \t"<<sheng<<" \t"<<ping<<" \t"<<fu<<" \t"<<jinqiu<<" \t"<<shiqiu<<" \t"<<jingsheng<<" \t"<<jifen<<endl;
}
void paixu(qiudui c[]) //联赛排序的实现
{
int n;
n=sizeof(c);
for(int j=0;j<n;j++)
{
for(int i=0;i<n-j;i++)
{
if(c[i].jifen!=c[i+1].jifen)
{
if(c[i].jifen<c[i+1].jifen)
{
qiudui pt=c[i];
c[i]=c[i+1];
c[i+1]=pt;
}
}
else
{
if(c[i].jingsheng!=c[i+1].jingsheng)
{
if(c[i].jingsheng<c[i+1].jingsheng)
{
qiudui pt=c[i];
c[i]=c[i+1];
c[i+1]=pt;
}
}
else
{
srand(time(NULL));
int x=rand();
if(x==1)
{
qiudui pt=c[i];
c[i]=c[i+1];
c[i+1]=pt;
}
}
}
}
}
}
void main()
{
qiudui q[5];
q[0].set("河南队",3,5,2);
q[0].jinqiu=20;q[0].shiqiu=16;q[0].jieguo();
q[1].set("山东队",5,3,2);
q[1].jinqiu=14;q[1].shiqiu=16;q[1].jieguo();
q[2].set("江西队",4,4,2);
q[2].jinqiu=21;q[2].shiqiu=13;q[2].jieguo();
q[3].set("浙江队",7,1,2);
q[3].jinqiu=18;q[3].shiqiu=21;q[3].jieguo();
q[4].set("西北队",6,2,2);
q[4].jinqiu=10;q[4].shiqiu=19;q[4].jieguo();
ofstream f1("第五题1.txt");
for(int i=0;i<5;i++)
{
f1.write((char*)(&q[i]),sizeof(q[i]));
}
f1.close();
ifstream f2("第五题1.txt");
cout<<"--------------------------主客场双循环赛制的比赛结果-----------------------"<<endl;
cout<<endl;
cout<<"队名\t场次\t胜 \t平 \t负 \t进球 \t失球 \t净胜球 \t积分"<<endl;
for(i=0;i<5;i++)
{
f2.read((char*)(&q[i]),sizeof(q[i]));
q[i].show();
}
cout<<endl;
cout<<"---------------------------------------------------------------------------"<<endl;
cout<<"按任意键开始排名"<<endl;
getch();
system("cls");
cout<<"----------------------------主客场双循环赛的赛后排名-----------------------"<<endl;
cout<<endl;
paixu(q);
ofstream f3("第五题2.txt");
for(i=0;i<5;i++)
{
f3.write((char*)(&q[i]),sizeof(q[i]));
}
f3.close();
ifstream f4("第五题2.txt");
cout<<"名次 \t队名\t场次\t胜 \t平 \t负 \t进球 \t失球 \t净胜球 \t积分"<<endl;
for(i=0;i<5;i++)
{
f4.read((char*)(&q[i]),sizeof(q[i]));
cout<<i+1<<"\t";
q[i].show();
}
f4.close();
cout<<endl;
cout<<"---------------------------------------------------------------------------"<<endl;
cout<<"按任意键开始比赛"<<flush;
getch();
system("cls");
cout<<"---------------------正在经行比赛,比赛结果请耐心等待-----------------------"<<endl;
for(int j=0;j<5;j++)
{
for(int i=1+j;i<5;i++)
{
int x,y;
srand(time(NULL));
x=rand()%6;
y=rand()%6;
cout<<q[j].getname()<<"与"<<q[i].getname()<<"正在比赛中"<<flush;
Sleep(350);
cout<<".";
Sleep(350);
cout<<".";
Sleep(350);
cout<<".";
Sleep(350);
cout<<"\b\b\b\b\b\b\b\b\b\b\b\b\b";
cout<<"比分为:"<<x<<":"<<y<<" "<<endl;
q[j].score=x;
q[i].score=y;
q[j].compare(q[i]);
q[i].jinqiu=q[i].jinqiu+y;
q[i].shiqiu=q[i].shiqiu+x;
q[j].jinqiu=q[j].jinqiu+x;
q[j].shiqiu=q[j].shiqiu+y;
q[i].jieguo();q[j].jieguo();
}
}
paixu(q);
cout<<"最后一场比赛已经结束,按任意键开始排名"<<endl;
getch();
system("cls");
cout<<"--------------------------最后一场比赛前的排名情况-------------------------"<<endl;
cout<<endl;
ofstream f5("第五题3.txt");
for(i=0;i<5;i++)
{
f5.write((char*)(&q[i]),sizeof(q[i]));
}
f5.close();
qiudui q1[5];
ifstream f6("第五题2.txt");
cout<<"名次 \t队名\t场次\t胜 \t平 \t负 \t进球 \t失球 \t净胜球 \t积分"<<endl;
for(i=0;i<5;i++)
{
f6.read((char*)(&q1[i]),sizeof(q1[i]));
cout<<i+1<<"\t";
q1[i].show();
}
f6.close();
cout<<endl;
cout<<"---------------------------------------------------------------------------"<<endl;
cout<<"-------------------------最后一场比赛后的排名更新情况----------------------"<<endl;
cout<<endl;
ifstream f7("第五题3.txt");
cout<<"名次 \t队名\t场次\t胜 \t平 \t负 \t进球 \t失球 \t净胜球 \t积分"<<endl;
for(i=0;i<5;i++)
{
f7.read((char*)(&q1[i]),sizeof(q1[i]));
cout<<i+1<<"\t";
q1[i].show();
}
cout<<endl;
cout<<"---------------------------------------------------------------------------"<<endl;
f7.close();
cout<<"按任意键开始颁奖"<<endl;
getch();
system("cls");
cout<<endl;
cout<<"冠军为:"<<q1[0].getname()<<"积分为:"<<q1[0].jifen<<endl;
cout<<endl;
cout<<"亚军为:"<<q1[1].getname()<<"积分为:"<<q1[1].jifen<<endl;
cout<<endl;
cout<<"季军为:"<<q1[2].getname()<<"积分为:"<<q1[2].jifen<<endl;
cout<<endl;
cout<<"足球联赛告一段落,按任意键退出联赛"<<endl;
getch();
}