谭浩强(第三版)C语言的课后答案 要第13章
第十三章
13.4从键盘输入一个字符串,将其中的小写字母全部转换成大写字母,然后输出到一个磁盘文件“test”中保存。输入的字符串以“!”结束。
解:#include <stdio.h>
main()
{
File *fp;
Char str[100];
Int I=0;
If((fp=fopen(“test”,”w”)==NULL)
{printf(“Can not open the file\n”);
exit(0);
}
printf(“Input a string:\n”);
gets(str);
while(str[i]!=’!’)
{if (str[i]>=’a’&&str[i]<=’z’)
str[i]=str[I-32];
fputc(str[i],fp);
I++;
}
fclose(fp);
fp=fopen(“test”,”r”);
fgets(str,strlen(str)+1,fp);
printf(“%s\n”,str);
fclose(fp);
}
13.5有两个磁盘文件”A”和”B”,各存放一行字母,要求把这两个文件中的信息合并(按字母顺序排列),输出到一个新文件”C”中。
解:#include<stdio.h>
main()
{
FILE *fp;
Int I,j,n,i1;
Char c[100],t ,ch;
If((fp=fopen(“a1”,”r”))==NULL)
{printf(“can not open the file\n”);
exit(0);
}
printf(“\n file A:\n”);
for(I=0;(ch=fgetc(fp)!=EOF;I++)
{c[i]=ch;
putchar(c[i]);
}
fclose(fp);
i1=I;
if((fp=fopen(“b1”,”r”))==NULL)
{printf(“\n can not ipen the file”);
exit(0);
}
printf(“\n file B:\n”);
for(I=i1;(ch=fgenc(fp))!=EOF;I++)
{c[i]=ch;
putchar(c[i]);
}
fclose(fp);
n=I;
for(i=0;I<n;I++)
for(j=I+1;j<n;j++)
if(c[i]>c[j])
{t=c[i];
c[i]=c[j];
c[j]=t;
printf(“\n file C:\n”);
fp=fopen(“c1”,”w”);
for(I=0;I<n;I++)
{putc(c[i],fp);
putchar(c[i]);
}
fclose(fp);
}
13.6有5个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号、姓名、三门课成绩),计算出平均成绩,将原有数据和计算出的平均分数存放在磁盘文件stud中。
解:
#include<stdio.h>
struct student
{char num[10];
char name[8];
int score[3];
float ave;
}stu[5];
main()
{int I,j,sum;
FILE *fp;
For(I=0;I<5;I++)
{printf(“\n input score of student%d:\n”,I+1);
printf(“NO.:”);
scanf(“%s”,stu[i].num);
printf(“name:”);
scanf(“%s”,stu[i].name);
sum=0;
for(j=0;j<3;j++)
{printf(“score %d :”j+1);
scanf(“%d”,&stu[i].score[j]);
sum+=stu[i].score[j];
}
stu[i].ave=sum/3.0
}
fp=fopen(“stud”,”w”);
for(I=0;I<5;I++)
if(fwrite(&stu[i],sizeof(struct student),1,fp)!=1)
printf(“File write error\n”);
fclose(fp);
fp=fopen(“stud”,”r”);
for(I=0;I<5;I++)
{fread(&stu[i],sizeof(struct student),1,fp);
printf(“%s,%s,%d,%d,%d,%6.2f\n”,stu[i].num,stu[i].name,stu[i].score[0], stu[i].score[1], stu[i].score[2] ,stu[i].ave);
}
}
13.7将上题stud文件中的学生数据按平均分进行排序处理,并将已排序的学生数据存入一个新文件stu-sort中。
解:
#include <stdio.h>
#define N 10
struct student
{char num[10];
char name[8];
int score[3];
float ave;
}st[N],temp;
main()
{
FILE *fp;
Int I,j,n;
If((fp=fopen(“stud”,”r”))==NULL)
{printf(“can not open the file”);
exit(0);
}
printf(“\n file ‘stud’:”);
for(I=0;fread(&st[i],sizef(struct student),1,fp)!=0;I++)
{printf(“\n%8s%8s”,st[i].num,,st[i].name);
for(j=0;j<3;j++)
printf(“%8d”,st[i].score[j]);
printf(“%10.f”,st[i].ave);
fclose(fp);
n=I;
for(I=0;I<n;I++)
for(j=I+1;j<n;j++)
if(st[i].ave<st[j].ave)
{temp=st[i];
st[i]=st[j];
st[j]=temp;
}
printf(“\nnow:”);
fp=fopen(“stu-sort”,”w”);
for(I=0;I<n;I++)
{fwrite(&st[i],sizeof(struct student),1,fp);
printf(“\n%8s%8s”,st[i].num,st[i].name);
for(j=0;j<3;j++)
printf(“%8d”,st[i].score[j]);
printf(“%10.2f”,st[i].ave);
fclose(fp);
}
13.8将上题以排序的学生成绩文件进行插入处理。插入一个学生的3门课成绩,程序先计算新插入学生的平均成绩,然后将它按平均成绩高低顺序插入,插入后建立一个新文件。
解:#include <stdio.h>
struct student
{char num[10];
char name[8];
int score[3];
float ave;
}st[10],s;
main()
{FILE *fp, * fp1 ;
int I,j,t,n;
printf(“\n NO.:”);
scanf(“%s”,s.num);
printf(“name:”);
scanf(“%s”,s.name);
printf(“score1,score2,score3:”);
scanf(“%d,%d,%d”,&s. score[0], &s. score[1], &s. score[2]);
s.ave=(s.score[0]+s.score[1]+s.score[2])/3.0;
if((fp=fopen(“stu_sort”,”r”))==NULL)
{printf(“can not open file.”);
exit(0);
}
printf(“original data:\n”);
for(I=0;fread(&st[i],sizeof(struct student),1,fp)!=0;I++)
{printf(“\n%8s%8s”,st[i].num,st[i].name);
for(j=0;j<3;j++)
printf(“%8d”,st[i].score[j]);
printf(“%10.2f”,st[i].ave);
}
n=I;
for(t=0;st[t].ave>s.ave&&t<n;t++);
printf(“\nnow:\n”);
fp1=fopen(“sort1.dat”,”w”);
for(I=p;j<t;I++)
{fwrite(&st[i],sizeof(stuct student),1,fp1);
print(“\n%8s%8s”,st[i],num,st[i].name);
for(j=0;j<3;j++)
ptintf(“%8d”,st[i].score[j]);
printf(“%10.2f”,st[i].ave);
}
fwrite(&s,sizeof(struct student),1,fp1);
printf(“\n%8s%7s%7d%7d%7d%10.2f”,s.num,s.name,s.score[0],s.score[1],s.score[2],s.ave);
for(I=t;I<n;I++)
{fwrite(&st[i],sizeof(struct student),1,fp1);
printf(“\n %8s%8s”,st[i].num,st[i].name);
for(j=0;j<3;j++)
printf(“%8d”,st[i].score[j]);
printf(“10.2f”,st[i].ave);
fclose(fp);
fclose(fp1);
}
13.9上题结果仍存入原有的stu_sort文件而不另建立新文件。
解: #include<stdio.h>
struct student
{char num[10];
char name[8];
int score[3];
float ave;
}st[10],s;
main()
{FILE *fp, *fp1;
int I ,j,t,n;
printf(“\nNO.:”);
scanf(“%s’,s.num);
printf(“name:”);
scanf(“%s’,s.name);
printf(“score1,score2,score3:”);
scanf(“%d%d%d”,&s.score[0]+&s.score[1]+&s.score[1], &s.score[2]);
s.ave=( s.score[0]+ s.score[1]+ s.score[2])/3.0;
if((fp=fopen(“stu=sort”,”r”))==NULL)
{printf(“can not open the file.”);
exit(0);
}
printf(“original data:\n”);
for(I=0;fread(&st[i],sizeof(struct student),1,fp)!=0;I++)
{printf(“\n%8s%8s”,st[i].num,st[i].name);
for(j=0;j<3;j++)
printf(“%8d”,st[i].score[j]);
printf(“%10.2f”,st[i].ave);
}
fclose(fp);
n=I;
for(t=0;st[t].ave>s.ave+&&t<n;t++);
ptintf(“\nnow:\n”);
fp=fopen(“stu_sort”,”w”);
for(I=0;I<t;I++)
{fwrite(&st[i],sizeof(struct student),1,fp);
printf(“\n%9s%8s%8d%8d%8d%10.2f”,s.num,s.name,s.score[0],s.score[1] s.score[2] s.ave);
for(I=t;I<n;I++)
{fwrit(&sr[i],sizeof(struct srudent),1,fp);
printf(“\n %8s%8s”,st[i].num,st[i].name);
for(j=0;j<3;j++)
printf(“%8d”,st[i].score[j]);
printf(“%10.2f”,st[i].ave);
}
fclose(fp);
}
13.10 有一磁盘文件emploee,内存放职工的数据。每个职工的数据包括:职工姓名、职工号、性别、年龄、住址、工资、健康状况、文化程度。要求将职工名和工资的信息单独抽出来另建一个简明的职工工资文件。
解:#include<stdio.h>
struct emploee
{char num[6];
char name[10];
char sex[2];
int age;
char addr[20];
int salary;
char health[8];
char class[10];
}en[10];
struct emp
{char name[10];
int salary;
}em-case[10];
main()
{FILE *fp1, *fp2;
int I,j;
if ((fp1=fopen(“emploee”,”r”))==NULL)
{printf(“can not open the file.”);
exit(0);
}
printf(“\n NO. name sex age addr salary health class\n”);
for(I=0;fread(&em[i],sizeof(struct emploee),1,fp1)!=p;I++)
{printf(“\n%4s%8s%4s%6s%10s%6s%10s%8s”,em[i].num,em[i].name,em[i].sex, em[i].age, em[i].addr, em[i].salary, em[i].health, em[i].class);
strcpy(em_case[i].name, em[i].name);
em_case[i].salary=en[i].salary;
}
printf(“\n\n*****************************************”);
if((fp2=fopen(“emp_salary”,”wb”))==NULL)
{printf(“can not open the file.”);
exit(0);}
for(j=0;j<I;j++)
{if(fwrite(&en_case[j],sizeof(struct emp),1,fp2)!=1)
printf(“error!”);
printf(“\n %12s%10d”,em_case[j].name,em_case[j].salary);
}
printf(“\n*******************************************”);
fclose(fp1);
fclose(fp2);
}
13.11从上题的“职工工资文件”中删区一个职工的数据,再存回原文件。
解:#incude <stdio.h>
#incude <string.h>
struct emploee
{char name[10];
int salary;
}emp[20];
main()
{FILE *fp;
int I,j,n,flag;
char name[10];
int salary;
if((fp=fopen(“emp_salary”,”rb”))==NULL)
{printf(“can not open file.”);
exit(0);
}
printf(“\n original data:”);
for(I=0;fead(&emp[i],sizeof(struct emploee),1,fp)!=0;I++)
printf(“\n %8s %7d”,emp[i].name,emp[i].salary);
fclose(fp);
n=I;
printf(“\n input name deleted:”);
scanf(“%s”,name);
for(flag=1,I=0;flag&&I<n;I++)
{if(strcmp(name,emp[i].name)==0)
{for(j=I;j<n-1;j++)
{strcmp(name,emp[i].name)==0
{for(j=I;j<n-1;j++)
{strcpy(emp[j].name,emp[j+1].name);
emp[j].salary=emp[j+1].salary;
}
flag=0;
}
}
if(!flag)
n=n-1;
else
printf(“\n Now,the content of file:\n”);
fp=fopen(“emp-dalary”,”wb”);
for(I=p;I<n;I++)
fwrite(&emp[i],sizeof(struct emploee),1,fp);
fclose(fp);
fp=fopen(“emp_salary”,”r”);
for(I=0;fread(&emp[i],sezeof(struct emploee),1,fp)!=0;I++)
printf(“\n%8s%7d”,emp[i].name,emp[i].salary);
fclose(fp);
}
13.12 从键盘输入若干行字符(每行长度不等),输入后把它们存储到一磁盘文件中。再从该文件中读入这些数据,将其中小写字母转换成大写字母后在显示屏上输出。
解: #include<stdio.h>
main()
{int I,flag;
char str[80],c;
FILE *fp;
Fp=fopen(“text”,”w”);
Flag=1;
While(flag==1)
{printf(“\n Input string:\n”);
ges(str);
fprintf(fp,”%s”,str);
printf(“\nContinue?”);
c=getchar();
if((c==’N’)||(c==’n’))
flag=0;
getchar();
}
fcolse()fp;
fp=fopen(“text”,”r”);
while(fscanf(fp,”%s”,str)!=EOF)
{for(I=0;str[i]!=’\0’;I++)
if((str[i]>=’a’)&& (str[i]<=’z’))
str[i]-=32;
printf(“\n%s\n”,str);
}
fclose(fp);
}