【问题标题】:sorting members of structure array排序结构数组的成员
【发布时间】:2012-11-02 14:05:17
【问题描述】:

给定一个结构数组(在 C 中),我试图按性别分组和按数字顺序按子顺序打印结果。例如:

struct employee{
char gender[13]
char name[13];
int id;
};

假设我这样定义结构数组:

struct employee info[2]={{"male","Matt",1234},{"female","Jessica",2345},{"male","Josh",1235}};

我怎么能像这样打印结果

1234 Matt
1235 Josh


2345 Jessica

【问题讨论】:

    标签: c arrays sorting structure


    【解决方案1】:

    您需要实现一个排序功能,根据需要比较结构

    int compare(const void *s1, const void *s2)
    {
      struct employee *e1 = (struct employee *)s1;
      struct employee *e2 = (struct employee *)s2;
      int gendercompare = strcmp(e1->gender, e2->gender);
      if (gendercompare == 0)  /* same gender so sort by id */
        return e1->id - e2->id;
      else
        return -gendercompare;  /* the minus puts "male" first as in the question */
    }
    

    然后使用标准库中的 qsort。

    qsort(data, count, sizeof(struct employee), compare);
    

    在比较函数中,您可能想要检查 id 是否相等,然后您可以按名称排序(也可以使用 strcmp()),但您喜欢。

    编辑:刚刚编译并修复了这个。这是一个小测试程序

        #include <stdio.h>
        #include <stdlib.h>
    
        struct employee{
          char gender[13];
          char name[13];
          int id;
        };
    
        int compare(const void *s1, const void *s2)
        {
          struct employee *e1 = (struct employee *)s1;
          struct employee *e2 = (struct employee *)s2;
          int gendercompare = strcmp(e1->gender, e2->gender);
          if (gendercompare == 0)  /* same gender so sort by id */
            return e1->id - e2->id;
          else
            return -gendercompare;
        }
    
        main()
        {
          int i;
          struct employee info[]={{"male","Matt",1234},{"female","Jessica",2345},{"male","Josh",1235}};
    
          for (i = 0; i < 3; ++i)
            printf("%d\t%s\t%s\n", info[i].id, info[i].gender, info[i].name);
    
          qsort(info, 3, sizeof(struct employee), compare);
    
          for (i = 0; i < 3; ++i)
            printf("%d\t%s\t%s\n", info[i].id, info[i].gender, info[i].name);
        }
    

    有输出:

    $ ./a.exe
    1234    male    Matt
    2345    female  Jessica
    1235    male    Josh
    1234    male    Matt
    1235    male    Josh
    2345    female  Jessica
    

    【讨论】:

    • 我想我们想要“按数字顺序排列”
    • 这是一个问题,但说我定义了一个函数。我如何将员工信息的内容传递给该函数?
    • 如果有人理解这个有问题,可以参考ibm.com/support/knowledgecenter/en/SSLTBW_2.3.0/…
    【解决方案2】:

    在结构数组上使用您最喜欢的排序算法。在比较数组的两个元素以决定哪个“更大”时,比较它们的性别;如果性别相同,请比较他们的数字。 (您可能需要定义一个单独的函数来进行此比较,以使事情更清楚。)然后,使用所需的格式按顺序打印排序的数组。跟踪性别何时从男性变为女性,以便您可以添加额外的三个换行符,如您的示例所示。

    编辑:要厚颜无耻地从 kallikak 借用,您可以将比较函数传递给 qsort,但如果一个结构“更大”,则返回 1,如果“更少”则返回 -1,如果需要,则返回 0是相同的(使用我上面概述的过程)。查看How to write a compare function for qsort from stdlib? 获取有关编写自定义比较函数的帮助。

    【讨论】:

    • 感谢您的回复。我想知道如何在使用排序算法的同时检查性别。例如说我正在使用快速排序,如果右是
    • 参见我的示例中的比较方法。您可以在决定订购时检查结构中的任何字段。
    【解决方案3】:

    觉得比较容易理解,因为我的指针比较弱

    #include<bits/stdc++.h>
    
    using namespace std;
    
    
    struct employee{
      char gender[13];
      char name[13];
      int id;
    };
    
    bool compare(employee s1,employee s2)
    {
      return s1.id<s2.id;
    }
    
    main()
    {
      int i;
      struct employee info[]={{"male","Matt",1234},{"female","Jessica",2345},{"male","Josh",1235}};
      sort(info,info+3,compare);
      for (i = 0; i < 3; i++)
      printf("%d\t%s\t%s\n",info[i].id,info[i].gender,info[i].name);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-08
      • 1970-01-01
      • 2020-06-17
      • 2017-06-11
      • 2012-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多