#define _CRT_SECURE_NO_WARNINGS //关闭安全周期检查
#include
#include
#include
// 作用: 输出(商品)主菜单.
void hhhh()
{
printf("n <超 市 商 品 管 理 系 统>n (商品管理主菜单)n"
" ╔═════════════════════╗n"
" ║ 1.增加商品 ║n"
" ║┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄║n"
" ║ 2.删除指定商品 ║n"
" ║┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄║n"
" ║ 3.查找指定商品 ║n"
" ║┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄║n"
" ║ 4.修改指定商品 ║n"
" ║┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄║n"
" ║ 5.选择指定商品操作 ║n"
" ║┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄║n"
" ║ 6.退出系统 ║n"
" ╚═════════════════════╝n");
}
// 作用: 输出(商品)副菜单.
void hhhh1()
{
printf("n <超 市 商 品 管 理 系 统>n (商品管理子菜单)n"
" ╔═══════════════════╗n"
" ║1.删除指定商品 ║n"
" ║┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄║n"
" ║2.修改指定商品 ║n"
" ║┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄║n"
" ║3.选择指定商品操作 ║n"
" ║┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄║n"
" ║4.退出系统 ║n"
" ╚═══════════════════╝n");
}
// 作用: 输出(商品)主菜单.
void hhhh2()
{
printf("n <超 市 商 品 管 理 系 统>n (商品记录管理菜单)n"
" ╔═════════════════════╗n"
" ║ 1.增加指定商品记录 ║n"
" ║┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄║n"
" ║ 2.删除指定商品记录 ║n"
" ║┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄║n"
" ║ 3.修改指定商品记录 ║n"
" ║┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄║n"
" ║ 4.价格排序 ║n"
" ║┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄║n"
" ║ 5.数量排序 ║n"
" ║┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄║n"
" ║ 6.退出系统 ║n"
" ╚═════════════════════╝n");
}
// (商品记录)节点结构体
typedef struct ZhiJieDian
{
char ee[5];//购入或卖出
char bb[15];//时间
int cc;//数量
int dd;//价格
struct ZhiJieDian*next;
}ZJD;
// (商品)节点结构体
typedef struct JieDian
{
char ch[12];//商品名
int aa;//保质期
ZJD*nen_tou;
ZJD*nen_wei;
int val2;
struct JieDian*next;
}JD;
// (商品)单链表结构体
typedef struct LianBiao
{
JD*tou;
JD*wei;
int val;
}LB;
void gggg(LB*aaa);
void llll(JD*eee);
void jjjj(LB*aaa);
// 作用: 创建(商品)链表.
LB*aaaa()
{
LB*aaa = (LB*)malloc(sizeof(LB));
if(NULL==aaa)
{
printf(" 单链表创建失败!n");
return NULL;
}
aaa->tou = aaa->wei = NULL;
aaa->val = 0;
return aaa;
}
// 作用: 创建商品节点.
JD*bbbb(LB*aaa)
{
char ch1[12];
int aa1;
JD*bbb = (JD*)malloc(sizeof(JD));//创建节点
if(NULL==bbb)
{
printf(" 单链表节点创建失败!n");
return NULL;
}
printf(" 输入商品名:");
scanf("%s",ch1);//输入商品名
printf(" 输入商品保质期:");
scanf("%d",&aa1);//输入商品保质期
if(aaa->tou==NULL)//判断链表是否有节点
{
aaa->tou = aaa->wei = bbb;
}
bbb->nen_tou = bbb->nen_wei = NULL;//
bbb->val2 = 0;
bbb->next = NULL;//
strcpy(bbb->ch,ch1);//储存商品名
bbb->aa = aa1;//储存商品保质期
aaa->val++;//链表长度加1
return bbb;//返回节点地址
}
// 作用: 创建(商品记录)节点.
ZJD*bbbb2(JD*aaa)
{
char ee[5];//购入或卖出
char bb[15];//时间
int cc;//数量
int dd;//价格
ZJD*bbb = (ZJD*)malloc(sizeof(ZJD));//创建节点
if(NULL==bbb)
{
printf(" 单链表节点创建失败!n");
return NULL;
}
printf(" 输入购入或卖出:");
scanf("%s",ee);//输入购入或卖出
printf(" 输入时间:");
scanf("%s",bb);//输入时间
printf(" 输入数量:");
scanf("%d",&cc);//输入数量
printf(" 输入价格:");
scanf("%d",&dd);//输入价格
if(aaa->nen_tou==NULL)//判断链表是否有节点
{
aaa->nen_tou = aaa->nen_wei = bbb;
}
bbb->next = NULL;//
strcpy(bbb->ee,ee);//储存
strcpy(bbb->bb,bb);//储存
bbb->cc = cc;//储存
bbb->dd = dd;//储存
aaa->val2++;//链表长度加1
return bbb;//返回节点地址
}
// 作用: 清屏后输出命令菜单和全部商品目录.
void jjjj(LB*aaa)
{
system("cls");
hhhh();
gggg(aaa);
}
// 作用: 清屏后输出命令菜单和全部商品目录.
void jjjj2(JD*eee)
{
system("cls");
hhhh2();
llll(eee);
}
// 作用: 清屏后判断输出主菜单还是子菜单.
void kkkk(LB*aaa,int bb)
{
system("cls");
if(bb==0){
hhhh();
}
else
{
hhhh1();
}
gggg(aaa);
}
// 作用: 添加指定商品.
void cccc(LB*aaa)
{
jjjj(aaa);
int val1;
if(NULL==aaa)
{
jjjj(aaa);
printf(" 单链表不存在,商品增加失败!n");
return;
}
printf(" 输入位置:");
scanf("%d",&val1);//输入添加的商品位置
if(val1<=0||val1>aaa->val+1)
{
jjjj(aaa);
printf(" 商品增加位置错误!n");
return;
}
if(val1==1)//头部添加
{
JD*nev = bbbb(aaa);
nev->next = aaa->tou;
aaa->tou = nev;
}
else if(val1==aaa->val+1)//尾部添加
{
aaa->wei = aaa->wei->next = bbbb(aaa);
}
else
//中间添加
{
JD*nev = bbbb(aaa);
JD*nev_tou = aaa->tou;
for(int i = 1;i
{
nev_tou = nev_tou->next;
}
nev->next = nev_tou->next;
nev_tou->next = nev;
}
jjjj(aaa);//更新
}
// 作用: 添加指定商品记录.
void cccc2(JD*eee)
{
jjjj2(eee);
int val1;
if(NULL==eee)
{
jjjj2(eee);
printf(" 商品不存在!n");
return;
}
printf(" 输入位置:");
scanf("%d",&val1);//输入添加的商品位置
if(val1<=0||val1>eee->val2+1)
{
jjjj2(eee);
printf(" 商品记录增加位置错误!n");
return;
}
if(val1==1)//头部添加
{
ZJD*nev = bbbb2(eee);
nev->next = eee->nen_tou;
eee->nen_tou = nev;
}
else if(val1==eee->val2+1)//尾部添加
{
eee->nen_wei = eee->nen_wei->next = bbbb2(eee);
}
else
//中间添加
{
ZJD*nev = bbbb2(eee);
ZJD*nev_tou = eee->nen_tou;
for(int i = 1;i
{
nev_tou = nev_tou->next;
}
nev->next = nev_tou->next;
nev_tou->next = nev;
}
jjjj2(eee);//更新
}
// 作用: 删除指定商品.
void dddd(LB*aaa,int bb)
{
kkkk(aaa,bb);
int val1;
if(NULL==aaa)
{
printf(" 单链表不存在,数据删除失败!n");
return;
}
if(NULL==aaa->tou)
{
printf(" 单链表没有数据,数据删除失败!n");
return;
}
printf(" 输入删除第几行的商品:");
scanf("%d",&val1);//输入删除的商品位置
if(val1<=0||val1>aaa->val)
{
printf(" 数据删除位置错误!n");
return;
}
if(val1==1)//头部删除
{
JD*nev_tou = aaa->tou;
aaa->tou = aaa->tou->next;
free(nev_tou);
nev_tou = NULL;
}
else if(val1==aaa->val)//尾部删除
{
JD*nev_tou = aaa->tou;
for(int i = 1;ival-1;i++)
{
nev_tou = nev_tou->next;
}
nev_tou->next = NULL;
free(aaa->wei);
aaa->wei = nev_tou;
}
else
//中间删除
{
JD*nev_tou = aaa->tou;
for(int i = 1;i
{
nev_tou = nev_tou->next;
}
JD*nev = nev_tou->next->next;
free(nev_tou->next);
nev_tou->next = nev;
}
aaa->val--;
kkkk(aaa,bb);
}
// 作用: 删除指定记录.
void dddd2(JD*eee)
{
jjjj2(eee);
int val1;
if(NULL==eee)
{
printf(" 商品不存在n");
return;
}
if(NULL==eee->nen_tou)
{
printf(" 该商品没有记录,记录删除失败!n");
return;
}
printf(" 输入删除第几行的商品:");
scanf("%d",&val1);//输入删除的商品位置
if(val1<=0||val1>eee->val2)
{
printf(" 数据删除位置错误!n");
return;
}
if(val1==1)//头部删除
{
ZJD*nev_tou = eee->nen_tou;
eee->nen_tou = eee->nen_tou->next;
free(nev_tou);
nev_tou = NULL;
}
else if(val1==eee->val2)//尾部删除
{
ZJD*nev_tou = eee->nen_tou;
for(int i = 1;i
val2-1;i++) {
nev_tou = nev_tou->next;
}
nev_tou->next = NULL;
free(eee->nen_wei);
eee->nen_wei = nev_tou;
}
else
//中间删除
{
ZJD*nev_tou = eee->nen_tou;
for(int i = 1;i
{
nev_tou = nev_tou->next;
}
ZJD*nev = nev_tou->next->next;
free(nev_tou->next);
nev_tou->next = nev;
}
eee->val2--;
jjjj2(eee);
}
// 作用: 输出全部商品目录.
void gggg(LB*aaa)
{
if(NULL==aaa)
{
printf(" 单链表不存在,商品输出失败!n");
return;
}
if(NULL==aaa->tou||aaa->val==0)
{
printf(" 单链表没有数据,商品输出失败!n");
return;
}
JD*nev_tou2 = aaa->tou;
printf(" 商品名 保质期n");
for(int i = 1;i<=aaa->val;i++)
{
printf(" %-12s ",nev_tou2->ch);
printf(" %-12dn",nev_tou2->aa);
nev_tou2 = nev_tou2->next;
}
}
// 作用: 修改指定商品.
void ffff(LB*aaa,int bb)
{
kkkk(aaa,bb);
int val1,aa1;
char ch1[12];
if(NULL==aaa)
{
printf(" 单链表不存在,数据更新失败!n");
return;
}
if(NULL==aaa->tou)
{
printf(" 单链表没有数据,数据更新失败!n");
return;
}
printf(" 输入要更改的商品位置:");
scanf("%d",&val1);
if(val1<0||val1>aaa->val)
{
printf(" 位置错误!n");
return;
}
else
{
JD*nev_tou = aaa->tou;
for(int i = 1;i
{
nev_tou = nev_tou->next;
}
printf(" 更改后的商品名:");
scanf("%s",ch1);
printf(" 更改后的保质期:");
scanf("%d",&aa1);
strcpy(nev_tou->ch,ch1);
nev_tou->aa = aa1;
}
kkkk(aaa,bb);
}
// 作用: 修改指定记录.
void ffff2(JD*eee)
{
jjjj2(eee);
int val1,cc2,dd2;
char ee2[12],bb2[12];
if(NULL==eee)
{
printf(" 商品不存在n");
return;
}
if(NULL==eee->nen_tou)
{
printf(" 商品没有记录,修改失败!n");
return;
}
printf(" 输入要更改的商品位置:");
scanf("%d",&val1);
if(val1<0||val1>eee->val2)
{
printf(" 位置错误!n");
return;
}
else
{
ZJD*nev_tou = eee->nen_tou;
for(int i = 1;i
{
nev_tou = nev_tou->next;
}
printf(" 更改后为购入或卖出:");
scanf("%s",ee2);
printf(" 更改后的时间:");
scanf("%s",bb2);
printf(" 更改后的数量:");
scanf("%d",&cc2);
printf(" 更改后的价格:");
scanf("%d",&dd2);
strcpy(nev_tou->ee,ee2);
strcpy(nev_tou->bb,bb2);
nev_tou->cc = cc2;
nev_tou->dd = dd2;
}
jjjj2(eee);
}
// 作用: 查找指定商品.
LB*eeee(LB*aaa)
{
char ch1[12];
jjjj(aaa);
if(NULL==aaa)
{
printf(" 单链表不存在,商品查找失败!n");
return NULL;
}
if(NULL==aaa->tou)
{
printf(" 单链表没有商品,商品查找失败!n");
return NULL;
}
printf(" 输入查找的商品名:");
scanf("%s",ch1);//输入查找的商品名
LB*bbb = aaaa();//创建一个查找出的商品链表
JD*nev_tou = aaa->tou;//创建一个临时头节点
int ab = 0;
for(int i = 1;i<=aaa->val;i++)
{
if(strcmp(nev_tou->ch,ch1)==0)
{
ab = 1;
ZJD*nnn = nev_tou->nen_tou;
JD*nev = (JD*)malloc(sizeof(JD));//创建节点
if(bbb->tou==NULL)//判断链表是否有节点
{
bbb->tou = bbb->wei = nev;
}
else
{
bbb->wei = bbb->wei->next = nev;
}
nev->next = NULL;
strcpy(nev->ch,nev_tou->ch);//转存商品名地址
nev->aa = nev_tou->aa;//转存商品保质期地址
nev->val2 = nev_tou->val2;//转存地址
for(int i = 1;i<=nev->val2;i++)
{
ZJD*fff = (ZJD*)malloc(sizeof(ZJD));//创建商品记录节点
if(nev_tou->nen_tou==NULL)//判断链表是否有节点
{
nev_tou->nen_tou = nev_tou->nen_wei = fff;
}
else
{
nev_tou->nen_wei = nev_tou->nen_wei->next = fff;
}
strcpy(fff->ee,nnn->ee);//转存地址
strcpy(fff->bb,nnn->bb);//转存地址
fff->cc = nnn->cc;//转存地址
fff->dd = nnn->dd;//转存地址
nnn = nnn->next;
}
bbb->val++;//链表长度加1
}
nev_tou = nev_tou->next;//移动
}
if(ab)//是否查找到
{
system("cls");
hhhh1();
gggg(bbb);//输出查找出的商品链表
return bbb;
}
else
{
printf(" 没找到该商品n");
return NULL;
}
}
// 作用: 输出商品操作记录
void llll(JD*eee)
{
if(NULL==eee)
{
printf(" 商品不存在!n");
return;
}
ZJD*nev_tou = eee->nen_tou;
printf(" 商品名 保质期n");
printf(" %-10s ",eee->ch);
printf(" %-12dn",eee->aa);
if(NULL==eee->nen_tou||eee->val2==0)
{
printf(" 商品没有记录!n");
return;
}
printf(" 购入或卖出 时间 数量 价格 n");
for(int i = 1;i<=eee->val2;i++)
{
printf(" %-12s ",nev_tou->ee);
printf(" %-11s ",nev_tou->bb);
printf(" %-8d",nev_tou->cc);
printf(" %dn",nev_tou->dd);
nev_tou = nev_tou->next;
}
}
// 作用: 选择指定商品操作记录
JD*iiii(LB*aaa,int bb)
{
system("cls");
if(bb==0){
hhhh();
}
else
{
hhhh1();
}
gggg(aaa);
int val1;
if(NULL==aaa)
{
printf("单链表不存在,选择商品失败!n");
return NULL;
}
if(NULL==aaa->tou)
{
printf("单链表没有商品,选择商品失败!n");
return NULL;
}
printf("输入要选择的商品位置:");
scanf("%d",&val1);
if(val1<=0||val1>aaa->val)
{
printf("选择位置错误!n");
return NULL;
}
else
{
JD*nev_tou = aaa->tou;
for(int i = 1;i
{
nev_tou = nev_tou->next;
}
jjjj2(nev_tou);
return nev_tou;
}
}
void rrrr(LB*aaa)
{
JD*qwq = aaa->tou;
FILE*hh;
if((hh = fopen("file1.txt","w"))!=NULL)
{
for(int i = 0;aaa->val>i;i++)
{
fputs(" 商品名 保质期n",hh);
fprintf(hh,"%s%dn",qwq->ch,qwq->aa);
for(int ii = 0;qwq->val2>ii;)
{
ZJD*qwq2 = qwq->nen_tou;
fputs(" 购入或卖出 时间 数量 价格 n",hh);
for(;qwq->val2>ii;ii++)
{
fprintf(hh,"%s%s%d%dn",qwq2->ee,qwq2->bb,qwq2->cc,qwq2->dd);
qwq2 = qwq2->next;
}
qwq = qwq->next;
}
}
fclose(hh);
}
}
void pppp(JD*eee)
{
for(int ii = 1;eee->val2>ii;ii++)
{
ZJD*nev_tou = eee->nen_tou;
ZJD*nev = eee->nen_tou->next;
for(int i = 1;eee->val2>i;i++)
{
if(nev_tou->next==nev)
{
if(nev_tou->dd
dd) {
nev_tou->next = nev->next;
nev->next = nev_tou;
}
}
else
{
if(nev->dd
dd) {
nev->next = nev_tou->next;
nev_tou->next = nev;
}
}
nev_tou = nev_tou->next;
nev = nev->next;
}
}
jjjj2(eee);
}
void pppp2(JD*eee)
{
for(int ii = 1;eee->val2>ii;ii++)
{
ZJD*nev_tou = eee->nen_tou;
ZJD*nev = eee->nen_tou->next;
for(int i = 1;eee->val2>i;i++)
{
if(nev_tou->next==nev)
{
if(nev_tou->cc
cc) {
nev_tou->next = nev->next;
nev->next = nev_tou;
}
}
else
{
if(nev->cc
cc) {
nev->next = nev_tou->next;
nev_tou->next = nev;
}
}
nev_tou = nev_tou->next;
nev = nev->next;
}
}
jjjj2(eee);
}
int main()
{
LB*link = aaaa();//创建链表
LB*nev1 = NULL;
JD*eee = NULL;
char ch;
int sele1,bb = 0,mm = 1;//储存选择结果
hhhh();
gggg(link);
while(mm==1)
{
printf(" 选择:");
while(scanf("%d",&sele1)!=1||!(sele1<=6&&sele1>=1))
{
while((ch = getchar())!='n')
{
putchar(ch);
}
jjjj(link);
printf(" 输入错误,重新输入n");
printf(" 选择:");
}
switch(sele1)
{
case 1:cccc(link);//1.增加指定商品**
break;
case 2:dddd(link,bb);//2.删除指定商品**
break;
case 3:system("cls");// 3.查找指定商品**
nev1 = eeee(link);
if(NULL==nev1){
}
else if(NULL==nev1->tou){
}
else
{
for(bb = 1,mm = 0;mm==0;)
{
printf(" 选择:");
while(scanf("%d",&sele1)!=1||!(sele1<=4&&sele1>=1))
{
while((ch = getchar())!='n')
{
putchar(ch);
}
kkkk(link,bb);
printf(" 输入错误,重新输入n");
printf(" 选择:");
}
switch(sele1)
{
case 1:dddd(nev1,bb);//1.删除指定商品**
break;
case 2:ffff(nev1,bb);//2.修改指定商品**
break;
case 3:eee = iiii(nev1,bb);//3.选择指定商品操作记录
for(mm = 1;mm==1;)
{
printf(" 选择:");
while(scanf("%d",&sele1)!=1||!(sele1<=4&&sele1>=1))
{
while((ch = getchar())!='n')
{
putchar(ch);
}
jjjj2(eee);
printf(" 输入错误,重新输入n");
printf(" 选择:");
}
switch(sele1)
{
case 1:cccc2(eee);//1.1.增加指定商品记录
break;
case 2:dddd2(eee);//2.删除指定商品记录
break;
case 3:ffff2(eee);//3.修改指定商品记录
break;
case 4:pppp(eee);//4.数量排序
break;
case 5:pppp2(eee);//5.价格排序
break;
case 6:mm = 0;//6.退出子菜单
kkkk(nev1,bb);
break;
}
}
break;
case 4:mm = 1;//4.退出子菜单
free(nev1);
nev1 = NULL;
jjjj(link);
break;
}
}
}
break;
case 4:ffff(link,bb);//4.修改指定商品
break;
case 5:eee = iiii(link,bb);//5.选择指定商品操作
if(NULL==eee){
}
else
{
for(mm = 0;mm==0;)
{
printf(" 选择:");
while(scanf("%d",&sele1)!=1||!(sele1<=4&&sele1>=1))
{
while((ch = getchar())!='n')
{
putchar(ch);
}
jjjj2(eee);
printf(" 输入错误,重新输入n");
printf(" 选择:");
}
switch(sele1)
{
case 1:cccc2(eee);//1.1.增加指定商品记录
break;
case 2:dddd2(eee);//2.删除指定商品记录
break;
case 3:ffff2(eee);//3.修改指定商品记录
break;
case 4:pppp(eee);//4.数量排序
break;
case 5:pppp2(eee);//4.价格排序
break;
case 6:mm = 1;//5.退出子菜单
jjjj(link);
break;
}
}
}
break;
case 6:mm = 0;//6.退出系统
rrrr(link);
break;
}
bb = 0;
}
}



