这个是我写的代码而且编译运行完全正确,大家写写看互相学习,顺便帮我看看有哪些不合理的地方
主要是排版问题!
/*******************************************
作者:发哥
版本历史:2008-11-15 V1.0
********************************************/
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct goods)
struct goods
{
long number;
char name[20];
int count;
struct goods* next;
};
struct goods* creat(struct goods* head);//链表构造函数
struct goods* destory(struct goods* head);//清空物品函数
struct goods* sell(long number,int count,struct goods* head);//售货函数
struct goods* stock(long number,int count,struct goods* head);//进货函数
void list(struct goods* head);//打印函数
struct goods* del(struct goods* p1,struct goods* head);//删除一个接点函数
void main()
{
struct goods* head=NULL;//让head初始值为0
long number;
int count,choose;
do//菜单选择
{
printf("1. 输入商品信息\n2. 销售\n3. 进货\n4. 列举商品信息\n5. 清除所有商品\n6. 退出\n\n");
printf("input choose:\n");
scanf("%d",&choose);
if(choose==1) head=creat(head);
else if(choose==2)
{
printf("input number and count:\n");
scanf("%ld%d",&number,&count);
head=sell(number,count,head);
}
else if(choose==3)
{
printf("input number and count:\n");
scanf("%ld%d",&number,&count);
head=stock(number,count,head);
}
else if(choose==4) list(head);
else if(choose==5) head=destory(head);
}while(choose!=6);
}
struct goods* creat(struct goods* head1)
{
struct goods* head=NULL,*p1,*p2;
int n=0;
if(head1) destory(head1);//如果head有非0的值就先清空
p2=p1=(struct goods*)malloc(LEN);//开辟存储单元
printf("input number and name:\n");
scanf("%ld%s",&p2->number,p2->name);
p2->count=0;
while(p2->number)//链表的创建
{
if(n==0) head=p1;
else p1->next=p2;
p1=p2;
p2=(struct goods*)malloc(LEN);
printf("input number and name:\n");
scanf("%ld%s",&p2->number,p2->name);
p2->count=0;
n++;
}
p1->next=NULL;
return head;
}
struct goods*destory(struct goods* head)
{
char word;
struct goods *p1,*p2;
p1=p2=head;
do
{
getchar();//用来接收菜单选择时的回车键
printf("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("the goods system have been destoried!\n");
return NULL;
}
else printf("the operation of destory have been canceled\n");//反悔就不清空原样返回
return head;
}
struct goods* sell(long number,int count,struct goods* head)
{
struct goods *p1,*p2;
if(head==NULL)
{
printf("error! the system is null!\n");
system("pause");
return NULL;
}//如果系统什么也没有就提示错误
for(p1=head;p1!=NULL;p1=p1->next)
if(p1->number==number) break;//找要卖的货物
if(p1)//如果找到了
{
if(p1->count<count)//如果货物不足就提示错误并且返回原样
{
printf("The count is too less!\n");
return head;
}
else p1->count-=count;//足够的话就卖出去
}
else
printf("error! cannot find %ld's name\n",number);//找不到就提示
for(p1=head;p1!=NULL;p1=p1->next)
if(p1->count==0) head=del(p1,head);//查找货物为0的并把它的接点删掉
return head;
}
struct goods* stock(long number,int count,struct goods* head)
{
struct goods *p1,*p2;
for(p1=head;p1;p1=p1->next)
if(p1->number==number)//如果找到了对应的号就进货
{
p1->count+=count;
break;
}
if(p1==NULL)
{
p2=(struct goods*)malloc(LEN);//找不到就建立一个接点接在最前面
printf("input name:\n");
getchar();//接收空格
gets(p2->name);//对新货物进行初始化
p2->number=number;
p2->count=count;
p2->next=head;
head=p2;
}
return head;//把表头返回去
}
void list(struct goods* head)
{
if(head)
do
{
printf("%ld %s %d\n",head->number,head->name,head->count);
head=head->next;
}while(head);//打印系统清单
else printf("the system is null!\n");
}
struct goods* del(struct goods* p1,struct goods* head)
{
struct goods *p2;
if(p1==head) head=head->next;//如果接点是表头就把表头移后
else if(p1==NULL) return head;//如果找不到要删除的点就不删除
else
{
for(p2=head;;p2=p2->next)
if(p2->next==p1)//删除非表头的接点
{
p2->next=p1->next;
break;
}
}
free((void*)p1);//删除并释放空间
return head;
}
[[it] 本帖最后由 faminxmu 于 2008-12-10 20:13 编辑 [/it]]