我有一个产品销售管理计划,我有一个用于存储产品数据的结构,另一个用于存储客户数据,另一个用于存储销售数据。当我插入新的销售时,必须与现有的产品序列号和现有客户的ID相关联。如何更新数据产品和客户结构更新销售结构?
以下是我所拥有的:
typedef struct{
char serialnumber[10]
char description[100]
float price
}stproducts;
typedef struct{
int id;
char name[50]
char adress[100]
int phonenumber
}stcustomers;
typedef struct{
int idcustomersale;
char serialnumberproductsale[10]
float endprice;
}stsales;
int main()
{
stproducts pr[1000];
int countproducts =0;
stcustomers cust[500];
int countcustomers=0;
stsales sal[1000];
int countsales=0;
}
部分功能插件销售:
void insertsale(stsales sal[], int *countsales, stproduct pr[], int countproduct,stcustomers cust[], int countcustomers)
{
char psale[10];
int number;
consultproducts(pr, countproducts);
consultcustomers(cust,countcustomers);
printf("insert the product serial number of the sale:");
fgets(psale, sizeof psale, stdin);
strcpy(sal[*countsales].serialnumberproductsale,psale);
printf("insert the customer id of the sale:");
scanf ("%d", &number);
sal[*countsales].idcustomersale=number;
//....................
}
例如,让我们想象在客户结构上更改了id字段,并且还必须自动更新与销售相关联的客户ID(idcustomersale)。
答案 0 :(得分:0)
答案很简单,它是一个独立于您使用的语言的基本原则:避免重复/复制信息。
让sale
引用其他对象(即产品和客户)。正如您在关系数据库中使用外键一样。
假设每次销售都是由单一产品组成的:
typedef struct {
stcustomers* customer;
stproducts* product;
} stsales;
所以,让我们说销售保存在一个静态数组中
的销售实例,就像你已经完成的那样
unsigned int n=0; // the count of sales reached at that point in time
stsales sal[MAXSALES];
stsales *p;
...
p = &sal[n++];
或指针,它们按需分配
stsales *sal[MAXSALES];
p = malloc(sizeof(stsales)); // no error checking, just a demo!
sal[n++] = p;
在这两种情况下,您都可以使用
访问您复制过的字段(以及任何其他字段)p->customer->id;
p->product->serialnumber;
我建议您通过dynamic memory allocation管理您的数据结构(针对销售和其他对象)。
因此,假设sal
和n
是全局变量(即在main
之外声明,不是一个非常好的设计实践,但它现在会做),{{1}功能可能变成
insertsale
当然,如果/删除销售实例应该被释放。我离开练习,了解你是如何做到的。 :)