简体   繁体   English

在C中使用qsort()和Structs

[英]Using qsort() and Structs in C

For our exam practice sheet, we have to create a struct using river data. 对于我们的考试练习表,我们必须使用河流数据创建结构。 The struct is: 结构是:

struct river
{
    char name[40]; //name of river max 40 char long
    int length; 
    int drainage_area;
}

And the data is supposed to be organised from largest to smallest river length using a file called data.txt. 并且应该使用名为data.txt的文件将数据从最大到最小的河流长度组织起来。 This file has the following information: 该文件包含以下信息:

Nile 6650 334900 尼罗河6650 334900

Amazon 6400 6915000 亚马逊6400 6915000

Yangtze 6300 1800000 长江6300 1800000

Mississippi-Missouri 6275 2980000 Mississippi-Missouri 6275 2980000

Yenisei-Angara-Selenga 5539 2580000 Yenisei-Angara-Selenga 5539 ​​2580000

Yellow 5464 745000 黄色5464 745000

Ob-Irtysh 5410 2990000 Ob-Irtysh 5410 2990000

Congo-Chambeshi 4700 3680000 Congo-Chambeshi 4700 3680000

So I know for using the function qsort(), you need a comparison function. 所以我知道使用函数qsort(),你需要一个比较函数。 However I'm not so sure how to do this when there's a file involved. 但是,当涉及到文件时,我不太确定如何执行此操作。 I have the following for the comparison function (general comparison function): 我有以下比较功能(一般比较功能):

int compare_rivers(void *r1, void *r2)
{
    int *_r1 = (int *)r1;
    int *_r2 = (int *)r2;

    if( *_r1 > *_r2)
    {
        return -1;
    }
    if (*_r1 == *_r2)
    {
        return 0;
    }

    return 1;
}

My question is: how to implement the compare function and then the main? 我的问题是:如何实现比较功能然后主要? I know how to open and read files, but is this different than using arrays? 我知道如何打开和读取文件,但这与使用数组不同吗?

An explanation would be really helpful! 解释会非常有用! I'm not asking for a full-fledged code. 我不是要求一个完整的代码。 I just want an explanation/example how I would do this! 我只想要一个解释/示例我将如何做到这一点! Thanks in advance! 提前致谢!

You have to read the file into memory first. 您必须先将文件读入内存。 Store it as an (unsorted) array of structs. 将其存储为(未排序)结构数组。

You probably don't want to sort this array(?) because it contains "large data". 您可能不希望对此数组(?)进行排序,因为它包含“大数据”。 Ok in this case it really doesn't, but for all I know it might be the purpose of the assignment to recognize this. 好吧,在这种情况下,它确实没有,但是对于所有我知道,分配的目的可能是认识到这一点。 qsort will do a lot of data shuffling if you sort whole structs. 如果你对整个结构进行排序, qsort会做很多数据改组。

So in parallel to this array, you can create an array of struct pointers, each pointing to a struct item of the unsorted array. 因此,与此数组并行,您可以创建一个struct指针数组,每个指针指向未排序数组的结构项。

Apply qsort on this array of pointers. 在这个指针数组上应用qsort The comparison function should have the form 比较函数应该具有表单

int (*compar)(const void* obj1, const void* obj2)

But note that since you are sorting an array of pointers, qsort will actually pass you the address to a pointer . 但请注意,由于您要对指针数组进行排序,因此qsort实际上会将地址传递给指针 So you'll have to do something like: 所以你必须做的事情如下:

  const struct river* r1 = *(const struct river**)obj1;

Then compare the contents in some meaningful way. 然后以一些有意义的方式比较内容。

When you hold the file in memory as an array of struct river the compare function gets two struct river pointers as parameter. 当你将文件作为struct river数组保存在内存中时,compare函数会获得两个struct river指针作为参数。 So cast those first and then do your compare logic. 首先抛出那些,然后做你的比较逻辑。

struct river rivers[10]; // filled with the file

qsort(rivers, 10, sizeof *rivers, compare_rivers);

    int compare_rivers(const void *r1, const void *r2)
    {
        const struct river*_r1 = (const struct river *)r1;
        const struct river*_r2 = (const struct river*)r2;

        // do compare logic
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM