gpt4 book ai didi

在 C 中创建嵌套结构的动态自定义数组

转载 作者:行者123 更新时间:2023-11-30 17:53:35 25 4
gpt4 key购买 nike

我正在尝试创建以下数组:

"Fruits", 25, {
{"Apple", 2},
{"Grapes", 13},
{"Melon", 10}
}
"Meats", 40, {
{"Beef", 9},
{"Chicken", 27},
{"Pork", 4}
}

...

感觉有一种更优雅的方式来完成我到目前为止所做的事情。任何有关如何在给定输入结构的情况下更有效地创建此结构的反馈/示例将不胜感激。

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

typedef struct Product {
char *name;
int qty;
} Prods;

typedef struct Category {
char *name;
int qty;
int prods_count;
Prods *prod;
} Cats;

typedef struct Inventory {
Cats *cat;
int cats_count;
} Inv;

struct tmp_input {
char name[12];
int qty;
char cat[12];
};

// return index if found
int in_array(Inv *inv, char *k) {
int i;
if (inv->cats_count == 0)
return -1;
for (i = 0; i < inv->cats_count; i++) {
if (strcmp (k, inv->cat[i].name) == 0) return i;
}
return -1;
}

int main () {
int i, j, exists = 0;
// temp struct.
struct tmp_input items[] = {
{"Apple", 2, "Fruit"}, {"Coke", 10, "Drink"}, {"Pork", 4, "Meat"},
{"Beef", 9, "Meat"}, {"Chicken", 27, "Meat"}, {"Melon", 10, "Fruit"},
{"Tea", 3, "Drink"}, {"Coffee", 20, "Drink"}, {"Grapes", 13, "Fruit"}
};

size_t len = sizeof (items) / sizeof (struct tmp_input);

Inv *inven = malloc(sizeof(Inv));
inven->cats_count = 0;
inven->cat = calloc(1, sizeof(Cats));

for (i = 0; i < len; i++) {
exists = in_array(inven, items[i].cat);
// category does not exist
if (exists == -1) {
inven->cat = realloc(inven->cat, sizeof(Cats) * (inven->cats_count + 1));
inven->cat[inven->cats_count].name = strdup(items[i].cat);
inven->cat[inven->cats_count].qty += items[i].qty;

inven->cat[inven->cats_count].prods_count = 1;
inven->cat[inven->cats_count].prod = calloc (1, sizeof (Prods));
inven->cat[inven->cats_count].prod->name = strdup (items[i].name);
inven->cat[inven->cats_count].prod->qty = items[i].qty;
inven->cats_count++;
}
// category found
else {
inven->cat[exists].qty += items[i].qty;

int size = inven->cat[exists].prods_count + 1;
inven->cat[exists].prod = realloc(inven->cat[exists].prod, sizeof(Prods) * (size));
inven->cat[exists].prod[size - 1].name = strdup (items[i].name);
inven->cat[exists].prod[size - 1].qty= items[i].qty;
inven->cat[exists].prods_count++;
}
}

for (i = 0; i < inven->cats_count; i++) {
printf("%3d %s\n", inven->cat[i].qty, inven->cat[i].name);
for (j = 0; j < inven->cat[i].prods_count; j++) {
printf("%3d %s\n", inven->cat[i].prod[j].qty, inven->cat[i].prod[j].name);
}
}

return 0;
}

最佳答案

您没有为 Prod 数组分配任何内存。

类似的东西

...

if (exists == -1) {
inven->cat = realloc(inven->cat, sizeof(Cats) * (inven->cats_count + 1));
inven->cat[inven->cats_count].name = items[i].cat;
inven->cat[inven->cats_count].qty += items[i].qty;

// Allocate memory for 1 product
inven->cat[inven->cats_count].prods_count = 1;
inven->cat[inven->cats_count].prod = malloc (sizeof (Prods));

// Now allocate space and copy the name
inven->cat[inven->cats_count].prod->name = strdup (items[i].name + 1);

inven->cats_count++;
}
...

我将让您来处理一个类别中有超过 1 个产品的情况,此时您需要再次重新分配内存。

另一个错误是需要分配并复制类别名称

inven->cat[inven->cats_count].name = items[i].cat;

应替换为

inven->cat[inven->cats_count].name = strdup (items[i].cat);

这是因为 items 数组不存在于该函数之外,因此如果您这样做

inven->cat[inven->cats_count].name = items[i].cat;

那么在你离开这个函数后,invent->cat[inven->cats_count].name将指向垃圾内存。

最后的建议是将每个结构拆分为一个处理其创建的函数,只是为了清理代码。

--- 编辑以添加对抽象数据类型的注释

如果您知道将通过索引访问数据,则数组非常有用。如果您不知道所需项目的索引(如本例所示),则数组的用处不大。

与其他评论不同,我认为使用链接列表并不会真正给您带来任何有用的东西。当您需要按顺序遍历所有项目而不真正关心它们在列表中的位置时,链接列表非常有用。在我看来,像您正在创建的系统最常见的用例是搜索:我们有库存水果吗?在库存中添加 10 箱可乐...诸如此类的事情。

此外,您只需要每个类别/产品有一个条目。您不希望数据中有 3 个水果类别。数组和链表对于多次添加相同的结构实际上没有任何限制。这意味着每次您都需要检查整个列表以查看是否需要添加新结构。

出于这个原因,我当然会将类别和产品数组都放入映射名称 -> 结构的哈希表(或在某些语言中称为字典)中。这将加快您的搜索速度,因为您不必每次都搜索整个数据集,并且可以防止您多次添加相同的结构。

关于哈希表的维基百科文章:http://en.wikipedia.org/wiki/Hashtable

关于在 C 中创建嵌套结构的动态自定义数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15489654/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com