读书人

惭愧啊又卡住了解决办法

发布时间: 2012-02-12 17:16:34 作者: rapoo

惭愧啊,又卡住了
小弟在学老谭C,文件这一章的习题里。有5个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号、姓名、三门课成绩),计算出平均成绩,将原有数据和计算出的平均分数存放在磁盘文件stud中。
#include <stdio.h>
#include <stdlib.h>
#define SIZE 5
struct student
{
char name[8];
int num;
int score[3];
float ave;
}stud[SIZE];
void main()
{
void save(void);
int i;
float sum[SIZE];
FILE*fp1;
for (i=0;i<SIZE;i++)
{
scanf("%s %d %d %d %d",stud[i].name,&stud[i].num,&stud[i].score[0],&stud[i].score[1],&stud[i].score[2]);
sum[i]=stud[i].score[0]+stud[i].score[1]+stud[i].score[2];
stud[i].ave=sum[i]/3;
}
save();
fp1=fopen("stu.dat","rb");
printf("\n name NO. score1 score2 score3 ave\n");
for (i=0;i<SIZE;i++)
{
fread(&stud[i],sizeof(struct student),1,fp1);
printf(" %-9s %3d %7d %7d %7d %12.2f\n",stud[i].name,stud[i].num,stud[i].score[0],stud[i].score[1],stud[i].score[2],stud[i].ave);
}
fclose(fp1);
}
void save(void)
{
FILE*fp;
int i;
if((fp=fopen("stu.dat","wb"))==NULL)
{
printf("Can not open the file\n");
return;
}
for(i=0;i<SIZE;i++)
if(fwrite(&stud[i],sizeof(struct student),1,fp)!=1)
{
printf("File write error\n");
return;
}
fclose(fp);
}
可以编译通过。但是下一题,将上题stud文件中的学生数据按平均分进行排序处理,并将已排序的学生数据存入一个新文件stu_sort中。
#include <stdio.h>
#include <stdlib.h>
#define SIZE 5
struct student
{
char name[10];
int num;
int score[3];
float ave;
}stud[SIZE],work;
void main()
{
void sort(void);
int i;
FILE*fp;
sort();
fp=fopen("stud_sort.dat","rb");
printf("Sorted student's scores list is as follows\n");
printf("---------------------------------------------------\n");
printf("NAMENO.SCORE1SCORE2SCORE3AVE\n");
printf("---------------------------------------------------\n");
for(i=0;i<SIZE;i++)
{
fread(&stud[i],sizeof(struct student),1,fp);
printf("%-10s %3d %8d %8d %8d %9.2f\n",stud[i].name,stud[i].num,stud[i].score[0],stud[i].score[1],stud[i].score[2],stud[i].ave);
}
fclose(fp);
}
void sort(void)
{
FILE*fp1,*fp2;
int i,j;
if((fp1=fopen("stu.dat","rb"))==NULL)
{
printf("The file can not open the file\n");
exit(0);
}

for (i=0;i<SIZE;i++)
if (fread(&stud[i],sizeof(struct student),1,fp1)!=1)
{
printf("file read error\n");
exit(0);
}

if((fp2=fopen("stud_sort.dat","wb"))==NULL)
{
printf("The file write error\n");
exit(0);
}
for (i=0;i<SIZE;i++)
{
for(j=i+1;j<SIZE;j++)
if(stud[i].ave<stud[j].ave)
{
work=stud[i];
stud[i]=stud[j];
stud[j]=work;
}
fwrite(&stud[i],sizeof(struct student),1,fp2);
}
fclose(fp1);
fclose(fp2);
}
就出问题了,请大家赐教。谢谢!

[解决办法]
搜索了一下,给个参考:

有5个学生,每个学生有三门课程的成绩,从键盘输入学生数据(包括学号,姓名,3门课程成绩),计算出平均数据,将原有数据和计算出的平均分数存放在磁盘文件中:

#include "stdio.h"
#include "stdlib.h"
#include "soft_info.h"
#define LEN 30

typedef struct student
{
int num;
char name[LEN];
float score[3];
float aver;
} student;

void main()
{
char filename[20];
FILE *fp;
int i;
float temp;
student stu[5],*p=stu;
soft_info();
printf("Please enter the information of all students:\n");


for(;p<stu+5;p++)
{
printf("\nInformation of Student %d:\n",p-stu+1);
printf("Student ID:\n");
scanf("%d",&p->num);
getchar();
printf("Student Name:\n");
gets(p->name);
printf("Three scores:\n");
for(i=0;i<3;i++)
scanf("%f",&p->score[i]);
getchar();temp=0;
for(i=0;i<3;i++)
temp=temp+p->score[i];
p->aver=temp/3;
}
printf("\nPlease enter the file name:\n");
gets(filename);
if((fp=fopen(filename,"wb"))==NULL)
{
printf("Failure in opening the file!\n");
exit(0);
}
for(p=stu;p<stu+5;p++)
fwrite(p,sizeof(student),1,fp);
fclose(fp);
}

将文件中的数据读入后按平均分排序,写入一个新文件中:

#include "stdio.h"
#include "stdlib.h"
#include "soft_info.h"
#define LEN 30

typedef struct student
{
int num;
char name[LEN];
float score[3];
float aver;
} student;

void main()
{
char filename[20];
FILE *fp,*out;
int i,j;
student stu[5],tem,*p=stu;
if((fp=fopen("py.dat","rb"))==NULL)
{
printf("Failure in opening the file!\n");
exit(0);
}
for(;p<stu+5;p++)
{
if(fread(p,sizeof(student),1,fp)!=1)
printf("File Reading Error!\n");
}
p=stu;
for(j=0;j<4;j++)
{
for(i=0;i<4-j;i++)
{
if((p+i)->aver<(p+i+1)->aver)
{
tem=*(p+i);
*(p+i)=*(p+i+1);
*(p+i+1)=tem;
}
}
}
printf("Please enter the new file name:\n");
gets(filename);
if((out=fopen(filename,"wb"))==NULL)
{
printf("Failure in creating the file!\n");
exit(0);
}
for(;p<stu+5;p++)
{
if(fwrite(p,sizeof(student),1,out)!=1)
printf("File writing error!\n");
}
fclose(fp);
fclose(out);
}

读书人网 >C++

热点推荐