【C和C++】输入学生信息,创立文件,实现排序,插入

#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,l;
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<4;j++)
{
         k=j;
         for(l=j+1;l<6;l++)
                   if(stu[k].aver>stu[l].aver)k=l;
                   if(k!=j)
                   {
                            temp=stu[j];
                            stu[j]=stu[k];
                            stu[k]=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();
}

 

posted on 2015-01-13 16:27  雪原那么远  阅读(377)  评论(0编辑  收藏  举报

导航