C premier plus 第五版 结构和其他数据形式

14.18 编程练习 .6 “一个文件中存放着一个棒球队的信息。。。。”

利用程序清单14.14 booksave.c程序,可以解决这个问题。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LEN 100
#define SIZE 30

struct athlete 
{
    int index;         // 球员号码
    char lname[SIZE];  // 名
    char fname[SIZE];  // 姓
    int play_times;    // 上场次数
    int hit_numbers;   // 击中数
    int base_numbers;  // 走垒数
    int rbi;           // 跑点数
    double success_rate; // 击球平均成功率
};

struct player //总数据的结构。
{
	int tot_playtimes;
	int tot_hittimes;
	int tot_basetimes;
	int tot_rbitimes;
	double tot_sucess;
};
void show_statistical_data(struct athlete *, int);//结构指针的应用。

int main(void)
{
    struct athlete athletes[LEN];
    FILE *fp;
	int count=0;
    int index=0,filecount=0;
	int size = sizeof(struct athlete);//结构大小。

    if((fp = fopen("file.dat","a+b")) == NULL)
    {
        fputs("Can't open the file.dat\n",stderr);
        exit(EXIT_FAILURE);
    }
	rewind(fp);
	while(count<LEN && fread(&athletes[count],size,1,fp)==1)
	{
		if(count==0)
			puts("Current contents of book.dat:");
		printf("%d player is %s %s,data are %d %d %d %d and %.2f%%.\n",athletes[count].index,athletes[count].lname,athletes[count].fname,
			   athletes[count].play_times,athletes[count].hit_numbers,athletes[count].base_numbers,athletes[count].rbi,athletes[count].success_rate *100);
		count++;
	}
	filecount=count;//这个赋值很重要!可以从存在的结构之后继续添加,直到LEN。
	if(count==LEN)
	{
		fputs("The file.dat file is full.",stderr);
		exit(EXIT_FAILURE);
	}

	puts("Press [enter] at the start of a line to stop.");
	puts("Now enter the player number.");
	while(count<LEN && scanf("%d",&athletes[count].index)==1)//此时随便输入一个非数值就表示循坏结束了。
	{
		while(getchar()!='\n')
			continue;
		puts("Now enter the numbers player lastnames.");
		gets(athletes[count].lname);
		puts("Then enter the numbers player firstnames.");
		gets(athletes[count].fname);
		puts("Finally enter the player result.");
		scanf("%d %d %d %d",&athletes[count].play_times,&athletes[count].hit_numbers,&athletes[count].base_numbers,&athletes[count].rbi);
		while(getchar()!='\n')
			continue;
		count++;
		if(count<LEN)
		{
			puts("Enter the next player.");
	        puts("Now enter the player number.");
		}
	}
	
    for(int i=0; i < count; i++)//计算成功率。
        athletes[i].success_rate = (double)athletes[i].hit_numbers / athletes[i].play_times;

	if(count>0)
	{
		for( index = 0; index < count; index++)
            printf("球员%s %s的累计数据为:\n上场次数:%d \n击中数:%d \n走垒数:%d \n跑点数:%d \n击球平均成功率:%.2f%%\n", athletes[index].lname, athletes[index].fname,athletes[index].play_times,
			athletes[index].hit_numbers,athletes[index].base_numbers,athletes[index].rbi,athletes[index].success_rate * 100);
		fwrite(&athletes[filecount],size,count-filecount,fp);//表达式&athletes[filecount]是数组中第一个新输入的结构的地址,因此复制就从这一点开始。
	}
	else
		puts("No books? Too bad.\n");

    show_statistical_data(athletes, index);
	fclose(fp);
    return 0;
}

void show_statistical_data(struct athlete * player, int n)//此时指针player就是athletes[]。
{
	struct player tot_player;
	int a=0,b=0,c=0,d=0;
    for(int i = 0; i < n; i++)
    {
        a += player[i].play_times;
        b += player[i].hit_numbers;
        c += player[i].base_numbers;
        d += player[i].rbi;
    }
	tot_player.tot_playtimes=a;
	tot_player.tot_hittimes=b;
	tot_player.tot_basetimes=c;
	tot_player.tot_rbitimes=d;
	tot_player.tot_sucess=(double)tot_player.tot_hittimes / tot_player.tot_playtimes;
    printf("**********************综合统计数据*******************************\n");
    printf("所有球员的上场次数总和为:%d\n", tot_player.tot_playtimes);
    printf("击中数总和为:%d\n", tot_player.tot_hittimes);
    printf("走垒数总和为:%d\n", tot_player.tot_basetimes);
    printf("跑点数总和为:%d\n", tot_player.tot_rbitimes);
    printf("击球平均成功率为:%.2f%%\n", tot_player.tot_sucess * 100);
}

 

仅供参考。 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值