2015-06-14 60 views
-6

我是BS计算机科学的学生。我正在研究库存系统,这是我从uni开始的第一个项目。但是我在结束库存时遇到了一个问题。我想添加开盘和购买,然后减去卖出。但我无法这样做。请帮我解决这个问题。我想在c语言中乘以两种不同的结构

+1

欢迎来到StackOverflow。你能提供更多细节和一些与你的问题相关的代码吗? – johnnyRose

+0

向我们展示问题所在的代码,告诉我们它做了什么以及它应该做些什么... http://sscce.org – Arkku

+0

我无法在dev C++中打印两个不同结构中使用的两个不同值... ..我想打印它在一行 –

回答

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) 
} 

所以,一共是从两种结构(我假设每个数据数组的第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; 
} 

而且你去那里 - 一种方法来访问跨结构的多个实例的结构元素和方式访问和操作结构中的元素。

好运

+0

谢谢亲爱的朋友........ –