/********************************************************
* File Name : daily_exercise.cpp
* Created : 10/03/03
* Description : 日常练习二---乒乓球单打比赛程序模拟
********************************************************/
#include <stdio.h>
#include <math.h>
/******使用的数据结构******/
typedef struct {
char scoreA; // A选手局得分
char scoreB; // B选手局得分
} Match_t; //局比分信息
typedef struct {
char An; // A选手赢的局数
char Bn; // B选手赢的局数
Match_t Match[7]; //最多7局比分
}Score_t;
/*****************************************
函数名:Shot()
入参:无
返回值:如shot返回1则表示发球的一方得分,
如shot返回0则表示接球的一方得分;
函数功能:读文件,获取得分情况
*****************************************/
int shot()
{
FILE* fp;
static int nIndex = 0;
static int inputData[200] = {-1};
static int isfirst = 1;
int i;
if (isfirst) {
fp = fopen("D://book1.txt","r");
for(i = 0; i < 200; i++)
{
if(fscanf(fp,"%d,",&inputData[i]) == EOF)
break;
}
isfirst = 0;
fclose(fp);
}
if(nIndex == 200)
return -1;
return inputData[nIndex++];
}
/********************************************
函数名: Output(Score_t *p)
入参: Score_t *p 指向结构体的指针
返回值: 无
函数功能:输出最终比分
*******************************************/
void Output(Score_t *p)
{
FILE* fp = fopen("d://scores.txt","w");
int i;
fprintf(fp,"================A vs B Result====================/n");
fprintf(fp,"A vs B = %d : %d/n", p->An, p->Bn);
for(i = 0; i < p->An + p->Bn; i++)
{
fprintf(fp,"%dst Match Scores is %d : %d/n",i+1,p->Match[i].scoreA,p->Match[i].scoreB);
}
fprintf(fp,"/n");
fclose(fp);
}
/******************************************
函数名:isOver(Score_t *p,int n)
入参: Score_t *p 指向结构体的指针
n 第n局的分数数组下标
返回值:bool型
true表示本局未结束
false表示本局已结束
函数功能: 判断第n局比赛是否结束
******************************************/
bool isOver(Score_t *p,int n)
{
char cA = p->Match[n].scoreA;
char cB = p->Match[n].scoreB;
if((cA > 10) || (cB > 10))
{
if((cA > cB) && ((cA - cB) >= 2))
{
p->An++;
return false;
}
else if((cB > cA) && ((cB - cA) >= 2))
{
p->Bn++;
return false;
}
else
return true;
}
else
return true;
}
/***********************************************
函数名:initialize(Score_t *p)
入参: Score_t *p 指向结构体的指针
返回值:无
函数功能: 初始化结构体数据
**********************************************/
void initialize(Score_t *p)
{
int i;
p->An = 0;
p->Bn = 0;
for(i = 0; i < 7; i++)
{
p->Match[i].scoreA = 0;
p->Match[i].scoreB = 0;
}
return;
}
/***********************************************
函数名:game(Score_t *p)
入参: Score_t *p 指向结构体的指针
返回值:无
函数功能: 模拟比赛过程
**********************************************/
void game(Score_t *p)
{
int inning; //局数
int n; //每次发球次数
bool bA; //A发球标志
bool bNotOver; //结束标志
int iShot; //得分返回值
int i;
for(inning = 0; inning < 7; inning++)
{
n = 2;
bNotOver = true;
if(0 == inning % 2) //A先发球局
{
bA = true;
}
else //B先发球局
{
bA = false;
}
while(bNotOver)
{
if((p->Match[inning].scoreA == 10) && (p->Match[inning].scoreB == 10))
{
n =1; //如果AB得分都为10,改变每次发球次数为1
}
else;
if(bA) //A正在发球
{
for(i = 0; i < n; i++)
{
iShot = shot();
if(1 == iShot)
{
p->Match[inning].scoreA++;
}
else
{
p->Match[inning].scoreB++;
}
bNotOver = isOver(p,inning);
if(!bNotOver)
{
break;
}
else;
}
bA = false; //换B发球
}
else //B正在发球
{
for(i = 0; i < n; i++)
{
iShot = shot();
if(1 == iShot)
{
p->Match[inning].scoreB++;
}
else
{
p->Match[inning].scoreA++;
}
bNotOver = isOver(p,inning);
if(!bNotOver)
{
break;
}
else;
}
bA = true; //换A发球
}
}//while over
if((4 == p->An) || (4 == p->Bn))
{
break;
}
else;
}//for(inning) over
return;
}
int main(void)
{
Score_t record;
initialize(&record);//初始化数据
game(&record); //进行比赛
Output(&record); //输出比赛结果
return 0;
}
D://book1.txt
0 0 1 1 0 0 1 1 0 0 1
0 0 1 1 0 0 1 1 0 0 1
0 0 1 1 0 0 1 1 0 0 1
0 0 1 1 0 0 1 1 0 0 1
0 0 1 1 0 0 1 1 0 0 1
0 0 1 1 0 0 1 1 0 0 1
0 0 1 1 0 0 1 1 0 0 1