C课后习题参考答案(6)

2019-04-17 00:22

习题参考答案

float efinal; }stu; main()

{ float escore;

scanf(“%d,%f,%f”,&stu.num,&stu.emid,&stu.efinal); escore=stu.emid*0.3+stu.efinal*0.7; printf(“%f\\n”,escore); } 2、

已知结构体类型如下:

struct user { char name[20]; char num[10]; }; main

{struct user sp[5],temp; int i,j,k; for(i=0;i<5;i++)

{ gets(sp[i].name); gets(sp[i].num);} for(i=0;i<4;i++) { k=I;

for(j=i+1;i<5;j++)

if(strcmp(sp[k].name,sp[j].name)>0) k=j;

temp=sp[k]; sp[k]= sp[i]; sp[i]= temp; }

for(i=0;i<5;i++)

printf(“%s,%s\\n”,sp[i].name,sp[i].num); } 3、

struct result { char name[20]; float score[6]; float aver; }; main()

{ struct result player,*p; int i;

320

习题参考答案

float max,min,sum=0; p=&player; gets(p->name); for(i=0;i<6;i++)

scanf(“%f”,&player.score[i]); sum=max=min=player.score[0]; for(i=1;i<6;i++) {sum+=p->score[i];

if(p->score[i]>max) max=p->score[i]; else min=p->score[i];} =(sum-max-min)/4;

printf(“%s:%f\\n”,p->name,p->aver); } 4、

struct node { int x;

struct node *next; };

struct node *find_del(struct node *head,struct node **pm) {struct node *p1,*p2,*pmax,*pre; if(head==NULL) return NULL; pmax=head; p2=p1=pmax; while(p1)

{ if(p1->x>pmax->x) { pre=p2;pmax=p1;} p2=p1;p1=p1->next; }

if(pmax==head) head=pmax->next; else pre->next=pmax->next; *pm=pmax; return head; }

习 题 10

一、选择题

1、D 2、C 3、C 4、D 5、B 6、C 7、A 8、D 9、D 11、D 12、C 13、C 14、D 15、A 16、A 17、B 18、B 19、D 二、填空题

10、B 20、C 321

习题参考答案

1、16 8 2、stdio.h 3、”a+” 4、fread 5、0或NULL

6、缓冲文件系统 非缓冲文件系统 7、fseek(fp,32,0);或fseek(fp,32,SEEK_SET); 8、”abc.dat” “r” 不能打开abc.dat文件 打开abc.dat文件成功 9、fgetc(fp) ch!=EOF fgetc(fp) 10、10,11 30,31 50,51

三、编程题 1、

#include main() {FILE *fp;

char ch, fname[10];

printf(“Input the name of file.\\n”); gets(fname);

if((fp=fopen(fname,”w”))==NULL)

{printf(“Cannot open file!\\n”); exit(1); } printf(“Input data:\\n”); while((ch=getchar()!=?#?) fputc(ch,fp); fclose(fp); } 2、

#include #include main()

{FILE *fp; int letter=0,digit=0,space=0; char c;

if((fp=fopen(\ {printf(\ while((c=fgetc(fp))!=EOF) {if(isalpha(c)) letter++; if(isdigit(c)) digit++; if(c==? ?) space++; }

printf(“Letter=%d,Digit=%d,Space=%d\\n”,letter,digit,space); fclose(fp); } 3、

#include main() {FILE *fp; char c;

if((fp=fopen(\ {printf(\ c=fgetc(fp);

322

while(c!=EOF) {if(c>='a'&&c<='z') { c=c-32;

fseek(fp,-1,SEEK_CUR); fputc(c,fp);

fseek(fp,0,SEEK_CUR); }

c=fgetc(fp); }

fclose(fp); } 4、

#include main() {FILE *fp;

char str[][11]= {\\char s[8][11]; int i;

if((fp=fopen(\

{printf(\ exit(1); } for(i=0;i<8;i++) /*将字符串数组写入文件中*/ { fputs(str[i],fp); fputs(\ }

rewind(fp); /*将文件指针复位到文件开头*/ printf(\

for(i=0;i<8;i++) /*从文件中读出字符串输出到屏幕上*/ { fgets(s[i],11,fp); puts(s[i]); } fclose(fp); } 5、

#include #include struct employee {char num[6]; char name[10]; char sex[2]; int age;

char addr[20]; int salary; char health[8]; char class[10]; }em[10]; struct emp

{char name[10];

习题参考答案

323

习题参考答案

int salary; }em_new[10];

void creat() {FILE *fp; int i,j;

printf(\ for(i=0;i<4;i++) /*输入数据*/

scanf(\m[i].salary,em[i].health,em[i].class);

/*将数据写入文件*/

if((fp=fopen(\ { printf(\ exit(0); } for(i=0;i<4;i++)

if(fwrite(&em[i],sizeof(struct employee),1,fp)!=1) printf(\ fclose(fp); }

main()

{FILE *fp1,*fp2; int i,j; creat();

if((fp1=fopen(\ { printf(\ exit(0); }

printf(\ NO. name sex age address salary health clasee\\n\ for(i=0;fread(&em[i],sizeof(struct employee),1,fp1)!=0;i++)

{printf(\ em[i].sex,em[i].age,em[i].addr,em[i].salary,em[i].health,em[i].class); strcpy(em_new[i].name,em[i].name); em_new[i].salary=em[i].salary; }

printf(\

if((fp2=fopen(\

{ printf(\ exit(0); } printf(\ Name Salary\\n\ for(j=0;j

{if(fwrite(&em_new[j],sizeof(struct emp),1,fp2)!=1) printf(\

printf(\ s ?\ }

324


C课后习题参考答案(6).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:高压电动机技术规范书

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: