您的位置:首页 > 编程语言

有5名学生保存在结构体数组中,编程按学生的成绩升序排序,按学生的姓名降序排序,按年龄从低到高排序, 成绩, 年龄

2014-07-24 19:54 781 查看
typedef struct stu {
char name[20];//存储学生姓名
char sex;//存储学生性别
int age;//存储学生年龄
float score;
}Stu;

Stu stu[5] = {
{"mengmeng", 'N',38,
100},
{"shuaishuai", 'G',28,
10},
{"leilei", 'M',21,
23},
{"mingming", 'G',20,
39},
{"honghong", 'G',12,
101}
};
//输出结构体的结构体数组成员
void printAllStudentInfo(Stu a[],int count);//a用来接收结构体数组, count用来接收数组元素的个数
void sortStudentScore(Stu a[],int count);//按成绩排序
void sortStudentAge(Stu a[],int count);//按年龄排序
void sortStudentName(Stu a[],int count);//按姓名排序
void printAllStudentInfo(Stu a[],int count) {
for (int i =
0; i < count; i++) {
printf("name : %s, sex : %c, age : %d, score : %.2f\n", a[i].name, a[i].sex,
a[i].age, a[i].score);
}
}//打印所有学生的信息

void sortStudentScore(Stu a[],int count) {
Stu tempStudent = {0};
for (int i =
0; i < 4; i++) {
for (int j =0; j <
4 - i; j++) {
if (a[j].score > a[j +1].score) {
tempStudent = a[j + 1];
a[j + 1] = a[j];
a[j] = tempStudent;
}
}
}
}//按成绩排序

void sortStudentAge(Stu a[],int count) {
Stu tempStudent = {0};
for (int i =
0; i < 4; i++) {
for (int j =0; j <
4 - i; j++) {
if (a[j].age > a[j +1].age) {
tempStudent = a[j + 1];
a[j + 1] = a[j];
a[j] = tempStudent;
}
}
}
}//按年龄排序

void sortStudentName(Stu a[],int count) {
Stu tempStudent = {0};
for (int i =
0; i < 4; i++) {
for (int j =0; j <
4 - i; j++) {
if (strcmp(tempStudent.name, a[i].name) <0) {
tempStudent = a[j + 1];
a[j + 1] = a[j];
a[j] = tempStudent;
}
}
}
}//按姓名排序
printf("原结构体学生列表为:\n");
printAllStudentInfo(stu,5);
printf("按学生的成绩升序排序为:\n");
sortStudentScore(stu,
5);
printAllStudentInfo(stu,5);
printf("按学生的姓名降序排序排序为:\n");
sortStudentName(stu,
5);
printAllStudentInfo(stu,5);
printf("按学生的年龄从升序排序排序为:\n");
sortStudentAge(stu,
5);
printAllStudentInfo(stu,5);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐