用动态链表完成一个简单的商品库存信息管理系统
发布网友
发布时间:2022-04-21 20:02
我来回答
共1个回答
热心网友
时间:2023-08-14 07:25
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct goods)
struct goods
{
int codeID;
char name[50];
int mount;
struct goods *next;
};
int n;
struct goods *createlist(void)
{
struct goods *head;
struct goods *p1,*p2;
n=0;
p1=p2=(struct goods*)malloc(LEN);
printf("\ninput codeID,name and mount:\n");
scanf("%ld %s %d",&p1->codeID,p1->name,&p1->mount);
head=NULL;
while (p1->codeID!=0)
{
n=n+1;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct goods*)malloc(LEN);
printf("\ninput codeID,name and mount:\n");
scanf("%ld %s %d",&p1->codeID,p1->name,&p1->mount);
}
p2->next=NULL;
free(p1);
return head;
}
void list(struct goods *head)
{
struct goods *p;
p=head;
printf("\n codeID name mount \n");
while(p!=NULL)
{
printf("%ld %s %d",p->codeID,p->name,p->mount);
p=p->next; printf("\n");
}
printf("\n");
}
struct goods *del(struct goods *head,long codeID)
{
struct goods *p1,*p2;
if(head==NULL)
{
printf("\n list null!\n");return NULL;
p1=head;
while (codeID!=p1->codeID && p1->next!=NULL)
{
p2=p1;p1=p1->next;
}
if(p1==head) head=p1->next;
else
p2->next=p1->next;
free(p1);
printf("delete:%ld\n",codeID);
n=n-1;
}
else printf("%ld not been found!\n",codeID);
return head;
}
struct goods *destorylist(struct goods* head)
{
char word;
struct goods *p1,*p2;
p1=p2=head;
do
{
getchar();//用来接收菜单选择时的回车键
printf("\n Do you really want to destory the goods system y/n\n?");
scanf("%c",&word);//对于是否清空系统进行确认
}while(word!='y'&&word!='n');
if(word=='y')
{
if(p1)
do
{
p2=p1;
p1=p1->next;
free((void*)p2);
}while(p1);//清空系统并释放存储单元
printf("\n The goods system have been destoried!\n");
return NULL;
}
else printf("\n The operation of destory have been canceled.\n");
return head;
}
struct goods *sell(long codeID,int mount,struct goods*head)
{
struct goods *p1;
if(head==NULL)
{
printf("\nerror! the system is null!\n");
return NULL;
}//如果系统什么也没有就提示错误
for(p1=head;p1!=NULL;p1=p1->next)
if(p1->codeID==codeID) break;//找要卖的货物
if(p1)//如果找到了
{
if(p1->mount<mount)//如果货物不足就提示错误并且返回原样
{
printf("The count is too less!\n");
return head;
}
else p1->mount-=mount;//足够的话就卖出去
}
else printf("error! cannot find %ld's name\n",codeID);//找不到就提示
for(p1=head;p1!=NULL;p1=p1->next)
if(p1->mount==0) head=del(head,codeID);//查找货物为0的并把它的接点删掉
return head;
}
struct goods *stock(struct goods *head,struct goods *stu)
{
struct goods *p0,*p1,*p2;
p1=head;
p0=stu;
/*printf("input codeID and numbers:\n");
scanf("%ld %d",&p0->codeID,&p0->mount);*/
if(head==NULL)
{
head=p0;p0->next=NULL;
}
else
{
while ((p0->codeID!=p1->codeID)&&(p1!=NULL))
{
p2=p1;
p1=p1->next;
}
/*if(p0->codeID<=p1->codeID)
{
if(head==p1) head=p0;
else p2->next=p0;
p0->next=p1;
}*/
//else
if(p1==NULL)
{
p2->next=p0;p0->next=NULL;
}
else p1->mount=p1->mount+p0->mount;
}
return head;
}
void main()
{
int choose;
struct goods* head=NULL,*stu,*p0;//让head初始值为0
do//菜单选择
{
int codeID,mount;
printf("1. 输入商品信息\n2. 销售\n3. 进货\n4. 列举商品信息\n5. 清除所有商品\n6. 退出\n\n");
printf("input choose:\n");
scanf("%d",&choose);
if(choose==1)
head=createlist();
else if(choose==2)
{
printf("input codeID and mount:\n");
scanf("%ld %d",&codeID,&mount);
head=sell(codeID,mount,head);
}
else if(choose==3)
{
printf("input codeID and numbers:\n");
stu=(struct goods*) malloc(LEN);
p0=stu;
scanf("%ld %d",&p0->codeID,&p0->mount);
while(stu->codeID!=0)
{
head=stock(head,stu);
list(head);
printf("input codeID and numbers:\n");
stu=(struct goods*) malloc(LEN);
p0=stu;
scanf("%ld %d",&p0->codeID,&p0->mount);
}
}
else if(choose==4)
list(head);
else if(choose==5)
head=destorylist(head);
}
while(choose!=6);
}