C Primer Plus 第十四章 编程练习6

作者在 2013-05-16 17:49:15 发布以下内容
14 Chance Ruffin 5 2 1 1
0 Josh Phegley 12 8 4 3
1 Andrew Oliver 7 4 3 1
2 Mike Leake 2 1 0 0
3 Justin Marks 9 1 1 1
2 Mike Leake 3 2 1 1
4 Ackley Dustin 17 10 7 5
5 Bellamy Kyle 15 8 8 8
6 Dustin Coleman 14 12 7 5
7 Christian Colon 16 9 6 4
8 Davis Kentrail 16 14 10 3
9 Dietrich Derek 7 5 2 2
12 Gibbs Micah 12 9 8 5
13 Gibson Kyle 10 7 6 4
15 Rick Hague 9 8 7 6
16 Shaver Hansen 11 10 9 8
17 Chris Hernandez 7 6 4 3
18 Ryan Jackson 8 5 3 2
14 Chance Ruffin 5 2 1 1
0 Josh Phegley 12 8 4 3
1 Andrew Oliver 7 4 3 1
2 Mike Leake 2 1 0 0
3 Justin Marks 9 1 1 1
2 Mike Leake 3 2 1 1
4 Ackley Dustin 17 10 7 5
5 Bellamy Kyle 15 8 8 8
6 Dustin Coleman 14 12 7 5
7 Christian Colon 16 9 6 4
8 Davis Kentrail 16 14 10 3
9 Dietrich Derek 7 5 2 2
12 Gibbs Micah 12 9 8 5
13 Gibson Kyle 10 7 6 4
5 Bellamy Kyle 15 8 8 8
6 Dustin Coleman 14 12 7 5
7 Christian Colon 16 9 6 4
10 Fellhauer Josh 9 6 4 3
11 Fletcher Brian 11 7 5 1
12 Gibbs Micah 12 9 8 5
13 Gibson Kyle 10 7 6 4
15 Rick Hague 9 8 7 6
16 Shaver Hansen 11 10 9 8
17 Chris Hernandez 7 6 4 3
18 Ryan Jackson 8 5 3 2
5 Bellamy Kyle 15 8 8 8
6 Dustin Coleman 14 12 7 5
7 Christian Colon 16 9 6 4
8 Davis Kentrail 16 14 10 3
9 Dietrich Derek 7 5 2 2
10 Fellhauer Josh 9 6 4 3
11 Fletcher Brian 11 7 5 1

/*
一个文本文件中存放着一个棒球队的信息.每一行的数据都是这样排列的:
4 Jessie Joybat 5 2 1 1
第一项是球员号码,为了方便,范围是0到18.
第二项是球员的名
第三项是姓			姓和名都是单个的单词
第四项是官方统计的球员上场次数
第五项是击中数
第六项是走垒数
第七项是跑点数(RBI).
文件可能包括超过一场比赛的数据,因为同一个球员可能会有多于一行的数据
而且在不同的行之间有可能有别的球员的数据
.
写一个程序,把这些数据存储到一个结构数组中.
结构中必须含有姓, 名, 上场次数, 击中数, 走垒数和跑点数,以及击球平均成功率(稍后计算).
可以使用球员号码做为数组索引.
程序应读到文件末尾.
并且应该保存每个球员的累计总和. 
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NAMELEN 20
#define SIZE 10
#define NUMBER 19
#define BUFSIZE 50

struct playername{
	char fname[NAMELEN];
	char lname[NAMELEN];
};
struct player{
	unsigned int number;
	struct playername name;
	unsigned int times;
	unsigned int hits;
	unsigned int walks;
	unsigned int rbi;
	float rate;
};

void getinfo(struct player []);
void calc_avg(struct player []);
void display_info(struct player []);
int main(void)
{
	int count;
	struct player data[NUMBER] = {0};
	
	getinfo(data);
	calc_avg(data);
	display_info(data);
	return 0;
}

void getinfo(struct player data[])
{
	FILE * fp;
	char file[SIZE];
	char temp[BUFSIZE];
	int number = 0;
	int i = 0, j = 0;
	int count = 0;
	char p[7][NAMELEN];
	char *tmp;
	struct player savedata(char [][NAMELEN], struct player);
	
	printf("Enter file name: ");
	gets(file);
	
	if((fp = fopen(file, "r")) == NULL)
	{
		fprintf(stderr, "Can't open file: \"%s\"\n", file);
		exit(1);
	}
	
	while(fgets(temp, BUFSIZE, fp) != NULL)
	{
		tmp = temp;
		i = 0;
		j = 0;
		while(*tmp != '\n' && *tmp != '\0')
		{
			if(*tmp != ' ')
				p[i][j++] = *tmp;
			else
			{
				p[i][j] = '\0';
				i++;
				j = 0;
			}
			tmp++;
		}
		number = atoi(p[0]);
		
		data[number] = savedata(p, data[number]);
		
	}
	
	fclose(fp);
}

struct player savedata(char str[][NAMELEN], struct player data)
{
	data.number = atoi(str[0]);
	strcpy(data.name.fname, str[1]);
	strcpy(data.name.lname, str[2]);
	data.times += atoi(str[3]);
	data.hits += atoi(str[4]);
	data.walks += atoi(str[5]);
	data.rbi += atoi(str[6]);
	
	return data;
}

void calc_avg(struct player data[])
{
	int i;
	struct player * pst;
	pst = data;
	
	for(i = 0; i < NUMBER; i++, pst++)
	{
		if(strlen(pst->name.fname) != 0)
			pst->rate = (float)pst->hits / (float)pst->times;
	}
}

void display_info(struct player data[])
{
	int i;
	struct player * pst;
	
	for(i = 0, pst = data; i < NUMBER; i++, pst++)
	{
		if(strlen(pst->name.fname) != 0)
		{
			printf("%s %s:\n", pst->name.fname, pst->name.lname);
			printf("number  times  hits  walks  rbi  rate\n");
			printf("%6u  %5u  %4u  %5u  %3u  %4.2f\n\n",
				pst->number, pst->times, pst->hits, pst->walks, pst->rbi, pst->rate);
		}
	}
}
/*
将文件数据读入临时变量中,
然后把它们加到相应结构的内容中.
程序读完文件后,应该计算每个球员的击球平均成功率,
并把它保存到相应的结构成员里.
计算击球平均成功率是用球员的累计击中次数除以上场累计次数,浮点数
然后程序要显示每个球员的累计数据.
并且对整个时期显示一行综合统计数据. 
*/
文章评论,共0条
游客请输入验证码
浏览41406次