小超市管理系统
创建完商品档案进行其他功能时,文件里的数据变成乱码...
```c
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#define MAXSIZE 50
#define SIZESG sizeof(struct Goods)
#define SIZEJD sizeof(struct Jd)
struct Goods
{
char id[20];/*编号*/
char name[20];/*品名*/
double purchase_price;/*进价*/
double selling_price;/*售价*/
int purchase_quantity;/*进货量*/
int sales_volume;/*销售量*/
double amount;/*销售额*/
int remaining_quantity;/*剩余数*/
double gross_profit;/*毛利*/
};
struct Jd
{
struct Goods goods;
struct Jd *next;
};
void menu();/*菜单*/
void createFile(char *FName);/*创建文件,存放结构体数据*/
struct Jd *createchain(char *FName);/*利用文件创建链表*/
void edit(struct Jd *head);/*修改商品信息*/
void del(struct Jd *head);/*删除原有商品*/
void purchase(struct Jd *head);/*处理原有商品的新进货量*/
void sales(struct Jd *head);/*处理原有商品的新销售量*/
void baofei(struct Jd *head);/*处理原有商品的报废量*/
void sale(struct Jd *head);/*统计销售情况*/
void namechaxun(struct Jd *head);/*根据商品品名查询*/
void shuchaxun(struct Jd *head);/*根据剩余数查询*/
void xianshi(struct Jd *head);/*按原有顺序显示*/
void amountxianshi(struct Jd *head);/*销售额*/
void profitxianshi(struct Jd *head);/*毛利*/
void WriteToFile(struct Jd *head,char *FName);/*数据写入文件*/
int main()
{
struct Jd *head=NULL;
char FileName[128]={'\0'};
strcpy(FileName,"data.txt");
int i;
while(1)
{
menu();
scanf("%d",&i);
switch(i)
{
case 0:
system("cls");
printf("已退出系统!");
exit(0);
case 1:
system("cls");
createFile(FileName);
break;
case 2:
system("cls");
head=createchain(FileName);
edit(head);
WriteToFile(head,FileName);
break;
case 3:
system("cls");
head=createchain(FileName);
del(head);
WriteToFile(head,FileName);
break;
case 4:
system("cls");
head=createchain(FileName);
purchase(head);
WriteToFile(head,FileName);
break;
case 5:
system("cls");
head=createchain(FileName);
sales(head);
WriteToFile(head,FileName);
break;
case 6:
system("cls");
head=createchain(FileName);
baofei(head);
WriteToFile(head,FileName);
break;
case 7:
system("cls");
head=createchain(FileName);
sale(head);
WriteToFile(head,FileName);
break;
case 8:
system("cls");
head=createchain(FileName);
namechaxun(head);
break;
case 9:
system("cls");
head=createchain(FileName);
shuchaxun(head);
break;
case 10:
system("cls");
head=createchain(FileName);
xianshi(head);
break;
case 11:
system("cls");
head=createchain(FileName);
amountxianshi(head);
break;
case 12:
system("cls");
head=createchain(FileName);
profitxianshi(head);
break;
}
}
}
/*菜单*/
void menu()
{
printf("-------------------菜单-------------------\n");
printf("| 0. 退出系统 |\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("| 12.按毛利由高到低的顺序显示 |\n");
printf("| 13.添加新商品 |\n");
printf("------------------------------------------");
printf("\n");
printf("\n");
printf("请选择您要进行的操作选项: ");
}
/*创建文件,存放结构体数据*/
void createFile(char *FName)
{
int i=1;
struct Goods go;
FILE *fp;
if((fp=fopen(FName,"w"))==NULL)
{
printf("文件打开失败!\n");
exit(0);
}
while(i!=0)/*给结构体各成员变量赋值*/
{
printf("创建商品档案:\n");
printf("请输入商品编号: ");
scanf("%s",go.id);
printf("请输入商品品名: ");
scanf("%s",go.name);
printf("请输入商品进价: ");
scanf("%lf",&go.purchase_price);
printf("请输入商品售价: ");
scanf("%lf",&go.selling_price);
printf("请输入商品进货量: ");
scanf("%d",&go.purchase_quantity);
printf("请输入商品销售量: ");
scanf("%d",&go.sales_volume);
go.amount=(go.selling_price)*(go.sales_volume);
printf("该商品销售额为: %lf\n",go.amount);
go.remaining_quantity=(go.purchase_quantity)-(go.sales_volume);
printf("该商品剩余数为: %d\n",go.remaining_quantity);
go.gross_profit=(go.amount)-(go.purchase_price)*(go.sales_volume);
printf("该商品毛利为: %lf\n",go.gross_profit);
fprintf(fp,"%s %s %lf %lf %d %d %lf %d %lf\n",go.id,go.name,go.purchase_price,go.selling_price,go.purchase_quantity,go.sales_volume,go.amount,go.remaining_quantity,go.gross_profit);
printf("是否需要输入新的数据(1=继续,0=退出)?");
scanf("%d",&i);
}
fclose(fp);
}
/*利用文件创建链表*/
struct Jd *createchain(char *FName)
{
FILE *fp;
struct Jd *pt,*pEnd,*head=NULL;
struct Goods go;
if((fp=fopen(FName,"r"))==NULL)
{
printf("文件打开失败!\n");
exit(0);
}
while(fscanf(fp,"%s %s %lf %lf %d %d %lf %d %lf\n",go.id,go.name,&go.purchase_price,&go.selling_price,&go.purchase_quantity,&go.sales_volume,&go.amount,&go.remaining_quantity,&go.gross_profit)!=EOF) /*从文件里读出一个结构体数据*/
{
pt=(struct Jd *)malloc(SIZEJD); /*创建链表结点空间*/
pt->goods=go;/*把前面读出的结构体数据赋值给结点的goods变量*/
if(head==NULL)/*如果链表以前是空,则生成头结点*/
{
head=pt;
pEnd=pt;
pt->next=NULL;
}
else /*在尾部插入结点*/
{
pEnd->next=pt;/*将新结点链入链表*/
pt->next=NULL;/*将新结点的next指针置空,标识成尾结点*/
pEnd=pt;/*让pEnd指向尾结点,为下一次插入做准备*/
}
}
fclose(fp);
return head;
};
/*修改商品信息*/
void edit(struct Jd *head)
{
struct Jd *p=head,*p1=head;
int flag=0;
char num[MAXSIZE];
printf("请输入需修改的商品的编号: \n");
scanf("%s",num);
while(p1)
{
if(strcmp(num,p1->goods.id)==0)
{
flag=1;
break;
}
p1=p1->next;
}
if(flag==1)
{
while(p)
{
if(strcmp(num,p->goods.id)==0)
{
printf("请输入商品编号: \n");
scanf("%s",p->goods.id);
printf("请输入商品品名: \n");
scanf("%s",p->goods.name);
printf("请输入商品进价: \n");
scanf("%lf",&p->goods.purchase_price);
printf("请输入商品售价: \n");
scanf("%lf",&p->goods.selling_price);
printf("请输入商品进货量: \n");
scanf("%d",&p->goods.purchase_quantity);
printf("请输入商品销售量: \n");
scanf("%d",&p->goods.sales_volume);
p->goods.amount=(p->goods.sales_volume)*(p->goods.selling_price);
printf("该商品销售额为: %lf\n",p->goods.amount);
p->goods.remaining_quantity=(p->goods.purchase_quantity)-(p->goods.sales_volume);
printf("该商品剩余数为: %d\n",p->goods.remaining_quantity);
p->goods.gross_profit=(p->goods.amount)-(p->goods.purchase_price)*(p->goods.sales_volume);
printf("该商品毛利为: %lf\n",p->goods.gross_profit);
printf("\n");
printf("修改成功!\n");
}
p=p->next;
}
}
else
printf("该商品不存在!\n");
}
/*删除原有商品*/
void del(struct Jd *head)
{
int flag=0;
struct Jd *p=head,*p1=head;
char num[MAXSIZE];
printf("请输入要删除的商品编号: ");
scanf("%s",num);
while(p1)
{
if(strcmp(num,p1->goods.id)==0)
{
flag=1;
break;
}
p1=p1->next;
}
if(flag==1)
{
if(strcmp(num,p->goods.id)==0)
{
head=p->next;
printf("删除成功!\n");
}
else
{
while(p)
{
if(p->next&&strcmp(num,p->next->goods.id)==0)
{
p->next=p->next->next;
printf("删除成功!\n");
}
p=p->next;
}
}
}
else
printf("该商品不存在!\n");
}
/*处理原有商品的新进货量*/
void purchase(struct Jd *head)
{
int flag=0,num;
struct Jd *p=head,*p1=head,*p2;
p2=(struct Jd *)malloc(SIZEJD);
p2->next=NULL;
printf("请输入商品编号: \n");
scanf("%s",p2->goods.id);
while(p)
{
if(strcmp(p2->goods.id,p->goods.id)==0)
{
flag=1;
break;
}
p=p->next;
}
if(flag==1)
{
printf("请输入商品新进货量: \n");
scanf("%d",&num);
while(p1)
{
if(strcmp(p1->goods.id,p2->goods.id)==0)
{
p1->goods.purchase_quantity+=num;
p1->goods.remaining_quantity+=num;
}
p1=p1->next;
}
}
else
printf("该商品不存在!");
}
/*处理原有商品的新销售量*/
void sales(struct Jd *head)
{
int flag=0,num;
struct Jd *p=head,*p1=head,*p2;
p2=(struct Jd *)malloc(SIZEJD);
p2->next=NULL;
printf("请输入商品编号: \n");
scanf("%s",p2->goods.id);
while(p)
{
if(strcmp(p2->goods.id,p->goods.id)==0)
{
flag=1;
break;
}
p=p->next;
}
if(flag==1)
{
printf("请输入商品新销售量: \n");
scanf("%d",&num);
while(p1)
{
if(strcmp(p1->goods.id,p2->goods.id)==0)
{
p1->goods.sales_volume+=num;
p1->goods.remaining_quantity-=num;
}
p1=p1->next;
}
}
else
printf("该商品不存在!\n");
}
/*处理原有商品的报废量*/
void baofei(struct Jd *head)
{
int flag=0,num;
struct Jd *p=head,*p1=head,*p2;
p2=(struct Jd *)malloc(SIZEJD);
p2->next=NULL;
printf("请输入商品编号: \n");
scanf("%s",p2->goods.id);
while(p)
{
if(strcmp(p2->goods.id,p->goods.id)==0)
{
flag=1;
break;
}
p=p->next;
}
if(flag==1)
{
printf("请输入商品报废量: \n");
scanf("%d",&num);
while(p1)
{
if(strcmp(p1->goods.id,p2->goods.id)==0)
{
if(p1->goods.remaining_quantity>=num)
p1->goods.remaining_quantity-=num;
else
{
printf("数据不合理!\n");
break;
}
}
p1=p1->next;
}
}
else
printf("该商品不存在!\n");
}
/*统计销售情况*/
void sale(struct Jd *head)
{
int flag=0;
struct Jd *p=head,*p1=head,*p2;
p2=(struct Jd *)malloc(SIZEJD);
p2->next=NULL;
printf("请输入商品编号: \n");
scanf("%s",p2->goods.id);
while(p)
{
if(strcmp(p2->goods.id,p->goods.id)==0)
{
flag=1;
break;
}
p=p->next;
}
if(flag==1)
{
while(p1)
{
if(strcmp(p1->goods.id,p2->goods.id)==0)
{
printf("该商品的销售额为: %lf\n",p1->goods.amount);
printf("该商品的剩余数为: %d\n",p1->goods.remaining_quantity);
p->goods.gross_profit=(p->goods.amount)-(p->goods.purchase_price)*(p->goods.sales_volume);
printf("该商品的毛利为: %lf\n",p1->goods.gross_profit);
}
p1=p1->next;
}
}
else
printf("该商品不存在!\n");
}
/*根据商品名查询*/
void namechaxun(struct Jd *head)
{
struct Jd *p=head,*p1=head;
int flag=0;
char name[MAXSIZE];
printf("请输入要查询的商品品名: ");
scanf("%s",name);
while(p)
{
if(strcmp(p->goods.name,name)==0)
{
flag=1;
break;
}
p=p->next;
}
if(flag==1)
{
while(p1)
{
if(strcmp(p1->goods.name,name)==0)
{
printf("商品编号: %s\n",p1->goods.id);
printf("商品品名: %s\n",p1->goods.name);
printf("商品进价: %lf\n",p1->goods.purchase_price);
printf("商品售价: %lf\n",p1->goods.selling_price);
printf("商品进货量: %d\n",p1->goods.purchase_quantity);
printf("商品销售量: %d\n",p1->goods.sales_volume);
printf("商品销售额: %lf\n",p1->goods.amount);
printf("商品剩余数: %d\n",p1->goods.remaining_quantity);
printf("商品毛利: %lf\n",p1->goods.gross_profit);
}
p1=p1->next;
}
}
else
printf("该商品不存在!\n");
}
/*根据剩余数查询*/
void shuchaxun(struct Jd *head)
{
struct Jd *p=head;
int s,b;
int flag=0;
printf("请输入查询区间: \n");
scanf("%d%d",&s,&b);
if(s>b)
printf("输入错误,请重新输入!\n");
else
{
while(p)
{
if(p->goods.remaining_quantity>=s&&p->goods.remaining_quantity<=b)
{
flag=1;
printf("商品编号: %s\n",p->goods.id);
printf("商品品名: %s\n",p->goods.name);
printf("商品进价: %lf\n",p->goods.purchase_price);
printf("商品售价: %lf\n",p->goods.selling_price);
printf("商品进货量: %d\n",p->goods.purchase_quantity);
printf("商品销售量: %d\n",p->goods.sales_volume);
printf("商品销售额: %lf\n",p->goods.amount);
printf("商品剩余数: %d\n",p->goods.remaining_quantity);
printf("商品毛利: %lf\n",p->goods.gross_profit);
}
p=p->next;
}
if(flag==0)
printf("未找到任何记录!\n");
}
}
/*按原来商品顺序显示*/
void xianshi(struct Jd *head)
{
struct Jd *p=head;
if(p!=NULL)
{
while(p!=NULL)
{
printf("商品编号: %s\n",p->goods.id);
printf("商品品名: %s\n",p->goods.name);
printf("商品进价: %lf\n",p->goods.purchase_price);
printf("商品售价: %lf\n",p->goods.selling_price);
printf("商品进货量: %d\n",p->goods.purchase_quantity);
printf("商品销售量: %d\n",p->goods.sales_volume);
printf("商品销售额: %lf\n",p->goods.amount);
printf("商品剩余数: %d\n",p->goods.remaining_quantity);
printf("商品毛利: %lf\n",p->goods.gross_profit);
p=p->next;
}
}
else
printf("暂无商品信息!\n");
}
/*按销售额排序显示*/
void amountxianshi(struct Jd *head)
{
struct Jd *pt1,*pt2,*pt;
struct Goods temp;
for(pt1=head;pt1->next!=NULL;pt1=pt1->next)
{
pt=pt1;
for(pt2=pt1->next;pt2!=NULL;pt2=pt2->next)
{
if(pt->goods.amount>pt2->goods.amount)
pt=pt2;
}
if(pt!=pt1)
{
temp=pt1->goods;
pt1->goods=pt->goods;
pt->goods=temp;
}
}
xianshi(head);
}
/*按毛利显示*/
void profitxianshi(struct Jd *head)
{
struct Jd *pt1,*pt2,*pt;
struct Goods temp;
for(pt1=head;pt1->next!=NULL;pt1=pt1->next)
{
pt=pt1;
for(pt2=pt1->next;pt2!=NULL;pt2=pt2->next)
{
if(pt->goods.gross_profit>pt2->goods.gross_profit)
pt=pt2;
}
if(pt!=pt1)
{
temp=pt1->goods;
pt1->goods=pt->goods;
pt->goods=temp;
}
}
xianshi(head);
}
/*把链表结点数据写往文件*/
void WriteToFile(struct Jd *head,char *FName)
{
struct Goods go;
FILE *fp;
struct Jd *pt;
if((fp=fopen(FName,"w"))==NULL)
{
printf("文件打开失败!");
exit(0);
}
pt=head;
while(pt!=NULL)
{
fprintf(fp,"%s %s %lf %lf %d %d %lf %d %lf\n",go.id,go.name,go.purchase_price,go.selling_price,go.purchase_quantity,go.sales_volume,go.amount,go.remaining_quantity,go.gross_profit);
pt=pt->next;
}
fclose(fp);
}
```