有5个学生,每个学生有3门课程的成绩,编写成绩管理程序,要求:用c语言完成

2024-11-06 16:34:59
推荐回答(1个)
回答1:

#include
#include
#include
#define NAMELEN 10
#define IDLEN 20
typedef struct student
{
char name[NAMELEN];
char ID[IDLEN];
double first;
double second;
double third;
double average;
}STU;
STU stu[6];
int cmp( const void *a ,const void *b)
{
return (*(STU *)a).average > (*(STU *)b).average ? 1 : -1;
}
int main()
{
int i;
FILE *_fp=fopen("student.txt","w"),*fp=fopen("sort.txt","w"),*fp_=fopen("sort2.txt","w");

for(i=0;i<5;i++)
{
printf("输入第%d个学生的学号、姓名及三门课的成绩:\n",i+1);
scanf("%s%s%lf%lf%lf",stu[i].ID,stu[i].name,&stu[i].first,&stu[i].second,&stu[i].third);
stu[i].average=(stu[i].first+stu[i].second+stu[i].third)/3.0;
fprintf(_fp,"%s %s %.2lf %.2lf %.2lf %.2lf\n",stu[i].ID,stu[i].name,stu[i].first,stu[i].second,stu[i].third,stu[i].average);
}

qsort(stu,5,sizeof(stu[0]),cmp);

for(i=0;i<5;i++)
{
fprintf(fp,"%s %s %.2lf %.2lf %.2lf %.2lf\n",stu[i].ID,stu[i].name,stu[i].first,stu[i].second,stu[i].third,stu[i].average);
}

printf("增加一位学生的学号、姓名及三门课的成绩:\n");
scanf("%s%s%lf%lf%lf",stu[5].ID,stu[5].name,&stu[5].first,&stu[5].second,&stu[5].third);
stu[5].average=(stu[5].first+stu[5].second+stu[5].third)/3.0;
qsort(stu,6,sizeof(stu[0]),cmp);
for(i=0;i<6;i++)
{
fprintf(fp_,"%s %s %.2lf %.2lf %.2lf %.2lf\n",stu[i].ID,stu[i].name,stu[i].first,stu[i].second,stu[i].third,stu[i].average);

}

fclose(_fp);
fclose(fp);
fclose(fp_);

return 0;
}