我想用c语言

时间:2015-06-14 13:25:55

标签: c

我是BS计算机科学的学生。我正在研究库存系统,这是我从大学的第一个项目。但我在处理结束库存时面临一个问题。我想添加开盘和购买,然后从中减去卖出..但我无法这样做。请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

考虑结构:

struct inventory
{
    char id[10];
    char item[20];
    int  quant;
    int cost;
} data[20], data1[20];

现在,我们通过商店获取库存,然后我们通过仓库获得另一个库存。然后我们想要一个总库存(数据和数据1)。我们可以执行以下操作,包括打印输出:

int total;
for (i = 0; i < 20; i++)
{
    total = data[i].quant + data1[i].quant;
    printf("Item %s, ID %s: ", data[i].item, data[i].id);
    printf("Store: %5d  Warehouse: %5d  Total: %6d\n",
           data[i].quant, data1[i].quant, total)
}

所以,total是来自两个结构的总数(我假设每个数据数组的第i个元素是针对相同的项目 - 你应该在打印之前检查一下)。打印输出将在一行上进行(因为第一个printf的末尾没有\ n)。

现在,如果你想操纵结构的元素,那也很简单。考虑:

struct items
{
    int opening, purchase, sell;
} element;

int remaining;
// Calculate the remaining items:
remaining = element.opening + element.purchase - element.sell;

// ... <other processing>
// Do printouts, etc. with information
// ...

// Now update structure for the next cycle.
element.opening  = remaining;
element.purchase = 0;
element.sell     = 0;

此示例显示了操纵结构的元素。您还可以使用函数执行相同的操作并将指针传递给结构。这实际上更灵活,因为它不关心或知道您拥有多少不同的库存物品:

int getRemaining(struct items *item)
{
    int remaining;
    remaining = item->open + item->purchase - item->sell;
    item->open = remaining;
    item->purchase = 0;
    item->sell     = 0;
    return remaining;
}

然后你去了 - 一种跨结构的多个实例访问结构元素的方法,以及一种访问和操作结构中元素的方法。

祝你好运