简体   繁体   English

在嵌套结构数组上使用 qsort

[英]using qsort on a nested struct array

Below are my Structures and qsort declaration (to be arranged based on nID)下面是我的 Structures 和 qsort 声明(根据 nID 排列)

EDIT:编辑:

struct items{
    int nID;
    int nQuantity;
    int nItems;
    float fPrice;
    char cItemName[21];
    char cCategory[21];
    char cItemDesc[31];
};

struct Register{
    struct items List[20];
    char cID[11];
    char cPassword[11];
    char cAddress[31];
    char cContact[16];
    char cName[21];
}; 

int main ()
{
  /*These are the contents of List[]*/
  Users[nInd].List[1].nID = 1;
  strcpy(Users[nInd].List[1].cItemName, "iPhoneMAXMAX");
  strcpy(Users[nInd].List[1].cCategory, "Gadgettronss");
  strcpy(Users[nInd].List[1].cItemDesc, "This is an iphone");
  Users[nInd].List[1].nQuantity = 1;
  Users[nInd].List[1].fPrice = 100;

  Users[nInd].List[0].nID = 50;
  strcpy(Users[nInd].List[0].cItemName, "iPhone");
  strcpy(Users[nInd].List[0].cCategory, "Gadgets");
  strcpy(Users[nInd].List[0].cItemDesc, "This is an iphone");
  Users[nInd].List[0].nQuantity = 20;
  Users[nInd].List[0].fPrice = 50;

  for (i = 0; i < 2; i++)
    qsort (&Users[nInd].List[i], 2, sizeof (struct Register), sort);

  for (i = 0; i < 2; i++)
    printf ("%11d  %20s\t %15s  \t   % 10.2f  \t\t      %2d\n", 
        Users[nInd].List[i].nID, Users[nInd].List[i].cItemName,
        Users[nInd].List[i].cCategory, Users[nInd].List[i].fPrice, 
        Users[nInd].List[i].nQuantity);
/*rest of the code*/
}

Ideal Output:理想输出:

Product ID Item Name Category Price Quantity产品编号 项目名称 类别 价格 数量

1 Iphone Gadgets 50.00 20 1 Iphone 小工具 50.00 20

50 IphoneMAXMAX Gadgettronss 100.00 1 50 IphoneMAXMAX Gadgettronss 100.00 1

Actual Ouput:实际输出:

50 IphoneMAXMAX Gadgettronss 100.00 1 50 IphoneMAXMAX Gadgettronss 100.00 1

1 Iphone Gadgets 50.00 20 1 Iphone 小工具 50.00 20

However, my problem is that when I display the contents of List[] nothing changes.但是,我的问题是当我显示 List[] 的内容时没有任何变化。

This is my comparator function for qsort:这是我的 qsort 比较器函数:

int sort (const void*p, const void*q)
{
  const struct Register *ip = (struct Register*)p;
  const struct Register *iq = (struct Register*)q;

  if (ip->List[0].nID > iq->List[1].nID)
    return 1;
  else if (ip->List[0].nID < iq->List[1].nID)
    return -1;
  else
    return 0;

}

If you want to sort the elements in the List array using qsort , then do eg如果要使用qsortList数组中的元素进行排序,则执行 eg

qsort(Users[nInd].List, NumberOfElementsInList, sizeof(struct items), CompareItems);

This will sort the NumberOfElementsInList first struct items elements in the array Users[nInd].List .这将对数组Users[nInd].ListNumberOfElementsInList第一个struct items元素进行排序。

Your comparison function receives pointers to the struct items elements in the array:您的比较函数接收指向数组中struct items元素的指针:

int CompareItems(const void *a, const void *b)
{
    const struct items *item_a = (const struct items *) a;
    const struct items *item_b = (const struct items *) b;

    if (item_a->nID > item_b->nID)
        return 1;
    else if (item_a->nID < item_b->nID)
        return -1;
    else
        return 0;
}

If you want to "sort" only the two first elements in the Users[nInd].List array then there's no need for the qsort function, just compare the two elements directly and swap if needed:如果您只想“排序” Users[nInd].List数组中的前两个元素,则不需要qsort函数,只需直接比较两个元素并在需要时交换:

if (Users[nInd].List[0].nID > Users[nInd].List[1].nID)
{
    // Swap the items as index 0 and 1, thereby sorting them
    struct items temp_item = Users[nInd].List[0];
    Users[nInd].List[0] = Users[nInd].List[1];
    Users[nInd].List[1] = temp_item;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM