繁体   English   中英

内部包含数组的结构数组

[英]Struct array with array inside it

我在课堂上有一个作业,我想将变量保存在结构数组中,因为我认为这将是最干净的方法。

我用颜料画了这张快速图只是为了更容易解释结构数组思想

这只是结构中的“节点”之一,我想制作结构,这样我就可以在绘图中的文件之后复制所有内容

不完全确定我是如何做这件事的

到目前为止,我得到了一个可以正常工作的结构数组。 但我似乎无法弄清楚如何添加最后一行,最后一行我的意思是杂货店 1-3、数量 1-3 和单价 1-3。

对于我制作的结构:
结构

这部分工作正常,但仍然缺少最后一个“节点”

下面是在 C 中的结构体内部创建结构体数组的简短示例。我将其设为结构体侧面的结构体数组,因为我认为您可能想要自定义每个“杂货”/“价格”/ “单价”是。

在下面的代码中,我使用“malloc”来动态分配结构数组。 这是动态分配内存的“C”风格方式。 如果您使用的是 C++,那么您应该使用“new”

我只用 [0] 分配数组中的第一项。 您可以分别使用 [1] 和 [2] 索引分配其他人。

struct Grocery
{
   /* fill in details of Grocery here you could add more than 1 item*/
   int Value;
}

struct Quantity
{
   /* fill in details of Quantity here */
   int Value;
}

struct Unitprice
{
   /* fill in details of Unitprice here */
   int Value;
}

struct file
{
   struct Grocery* groceries;
   int totalGroceries;
   
   struct Quantity* quantities;
   int totalQuantities;
   
   struct Unitprice* unitprices;
   int totalUnitprices;
}

int main()
{
   struct file Table;
   
   Table.totalGroceries = 3
   Table.groceries = (struct Grocery*)malloc(Table.totalGroceries * sizeof(Grocery));
   
   Table.totalQuantities = 3
   Table.quantities = (struct Quantity*)malloc(Table.totalQuantities * sizeof(Quantity));
   
   Table.totalUnitprices = 3
   Table.unitprices = (struct Unitprice*)malloc(Table.totalUnitprices * sizeof(Unitprice));
   
   /* Assign details to groceries here */
   Table.groceries[0].Value = 5;
   
   /* Assign details to Quantity here */
   Table.quantities[0].Value = 5;
   
   /* Assign details to unitprices here */
   Table.unitprices[0].Value = 5;
   
   /* Other logic here */
   
   /* End of program deallocate allocated memory */ 
   free(Table.groceries);
   free(Table.quantities);
   free(Table.unitprices);
}

有关更多信息,我建议您查看此链接https://stackoverflow.com/questions/260915/how-can-i-create-a-dynamically-sized-array-of-structs#:~:text=If%20you %20want%20to%20allocate,type%20void%20(%20void*%20)关于如何在 C 中动态分配结构数组

虽然您的绘图和您的描述是在冲突在一定程度上,它似乎你想了许多项目文件的能够存储数据,其中每个项目协调一个grocery值, quantity值和unitprice值(类似于您将用作库存系统的基础)。

从您的绘图中,您的Filenr结构成员没有多大意义。 为什么? 如果你的基础Item结构坐标groceryquantityunitprice ,那么除非Filenr提供的信息的附加唯一的位即是,部分groceryquantityunitprice ,那么它可以简单地省略,你可以使用它的索引来形容在groceryquantityunitprice

此外,作为char类型,其值将被限制为 256 个值之一( -128 - 127 )。 如果您实际上将其视为可打印字符,那么您的值范围将减少到仅 96 个可打印字符。

您可以根据您拥有的数据定义结构。 如果Filenr是一块数据的是你的输入的一部分,并且相关联groceryquantity ,以及unitprice ,然后将其添加到你的结构作为成员。 如果您使用的是指一种独特的结构,则不需要它只是东西-除非-你可以有两种结构在完全相同的值groceryquantityunitprice和你正在使用Filenr消除歧义在两个其他相同的结构之间。 否则,省略它并使用索引。

没有它,您只需创建一个结构数组。 这将使您能够按任何成员值进行排序、查询和求和。 打印存储(虚构)值的简单实现是:

#include <stdio.h>

typedef struct {                /* your struct using a typedef for convenience */
    int grocery, quantity;
    double unitprice;
} item;

/* simple function to output all n struct in any array of item */
void prn_items (item *items, size_t n)
{
    for (size_t i = 0; i < n; i++)
        printf ("\nFilenr[%2zu]:\n grocery   : %d\n quantity  : %d\n unitprice : %0.4f\n",
                i, items[i].grocery, items[i].quantity, items[i].unitprice);
}

int main (void) {
    
    item files[] = {{ 31756, 22, 1.3405 },      /* made-up example data for array */
                    {  7818, 83, 2.4722 },
                    { 17920, 63, 1.3795 },
                    {  2937, 32, 2.8648 },
                    {  8423, 44, 2.6031 }};
    size_t n = sizeof files / sizeof *files;    /* number of elements in array */
    
    prn_items (files, n);                       /* output all struct */
}

示例使用/输出

运行程序简单地将所有存储的由索引协调的结构值输出为Filenr

$ ./bin/grocerystruct

Filenr[ 0]:
 grocery   : 31756
 quantity  : 22
 unitprice : 1.3405

Filenr[ 1]:
 grocery   : 7818
 quantity  : 83
 unitprice : 2.4722

Filenr[ 2]:
 grocery   : 17920
 quantity  : 63
 unitprice : 1.3795

Filenr[ 3]:
 grocery   : 2937
 quantity  : 32
 unitprice : 2.8648

Filenr[ 4]:
 grocery   : 8423
 quantity  : 44
 unitprice : 2.6031

现在,如果您打算将每个结构保存在单独的文件中,您可以简单地使用sprintf()创建一个输出文件名,该文件名包含基于索引的唯一后缀(或前缀)。

如果你真的想要一个单一的charFilenr作为你结构的一部分,你可以简单地通过将它作为成员添加回来来包含它,例如

typedef struct {                /* your struct using a typedef for convenience */
    char Filenr;
    int grocery, quantity;
    double unitprice;
} item;

调整代码提醒处理新增成员。

最后,您不想使用与价格相关的浮点数 (当你因为舍入错误而亏钱时,公司会生气)。 最好使用相应乘以的整数值以确保不会发生舍入错误。 您可以搜索“货币的浮点类型”并找到有关该主题的大量附加信息。

仔细检查一下,如果您还有其他问题,请告诉我。

暂无
暂无

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

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