2012-06-10 101 views
2

我想以一种particuar的方式排序一个结构。正如你可以在下面的结构中看到的,字段按product名称排序。自定义qsort比较函数与结构中的多个字段C

 a - $13.00 
a.0|100 - $3.00 
a.1|100 - $6.00 
a.2|100 - $4.00 
     b - $25.00 
b.0|100 - $2.00 
b.1|100 - $10.00 
b.2|100 - $13.00 

我不知道我怎么会通过product名称保存排序,但在同一时间,“子样”每个产品的price

这是我到目前为止有:

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

struct st_ex { 
    char product[16]; 
    float price; 
}; 

int struct_cmp_by_product(const void *a, const void *b) { 
    struct st_ex *ia = (struct st_ex *)a; 
    struct st_ex *ib = (struct st_ex *)b; 
    return strcmp(ia->product, ib->product); 
} 

int main() { 
    struct st_ex structs[] = { 
     {"b", 25}, 
     {"b.0|100", 2}, 
     {"b.1|100", 10}, 
     {"b.2|100", 13}, 
     {"a", 13}, 
     {"a.0|100", 3}, 
     {"a.1|100", 6}, 
     {"a.2|100", 4}, 
    }; 
    size_t structs_len = sizeof(structs)/sizeof(struct st_ex); 
    qsort(structs, structs_len, sizeof(struct st_ex), struct_cmp_by_product);  
    size_t i; 
    for(i=0; i<structs_len; i++) 
     printf("%8s - $%.2f\n", structs[i].product, structs[i].price); 
    return 0; 
} 

更新:按价格排序,但按名称分组。例如,b:25>a:13

 b - $25.00 
b.2|100 - $13.00 
b.1|100 - $10.00 
b.0|100 - $2.00 
     a - $13.00 
a.1|100 - $6.00 
a.2|100 - $4.00 
a.0|100 - $3.00 
+0

谢谢你们,使得现在很多更有意义。 – user1024718

回答

0

作业?有人用C写这种东西吗?

想想你的目标是什么:如果产品名称是不同的,那么它是没有区别的价格是什么,所以你返回的名称比较结果。

只有在产品名称与价格相同的情况下才是如此。

因此需要固定问题。需要更多应用感知的替代品strcmp(3),知道b,b.0|100b.0|200应该作为产品“名称”进行同等比较。当然,在平等的情况下,有必要添加一个价格比较。下面的代码取代了您的比较功能并已经过测试。顺便说一句,你不需要投的转换和从void *如果你是工作在纯C.

int baseproductname(const char *a, const char *b) { 
    for(;;) { 
     int c = *a; 
     int d = *b; 
     if (c == '.') 
      c = 0; 
     if (d == '.') 
      d = 0; 
     if(c != d || !c || !d) 
      return c - d; 
     ++a; 
     ++b; 
    } 
} 

int productorder(const struct st_ex *a, const struct st_ex *b) { 
    int n = baseproductname(a->product, b->product); 
    printf("%s <=> %s is %d\n", a->product, b->product, n); 
    if (n) 
     return n; 
    if (a->price > b->price) 
     return -1; 
    return a->price < b->price; 
} 

int struct_cmp_by_product(const void *a, const void *b) { 
    return productorder(a, b); 
} 
0

最简单的方法是修改您的struct_cmp_by_product。而不是做一个strcmp,比较产品名称(取决于您的产品名称的长度,您可能希望在product成员的子字符串上使用strcmp)。

但我宁愿通过保持相结合,当两个成员加入st_ex类型中这种区别编码给出了实际的产品名称。

struct st_ex { 
     char product[ 4 ]; // product group 
     char subgroup[ 12 ]; // sub group 
     float price; 
} 

int struct_cmp_by_product(const void *a, const void *b) { 
    struct st_ex const *ia = a; // do not cast away const-ness! 
    struct st_ex const *ib = b; 
    int x = strcmp(ia->product, ib->product); 
    if (!x) { 
     return ia->price > ib->price; 
    } 
    return x; 
} 
+0

“x”在任何时候都不会成立吗? – user1024718

+0

我假设'product'字段包含'a','b'等,而'subgroup'字段包含'.0 | 100'部分。所以,我不明白为什么'x'总会是真的。但是当它是真的,我们按价格(而不是名称)来分类。这不是你想要的吗? – dirkgently

0

关键是要能够识别农产品的名称。这当然取决于产品名称的格式。如果你在这里写的是确切的,那么它表示在.之前的词(如果有的话)。让我们来构建一块比较功能块:

int struct_cmp_by_product(const void *a, const void *b) { 
    struct st_ex *ia = (struct st_ex *)a; 
    struct st_ex *ib = (struct st_ex *)b; 

首先,我们必须找到在.发生:

size_t len_a, len_b; 
    char *a_dot, *b_dot; 
    a_dot = strchr(ia->product, '.'); 
    b_dor = strchr(ib->product, '.'); 
    if (a_dot) 
     len_a = a_dot - ia->product; 
    else 
     len_a = strlen(ia->product); 
    if (b_dot) 
     len_b = b_dot - ib->product; 
    else 
     len_b = strlen(ib->product); 

然后,我们要检查如果两个产品名称都是平等的:

int res; /* define above */ 
    if (len_a == len_b && strncmp(ia->product, ib->product, len_a) == 0) 
    { 
     /* then check for price */ 
    } 
    else 
     return strcmp(ia->product, ib->product); 

,并比较价格的部分被简单地表示为:

 if (ia->price < ib->price) 
      return -1; 
     if (ia->price > ib->price) 
      return 1; 
     return 0; 

终收功能:

} 

注:我可能会丢失错误检查或有错别字,请确保您在使用它之前理解的代码。不要只复制粘贴。

+0

我认为有关使用'strchr'的观点。谢谢 – user1024718

+0

@ user1024718,如果答案对您有帮助,您可以upvote /接受它。 – Shahbaz

0
int struct_cmp_by_product(const void *a, const void *b) { 
    struct st_ex *ia = (struct st_ex *)a; 
    struct st_ex *ib = (struct st_ex *)b; 
    int rc; 
    size_t len_a, len_b, len; 

    len_a = strcspn(ia->product, "."); 
    len_b = strcspn(ib->product, "."); 
    len = (len_a > len_b) ? len_a : len_b; /* assuming ascii */ 
    rc = strncmp(ia->product, ib->product, len); 
    if (rc) return rc; 

    if (ia->price > ab-price) rc = 1; 
    else if (ia->price < ab-price) rc = -1; 
    return rc; 
} 

UPDATE:STRCMP() - >> STRNCMP +计算尺寸

+0

不会'strcmp'一直返回true?我不明白什么时候会比较价格。 – user1024718

+0

strcmp()返回零,或者小于零,或者大于零。 'if(rc)'条件测试不等于零的条件,所以比较价格只会在产品名称相同时才会发生。 – wildplasser

+0

但这不起作用。产品名称在strcmp的意义上实际上是不同的......您只需要对产品“名称”的一部分进行排序。 – DigitalRoss