#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define NLEN 10
#define MAXRS 100
struct name {
char fname[NLEN];
char lname[NLEN];
};
struct data {
unsigned int P_count; //上场数
unsigned int D_count; //击中数
unsigned int R_count; //走垒
unsigned int I_count; //打点
};
struct record {
unsigned int P_number; //球员号
struct name names;
struct data datas;
};
struct player {
unsigned int P_number; //球员号
struct name names;
struct data datas;
float Rate; //安打率
};
int main(void)
{
FILE * fp;
struct player total[19]={{0,{"abc","def"},{0,0,0,0},0},
{1,{"ghi","jkl"},{0,0,0,0},0},
{2,{"mno","pqr"},{0,0,0,0},0},
{3,{"stu","vwx"},{0,0,0,0},0},
{4,{"bcd","efg"},{0,0,0,0},0},
{5,{"hij","klm"},{0,0,0,0},0},
{6,{"nop","qrs"},{0,0,0,0},0},
{7,{"tuv","wxy"},{0,0,0,0},0},
{8,{"cde","fgh"},{0,0,0,0},0},
{9,{"ijk","lmn"},{0,0,0,0},0},
{10,{"opq","rst"},{0,0,0,0},0},
{11,{"uvw","xyz"},{0,0,0,0},0},
{12,{"def","ghi"},{0,0,0,0},0},
{13,{"jkl","mno"},{0,0,0,0},0},
{14,{"pqr","stu"},{0,0,0,0},0},
{15,{"vwx","yxa"},{0,0,0,0},0},
{16,{"acd","bef"},{0,0,0,0},0},
{17,{"gij","hkl"},{0,0,0,0},0},
{18,{"mop","nqr"},{0,0,0,0},0},
};
struct record temp;
int count=0;
int i=0;
int index;
if((fp=fopen("records.dat","a+b"))==NULL) //给文件添加一些原始数据
{
fputs("Error in open file.",stderr);
exit(EXIT_FAILURE);
}
puts("Enter the player/'s records:");
puts("Enter the number of the player(<0||>18 quit):");
while(scanf("%u",&temp.P_number)==1&&temp.P_number>=0&&temp.P_number<19)
{
temp.names=total[temp.P_number].names;
puts("Enter the score datas(P_count D_count R_count I_count) of the player:");
scanf("%u %u %u %u",&temp.datas.P_count,&temp.datas.D_count,
&temp.datas.R_count,&temp.datas.I_count);
fwrite(&temp,sizeof(struct record),1,fp);
puts("Enter next record:");
puts("Enter the number of the player(<0||>18 quit)");
}
rewind(fp);
while(fread(&temp,sizeof(struct record),1,fp)==1) //统计球员数据
{
switch(temp.P_number)
{
case 0:total[0].datas.P_count+=temp.datas.P_count;
total[0].datas.D_count+=temp.datas.D_count;
total[0].datas.R_count+=temp.datas.R_count;
total[0].datas.I_count+=temp.datas.I_count;
break;
case 1:total[1].datas.P_count+=temp.datas.P_count;
total[1].datas.D_count+=temp.datas.D_count;
total[1].datas.R_count+=temp.datas.R_count;
total[1].datas.I_count+=temp.datas.I_count;
break;
case 2:total[2].datas.P_count+=temp.datas.P_count;
total[2].datas.D_count+=temp.datas.D_count;
total[2].datas.R_count+=temp.datas.R_count;
total[2].datas.I_count+=temp.datas.I_count;
break;
case 3:total[3].datas.P_count+=temp.datas.P_count;
total[3].datas.D_count+=temp.datas.D_count;
total[3].datas.R_count+=temp.datas.R_count;
total[3].datas.I_count+=temp.datas.I_count;
break;
case 4:total[4].datas.P_count+=temp.datas.P_count;
total[4].datas.D_count+=temp.datas.D_count;
total[4].datas.R_count+=temp.datas.R_count;
total[4].datas.I_count+=temp.datas.I_count;
break;
case 5:total[5].datas.P_count+=temp.datas.P_count;
total[5].datas.D_count+=temp.datas.D_count;
total[5].datas.R_count+=temp.datas.R_count;
total[5].datas.I_count+=temp.datas.I_count;
break;
case 6:total[6].datas.P_count+=temp.datas.P_count;
total[6].datas.D_count+=temp.datas.D_count;
total[6].datas.R_count+=temp.datas.R_count;
total[6].datas.I_count+=temp.datas.I_count;
break;
case 7:total[7].datas.P_count+=temp.datas.P_count;
total[7].datas.D_count+=temp.datas.D_count;
total[7].datas.R_count+=temp.datas.R_count;
total[7].datas.I_count+=temp.datas.I_count;
break;
case 8:total[8].datas.P_count+=temp.datas.P_count;
total[8].datas.D_count+=temp.datas.D_count;
total[8].datas.R_count+=temp.datas.R_count;
total[8].datas.I_count+=temp.datas.I_count;
break;
case 9:total[9].datas.P_count+=temp.datas.P_count;
total[9].datas.D_count+=temp.datas.D_count;
total[9].datas.R_count+=temp.datas.R_count;
total[9].datas.I_count+=temp.datas.I_count;
break;
case 10:total[10].datas.P_count+=temp.datas.P_count;
total[10].datas.D_count+=temp.datas.D_count;
total[10].datas.R_count+=temp.datas.R_count;
total[10].datas.I_count+=temp.datas.I_count;
break;
case 11:total[11].datas.P_count+=temp.datas.P_count;
total[11].datas.D_count+=temp.datas.D_count;
total[11].datas.R_count+=temp.datas.R_count;
total[11].datas.I_count+=temp.datas.I_count;
break;
case 12:total[12].datas.P_count+=temp.datas.P_count;
total[12].datas.D_count+=temp.datas.D_count;
total[12].datas.R_count+=temp.datas.R_count;
total[12].datas.I_count+=temp.datas.I_count;
break;
case 13:total[13].datas.P_count+=temp.datas.P_count;
total[13].datas.D_count+=temp.datas.D_count;
total[13].datas.R_count+=temp.datas.R_count;
total[13].datas.I_count+=temp.datas.I_count;
break;
case 14:total[14].datas.P_count+=temp.datas.P_count;
total[14].datas.D_count+=temp.datas.D_count;
total[14].datas.R_count+=temp.datas.R_count;
total[14].datas.I_count+=temp.datas.I_count;
break;
case 15:total[15].datas.P_count+=temp.datas.P_count;
total[15].datas.D_count+=temp.datas.D_count;
total[15].datas.R_count+=temp.datas.R_count;
total[15].datas.I_count+=temp.datas.I_count;
break;
case 16:total[16].datas.P_count+=temp.datas.P_count;
total[16].datas.D_count+=temp.datas.D_count;
total[16].datas.R_count+=temp.datas.R_count;
total[16].datas.I_count+=temp.datas.I_count;
break;
case 17:total[17].datas.P_count+=temp.datas.P_count;
total[17].datas.D_count+=temp.datas.D_count;
total[17].datas.R_count+=temp.datas.R_count;
total[17].datas.I_count+=temp.datas.I_count;
break;
case 18:total[18].datas.P_count+=temp.datas.P_count;
total[18].datas.D_count+=temp.datas.D_count;
total[18].datas.R_count+=temp.datas.R_count;
total[18].datas.I_count+=temp.datas.I_count;
break;
}
}
for(index=0;index<19;index++) //计算安打率
{
if(total[index].datas.P_count!=0)
total[index].Rate=total[index].datas.D_count/total[index].datas.P_count;
else
total[index].Rate=0.0;
}
for(index=0;index<19;index++) //输出
printf("%s %s,%u %u %u %u %.2f.\n",total[index].names.fname,total[index].names.lname,
total[index].datas.P_count,total[index].datas.D_count,
total[index].datas.R_count,total[index].datas.I_count,
total[index].Rate);
fclose(fp);
puts("Bye.");
return 0;
}
总结
提示:这里对文章进行总结:
不知道有没有更简单的解答?