c - 对 struct c 数组中的 struct 数组进行排序

标签 c arrays data-structures qsort

我正在尝试按结构的每个成员对结构数组进行排序;即,我想要 打印 1 个按结构中每个成员排序的列表。当成员们 结构体都是整数,没问题。但其中一个成员是另一个结构体数组, 我还想按该结构的每个成员对整个困惑进行排序。这是代码:

 #define PROPHET_COUNT 9000
 #define MAX_FAITH_COUNT 600

typedef struct s_ProphetStat {  
    int     precursorScore;
    int     cassandraScore;
    int     prophetId;} prophetStat;

typedef struct s_FaithStat{
    int     precursorScore;
    int     cassandraScore;
    int     faithId;
    prophetStat ProphetStat[PROPHET_COUNT];  } faithStat; 

void fauxScoringFunction(faithStat *FaithStat)
{
    for (int faithIndex = 0; faithIndex < MAX_FAITH_COUNT; ++faithIndex){
        for (int prophetIndex = 0; prophetIndex < PROPHET_COUNT; ++prophetIndex){
            int randomNumber = rand();
            FaithStat[faithIndex].ProphetStat[prophetIndex].precursorScore +=   randomNumber;
            FaithStat[faithIndex].ProphetStat[prophetIndex].cassandraScore +=   randomNumber;
            FaithStat[faithIndex].precursorScore += randomNumber;
            FaithStat[faithIndex].cassandraScore += randomNumber; }}
}

typedef int (*compfn)(const void*, const void*);`enter code here`

   int compareFaithPrecursorScores(faithStat *faithA, faithStat *faithB){
 if (faithA->precursorScore > faithB->precursorScore) return 1; if (faithA->precursorScore < faithB->precursorScore) return -1; return 0; }
    int compareFaithCassandraScores(faithStat *faithA, faithStat *faithB) {
  if (faithA->cassandraScore > faithB->cassandraScore) return 1; if (faithA->cassandraScore < faithB->cassandraScore) return -1; return 0; }
    int cannotFigureOut(...) { return 0; }

void fakemain(void)
{
    faithStat   *FaithStat =  (faithStat *)   calloc(MAX_FAITH_COUNT,   sizeof(faithStat) );
    fauxScoringFunction(FaithStat);
    // sort by cumulative precursorScore for each faith 
    qsort(FaithStat, MAX_FAITH_COUNT, sizeof(faithStat *), (compfn) compareFaithPrecursorScores);
    // print results();
    // sort by cumulative precursorScore for each faith
    qsort(FaithStat, MAX_FAITH_COUNT, sizeof(faithStat *), (compfn) compareFaithCassandraScores);
    // print results()
    // sort by prophet precursor score
    qsort(FaithStat, MAX_FAITH_COUNT * PROPHET_COUNT, sizeof(faithStat *), (compfn) cannotFigureOut);
}

这是我正在尝试编写的“cannotFigureOut()”比较函数。 (我正在使用 VS2010 C++ 编译 C 代码(不是我的决定),因此令人讨厌的 calloc 转换。所有其他丑陋都是我的。)

编辑:在试图简化时,搞砸了比较功能。解决了这个问题。还, 编辑:我遗漏了一条重要的信息:每个信仰的先知集都是相同的。所以我想做的是排序 每个先知的累积先驱分数(然后分别通过累积的 cassandra 分数)。即:Prophet[0]cumulativeScore = (Faith[0].Prophet[0].precursorScore + (信仰[1].Prophet[0].precursorScore ...信仰[ MAX_FAITH_COUNT - 1].Prophet[0].precursorScore);

最佳答案

首先,return (a,b); 只返回b;在 compareFaithPrecursorScorescompareFaithCassandraScores 中,您可能需要将 , 替换为 -。现在,您的 main 中只分配了 faiths,因此在最后一个中,您应该对与之前相同长度的 FaithStat 进行排序:MAX_FAITH_COUNT,而不是 MAX_FAITH_COUNT * PROPHET_COUNT

现在在您的 cannotFigureOut 中,您只需像以前一样比较两个faithStats,因此签名仍然相同:

int cannotFigureOut(faithStat *faithA, faithStat *faithB){
 .... } 

(编辑:)好吧,所以(在您澄清之后)这根本不称为“累积”,这是误导性的。您指的是分数。您最后添加的内容似乎是说您想要对另一个数组进行排序,这次是 9000 个先知(而不是 600 个信仰)。每个先知的分数将是他在所有信仰中分数的总和;只需创建一个函数来在创建后填充预言家数组,然后照常排序。

您可以使用相同的 prophetStat 结构来保存这次的总分,而不是每个信仰的值,这样签名就会是

int cannotFigureOut(prophetStat *prophetA, prophetStat *prophetB){
 .... } 

关于c - 对 struct c 数组中的 struct 数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9456513/

相关文章:

algorithm - 面试题: Data structure for a large social network

c - 这两种代码有什么区别?我不知道,即使他们输出不同的值

在 Mac OS X 上找不到 C 头文件

clang 中的 C 可变参数宏 __VA_ARGS__ 与 ##__VA_ARGS__

c++ - C函数将原始图像转换为png

javascript - 在 PHP 中,我从 mySQL 编码了一个 JSON 数组。我想在不同的 php 文件中解码它

c - 字符数组到链表 - 使用数组中的地址

c++ - 错误 : pointer to incomplete class type is not allowed

c - 在C中合并多个整数范围的数据结构和算法

c - 如何从文件读入并从缓冲区输出到另一个数组