P354.输入学生信息,创立文件,实现排序,插入
#include<stdio.h> #include<stdlib.h>
struct student { char name[10]; int num; float score_1; float score_2; float score_3; float aver; }stu[6],temp;
void save() {FILE*fp; int i; if((fp=fopen ("stu.dat","wb"))==NULL) { printf("cannot open file1\n"); return; } for(i=0;i<5;i++) if(fwrite(&stu[i],sizeof(struct student),1,fp)!=1) printf("file write error\n"); fclose(fp); }
void save_1() {FILE*fp; int i; if((fp=fopen ("stu_sort.dat","wb"))==NULL) { printf("cannot open file4\n"); return; } for(i=0;i<5;i++) if(fwrite(&stu[i],sizeof(struct student),1,fp)!=1) printf("file write error\n"); fclose(fp); }
void save_2() {FILE*fp; int i; if((fp=fopen ("aver.dat","wb"))==NULL) { printf("cannot open file7\n"); return; } for(i=0;i<5;i++) if(fwrite(&stu[i],sizeof(struct student),1,fp)!=1) printf("file write error\n"); fclose(fp); }
void print() {int i; FILE*fp; if((fp=fopen("stu.dat","rb"))==NULL) { printf("cannot open file2\n"); exit(0); } for(i=0;i<5;i++) { fread(&stu[i],sizeof(struct student),1,fp); printf("%s\t%d\t%.2lf\t%.2lf\t%.2lf\t%.2lf\n",stu[i].name,stu[i].num,stu[i].score_1,stu[i].score_2,stu[i].score_3,stu[i].aver); } fclose(fp); }
void print_1() {int i; FILE*fp; if((fp=fopen("stu_sort.dat","rb"))==NULL) { printf("cannot open file5\n"); exit(0); } printf("the aver is\n"); for(i=0;i<5;i++) { fread(&stu[i],sizeof(struct student),1,fp); printf("%s\t%d\t%.2lf\t%.2lf\t%.2lf\t%.2lf\n",stu[i].name,stu[i].num,stu[i].score_1,stu[i].score_2,stu[i].score_3,stu[i].aver); } fclose(fp); }
void print_2() {int i; FILE*fp; if((fp=fopen("aver.dat","rb"))==NULL) { printf("cannot open file8\n"); exit(0); } for(i=0;i<5;i++) { fread(&stu[i],sizeof(struct student),1,fp); printf("%s\t%d\t%.2lf\t%.2lf\t%.2lf\t%.2lf\n",stu[i].name,stu[i].num,stu[i].score_1,stu[i].score_2,stu[i].score_3,stu[i].aver); } fclose(fp); }
void aver() {int i,j,k,l,m; FILE*fp; if((fp=fopen("stu.dat","rb"))==NULL) { printf("cannot open file3\n"); exit(0); } for(i=0;i<5;i++) fread(&stu[i],sizeof(struct student),1,fp); fclose(fp); for(j=0;j<4;j++) { k=j; for(l=j+1;l<5;l++) if(stu[k].aver>stu[l].aver)k=l; if(k!=j) { temp=stu[j]; stu[j]=stu[k]; stu[k]=temp; } } }
void insert() { int i,j,k; FILE*fp; if((fp=fopen("stu_sort.dat","rb"))==NULL) { printf("cannot open file6\n"); exit(0); } for(i=0;i<5;i++) fread(&stu[i],sizeof(struct student),1,fp); fclose(fp); printf("please insert data of students num,score_1,score_2,score_3,name\n"); scanf("%d,%f,%f,%f,%s",&stu[5].num,&stu[5].score_1,&stu[5].score_2,&stu[5].score_3,stu[5].name); stu[5].aver=(stu[5].score_1+stu[5].score_2+stu[5].score_3)/3; for(j=0;j<5;j++) { if(stu[5].aver<stu[j].aver);break; } temp=stu[5]; for(k=5;k>(j+1);k--) { stu[k]=stu[k-1]; } stu[j+1]=temp; } void main() { int i; printf("please enter data of students num,score_1,score_2,score_3,name\n"); for(i=0;i<5;i++) { scanf("%d,%f,%f,%f,%s",&stu[i].num,&stu[i].score_1,&stu[i].score_2,&stu[i].score_3,stu[i].name); stu[i].aver=(stu[i].score_1+stu[i].score_2+stu[i].score_3)/3; } save(); print(); aver(); save_1(); print_1(); insert(); save_2(); print_2(); }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。