本文主要是介绍C Primer Plus(第五版) 第十四章 课后习题 6,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
提前在项目目录下建立信息文件。
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_char 20
#define MAX_P 20
struct baseball
{
int NO; //0~18
char First[MAX_char];
char Last[MAX_char];
int games;
int hit;
int run;
int rbi;
float sucess;
};
void init(struct baseball *a,int num);
int main(int argc, char **argv)
{
printf("hello world\n");
FILE * in;
char words[20];
int count=0;//0~6
int i=0;//0~5
int index;
struct baseball b[19];//0~18
init(b,19);
//FILE * out;
char a[20]="BaseBall";
strcat(a,".txt");
if((in=fopen(a,"a+"))==NULL)
{
fputs("Can't opent BaseBall.txt.\n",stderr);
exit(1);
}
while(fscanf(in,"%s",words)==1)
{
//(1)
if(count==0)
{
index=atoi(words);
b[index].NO=atoi(words);
i=index;
}
if(count==1) strcpy(b[i].First,words);
if(count==2) strcpy(b[i].Last,words);
if(count==3) b[i].games+=atoi(words);
if(count==4) b[i].hit+=atoi(words);
if(count==5) b[i].run+=atoi(words);
if(count==6) b[i].rbi+=atoi(words);
count++;
if(count%7==0)
{
count=0;
//caculate sucess
b[i].sucess=b[i].hit/(float)b[i].games;
}
//(2)
/*switch(count){
case 0:b[i].NO=atoi(words);break;
case 1:strcpy(b[i].First,words);break;
case 2:strcpy(b[i].Last,words);break;
case 3:b[i].games=atoi(words);break;
case 4:b[i].hit=atoi(words);break;
case 5:b[i].run=atoi(words);break;
case 6:b[i].rbi=atoi(words);break;
default:break;
//puts(words);
}
count++;
if(count%7==0)
{
count=0;
i++;
}*/
}
i=0;
while(i<19)
{
if(b[i].NO!=-1)
{
printf("%s %s \nNO:%d \nGames:%d \nHit:%d \nRun:%d \nRBI:%d\nSUCESS:%.2f\n",b[i].First,b[i].Last,b[i].NO,b[i].games,b[i].hit,b[i].run,b[i].rbi,b[i].sucess);
puts("\n");
}
i++;
}
fclose(in);
return 0;
}
void init(struct baseball *a,int num)
{
int count=0;
while(count++<num)
{
a->NO=-1;
strcpy(a->First,"NULL");
strcpy(a->Last,"NULL");
a->games=0;
a->hit=0;
a->run=0;
a->rbi=0;
a->sucess=0.0;
a++;
}
}
这篇关于C Primer Plus(第五版) 第十四章 课后习题 6的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!