我想使用qsort按名称对 struct 数组进行排序,但输出结果与我预期的不同.

typedef struct node{
    char name[64];
    char ingredient[10][64];
}node;

int compare(const void *a, const void *b){
    node *nodeA = (node *)a;
    node *nodeB = (node *)b;
    return strcmp(nodeA->name, nodeB->name);
}

int main(){
    int num; scanf("%d", &num);
    int sum[num];
    node recipe[num];
    //input
    for(int i = 0; i < num; i++){
        scanf("%s", recipe[i].name);
        scanf("%d", &sum[i]);
        for(int j = 0; j < sum[i]; j++){
            scanf("%s", recipe[i].ingredient[j]);
        }
    }
    //sort
    qsort(recipe, num, sizeof(node), compare);

    //print out to check
    printf("\n");
    for(int i = 0; i < num; i++){
        printf("%s ", recipe[i].name);
        for(int j = 0; j < sum[i]; j++){
            printf("%s ", recipe[i].ingredient[j]);
        }
        printf("\n");
    }

输入如下

5
cake 4 egg flour sugar butter
omelet 4 egg bacon ham butter
bread 1 flour
breed 0
breag 0

输出如下

bread flour    
breag H??    
breed ?i?
          
cake 
omelet 

expected 输出如下

bread flour 
breag 
breed 
cake egg flour sugar butter 
omelet egg bacon ham butter 

此外,如果我还想按如下方式对每个 node 中的配料进行排序,该如何实现呢?

//maybe I can do something like this
for(int i = 0; i < num; i++){
    qsort(recipe[i].ingredient, num, sizeof(recipe[i].ingredient[0]), mycompare//to compare each ingredient);
}

expected 输出如下

bread flour 
breag 
breed 
cake butter egg flour sugar  
omelet bacon butter egg ham

推荐答案

代码对食谱数组进行排序,但每个食谱都与它的sum个食谱分开.

以下是所需的更改:

typedef struct node{
    char name[64];
    int sum; // <== here!!
    char ingredient[10][64];
}node;

int main(){
    int num; scanf("%d", &num);
    node recipe[num];
    //input
    for(int i = 0; i < num; i++){
        printf( "Recipe #%d:\n", i + 1 ); // alert user to start of next recipe
        scanf("%s", recipe[i].name);
        scanf("%d", &recipe[i].sum); // <== here!!
        for(int j = 0; j < 10 && j < recipe[i]sum; j++){ // <==here!!
            scanf("%s", recipe[i].ingredient[j]);
        }
    }
    //sort
    qsort(recipe, num, sizeof(node), compare);

    //print out to check
    printf("\n");
    for(int i = 0; i < num; i++){
        printf("%s ", recipe[i].name);
        for(int j = 0; j < recipe[i].sum; j++){ // <== finally here
            printf("%s ", recipe[i].ingredient[j]);
        }
        printf("\n");
    }
}

是的,我用了一个"魔术数字",10出现在代码中.将来,最好使用#define MAX_INGRED 10(或某些类似的值)并使用该令牌.


"I also want to sort the ingredient within each node"
try 得很好,但这可能效果更好

for( int i = 0; i < num; i++)
    qsort(
        recipe[i].ingredient,
        recipe[i].sum, // the correct # for each different recipe
        sizeof(recipe[0].ingredient[0]),
        mycompare
    );

C++相关问答推荐

如何将FileFilter添加到FileDialog GTK 4

C:scanf(%d&q;,...)输入只有一个减号

SDL 2.0-从数组渲染纹理

识别和处理c中整数溢出的最佳方法?

带有sigLongjMP中断I/O的异常处理程序

Win32API Wizzard97 PropSheet_SetWizButton不工作

为什么STM32G474RE上没有启用RCC PLL

在列表中插入Int指针(C)

如何使用[BTStack]BLE发送大型(>;2kb)信息包

1处的解析器错误:yacc语法的语法错误

错误Cygwin_Except::Open_stackdupfile:正在转储堆栈跟踪是什么?

SSH会话出现意外状态

将字符串数组传递给C中的函数:`str[dim1][str_size]`vs`*str[dim1]`

收到不兼容的指针类型警告,因为函数的返回不是空*,而是 struct 指针

pthread_create的用法

从CentOS 7到Raspberry PI 2B的交叉编译-无法让LIBC和System Include标头一起工作

如何在C宏定义中包含双引号?

使用mmap为N整数分配内存

C循环条件内的函数

定义 int a = 0, b = a++, c = a++;在 C 中定义了行为吗?