#include<stdio.h> #include<string.h> typedef struct nodelink { long num; char name[20]; int Chinese; int Math; int English; float aver; }stu; int main() { stu b[10]; FILE *pf; pf=fopen("stud.txt","a"); stu a[10]; for(int i=0;i<3;i++) { printf("请输入第%d个学生的学号,姓名,语文成绩,数学成绩,英语成绩:\n", i + 1); scanf("%ld", &a[i].num); getchar(); fgets(a[i].name, 20, stdin); a[i].name[strlen(a[i].name) - 1] = '\0'; scanf("%d%d%d", &a[i].Chinese, &a[i].Math, &a[i].English); a[i].aver=(a[i].Chinese+a[i].Math+a[i].English)/3.0; fprintf(pf,"%ld\t%s\t%d\t%d\t%d\t%.1f\n",a[i].num,a[i].name,a[i].Chinese,a[i].Math,a[i].English,a[i].aver); } for(int i=0;i<3;i++) { fscanf(pf,"%ld\t%s\t%d\t%d\t%d\t%.1f\n",&b[i].num,b[i].name,&b[i].Chinese,&b[i].Math,&b[i].English,&b[i].aver); printf("%ld\t%s\t%d\t%d\t%d\t%.1f\n",b[i].num,b[i].name,b[i].Chinese,b[i].Math,b[i].English,b[i].aver); } fclose(pf); FILE *pf1; pf1=fopen("studsort.txt","a"); stu k; for(int i=0;i<3;i++) for(int j=i;j<3;j++) { if(b[i].aver>b[j].aver) { k=b[i]; b[i]=b[j]; b[j]=k; } } for(int i=0;i<3;i++) { fprintf(pf1,"%ld\t%s\t%d\t%d\t%d\t%.1f\n",b[i].num,b[i].name,b[i].Chinese,b[i].Math,b[i].English,b[i].aver); } fclose(pf1); return 0; }错误修改
时间: 2023-12-06 16:41:35 浏览: 107
#include<stdio.h>
#include<stdio.h>
#include<string.h>
typedef struct nodelink
{
long num;
char name[20];
int Chinese;
int Math;
int English;
float aver;
}stu;
int main()
{
stu a[10];
FILE *pf;
pf=fopen("stud.txt","w"); // 修改为写入模式
for(int i=0;i<3;i++)
{
printf("请输入第%d个学生的学号,姓名,语文成绩,数学成绩,英语成绩:\n", i + 1);
scanf("%ld", &a[i].num);
getchar();
fgets(a[i].name, 20, stdin);
a[i].name[strlen(a[i].name) - 1] = '\0';
scanf("%d%d%d", &a[i].Chinese, &a[i].Math, &a[i].English);
a[i].aver=(a[i].Chinese+a[i].Math+a[i].English)/3.0;
fprintf(pf,"%ld\t%s\t%d\t%d\t%d\t%.1f\n",a[i].num,a[i].name,a[i].Chinese,a[i].Math,a[i].English,a[i].aver);
}
fclose(pf);
FILE *pf1;
pf1=fopen("stud.txt","r"); // 修改为读取模式
stu b[10]; // b 数组定义位置修改到文件读取后
for(int i=0;i<3;i++)
{
fscanf(pf1,"%ld\t%s\t%d\t%d\t%d\t%f\n",&b[i].num,b[i].name,&b[i].Chinese,&b[i].Math,&b[i].English,&b[i].aver);
printf("%ld\t%s\t%d\t%d\t%d\t%.1f\n",b[i].num,b[i].name,b[i].Chinese,b[i].Math,b[i].English,b[i].aver);
}
fclose(pf1);
FILE *pf2;
pf2=fopen("studsort.txt","w"); // 修改为写入模式
stu k;
for(int i=0;i<3;i++)
for(int j=i;j<3;j++)
{
if(b[i].aver>b[j].aver)
{
k=b[i];
b[i]=b[j];
b[j]=k;
}
}
for(int i=0;i<3;i++)
{
fprintf(pf2,"%ld\t%s\t%d\t%d\t%d\t%.1f\n",b[i].num,b[i].name,b[i].Chinese,b[i].Math,b[i].English,b[i].aver);
}
fclose(pf2);
return 0;
}
阅读全文