定义结构体,存储学生姓名、分数、出生年月日,输入5个学生的信息,按分数降序输出信息。
时间: 2024-05-06 14:15:46 浏览: 23
下面是一个示例代码,实现了题目要求的功能:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20
// 定义学生结构体
struct student {
char name[MAX_NAME_LEN];
int score;
int year;
int month;
int day;
};
// 比较函数,用于qsort排序
int compare(const void *a, const void *b) {
struct student *s1 = (struct student *) a;
struct student *s2 = (struct student *) b;
return s2->score - s1->score;
}
int main() {
struct student stu[5];
int i;
// 输入学生信息
for (i = 0; i < 5; i++) {
printf("请输入第%d个学生的信息:\n", i + 1);
printf("姓名:");
scanf("%s", stu[i].name);
printf("分数:");
scanf("%d", &stu[i].score);
printf("出生年月日(格式为yyyy mm dd):");
scanf("%d %d %d", &stu[i].year, &stu[i].month, &stu[i].day);
}
// 按分数降序排序
qsort(stu, 5, sizeof(struct student), compare);
// 输出学生信息
printf("按分数降序排列的学生信息如下:\n");
for (i = 0; i < 5; i++) {
printf("姓名:%s,分数:%d,出生年月日:%d年%d月%d日\n",
stu[i].name, stu[i].score, stu[i].year, stu[i].month, stu[i].day);
}
return 0;
}
```
运行结果示例:
```
请输入第1个学生的信息:
姓名:Tom
分数:90
出生年月日(格式为yyyy mm dd):2001 3 14
请输入第2个学生的信息:
姓名:Jack
分数:80
出生年月日(格式为yyyy mm dd):2002 4 15
请输入第3个学生的信息:
姓名:Lucy
分数:95
出生年月日(格式为yyyy mm dd):2001 2 1
请输入第4个学生的信息:
姓名:Mike
分数:85
出生年月日(格式为yyyy mm dd):2003 6 12
请输入第5个学生的信息:
姓名:Lily
分数:92
出生年月日(格式为yyyy mm dd):2000 9 23
按分数降序排列的学生信息如下:
姓名:Lucy,分数:95,出生年月日:2001年2月1日
姓名:Lily,分数:92,出生年月日:2000年9月23日
姓名:Tom,分数:90,出生年月日:2001年3月14日
姓名:Mike,分数:85,出生年月日:2003年6月12日
姓名:Jack,分数:80,出生年月日:2002年4月15日
```
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)