c语言作业通信录按年龄排序,C语言,有5名学生保存在结构体数组中,编程按学生的成绩升序排序,按学生的姓名降序排序,按年龄从低到高排序...

学生信息排序
本文介绍了一个使用C语言编写的程序,该程序能够对5名学生的信息(包括姓名、成绩和年龄)进行不同条件下的排序,包括成绩升序、姓名降序及年龄升序排序。

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

//我把声明文件放在.h文件中

//把函数实现部分放在.m文件中

//最后的函数调用放在主函数main中,代码如下

.h文件

typedef struct student{

char name[10]; //存储学生姓名

float score; //存储学生成绩

int age; //存储学生年龄

}Stud;

//输出结构体成员

void outputStudent(Stud stu);

//输出所有成员的信息

void outputAllStudent(Stud b[], int count);.m文件

//输出结构体成员

void outputStudent(Stud stu){

printf("Name = %s Score = %.2f Age = %d", stu.name, stu.score, stu.age);

}

//输出所有学生的信息

void outputAllStudent(Stud b[], int count){

for (int i = 0; i < count; i++) {

printf("\n");

outputStudent(b[i]);

}

}主函数main内

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

Stud a[5] ={

{"oubama", 59.9, 44},

{"xilayi", 76.0, 55},

{"baideng",49.01, 50},

{"bushen",71.9, 66},

{"kelindun",83.3, 33},

};

//输出所有学生结构体成员

printf("输出前:");

outputAllStudent(a, 5);

printf("\n");

//1)编程按学生的成绩升序排序

printf("成绩生序排序后为:");

for (int i = 0; i < 5 - 1; i++) {

for (int j = 0; j < 5 - 1 - j; j++) {

if (a[j].score > a[j + 1].score) {

Stud tempStud = {0};

tempStud = a[j];

a[j] = a[j + 1];

a[j + 1] = tempStud;

}

}

}

outputAllStudent(a, 5);

//2)按学生的姓名降序排序

printf("\n按学生姓名排序后:");

for (int i = 0; i < 5 - 1; i++) {

for (int j = 0; j < 5 - 1 - i; j++) {

if (strcmp(a[j].name, a[j + 1].name) < 0) {

Stud tempStud = {0};

tempStud = a[j];

a[j] = a[j + 1];

a[j + 1] = tempStud;

}

}

}

outputAllStudent(a, 5);

//2)按年龄从低到高排序

printf("\n按年龄从低到高排序后:");

for (int i = 0; i < 5 - 1; i++) {

for (int j = 0; j < 5 - 1 - i; j++) {

if (a[j].age > a[j + 1].age) {

Stud tempStud = {0};

tempStud = a[j];

a[j] = a[j + 1];

a[j + 1] = tempStud;

}

}

}

outputAllStudent(a, 5);

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值