#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
typedef struct
{
int no;
char name[5];
int number;
}Data;
typedef struct node
{
Data inform;
struct node* next;
}LNode,*LinkList;
void Onit_LinkList(LinkList L);
LinkList Creat_LinkList(LinkList L);
void Print_LinkList(LNode* L);
LinkList Search_Data(int No,LinkList L);
LinkList In_Data(Data d,LinkList L);
void Out_Data(int No,int number,LinkList L);
LinkList Sort_LinkList(LinkList L);
LNode* Compare_NO(LinkList L);
LNode* Mix(LinkList L);
int main()
{
int option;
LNode* L;
L=(LNode*)malloc(sizeof(LNode));
Onit_LinkList(L);
do
{
cout<<"\n----------------------------------"
<<"请选择相应的操作"
<<"------------------------------\n"
<<"1.........录入货物信息"<<endl
<<"2.........查找货物并输出相应信息"<<endl
<<"3.........加入货物"<<endl
<<"4.........提取货物"<<endl
<<"5.........输出货物信息"<<endl
<<"0.........退出仓库管理系统"<<endl;
cin>>option;
switch(option)
{
case 0:
break;
case 1: L=Creat_LinkList(L);
break;
case 2:
{
int n1;
LinkList L1;
L1=(LNode*)malloc(sizeof(LNode));
cout<<"\n键入0退出查找。\n";
do
{
cout<<"\n输入要查找的货号:";
cin>>n1;
if(n1==0)break;
L1=Search_Data(n1, L);
if(L1==NULL)
{
cout<<"\n找不到指定的货物!!!!\n";
}
else
{
cout<<"------------------------------------\n"
<<"货物代号"<<setw(15)
<<"货物名称"<<setw(15)
<<"货物数量"<<endl
<<L1->inform.no<<setw(18)
<<L1->inform.name<<setw(18)
<<L1->inform.number<<endl;
}
}while(n1);
break;
}
case 3:
{
LinkList L1;
L1=(LNode*)malloc(sizeof(LNode));
cout<<"键入0退出加入货物。\n";
do
{
Data d;
cout<<"\n输入加入的货号:";
cin>>d.no;
L1=Search_Data(d.no,L);
if(d.no==0)break;
if(L1!=NULL)
{
cout<<"\n仓库中已存在要加入的货物,\n"
<<"只需输入加入的数量:";
cin>>d.number;
}
else
{
cout<<"\n输入货物名称及数量:";
cin>>d.name>>d.number;
}
L=In_Data(d,L);
}while(1);
break;
}
case 4:
{
LinkList L1;
L1=(LNode*)malloc(sizeof(LNode));
cout<<"\n键入0退出提取货物。\n";
do
{
int No,number;
cout<<"\n输入要提取货号:";
cin>>No;
if(No==0)break;
L1=Search_Data(No,L);
if(L1==NULL)
{
cout<<"该货物信息不存在!!!\n";
continue;
}
cout<<"------------------------------------\n"
<<"货物代号"<<setw(15)
<<"货物名称"<<setw(15)
<<"货物数量"<<endl
<<L1->inform.no<<setw(18)
<<L1->inform.name<<setw(18)
<<L1->inform.number<<endl;
cout<<"\n输入提取货物的数量:";
cin>>number;
Out_Data( No,number,L);
}while(1);
break;
}
case 5:
{
Print_LinkList(L);
break;
}
default:
cout<<"无效操作!!";
break;
}
}while(option);
return 0;
}
void Onit_LinkList(LinkList L)
{
L->next=NULL;
}
LinkList Creat_LinkList(LinkList L)
{
LNode *s,*r=L;
cout<<"键入0退出录入。\n";
do
{
cout<<"\n输入货号:";
s=(LNode*)malloc(sizeof(LNode));
cin>>s->inform.no;
if(s->inform.no==0)
{
free(s);
break;
}
if(r->inform.no==s->inform.no)
{
cout<<"\n货号重复!!!!\n";
continue;
}
cout<<"\n输入货物名称及数量:";
cin>>s->inform.name
>>s->inform.number;
r->next=s;
r=s;
}while(1);
if(r!=NULL)r->next=NULL;
L=Sort_LinkList(L);
return L;
}
void Print_LinkList(LNode* L)
{
LNode *r=L;
if(L->next==NULL)cout<<"仓库中无货物\n";
else
{
cout<<"------------------------------------\n"
<<"货物代号"<<setw(15)
<<"货物名称"<<setw(15)
<<"货物数量"<<endl;
do
{
r=r->next;
cout<<r->inform.no<<setw(18)
<<r->inform.name<<setw(18)
<<r->inform.number
<<endl;
}while(r->next!=NULL);
}
}
LinkList Search_Data(int No,LinkList L)
{
LinkList r;
r=L->next;
while(r!=NULL && r->inform.no!=No)
r=r->next;
return r;
}
LinkList In_Data(Data d,LinkList L)
{
LinkList L1;
L1=(LNode*)malloc(sizeof(LNode));
L1=Search_Data(d.no,L);
if(L1==NULL)
{
LNode *r,*p,*s;
r=L->next;
s=L;
p=(LNode*)malloc(sizeof(LNode));
p->inform.no=d.no;
for(int i=0;i<6;i++)
{
p->inform.name[i]=d.name[i];
}
p->inform.number=d.number;
while(r->inform.no<d.no)r=r->next;
while(s->next!=r)s=s->next;
p->next=s->next;
s->next=p;
}
else
L1->inform.number +=d.number;
return L;
}
LinkList Sort_LinkList(LinkList L)
{
LinkList new_list;
LNode *r,*p,*mix;
new_list=(LNode*)malloc(sizeof(LNode));
new_list->next=NULL;
r=new_list;
mix=Mix(L);
do
{
p=(LNode*)malloc(sizeof(LNode));
p=Compare_NO(L);
r->next=p;
r=p;
}while(p->inform.no<mix->inform.no);
if(r!=NULL)r->next=NULL;
L=new_list;
return L;
}
void Out_Data(int No,int number,LinkList L)
{
LinkList L1;
L1=(LNode*)malloc(sizeof(LNode));
L1=Search_Data(No,L);
if(L1->inform.number>=number)
L1->inform.number-=number;
else
{
char p;
cout<<"\n该货物库存不足,是否提取(Y/N):";
cin>>p;
if(p=='y'||p=='Y')
{
cout<<"\n货物"<<L1->inform.name<<endl
<<"提取了"<<L1->inform.number
<<"件,还需"<<number-L1->inform.number
<<"件才能满足您的要求~~"<<endl;
L1->inform.number=0;
}
}
}
LNode* Compare_NO(LinkList L)
{
LNode *r,*p,*s;
s=(LNode*)malloc(sizeof(LNode));
p=L->next;
if(p->next==NULL)return p;
r=p->next;
while(r!=NULL)
{
if(p->inform.no>r->inform.no)p=r;
r=r->next;
}
*s=*p;
while(L->next!=p)L=L->next;
if(p->next==NULL)L->next=NULL;
else L->next=p->next;
free(p);
return s;
}
LNode* Mix(LinkList L)
{
LNode *r,*p,*s;
s=(LNode*)malloc(sizeof(LNode));
p=L->next;
if(p->next==NULL)return p;
r=p->next;
while(r!=NULL)
{
if(p->inform.no<r->inform.no)p=r;
r=r->next;
}
return p;
}
刚学链表时候做的.