/*
姓名 : 胥立畅
胥立畅的学号 : 90889
胥立畅的英语成绩 : 90
胥立畅的编程成绩 : 87
姓名 : 李海燕
李海燕的学号 : 90890
李海燕的英语成绩 : 90
李海燕的编程成绩 : 78
姓名 学号 英语 编程 总成绩 平均成绩
胥立畅 90889 90.00 87.00 177.00 88.50
李海燕 90890 90.00 78.00 168.00 84.00
排序后:
姓名 学号 英语 编程 总成绩 平均成绩
胥立畅 90889 90.00 87.00 177.00 88.50
李海燕 90890 90.00 78.00 168.00 84.00
姓名 : 汪大海
汪大海的学号 : 90891
汪大海的英语成绩 : 78
汪大海的编程成绩 : 98
插入后:
姓名 学号 英语 编程 总成绩 平均成绩
胥立畅 90889 90.00 87.00 177.00 88.50
汪大海 90891 78.00 98.00 176.00 88.00
李海燕 90890 90.00 78.00 168.00 84.00
Press any key to continue
*/
#include <stdio.h>
struct student {
char name[10];
char num[20];
float score[4];
};
void Show(struct student *stu,int n) {
int i,j;
printf("姓名\t学号\t英语\t编程\t总成绩\t平均成绩\n");
for(i = 0; i < n; ++i) {
printf("%s\t%s\t",stu[i].name,stu[i].num);
for(j = 0; j < 4; ++j) printf("%.2f\t",stu[i].score[j]);
printf("\n");
}
}
void Sort(struct student *stu,int n) {
int i,j,k;
struct student t;
for(i = 0; i < n - 1; ++i) {
k = i;
for(j = i + 1; j < n; ++j) {
if(stu[k].score[2] < stu[j].score[2])
k = j;
}
if(k != i) {
t = stu[i];
stu[i] = stu[k];
stu[k] = t;
}
}
}
struct student Read(void) {
struct student stu;
printf("姓名 : ");
scanf("%s",stu.name);
printf("%s的学号 : ",stu.name);
scanf("%s",stu.num);
printf("%s的英语成绩 : ",stu.name);
scanf("%f",&stu.score[0]);
printf("%s的编程成绩 : ",stu.name);
scanf("%f",&stu.score[1]);
stu.score[2] = stu.score[0] + stu.score[1]; // 总成绩
stu.score[3] = stu.score[2] / 2.0f; // 平均成绩
return stu;
}
void Insert(struct student *stu,struct student astu,int n) {
int i,j;
for(i = 0; i < n; ++i) {
if(stu[i].score[2] < astu.score[2]) {
for(j = n; j > i; --j) stu[j] = stu[j - 1];
stu[i] = astu;
break;
}
}
if(i == n) stu[n] = astu;
}
int main() {
int i,n = 2;
struct student stu[10],tstu;
for(i = 0; i < n; ++i) stu[i] = Read();
Show(stu,n);
Sort(stu,n);
printf("排序后:\n");
Show(stu,n);
tstu = Read();
Insert(stu,tstu,n++);
printf("插入后:\n");
Show(stu,n);
return 0;
}
温馨提示:答案为网友推荐,仅供参考