gpt4 book ai didi

c - 根据结构体内部的元素对结构体指针数组进行排序

转载 作者:行者123 更新时间:2023-11-30 19:51:09 25 4
gpt4 key购买 nike

我在这里想做的是根据 Struct 内部的字符串对指向 Structs 的指针数组进行排序。我认为只交换数组内部的指针而不是交换整个 struct 会很容易。所有的Element都是动态分配的。我的代码是用德语编写的,因此我将只用英语编写我不懂的部分。预先感谢您,感谢您的帮助。

typedef struct Vehicle {
char *manufacturer;
char *serialnumber;
int weight;
};
void SortByID(struct fahrzeug** data, int Anzahl){
struct Vehicle *temp= (struct Vehicle*)malloc( sizeof(struct Vehicle) );
int i =0 ;// I think i've written something wrong here ( strcmp)
while ( i < Anzahl && strcmp(data[i]->serialnumber, data[i+1]->serialnumber) < 0)
{
temp = data[i+1];
data[i+1] = data[i];
data[i]=temp ;
i++;
}
free(temp);
temp=NULL;
}
int main(){
int count =0 ;
struct Vehicle **array = NULL ;
array=(struct Vehicle**)malloc( 5 * sizeof(struct Vehicle*) );
for(int i=0;i<5 ;i++){
array[i] = (struct Vehicle*)malloc( sizeof(struct Vehicle) ) ;
count++;
}
SortByID ( &array, count ) ; // is this right ?
return 0;
}

我也尝试使用 qsort 函数对其进行排序,但没有成功

int compare(const void *a, const void *b) {
struct Vehicle * const *one = a;
struct Vehicle * const *two = b;

return strcmp((*one)->serialnumber, (*two)->serialnumber);
}
void SortByID(struct Vehicle** data, int Anzahl){
qsort( data, Anzahl, sizeof(struct Vehicle*) ,compare );
}

最佳答案

这是一个使用qsort()的工作示例。它可以按序列号和制造商排序。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Vehicle {
char *manufacturer;
char *serialnumber;
int weight;
};

int cmp_serialnumber(const void *p1, const void *p2)
{
const struct Vehicle *v1 = *(struct Vehicle * const *)p1;
const struct Vehicle *v2 = *(struct Vehicle * const *)p2;

return strcmp(v1->serialnumber, v2->serialnumber);

/*
// Alternatively You could write this function as this less readable one liner
return strcmp((*(struct Vehicle * const *)p1)->serialnumber,
(*(struct Vehicle * const *)p2)->serialnumber);
*/
}

int cmp_manufacturer(const void *p1, const void *p2)
{
const struct Vehicle *v1 = *(struct Vehicle * const *)p1;
const struct Vehicle *v2 = *(struct Vehicle * const *)p2;

return strcmp(v1->manufacturer, v2->manufacturer);
}

void print_vehicles(struct Vehicle **vehicles, int n) {
for (int i=0; i<n; i++) {
printf("%s, %s, %d\n", vehicles[i]->serialnumber,
vehicles[i]->manufacturer, vehicles[i]->weight);
}
}


#define N_VEHICLES 5
char *manufacturers[] = {"McLaren", "Ferrari", "Renault", "Mercedes", "Alfa Romeo"};
char *serialnumbers[] = {"SN500", "SN4", "SN8", "SN2", "SN1"};


int main(){
struct Vehicle **vehicles = NULL;

vehicles = (struct Vehicle**)malloc(N_VEHICLES * sizeof(struct Vehicle *));

for(int i=0; i<N_VEHICLES; i++) {
vehicles[i] = (struct Vehicle *)malloc(sizeof(struct Vehicle));
vehicles[i]->manufacturer = manufacturers[i];
vehicles[i]->serialnumber = serialnumbers[i];
vehicles[i]->weight = 1000;
}

printf("Before\n");
print_vehicles(vehicles, N_VEHICLES);
printf("\n");

// sort by serial number
qsort(vehicles, N_VEHICLES, sizeof(struct Vehicle *), cmp_serialnumber);
printf("Sorted by serial number\n");
print_vehicles(vehicles, N_VEHICLES);
printf("\n");

// sort by manufacturer
qsort(vehicles, N_VEHICLES, sizeof(struct Vehicle *), cmp_manufacturer);
printf("Sorted by manufacturer\n");
print_vehicles(vehicles, N_VEHICLES);

return 0;
}

关于c - 根据结构体内部的元素对结构体指针数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49998933/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com