void name(struct student *A)//用结构体指针做参数,用临时结构体进行交换。 { struct student temp; int i=0,j=0; for( i=0;i<number-1;i++) { for( j=0;j<number-i-1;j++) { if(A[j].name[0]>A[j+1].name[0]) { temp=A[j];//排序后的结构体数组返回给调用者 A[j]=A[j+1]; A[j+1]=temp; } } } for(i=0;i<number;i++) { printf("%d-%d-%d %d-%d %s %s %s\n",A[i].year,A[i].month,A[i].day,A[i].start,A[i].end,A[i].arr,A[i].name,A[i].type); }冒泡法翻译
时间: 2024-04-21 21:25:10 浏览: 15
这段代码使用了冒泡排序法来对一个结构体数组进行排序,排序的依据是结构体中的一个名为 `name` 的字符串。下面是对这段代码的翻译:
1. 定义一个临时的结构体 `temp`,以便在交换结构体数组中的两个元素时使用。
2. 使用两个循环嵌套,对结构体数组进行冒泡排序。
3. 外层循环从第一个元素开始遍历到倒数第二个元素,内层循环从第一个元素开始遍历到倒数第二个元素(由于内层循环在每次外层循环中都会遍历到已经排好序的元素,因此内层循环的范围逐次缩小)。
4. 每次内层循环比较相邻的两个元素的 `name` 字符串的第一个字符,如果前一个元素的 `name` 字符串的第一个字符大于后一个元素的 `name` 字符串的第一个字符,则交换这两个元素的位置。
5. 遍历完整个数组后,使用循环输出排好序的结构体数组中的每个元素的信息,包括 `year`、`month`、`day`、`start`、`end`、`arr`、`name` 和 `type` 字段。
总体来说,这段代码的功能是对一个结构体数组按照某个字段进行排序,并将排好序的数组返回给调用者。
相关问题
根据上文补全代码。#include<stdio.h> struct student { char number[15]; char name[20]; int score; }; void sort(struct student *p,int n) { /*********Begin*********/ /*********End**********/ } int main(void) { struct student arr[200],t; int n,i,j; scanf("%d",&n); /*********Begin*********/ /*********End**********/ sort(arr,n); for(i=0;i<n;i++) printf("%s %s %d\n",arr[i].number,arr[i].name,arr[i].score); return 0; }
好的,下面是代码的补全部分:
```c
#include<stdio.h>
struct student {
char number[15];
char name[20];
int score;
};
void sort(struct student *p, int n) {
int i, j;
struct student temp;
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if ((p + i)->score < (p + j)->score) {
temp = *(p + i);
*(p + i) = *(p + j);
*(p + j) = temp;
}
}
}
}
int main(void) {
struct student arr[200], t;
int n, i, j;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%s %s %d", arr[i].number, arr[i].name, &arr[i].score);
}
sort(arr, n);
for (i = 0; i < n; i++) {
printf("%s %s %d\n", arr[i].number, arr[i].name, arr[i].score);
}
return 0;
}
```
该程序中,我们定义了一个结构体 `student` 来存储每个学生的信息,包括学号、姓名、成绩。在 `main()` 函数中,我们先输入学生的数量 `n`,然后使用 `for` 循环输入每个学生的信息。接下来调用 `sort()` 函数进行排序,最后输出排序后的学生信息。在 `sort()` 函数中,我们使用了简单的选择排序算法,按照成绩从高到低排序。需要注意的是,在函数中使用 `p + i` 和 `p + j` 来访问结构体数组中的元素,使用 `*(p + i)` 和 `*(p + j)` 来访问结构体数组中的元素的值。
struct student{ int num; char name[20]; int sex;//sex=0女、sex=1男 int age; student *next; };以这个结构体组成的有头结点的链表冒泡排序
以下是使用冒泡排序对有头结点的链表进行排序的示例代码:
```c++
void bubbleSort(student *head) {
int len = 0;
student *p = head->next;
while (p != NULL) {
len++;
p = p->next;
}
for (int i = 0; i < len - 1; i++) {
p = head->next;
for (int j = 0; j < len - i - 1; j++) {
if (p->num > p->next->num) {
student *temp = p->next;
p->next = temp->next;
temp->next = p;
head->next = temp;
p = temp;
}
p = p->next;
}
}
}
```
该代码中,首先计算出链表中元素的个数 len。然后使用两重循环,外层循环控制排序的轮数,内层循环负责每一轮中相邻元素的比较和交换。在内层循环中,如果相邻元素的顺序不正确,就交换这两个元素的位置。需要注意的是,如果第一个元素被交换,那么头结点的指针需要重新指向链表的第一个元素。
以上是针对结构体中 num 字段进行排序的代码。如果需要根据其他字段进行排序,可以通过修改比较语句来实现。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)