C语言版商品库存管理系统.docx
《C语言版商品库存管理系统.docx》由会员分享,可在线阅读,更多相关《C语言版商品库存管理系统.docx(24页珍藏版)》请在冰豆网上搜索。
![C语言版商品库存管理系统.docx](https://file1.bdocx.com/fileroot1/2023-2/22/20f155d8-bcfe-4a01-8f50-e86314c14b7e/20f155d8-bcfe-4a01-8f50-e86314c14b7e1.gif)
C语言版商品库存管理系统
建议使用VS打开运行
#pragmaonce
#include
typedefstructinformation//商品信息
{
charnumber[20];//编号
charsname[20];//名称
floatprice;//价格
intcount;//数量
chardath[20];//生产日期
structinformation*next;
}inf;
voidmenu(inf*head);//显示主菜单
inf*create();//创建链表
voidInput(inf*head);//输入商品信息
voidtianjia(inf*head);//添加商品信息
voidoutput(inf*head);//输出商品信息
voidchaxun(inf*head);//查询商品信息
voidchaxun_number(inf*head);//按编号查询
voidchaxun_sname(inf*head);//按名称查询
voidxiugai(inf*head);//修改商品信息
voidshanchu(inf*head);//删除商品信息
voidwrite_file(inf*head);//保存商品信息到文件
voidpaixu(inf*head);
inf*Read__file();//读取文件内商品信息
voidfreeh(inf*head);//退出程序
#define_CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
#include
#pragmacomment(lib,"WINMM.lib")
#include"标头.h"
voidmenu(inf*head)//显示菜单
{
PlaySound(TEXT("sounds\\背景.wav"),NULL,
SND_FILENAME|SND_ASYNC|SND_LOOP);
system("color3B");
printf("\t\t*****************************************************\n");
printf("\t\t\t\t欢迎进入商品库存管理系统\n");
printf("\t\t*****************************************************\n");
printf("\n");
printf("\t\t|====================================================|\n");
printf("\t\t|______________________基本信息______________________|\n");
printf("\t\t|1.商品信息录入|2.添加商品信息|\n");
printf("\t\t|3.查找商品信息|4.修改商品信息|\n");
printf("\t\t|5.删除商品信息|6.保存文件信息|\n");
printf("\t\t|7.读取文件信息|8.显示商品信息|\n");
printf("\t\t|9.按价格从高到低进行排序|\n");
printf("\t\t|_________________0.释放链表。
退出___________________|");
printf("\n\t\t\t请输入你的选项(0---8):
\n");
printf("\n");
printf("\t请输入你的选择:
\n");
}
inf*create()//创建链表
{
inf*p;
p=(inf*)malloc(sizeof(structinformation));
if(p==NULL)
{
printf("链表创建失败.\n");
exit(0);
}
p->next=NULL;
return(p);
}
voidInput(inf*head)//录入货物信息
{
inf*p;
inti,n;
printf("请输入:
你想录入多少个商品信息:
\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
p=(inf*)malloc(sizeof(structinformation));
if(p==NULL)
{
printf("结点创建失败.\n");
exit(0);
}
printf("请输入商品:
编号,名称,价格,数量:
生产日期\n");
printf("编号:
");
scanf("%s",p->number);
printf("名称:
");
scanf("%s",p->sname);
printf("价格:
");
scanf("%f",&p->price);
printf("数量:
");
scanf("%d",&p->count);
printf("生产日期:
");
scanf("%s",p->dath);
p->next=NULL;
while(head->next!
=NULL)
head=head->next;
head->next=p;
}
}
voidtianjia(inf*head)//添加货物信息
{
inf*p;
inti,n;
printf("请输入:
你想添加多少个商品信息:
\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
p=(inf*)malloc(sizeof(structinformation));
if(p==NULL)
{
printf("链表结点创建失败。
\n");
exit(0);
}
printf("请输入商品:
编号,名称,价格,数量:
\n");
printf("编号:
");
scanf("%s",p->number);
printf("名称:
");
scanf("%s",p->sname);
printf("价格:
");
scanf("%f",&p->price);
printf("数量:
");
scanf("%d",&p->count);
printf("生产日期:
");
scanf("%s",p->dath);
p->next=NULL;
while(head->next!
=NULL)
head=head->next;
head->next=p;
}
}
voidoutput(inf*head)//输出货物信息
{
intn=0;
inf*p;
p=head->next;
if(p==NULL)
{
printf("链表为空,请先输入信息!
\n");
return;
}
while(p!
=NULL)
{
n++;
printf("商品信息:
编号:
%s,名称:
%s,价格:
%.2f数量:
%d生产日期:
%s\n",p->number,p->sname,p->price,p->count,p->dath);
p=p->next;
}
printf("商品总数为:
%d\n",n);
}
voidchaxun(inf*head)//查询货物信息函数
{
inta;
printf("\t\t查询信息\n");
while
(1)
{
printf("\t1.按编号查询\n");
printf("\t2.按名称查询\n");
printf("请输入你的选择:
\n");
scanf("%d",&a);
if(a==1)
{
chaxun_number(head);//调用按编号查询的函数
break;
}
if(a==2)
{
chaxun_sname(head);//调用按名称查询的函数
break;
}
if(a!
=1&&a!
=2)
{
printf("输入错误!
\n");
break;
}
getchar();
}
}
voidchaxun_number(inf*head)//按编号查询
{
charnum[20];
inf*p;
inti=-1;
p=head;
printf("请输入你要查询的商品的编号:
");
scanf("%s",num);
while(p->next!
=NULL)
{
p=p->next;
if(strcmp(p->number,num)==0)
{
i=1;
printf("已找到该商品信息\n");
printf("货物信息:
编号:
%s,名称:
%s,价格:
%.2f.数量:
%d,生产日期:
%s\n",p->number,p->sname,p->price,p->count,p->dath);
}
}
if(i<0)
printf("没有此商品信息!
\n");
}
voidchaxun_sname(inf*head)//按名称查询
{
chara[20];
inti=-1;
inf*p;
p=head;
printf("请输入要查找商品的名称:
\n");
scanf("%s",a);
while(p->next!
=NULL)
{
p=p->next;
if(strcmp(p->sname,a)==0)
{
i=1;
printf("已找到该商品信息\n");
printf("商品信息:
编号:
%s,名称:
%s,价格:
%.2f.数量:
%d,生产日期\n",p->number,p->sname,p->price,p->count,p->dath);
}
}
if(i<0)
printf("没有此商品!
\n");
}
voidxiugai(inf*head)//修改货物信息
{
printf("\t********************\n");
printf("\t请输入修改的方式:
\n");
printf("\t1.单个信息全部修改。
\n");
printf("\t2.单个信息逐个修改。
\n");
printf("\t********************\n");
inta;
printf("请输入选项:
\n");
scanf("%d",&a);
if(a==1)
{
charno[20];
inti=-1;
inf*p;
p=head;
printf("请输入要修改商品的编号:
\n");
scanf("%s",no);
while(p->next!
=NULL)
{
p=p->next;
if(strcmp(p->number,no)==0)
{
i=1;
printf("已找到该商品\n请输入新的的编号,名称,价格,数量:
生产日期:
\n");
printf("编号:
");
scanf("%s",p->number);
printf("名称:
");
scanf("%s",p->sname);
printf("价格:
");
scanf("%f",&p->price);
printf("数量:
");
scanf("%d",&p->count);
printf("生产日期:
");
scanf("%s",p->dath);
}
}
if(i<0)
printf("没有此商品信息!
\n");
}
if(a==2)
{
inf*p=NULL;
intb;
charnumber2[20];
charname2[100];
floatprice2;
intcount2;
p=head->next;
if(p==NULL)
{
printf("没有商品信息,请先保存商品信息!
\n");
}
printf("\t\t请输入要修改的内容\n");
printf("\t\t1.修改编号2.修改名称\n");
printf("\t\t3.修改价格4.修改数量\n");
printf("\t\t5.修改生产日期\n");
printf("请选择\n");
scanf("%d",&b);
switch(b)
{
case1:
printf("请输入原商品编号:
\n");
scanf("%s",&number2);
while(p!
=NULL)
{
if(strcmp(p->number,number2)==0)
{
printf("已找到该商品\n请输入新的编号:
\n");
scanf("%s",&p->number);
}
p=p->next;
}
printf("该商品信息已修改成功。
\n");
break;
case2:
printf("请输入原名称:
\n");
scanf("%s",&name2);
while(p!
=NULL)
{
if(strcmp(p->sname,name2)==0)
{
printf("请输入新的名称:
\n");
scanf("%s",&p->sname);
break;
}
p=p->next;
}
printf("该商品信息已修改成功。
\n");
break;
case3:
printf("请输入该商品编号:
");
scanf("%s",&number2);
printf("请输入原价格:
\n");
scanf("%f",&price2);
while(p!
=NULL)
{
if(strcmp(p->number,number2)==0&&price2==p->price)
{
printf("请输入新的价格:
\n");
scanf("%f",&p->price);
break;
}
p=p->next;
}
printf("该商品信息已修改成功。
\n");
break;
case4:
printf("请输入该商品编号:
");
scanf("%s",&number2);
printf("请输入被修改的数量:
\n");
scanf("%d",&count2);
while(p!
=NULL)
{
if(count2==p->count&&strcmp(p->number,number2)==0)
{
printf("请输入新的数量:
\n");
scanf("%d",&p->count);
break;
}
p=p->next;
}
printf("该商品信息已修改成功。
\n");
break;
default:
printf("修改内容有错误!
\n");
}
}
}
voidshanchu(inf*head)//删除货物信息
{
inta;
printf("\t\t1.按编号删除2.按名称删除\n");
printf("\t\t请输入选项:
\n");
scanf("%d",&a);
switch(a)
{
case1:
{
charnumber[20];
inti=-1;
inf*p,*h;
p=head;
printf("请输入你要删除的商品的编号:
\n");
scanf("%s",number);
while(p->next!
=NULL)
{
if(strcmp(number,p->next->number)==0)
{
i=1;
h=p->next;
p->next=p->next->next;
free(h);
printf("已删除该商品信息。
\n");
break;
}
p=p->next;
}
if(i<0)
printf("没有此商品信息!
\n");
break;
}
case2:
{
charname[20];
inti=-1;
inf*p,*h;
p=head;
printf("请输入你要删除的商品的名称:
\n");
scanf("%s",name);
while(p->next!
=NULL)
{
if(strcmp(name,p->next->sname)==0)
{
i=1;
h=p->next;
p->next=p->next->next;
free(h);
printf("已删除该商品信息。
\n");
break;
}
p=p->next;
}
if(i<0)
printf("没有此商品信息!
\n");
break;
}
}
}
voidwrite_file(inf*head)//保存货物信息到文件
{
inf*p;
FILE*fp;
p=head;
if((fp=fopen("商品信息.txt","wb"))==NULL)
{
printf("Donothavethisfile\n");
exit(0);
}
while(p->next!
=NULL)
{
p=p->next;
fwrite(p,sizeof(structinformation),1,fp);
}
fclose(fp);
}
inf*Read__file()//读取文件内的货物信息
{
inf*p,*d,*head;
FILE*fp;
head=(inf*)malloc(sizeof(structinformation));
p=(inf*)malloc(sizeof(structinformation));
if((fp=fopen("商品信息.txt","rb"))==NULL)
{
printf("不能打开文件!
\n");
exit(0);
}
if(fread(head,sizeof(structinformation),1,fp))
{
d=head;
while(fread(p,sizeof(structinformation),1,fp))
{
d->next=p;
d=p;
p=(inf*)malloc(sizeof(structinformation));
}
}
d->next=NULL;
fclose(fp);
return(head);
}
voidfreeh(inf*head)//释放空间退出程序
{
inf*p;
p=(inf*)malloc(sizeof(structinformation));
p=head;
while(head->next!
=NULL)
{
head=head->next;
free(p);
p=head;
}
free(p);
}
voidpaixu(inf*head)
{
inf*p=head,*q=NULL,*t=NULL,*subhead=NULL;
inti,j,count=0;
while(p->next!
=NULL)
{
count++;
p=p->next;
}
for(i=0;i{
subhead=head;
p=head->next;
q=p->next;
for(j=0;j{
if(p->priceprice)
{
subhead->next=p->next;
p->next=q->next;
q->next=p;
t=p;
p=q;
q=t;
}
subhead=subhead->next;
p=p->next;
q=q->next;
}
}
output(head);
}
intmain()
{
inf*head;
head=(inf*)malloc(sizeof(structinformation));
head=create();
while
(1)
{
menu(head);
inth;
scanf("%d",&h);
switch(h)//功能选择
{
case1:
Input(head);
Sleep(500);
break;
case2:
tianjia(head);
Sleep(1000);
break;
case3:
chaxun(head);
break;
case4:
xiugai(head);
write_file(head);
break;
case5:
shanchu(head);
write_file(head);
output(head);
break;
case6:
write_file(head);
printf("\t已经成功保存信息!
\t\n");
Sleep(1000);
brea