gpt4 book ai didi

c - Qsort 未正确排序结构元素

转载 作者:行者123 更新时间:2023-11-30 18:36:57 27 4
gpt4 key购买 nike

我正在编写一些代码,读取二进制文件,对其进行排序,然后将其写出并输出按字典顺序排序的文件。除了各种元素的排序之外,我不会抛出任何错误。

struct lab1_data
{
float goodbye;
char balance;
unsigned char clouds;
float badge;
double soda;
char bat;
short int parcel;
char vessel;
char spade;
long int cover;
unsigned long int hobbies;
short int voyage;
int stomach;
char sort;
char system [11];
unsigned short adjustment;
};


int compare(const void * a, const void * b)
{
struct lab1_data** a1 = (struct lab1_data**) a;
struct lab1_data** b1 = (struct lab1_data**) b;

if ((*a1)->soda > (*b1)->soda)
{
return 1;
}

if ((*b1)->soda < (*b1)->soda)
{
return -1;

}

if ((*a1)->stomach > (*b1)->stomach)
{
return -1;
}

if ((*b1)->stomach < (*b1)->stomach)
{
return 1;

}
if ((*a1)->bat > (*b1)->bat)
{
return -1;
}

if ((*b1)->bat < (*b1)->bat)
{
return 1;

}
//This keeps going for all elements, just varying descending/ascending order.
.
.
.
}

int main(int argc, char **argv)
{

int size = 8;
int count = 0;
int i;

struct lab1_data *lab1_struct;
lab1_struct = (struct lab1_data*) malloc (size * sizeof(struct lab1_data));

if (!lab1_struct)
{
fprintf(stderr, "Could not allocate memory");
exit(-2) ;
}

FILE *fh_i = fopen(argv[1], "rb");
FILE *fh_o = fopen(argv[2], "wb");

if (argc != 3)
{
fprintf(stderr, "Incorrect file names");
exit(1);
}

if (!fh_o)
{
fprintf(stderr, "Could not open file %s.", argv[2]);
exit(-3);
}
if (!fh_i)
{
fprintf(stderr, "Could not open file %s.", argv[1]);
exit(-3);
}

while (!feof(fh_i))
{
if (count == size)
{
size = size * 2;
lab1_struct = realloc(lab1_struct, sizeof(struct lab1_data) * size);
}

fread((void*)&lab1_struct[count].goodbye, sizeof(float), 1, fh_i);
fread((void*)&lab1_struct[count].balance, sizeof(char), 1, fh_i);
fread((void*)&lab1_struct[count].clouds, sizeof(unsigned char), 1, fh_i);
fread((void*)&lab1_struct[count].badge, sizeof(float), 1, fh_i);
fread((void*)&lab1_struct[count].soda, sizeof(double), 1, fh_i);
fread((void*)&lab1_struct[count].bat, sizeof(char), 1, fh_i);
fread((void*)&lab1_struct[count].parcel, sizeof(short), 1, fh_i);
fread((void*)&lab1_struct[count].vessel, sizeof(char), 1, fh_i);
fread((void*)&lab1_struct[count].spade, sizeof(char), 1, fh_i);
fread((void*)&lab1_struct[count].cover, sizeof(long), 1, fh_i);
fread((void*)&lab1_struct[count].hobbies, sizeof(unsigned long), 1, fh_i);
fread((void*)&lab1_struct[count].voyage, sizeof(short), 1, fh_i);
fread((void*)&lab1_struct[count].stomach, sizeof(int), 1, fh_i);
fread((void*)&lab1_struct[count].sort, sizeof(char), 1, fh_i);
fread((void*)&lab1_struct[count].system, 11 * sizeof(char), 1, fh_i);
fread((void*)&lab1_struct[count].adjustment, sizeof(short), 1, fh_i);

count++;
}

struct lab1_data** plab1_struct = (struct lab1_data**) malloc (size * 8);
for (i = 0; i < count; i++)
{
plab1_struct[i] = &lab1_struct[i];
}

qsort(plab1_struct, count - 1, 8, compare);

for (i = 0; i < count; i++)
{
fwrite((void*) & (*plab1_struct[i]).goodbye, sizeof(float), 1, fh_o);
fwrite((void*) & (*plab1_struct[i]).balance, sizeof(char), 1, fh_o);
fwrite((void*) & (*plab1_struct[i]).clouds, sizeof(unsigned char), 1, fh_o);
fwrite((void*) & (*plab1_struct[i]).badge, sizeof(float), 1, fh_o);
fwrite((void*) & (*plab1_struct[i]).soda, sizeof(double), 1, fh_o);
fwrite((void*) & (*plab1_struct[i]).bat, sizeof(char), 1, fh_o);
fwrite((void*) & (*plab1_struct[i]).vessel, sizeof(short), 1 , fh_o);
fwrite((void*) & (*plab1_struct[i]).parcel, sizeof(char), 1, fh_o);
fwrite((void*) & (*plab1_struct[i]).spade, sizeof(char), 1 , fh_o);
fwrite((void*) & (*plab1_struct[i]).cover, sizeof(long), 1, fh_o);
fwrite((void*) & (*plab1_struct[i]).hobbies, sizeof(unsigned long), 1, fh_o);
fwrite((void*) & (*plab1_struct[i]).voyage, sizeof(short), 1, fh_o);
fwrite((void*) & (*plab1_struct[i]).stomach, sizeof(int), 1, fh_o);
fwrite((void*) & (*plab1_struct[i]).sort, sizeof(char), 1, fh_o);
fwrite((void*) & (*plab1_struct[i]).system, 11 * sizeof(char), 1, fh_o);
fwrite((void*) & (*plab1_struct[i]).adjustment, sizeof(short), 1, fh_o);
}

fclose(fh_i);
fclose(fh_o);

free(lab1_struct);
free(plab1_struct);

return 0;
}

我的元素根本没有被排序。我确信我已经正确调用并初始化了 qsort 函数,但我看不到我缺少什么。

最佳答案

比较函数有这一行,这似乎是一个拼写错误,因为 b1 使用了两次:

    if ((*b1)->soda < (*b1)->soda)
^^ ^^

同样适用于:

    if ((*b1)->stomach < (*b1)->stomach)

if ((*b1)->bat < (*b1)->bat)

关于c - Qsort 未正确排序结构元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39169998/

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