1、题目描述
设计一个商品进销存管理程序,该程序具有以下功能:
(1)录入商品信息;
(2)给定商品编号,修改该商品信息;
(3)给定商品编号,删除该商品信息;
(4)录入商品的进货与销售信息;
(5)给定商品编号或商品名,查看该商品及库存信息;
(6)统计功能:提供一些统计各类信息的功能。
源代码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define LEN1 sizeof(struct product)
#define LEN2 sizeof(struct sales)
#define LEN3 sizeof(struct purchase)
static int shu=0;
struct product
{
int id; //商品编号
char name[20]; //商品名称
double price; //商品单价
int stock; //商品库存
struct product* next; //指向下一个商品信息的指针
};
struct sales
{
int id; //商品ID
double pri; //销售价格
int quantity; //销售量
struct sales* next; //指向下一个销售信息的指针
};
struct purchase
{
int id; //商品ID
double pr; //进货价格
int num; //进货量
struct purchase* next; //指向下一个进货信息的指针
};
struct product *creat1(int n,struct product *h)//创建商品信息链表
{
struct product *head=NULL,*p1=NULL,*p2=NULL,*p3=NULL;
int i,j,f=0;
p3=h;
for(i=1;i<=n;i++)
{
p1=(struct product *)malloc(LEN1);
printf("请输入商品编号:");
scanf("%d",&j);
while(p3){
if(p3->id==j){
printf("以存在相同ID商品,请更换ID重新输入!");
p3=NULL;
f=1;
break;
}else{
p3=p3->next;
}
}
if(f==1){
i--;
f=0;
continue;
}
p1->id=j;
printf("请输入商品名字:");
scanf("%s",&p1->name);
printf("请输入商品价格:");
scanf("%lf",&p1->price);
printf("请输入商品库存:");
scanf("%d",&p1->stock);
p1->next=NULL;
if(i==1) head=p1;
else p2->next=p1;
p2=p1;
}
return(head);
};
struct product *save1(struct product *p1,int q,int s)//销售商品时改变商品信息中的商品数
{
struct product *pm=NULL,*p2_sa=NULL;
pm=p1;
int i=0;
while (pm!=NULL)//查找对应商品
{
if(pm->id==q){
pm->stock-=s;
i=1;
break;
}
pm=pm->next;
}
if(!i){
printf("未找到对应商品,请先录入商品信息或检查商品ID是否错误!\n");
}
return p2_sa;//返回空指针
}
struct product *save2(struct product *p1,int q,int s)//进货时改变商品信息中的商品数量
{
struct product *pm=NULL,*p2=NULL;
pm=p1;
int i=0;
while (pm!=NULL)//找到对应商品
{
if(pm->id==q){
pm->stock+=s;
i=1;
break;
}
pm=pm->next;
}
if(!i){
printf("未找到对应商品,请先录入商品信息或检查商品ID是否错误!\n");
}
return p2;
}
struct sales *creat2(int n,struct product *p)//创建商品销售链表
{
struct sales *p1_sl=NULL,*p2=NULL,*head=NULL;
struct product *m;
int i,q,s;
for(i=1;i<=n;i++)
{
p1_sl=(struct sales *)malloc(LEN2);
printf("请输入商品id:\n");
scanf("%d",&q);
p1_sl->id=q;
printf("请输入销售价格:\n");
scanf("%lf",&p1_sl->pri);
printf("请输入销售数量:\n");
scanf("%d",&s);
p1_sl->quantity=s;
m=save1(p,q,s);//调用函数改变商品数量
p1_sl->next=NULL;
if(i==1) head=p1_sl;
else p2->next=p1_sl;
p2=p1_sl;
}
return(head);
};
struct purchase *creat3(int n,struct product *p)//创建商品进货链表
{
struct purchase *p1_pu=NULL,*p2=NULL,*head=NULL;
struct product *m;
int i,q,s;
for(i=1;i<=n;i++)
{
p1_pu=(struct purchase *)malloc(LEN3);
printf("请输入商品id:\n");
scanf("%d",&q);
p1_pu->id=q;
printf("请输入进货价格:\n");
scanf("%lf",&p1_pu->pr);
printf("请输入进货数量:\n");
scanf("%d",&s);
p1_pu->num=s;
m=save2(p,q,s);//调用函数改变商品数量
p1_pu->next=NULL;
if(i==1) head=p1_pu;
else p2->next=p1_pu;
p2=p1_pu;
}
return(head);
};
void print1(struct product *head)//打印商品信息的函数
{
struct product *p1_h=NULL;
int i;
p1_h=head;
while (p1_h!=NULL)
{
printf("__________________________\n");
printf("|商品ID:%d \n",p1_h->id);
printf("|商品名字:%s \n",p1_h->name);
printf("|商品价格:%lf \n",p1_h->price);
printf("|商品库存:%d \n",p1_h->stock);
printf("__________________________\n\n");
p1_h=p1_h->next;
}
p1_h=NULL;
}
void print2(struct sales *head)//打印商品销售信息的函数
{
struct sales *p1_s=NULL;
int i;
p1_s=head;
while (p1_s!=NULL)
{
printf("__________________________\n");
printf("|商品ID:%d \n",p1_s->id);
printf("|商品价格:%lf \n",p1_s->pri);
printf("|出售量:%d \n",p1_s->quantity);
printf("__________________________\n\n");
p1_s=p1_s->next;
}
}
void print3(struct purchase *head)//打印商品进货信息的函数
{
struct purchase *p1_p=NULL;
int i;
p1_p=head;
while (p1_p!=NULL)
{
printf("__________________________\n");
printf("|商品ID:%d \n",p1_p->id);
printf("|商品价格:%lf \n",p1_p->pr);
printf("|进货量:%d \n",p1_p->num);
printf("__________________________\n\n");
p1_p=p1_p->next;
}
}
struct product *conect1(struct product *p1_c1,struct product *p2_c1)//商品信息链表链接函数
{
struct product *p3_c1=NULL;
p3_c1=p1_c1;
while (1)
{
if(p1_c1->next==NULL){
break;
}
p1_c1=p1_c1->next;
}
p1_c1->next=p2_c1;
return (p3_c1);
}
struct sales *conect2(struct sales *p1_c2,struct sales *p2_c2)//商品销售信息链表链接函数
{
struct sales *p3_c2=NULL;
p3_c2=p1_c2;
while (p3_c2->next!=NULL)
{
p3_c2=p3_c2->next;
}
p3_c2->next=p2_c2;
return (p1_c2);
}
struct purchase *conect3(struct purchase *p1_c3,struct purchase *p2_c3)//商品进货信息链表链接函数
{
struct purchase *p3_c3=NULL;
p1_c3=p1_c3;
while (p1_c3->next!=NULL)
{
p1_c3=p1_c3->next;
}
p1_c3->next=p2_c3;
return (p1_c3);
}
//以上为输入模块函数,下面为功能函数
struct product *find(struct product *p1_pf)//商品信息查找函数
{
struct product *p2_pf=NULL;
int i=0,n,f=0,l=1;
char a[20];
p2_pf=p1_pf;
while (l)
{
printf("__________________________________\n");
printf("|1.使用ID查找 | 2.使用名字查找|\n");
printf("|3.退出 | |\n");
printf("__________________________________\n");
printf("请输入1、2来使用对应功能:");
scanf("%d",&f);
printf("\n....................................................\n\n");
if(f==1){
printf("请输入您要查找的商品的id:\n");
scanf("%d",&n);
while(p2_pf){
if(p2_pf->id==n){
printf("__________________________________\n");
printf("商品名字:%s\n",p2_pf->name);
printf("商品价格:%lf\n",p2_pf->price);
printf("商品库存:%d\n",p2_pf->stock);
printf("__________________________________\n");
p2_pf=NULL;
i=1;
l=0;
break;
}else{
p2_pf=p2_pf->next;
}
}
}else if(f==2){
printf("请输入您要查找的商品的名字:\n");
scanf("%s",&a);
while(p2_pf){
if(strcmp(a,p2_pf->name)==0){
printf("__________________________________\n");
printf("商品名字:%s\n",p2_pf->name);
printf("商品价格:%lf\n",p2_pf->price);
printf("商品库存:%d\n",p2_pf->stock);
printf("__________________________________\n");
p2_pf=NULL;
i=1;
l=0;
break;
}else{
p2_pf=p2_pf->next;
}
}
}else if(f==3){
printf("退出\n");
break;
}else{
printf("未知操作,请重新输入!\n");
}
}
return p2_pf;
}
struct product *delet(struct product *p1_de)//商品信息删除函数
{
struct product *p2_de=NULL,*p3_de=NULL;
int i=0,n;
p2_de=p1_de;
p3_de=p1_de;
scanf("%d",&n);
while (p2_de)
{
if(p2_de->id==n){
printf("这是您要删除的商品的信息:\n");
printf("商品名字:%s\n",p2_de->name);
printf("商品价格:%lf\n",p2_de->price);
printf("商品库存:%d\n",p2_de->stock);
p3_de->next=p2_de->next;
free(p2_de);
break;
}
p3_de=p2_de;
p2_de=p2_de->next;
}
p2_de=NULL;
return p2_de;
}
struct product *change(struct product *p1_ch)//商品信息修改函数
{
struct product *p2_ch=NULL;
int i=0,t[5],d,j,n;
p2_ch=p1_ch;
scanf("%d",&n);
while (p2_ch)
{
if(p2_ch->id==n){
printf("这是您要修改的商品的信息:\n");
printf("1.商品ID:%d\n",p2_ch->id);
printf("2.商品名字:%s\n",p2_ch->name);
printf("3.商品价格:%lf\n",p2_ch->price);
printf("4.商品库存:%d\n",p2_ch->stock);
printf("请问您要修改几项数据呢?\n");
scanf("%d",&d);
printf("请输入您要修改的数据(1、2、3、4分别表示ID、名字、价格、库存,请以空格为间隔):\n");
for(j=0;j<d;j++){
scanf("%d",&t[j]);
}
for(j=0;j<d;j++){
switch (t[j])
{
case 1:
printf("正在修改商品ID,请问您要将商品ID改成:\n");
scanf("%d",&p2_ch->id);
break;
case 2:
printf("正在修改商品名字,请问您要将商品名字改成:\n");
scanf("%s",&p2_ch->name);
break;
case 3:
printf("正在修改商品价格,请问您要将商品价格改成:\n");
scanf("%lf",&p2_ch->price);
break;
case 4:
printf("正在修改商品库存,请问您要将商品库存改成:\n");
scanf("%d",&p2_ch->stock);
break;
default:
break;
}
}
printf("这是您修改过的商品信息:\n");
printf("1.商品ID:%d\n",p2_ch->id);
printf("2.商品名字:%s\n",p2_ch->name);
printf("3.商品价格:%lf\n",p2_ch->price);
printf("4.商品库存:%d\n",p2_ch->stock);
}
p2_ch=p2_ch->next;
}
p2_ch=NULL;
return p2_ch;
}
struct product *load(struct product *head)//商品信息文件加载函数
{
FILE *fp_l;
if((fp_l=fopen("menu.txt","r"))==NULL){
printf("加载商品信息文档出错。\n");
exit(1);
}else{
printf("加载商品信息文档成功。\n\n\n");
}
int flag=1;
int id_t; //商品编号
char name_t[20]; //商品名称
double price_t; //商品单价
int stock_t; //商品库存
struct product *p1_lo=NULL,*p2_lo=NULL,*p3_lo=NULL;
p2_lo=(struct product *)malloc(LEN1);
p2_lo->next=NULL;
while(!feof(fp_l))//检测文件是否结束
{
fscanf(fp_l,"%d",&id_t);
fscanf(fp_l,"%s",name_t);
fscanf(fp_l,"%lf",&price_t);
fscanf(fp_l,"%d",&stock_t);
if(flag){
p2_lo->id=id_t;
strcpy(p2_lo->name,name_t);
p2_lo->price=price_t;
p2_lo->stock=stock_t;
flag=0;
p1_lo=p2_lo;
p3_lo=p2_lo;
}else{
p2_lo=(struct product *)malloc(LEN1);
p2_lo->next=NULL;
p2_lo->id=id_t;
strcpy(p2_lo->name,name_t);
p2_lo->price=price_t;
p2_lo->stock=stock_t;
p3_lo->next=p2_lo;
p3_lo=p3_lo->next;
}
shu++;
}
fclose(fp_l);
return p1_lo;
};
struct sales *load_sl(struct sales *head)//商品销售信息文件加载函数
{
FILE *fp_l;
if((fp_l=fopen("sales form.txt","r"))==NULL){
printf("加载销售文档出错。\n");
exit(1);
}else{
printf("加载销售文档成功。\n");
}
int flag=1;
int id_sl; //商品ID
double pri_sl; //销售价格
int quantity_sl; //销售量
struct sales *p1_lo=NULL,*p2_lo=NULL,*p3_lo=NULL;
p2_lo=(struct sales *)malloc(LEN2);
p2_lo->next=NULL;
while(!feof(fp_l)){
fscanf(fp_l,"%d",&id_sl);
fscanf(fp_l,"%lf",&pri_sl);
fscanf(fp_l,"%d",&quantity_sl);
if(flag){
p2_lo->id=id_sl;
p2_lo->pri=pri_sl;
p2_lo->quantity=quantity_sl;
flag=0;
p1_lo=p2_lo;
p3_lo=p2_lo;
}else{
p2_lo=(struct sales *)malloc(LEN2);
p2_lo->next=NULL;
p2_lo->id=id_sl;
p2_lo->pri=pri_sl;
p2_lo->quantity=quantity_sl;
p3_lo->next=p2_lo;
p3_lo=p3_lo->next;
}
}
fclose(fp_l);
return p1_lo;
};
struct purchase *load_pu(struct purchase *head)//商品进货信息文件加载函数
{
FILE *fp_l;
if((fp_l=fopen("purchase form.txt","r"))==NULL){
printf("加载进货文档出错。\n");
exit(1);
}else{
printf("加载进货文档成功。\n");
}
int flag=1;
int id_pu; //商品ID
double pr_pu; //进货价格
int num_pu; //进货量
struct purchase *p1_lo=NULL,*p2_lo=NULL,*p3_lo=NULL;
p2_lo=(struct purchase *)malloc(LEN3);
p2_lo->next=NULL;
while(!feof(fp_l)){
fscanf(fp_l,"%d",&id_pu);
fscanf(fp_l,"%lf",&pr_pu);
fscanf(fp_l,"%d",&num_pu);
if(flag){
p2_lo->id=id_pu;
p2_lo->pr=pr_pu;
p2_lo->num=num_pu;
flag=0;
p1_lo=p2_lo;
p3_lo=p2_lo;
}else{
p2_lo=(struct purchase *)malloc(LEN3);
p2_lo->next=NULL;
p2_lo->id=id_pu;
p2_lo->pr=pr_pu;
p2_lo->num=num_pu;
p3_lo->next=p2_lo;
p3_lo=p3_lo->next;
}
}
fclose(fp_l);
return p1_lo;
};
struct product *write(struct product *head_wr)//商品信息写入函数
{
FILE *fp_wr;
struct product *p1_wr=NULL;
if((fp_wr=fopen("menu.txt","w+"))==NULL){
printf("加载文档出错。\n");
exit(1);
}
while(head_wr!=NULL){
fprintf(fp_wr,"\n%d\n",head_wr->id);
fprintf(fp_wr,"%s\n",head_wr->name);
fprintf(fp_wr,"%lf\n",head_wr->price);
fprintf(fp_wr,"%d",head_wr->stock);
head_wr=head_wr->next;
}
fclose(fp_wr);
printf("数据写入成功!\n\n\n");
return p1_wr;
}
struct sales *dailys(struct sales *head)//商品销售日志写入函数
{
FILE *fp_sl;
struct sales *p1_sle=NULL;
p1_sle=head;
if((fp_sl=fopen("sales form.txt","w+"))==NULL){
printf("加载文档出错。\n");
exit(1);
}
while(p1_sle!=NULL){
fprintf(fp_sl,"\n%d\n",p1_sle->id);
fprintf(fp_sl,"%lf\n",p1_sle->pri);
fprintf(fp_sl,"%d",p1_sle->quantity);
p1_sle=p1_sle->next;
}
fclose(fp_sl);
printf("数据写入成功!\n");
p1_sle=NULL;
return p1_sle;
}
struct purchase *dailyp(struct purchase *head)//商品进货日志写入函数
{
FILE *fp_ph;
struct purchase *p1_ph=NULL;
if((fp_ph=fopen("purchase form.txt","w+"))==NULL){
printf("加载文档出错。\n");
exit(1);
}
p1_ph=head;
while(p1_ph!=NULL){
fprintf(fp_ph,"\n%d\n",p1_ph->id);
fprintf(fp_ph,"%lf\n",p1_ph->pr);
fprintf(fp_ph,"%d",p1_ph->num);
p1_ph=p1_ph->next;
}
fclose(fp_ph);
printf("数据写入成功!\n");
p1_ph=NULL;
return p1_ph;
}
struct product *sort(struct product *head,int i)//商品信息排序函数(使用冒泡排序法)
{
struct product *p1_sot=NULL,*p2_sot=NULL,*p3_sot=NULL;
int t,j,k,p,s,fl=0;
char n[20];
for(t=0;t<i-1;t++){
p1_sot=head;
p3_sot=p1_sot->next;
for(j=0;j<i-1;j++){
if(p3_sot->price>p1_sot->price){
strcpy(n,p3_sot->name);
p=p3_sot->price;
s=p3_sot->stock;
k=p3_sot->id;
strcpy(p3_sot->name,p1_sot->name);
p3_sot->price=p1_sot->price;
p3_sot->stock=p1_sot->stock;
p3_sot->id=p1_sot->id;
p1_sot->price=p;
p1_sot->stock=s;
p1_sot->id=k;
strcpy(p1_sot->name,n);
fl=1;
}
p3_sot=p3_sot->next;
p1_sot=p1_sot->next;
}
}
if(fl){
printf("排序成功!\n");
}
return p2_sot;
}
struct sales *sumulates(struct sales *head)//商品销售数量与总额统计函数
{
struct sales *p1_sml=NULL,*p2_sml=NULL;
int num=0,i;
double sum=0;
p1_sml=head;
while (p1_sml!=NULL)//逐个遍历
{
num+=p1_sml->quantity;
sum+=(p1_sml->pri)*(p1_sml->quantity);
p1_sml=p1_sml->next;
}
printf("_________________________\n");
printf("|销售数量:%d \n",num);
printf("|销售总额:%lf \n",sum);
printf("_________________________\n\n");
return p2_sml;
}
struct purchase *sumulatep(struct purchase *head)//商品进货数量与总额统计函数
{
struct purchase *p1_smu=NULL,*p2_smu=NULL;
int num=0,i;
double sum=0;
p1_smu=head;
while (p1_smu!=NULL)
{
num+=p1_smu->num;
sum+=(p1_smu->pr)*(p1_smu->num);
p1_smu=p1_smu->next;
}
printf("_________________________\n");
printf("|进货数量:%d \n",num);
printf("|进货总额:%lf \n",sum);
printf("_________________________\n");
return p2_smu;
}
int main(){
struct product *head1=NULL,*head2=NULL,*flag=NULL;
struct sales *list1=NULL,*list2=NULL,*flag1=NULL;
struct purchase *form1=NULL,*form2=NULL,*flag2=NULL;
int n,i,j=0;
list1=load_sl(list1);
form1=load_pu(form1);
head1=load(head1);
while(j!=11){
system("cls");//清屏操作,以下同理
printf("______________________________________________________________________\n");
printf("....................欢迎使用商品进销管理系统..........................\n");
printf("______________________________________________________________________\n");
printf(". 您可以继续进行的操作是: .\n");
printf(". 1.录入商品销售信息。 .\n");
printf(". 2.录入商品进货信息。 .\n");
printf(". 3.录入商品信息。 .\n");
printf(". 4.查找商品信息。 .\n");
printf(". 5.删除商品信息。 .\n");
printf(". 6.修改商品信息。 .\n");
printf(". 7.统计销售额。 .\n");
printf(". 8.统计进货额。 .\n");
printf(". 9.按价格升序排列商品。 .\n");
printf(". 10.打印所有商品信息。 .\n");
printf(". 11.结束程序。 .\n");
printf("______________________________________________________________________\n");
printf("请输入1、2、3、4、5、6、7、8、9、10、11来进行对应操作。\n请问您要进行的操作是:");
scanf("%d",&j);
printf("\n即将执行操作%c......",j);
printf("......................................................................\n\n\n\n\n\n\n");
system("cls");
if(j==1){
list2=creat2(1,head1);
if(list1){//判断是否已经生成链表
list1=conect2(list1,list2);//将新链表与旧链表链接
}else{
list1=list2;
}
printf("\n按任意键继续......\n");
system("pause");
}else if(j==2){
form2=creat3(1,head1);
if(form1){//判断是否已经生成链表
form1=conect3(form1,form2);//将新链表与旧链表链接
}else{
form1=form2;
}
printf("\n按任意键继续......\n");
system("pause");
}else if(j==3){
printf("请输入您要录入的商品数量:");
scanf("%d",&n);
head2=creat1(n,head1);
if(head1!=NULL){//判断是否已经生成链表
head1=conect1(head1,head2);//将新链表与旧链表链接
}else{
head1=head2;
}
shu+=n;
printf("这是您输入的商品信息:\n");
print1(head2);
printf("\n按任意键继续......\n");
system("pause");
}else if(j==4){
printf("请输入您要查找的商品的id:\n");
flag=find(head1);
flag=NULL;
printf("\n按任意键继续......\n");
system("pause");
}else if(j==5){
printf("请输入您要删除的商品的id:\n");
flag=delet(head1);
shu--;
printf("\n按任意键继续......\n");
system("pause");
}else if(j==6){
printf("请输入您要修改的商品的信息的id:\n");
flag=change(head1);
printf("\n按任意键继续......\n");
system("pause");
}else if(j==8){
flag2=sumulatep(form1);
printf("\n按任意键继续......\n");
system("pause");
}else if(j==7){
flag1=sumulates(list1);
printf("\n按任意键继续......\n");
system("pause");
}else if(j==9){
flag=sort(head1,shu);
printf("\n按任意键继续......\n");
system("pause");
}else if(j==10){
print1(head1);
printf("\n按任意键继续......\n");
system("pause");
}else if(j==11){
break;
}else{
printf("未知操作,即将返回\n");
}
}
flag2=dailyp(form1);//存储
flag1=dailys(list1);
flag=write(head1);
system("cls");
printf(".......................程序结束..........................\n");
printf(".......................感谢使用..........................\n");
shu=0;
system("pause");
return 0;
}