一、实验题目:
- 问题描述:综合设计一个程序,要求存储每个学生的信息,包括学号、姓名、性别、出生日期、电话、5门课程的分数等。要求:
(1) 输入学生信息,计算每个学生的平均分,并能分别按平均分和年龄(不考虑月份)从高到低排序,在屏幕上输出排序后的学生信息;
(2) 将有不及格科目的学生信息写入文件fail.txt中。
二、实验代码:
#include<stdio.h>
struct student {
char num[20];
char name[20];
char sex[10];
int date;
double phonenum;
int score1,score2,score3,score4,score5;
double average;
};
int main()
{
int i,n,a,j,k,m=0;
FILE *fp;
struct student students[50],temp;
printf("请输入学生人数:");
scanf("%d",&n);
for(i=0;i<n;i++) {
printf("请输入第%d个学生的信息\n",i+1);
printf("学号:");
scanf("%s",&students[i].num);
printf("姓名:");
scanf("%s",&students[i].name) ;
printf("性别(boy//girl):");
scanf("%s",&students[i].sex);
printf("出生日期:");
scanf("%d",&students[i].date);
printf("电话号码:");
scanf("%lf",&students[i].phonenum);
printf("成绩1:");
scanf("%d",&students[i].score1);
printf("成绩2:");
scanf("%d",&students[i].score2);
printf("成绩3:");
scanf("%d",&students[i].score3);
printf("成绩4:");
scanf("%d",&students[i].score4);
printf("成绩5:");
scanf("%d",&students[i].score5);
students[i].average=(students[i].score1+students[i].score2+students[i].score3+students[i].score4+students[i].score5)/5.0;
}
for(i=0;i<n-1;i++){
a=i;
for(j=i+1;j<n;j++){
if(students[j].average>students[a].average){
a=j;
}
}
temp=students[a];
students[a]=students[i];
students[i]=temp;
}
printf("按照平均成绩从大到小排列:\n");
printf("学号\t\t姓名\t性别\t出生日期\t电话号码\t成绩1\t成绩2\t成绩3\t成绩4\t成绩5\t平均成绩\n");
for(i=0;i<n;i++){
printf("%s\t%s\t%s\t%d\t%.0lf\t%d\t%d\t%d\t%d\t%d\t%.2lf\n",students[i].num,students[i].name,students[i].sex,students[i].date,students[i].phonenum,students[i].score1,students[i].score2,students[i].score3,students[i].score4,students[i].score5,students[i].average);
}
for(i=0;i<n-1;i++){
a=i;
for(j=i+1;j<n;j++){
if((students[j].date/10000)<(students[a].date/10000)){
a=j;
}
}
temp=students[a];
students[a]=students[i];
students[i]=temp;
}
printf("按照出生年份从大到小排列:\n");
printf("学号\t\t姓名\t性别\t出生日期\t电话号码\t成绩1\t成绩2\t成绩3\t成绩4\t成绩5\t平均成绩\n");
for(i=0;i<n;i++){
printf("%s\t%s\t%s\t%d\t%.0lf\t%d\t%d\t%d\t%d\t%d\t%.2lf\n",students[i].num,students[i].name,students[i].sex,students[i].date,students[i].phonenum,students[i].score1,students[i].score2,students[i].score3,students[i].score4,students[i].score5,students[i].average);
}
for(i=0;i<n;i++){
if(students[i].score1<60||students[i].score2<60||students[i].score3<60||students[i].score4<60||students[i].score5<60){
if(m==0){
fp=fopen("d:\\fail.txt","wb");
fprintf(fp,"学号\t\t姓名\t性别\t出生日期\t\t电话号码\t\t成绩1\t成绩2\t成绩3\t成绩4\t成绩5\t平均成绩");
fprintf(fp,"\r\n");
m++;
}
fprintf(fp,"%s\t%s\t%s\t%d\t%.0lf\t%d\t%d\t%d\t%d\t%d\t%.2lf\r\n",students[i].num,students[i].name,students[i].sex,students[i].date,students[i].phonenum,students[i].score1,students[i].score2,students[i].score3,students[i].score4,students[i].score5,students[i].average);
}
}
if(m==1){
fclose(fp);
}
return 0;
}