计算机教程
le ‘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 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 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
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 #incude struct emploee {char name[10]; int salary; }emp[20]; main() {FILE *fp; int