根据结构元素之一对结构进行排序

时间:2018-07-02 08:16:20

标签: c

我必须使用qsort()函数按平均得分的升序对板球运动员进行排序,然后根据其打印所有详细信息。我无法弄清楚如何根据平均得分进行排序,然后根据该列表打印列表。

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

int compare(const void*a, const void*b)
{
    return (*(int*)a - *(int*)b);
}

void main()
{
    int i;
    struct cricket
    {
        char name[20];
        int age;
        int no_test;
        int avrun;
    }
    players[20] = {
        "Virat Kohli", 25, 29, 94,
        "Rohit Sharma", 26, 19, 86,
        "Mahendra Singh Dhoni", 32, 40, 69,
        "Gautum Gambhir", 29, 28, 90,
        "Hardik Pandya", 27, 18, 59,
        "Chris Gayle", 38, 50, 48,
        "James Watson", 40, 54, 68,
        "Brett Lee", 38, 53, 83,
        "Suresh Raina", 32, 29, 59,
        "Sachin Tendulkar", 40, 60, 95,
        "Virendra Sehwag", 45, 55, 83
    };
    qsort(players,11,sizeof(cricket),compare);
    for(i = 0; i < 11; i++)
    {
        printf("%s ", players[i].name);
        printf("%d ", players[i].age);
        printf("%d ", players[i].no_test);
        printf("%d \n", players[i].avrun);
    }
    getch();
}

2 个答案:

答案 0 :(得分:1)

void main()更改为int main(void)What should main() return in C and C++?

You might not want to use conio.h

struct cricket的定义应该对compare()可见,因此请将其移到该函数之前(紧随头文件之后)。

然后,在结构定义的末尾需要一个分号。

现在,您需要声明数组的类型,例如:struct cricket players[20]

然后,您应该收到以下警告:

  

警告:初始化程序[-Wmissing-braces]周围缺少括号

关于初始化播放器的方式。您需要这样做:

struct cricket players[20] = {
       {"Virat Kohli", 25, 29, 94},
       {"Rohit Sharma", 26, 19, 86},
       ...
};

然后,您必须修复此错误:

  

错误:未声明“板球”(此功能首次使用)        qsort(players,11,sizeof(cricket),compare);

只需将cricket替换为struct cricket

将所有内容放在一起,您将拥有:

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

struct cricket {
    char name[20];
    int age;
    int no_test;
    int avrun;
};

int compare(const void*a, const void*b) {
    return (*(int*)a - *(int*)b);
}

int main(void) {
    int i;
    // I changed the size of the array too, to 11.
    struct cricket players[11] = {
        {"Virat Kohli", 25, 29, 94},
        {"Rohit Sharma", 26, 19, 86},
        {"Mahendra Singh Dhoni", 32, 40, 69},
        {"Gautum Gambhir", 29, 28, 90},
        {"Hardik Pandya", 27, 18, 59},
        {"Chris Gayle", 38, 50, 48},
        {"James Watson", 40, 54, 68},
        {"Brett Lee", 38, 53, 83},
        {"Suresh Raina", 32, 29, 59},
        {"Sachin Tendulkar", 40, 60, 95},
        {"Virendra Sehwag", 45, 55, 83}
    };
    qsort(players,11,sizeof(struct cricket),compare);
    for(i = 0; i < 11; i++) {
        printf("%s ", players[i].name);
        printf("%d ", players[i].age);
        printf("%d ", players[i].no_test);
        printf("%d \n", players[i].avrun);
    }
    return 0;
}

将运行并产生一个无意义的输出。在您的compare函数中,您应该考虑要对数组进行排序的字段。

了解此问题中提到的内容,然后继续并修改比较功能。

答案 1 :(得分:1)

您的比较功能没有意义。

要使其正常工作,必须知道要排序的数据类型,因此您不能从中隐藏结构声明。

假设将其移至顶部,然后可以重新编写比较功能:

static int compare_crickets(const void *va, const void *vb)
{
  // Convert the generic arguments to actual structure pointers.
  const struct cricket * const ca = va, * const cb = vb;
  // Compare on the 'avrun' member, return -1/0/1 for LT/EQ/GT.
  return ca->avrun < cb->avrun ? -1 : ca->avrun > cb->avrun;
}