c语言选修某课程的学生有5人,学生的信息包括学号、姓名、性别、出生年月日,按学号由低到高对学生排序,输出排序后的学生信息。
时间: 2024-03-08 21:47:12 浏览: 20
好的,这道题目我可以帮你解答。下面是 C 语言的代码:
```c
#include <stdio.h>
#include <string.h>
struct student {
int id;
char name[20];
char gender[5];
char birth[11];
};
void sort(struct student *s, int n)
{
int i, j;
struct student temp;
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (s[i].id > s[j].id) {
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
}
}
int main()
{
int i, n = 5;
struct student s[5] = {
{ 3, "张三", "男", "1998-03-04" },
{ 1, "李四", "男", "1999-05-06" },
{ 4, "王五", "男", "2000-07-08" },
{ 2, "赵六", "女", "2001-09-10" },
{ 5, "钱七", "女", "2002-11-12" }
};
sort(s, n);
printf("学号\t姓名\t性别\t出生年月日\n");
for (i = 0; i < n; i++) {
printf("%d\t%s\t%s\t%s\n", s[i].id, s[i].name, s[i].gender, s[i].birth);
}
return 0;
}
```
这里定义了一个 `student` 结构体,包含了学生的学号、姓名、性别和出生年月日四个字段。然后通过 `sort` 函数对学生按照学号进行排序。最后在 `main` 函数中输出排序后的学生信息表格。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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_column_c1.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)