2.题目:编一个C程序,它能读入一个学生登记表(学生数<100=,并把它按学号从小到大排序后输出,每个学生登记项包含学号(int型),姓名(字符串),3门课程成绩(均为浮点型),当读到学号值为-1时,表示输入结束,-1不算在内。输入时,相邻两数据间用空各隔开。(注:可执行文件名为e8_2.exe)
程序设计:
#include "string.h"
struct student
{long num;
char name[10];
float score[3];
};
main()
{struct student s[100],x;
int i=0,j,k,n,m;
printf("Please enter student data:\n");
for (i=0;i<10;i++)
{scanf("%ld",&s[i].num);
if (s[i].num==-1) break;
scanf("%s",s[i].name);
for (m=0;m<3;m++) scanf("%f",&s[i].score[m]);
}
n=i;
for (k=1;k<n;k++)
for (j=n-1,i=0;i<n-k;i++,j--)
if (s[j].num<s[j-1].num)
{x.num=s[j].num;
strcpy(x.name,s[j].name);
for (m=0;m<3;m++) x.score[m]=s[j].score[m];
s[j].num=s[j-1].num;
strcpy(s[j].name,s[j-1].name);
for (m=0;m<3;m++) s[j].score[m]=s[j-1].score[m];
s[j-1].num=x.num;
strcpy(s[j-1].name,x.name);
for (m=0;m<3;m++) s[j-1].score[m]=x.score[m];
}
printf("Number Name Score1 Score2 Score3\n");
for (i=0;i<n;i++)
printf("%-8ld%-10s%-8.0f%-8.0f%-8.0f\n",s[i].num,s[i].name,s[i].score[0],s[i].score[1],s[i].score[2]);
}