69,371
社区成员
发帖
与我相关
我的任务
分享
/*
将题4.cpp已排序的学生成绩文件进行插入处理。插入一个学生的3门课程成绩,程序先计算新插入学生的平均成绩,然后将它按成绩高低顺序插入,
插入后建立一个新文件
*/
/*本题是5.cpp*/
#include <stdio.h>
struct student
{
char num[5];
char name[8];
int score[3];
float ave;
}st[5],s;
/*
st[5]这个地方,书上答案是st[10],我在4.cpp文件中建了5个学生信息,在这写哪个好,我写stu[5]没出错
说明:4.cpp是将5个学生按平均分排序
*/
void main()
{
int i,j,sum,n,t;
FILE *fp,*fp1;
printf("请输入学生信息:\n");
printf("No.:");
scanf("%s",s.num);
printf("姓名:");
scanf("%s",s.name);
sum=0;
for(j=0;j<3;j++)
{
printf("分数%d:",j+1);
scanf("%d",&s.score[j]);
sum+=s.score[j];
}
s.ave=sum/3.0; /*这报错,说不能from 'double' to 'float',加'f'就行了,但我在别的题中就不用加,这是为什么*/
printf("\n");
/*读取*/
if((fp=fopen("stu_sort","r"))==NULL)
{
printf("文件不能打开\n");
exit(0); /*这报错,说"undeclared identifier",我把它注释才能通过,这是为什么,感觉exit不需要定义什么啊*/
}
printf("原数据:\n");
printf("No. 姓名 分数1 分数2 分数3 平均分\n");
for(i=0;fread(&st[i],sizeof(struct student),1,fp)!=0;i++)
printf("%s%6s%8d%8d%8d%10.2f\n",st[i].num,st[i].name,st[i].score[0],st[i].score[1],st[i].score[2],st[i].ave);
printf("\n");
fclose(fp);
n=i;
for(t=0;st[t].ave>s.ave && t<n;t++);
/*向文件写数据*/
printf("添加后:\n");
fp1=fopen("sort_add","w");
printf("No. 姓名 分数1 分数2 分数3 平均分\n");
for(i=0;i<t;i++)
{
fwrite(&st[i],sizeof(struct student),1,fp1);
printf("%s%6s%8d%8d%8d%10.2f\n",st[i].num,st[i].name,st[i].score[0],st[i].score[1],st[i].score[2],st[i].ave);
}
fwrite(&s,sizeof(struct student),1,fp1);
printf("%s%6s%8d%8d%8d%10.2f\n",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("%s%6s%8d%8d%8d%10.2f\n",st[i].num,st[i].name,st[i].score[0],st[i].score[1],st[i].score[2],st[i].ave);
}
fclose(fp);
fclose(fp1);
/*
我每次第一次加完,再加第二次,第一次就没了,打个比方,一开始有5个学生,我第一次加了一个a学生,加完后显示a学生和以前5个学生,然后退出,再打开编译器,
加一个b学生,加完后显示的是b和已开始的5个学生,a学生不见了,感觉像是被覆盖了,这是为什么,上面不是已经把加入的学生写进文件里了吗
*/
}
/*本题是5.cpp*/
#include <stdlib.h>//--------这里添加头文件就不报错了exit就没问题了
#include <stdio.h>
struct student
{
char num[5];
char name[8];
int score[3];
float ave;
}st[5],s;
void main()
{
int i,j,sum,n,t;
FILE *fp,*fp1;
printf("请输入学生信息:\n");
printf("No.:");
scanf("%s",s.num);
printf("姓名:");
scanf("%s",s.name);
sum=0;
for(j=0;j<3;j++)
{
printf("分数%d:",j+1);
scanf("%d",&s.score[j]);
sum+=s.score[j];
}
s.ave=(double)sum/3.0;//----------这里类型不同强制转换一下就可以了
printf("\n");
/*读取*/
if((fp=fopen("stu_sort","r"))==NULL)
{
printf("文件不能打开\n");
exit(0);
}
printf("原数据:\n");
printf("No. 姓名 分数1 分数2 分数3 平均分\n");
for(i=0;fread(&st[i],sizeof(struct student),1,fp)!=0;i++)
printf("%s%6s%8d%8d%8d%10.2f\n",st[i].num,st[i].name,st[i].score[0],st[i].score[1],st[i].score[2],st[i].ave);
printf("\n");
fclose(fp);
n=i;
for(t=0;st[t].ave>s.ave && t<n;t++);
/*向文件写数据*/
printf("添加后:\n");
fp1=fopen("sort_add","w");
printf("No. 姓名 分数1 分数2 分数3 平均分\n");
for(i=0;i<t;i++)
{
fwrite(&st[i],sizeof(struct student),1,fp1);
printf("%s%6s%8d%8d%8d%10.2f\n",st[i].num,st[i].name,st[i].score[0],st[i].score[1],st[i].score[2],st[i].ave);
}
fwrite(&s,sizeof(struct student),1,fp1);
printf("%s%6s%8d%8d%8d%10.2f\n",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("%s%6s%8d%8d%8d%10.2f\n",st[i].num,st[i].name,st[i].score[0],st[i].score[1],st[i].score[2],st[i].ave);
}
fclose(fp);
fclose(fp1);
/*
我每次第一次加完,再加第二次,第一次就没了,打个比方,一开始有5个学生,我第一次加了一个a学生,加完后显示a学生和以前5个学生,然后退出,再打开编译器,
加一个b学生,加完后显示的是b和已开始的5个学生,a学生不见了,感觉像是被覆盖了,这是为什么,上面不是已经把加入的学生写进文件里了吗
*/
}