药店药品信息管理系统

用链表实现药店药品信息管理系统

#include <stdio.h>
#include <stdlib.h>
# include <string.h>
struct medicine{int number;int in_prince;int out_price;struct medicine *next;int amount;char name[100];char factory[100];int year;int month;int day;int bzyear;int bzmonth;}; /*结构体*/ 

void m1(void);   /*打开文件,建立一个链表并储存到文件,文件指针回到头,关闭文件,释放之前的链表*/
void m2(void);   /*打开文件,从文件中读取,关闭文件 删除节点,用覆盖的方式打开文件,链表到文件,删除旧连表,关闭文件,打开文件,读取新链表,遍历新链表*/ 
void m3(void);   //修改 
void m4 (void);    //销售 
void m5(void);   //查询 
void m6(void);    //统计 
void m7(void);     //增加 
void list(struct medicine *head);    /*输出链表中的所有节点*/
struct medicine *medfile_put(FILE *medfile);  //1文件内容到链表 2文件的指针回头 3函数返回新链表的头指针 4没有文件的打开和关闭 
void lianbiaointofile(struct medicine *head,FILE *medfile); //1链表到文件 2释放旧的链表 3文件指针回头 4没有文件的打开和关闭 
void lianbiaointofile111(struct medicine *head,FILE *medfile); //1链表到文件 3文件指针回头 4没有文件的打开和关闭 ,不释放 !!!!!!!!!!为了另外储存一份初始的数据以便于求出销售了多少 
struct medicine* del(struct medicine* head, int number);   /*只是删除了结点*/ 
void sflianbiao(struct medicine *head);   //释放链表 

void h1 (void); 
void h2 (void) ;   //按药品名称查询 
void z1(void) ;  //按药品名称精准查询 
void z2(void) ;  //按药品名称模糊查询 
void h3 (void);   //按药品厂家精准查询 
int main()
{      
    int x=1;
    while(x!=0)
    {
    	int n;
    	printf("\n\n\n");
    	printf("\t--------------------------------------------------------------\n");
        printf("\t*                  欢迎来到药店药品信息管理系统                   *\n");
	    printf("\t                       请选择您接下来的服务                    \n"); 
	    printf("\t--------------------------------------------------------------\n");
	    printf("\t                        1.创建药品信息\n");
	    printf("\t                        2.删除药品信息\n");
	    printf("\t                        3.修改药品信息\n");
	    printf("\t                        4.销售药品信息\n");
     	printf("\t                        5.查询药品信息\n");
	    printf("\t                        6.统计药品信息\n");
	    printf("\t                        7.添加药品信息\n");
	    printf("\t                        0.    结束     \n"); 
    	printf("\t                  请输入数字来选择您想要的功能:");
    	scanf("%d",&x);
    	printf("\n\n\n");
	    switch(x)
	    {
	    	case 0:   return 0; break;
		    case 1:    m1()    ;break;
		    case 2:    m2()    ;break;
	    	case 3:    m3()    ;break;
		    case 4:    m4()    ;break;
		    case 5:    m5()    ;break;
		    case 6:    m6()    ;break;
		    case 7:    m7()    ;break;
		    default:printf("按键错误,请重新选择!\n");break;
    	}
	}
	return 0;
}


void m1(void)   /*打开文件,建立一个链表并储存到文件,文件指针回到头,关闭文件,释放之前的链表        存了两份一份不动*/ 
{ 

  FILE *medfile;
  FILE *medfile1;
  struct medicine *head=NULL;
  int n_judg; 
  struct medicine *p0,*p1,*p2;
  
  printf("您是否想输入药品信息?按1继续输入,按0结束输入:");
  scanf("%d",&n_judg);
  while(n_judg==1)
  {
    p0=(struct medicine*)malloc(sizeof(struct medicine));
    p0->next=NULL; 
    printf("输入药瓶编号:");      scanf("%d",&p0->number); 
    printf("输入进价:");          scanf("%d",&p0->in_prince);
    printf("输入售价:");	      scanf("%d",&p0->out_price);
    printf("输入药品数量");       scanf("%d",&p0->amount) ;
    printf("输入药品名称");       scanf("%s",p0->name);
    printf("输入药品厂家");       scanf("%s",p0->factory);
    printf("输入生产年份");       scanf("%d",&p0->year);
    printf("输入生产月份");       scanf("%d",&p0->month);
    printf("输入生产日");       scanf("%d",&p0->day);
    printf("输入保质年份");       scanf("%d",&p0->bzyear);
    printf("输入保质月份");       scanf("%d",&p0->bzmonth);
    getchar();
    if(head==NULL) head=p0;
    else 
	{
	  p1=head;
      while((p0->number>p1->number)&&(p1->next!=NULL))
	  {p2=p1;p1=p1->next;}
      if(p0->number<=p1->number)
	  {
	      if(head==p1) head=p0;
		  else p2->next=p0;
		  p0->next=p1;
	  }
      else p1->next=p0;
	} 	
    printf("您是否想继续输入药品信息?按1继续输入,按0结束输入:");
    scanf("%d",&n_judg); 
  }
  
  medfile1=fopen("D:\\MEDFILE1.dat","w+");
  lianbiaointofile111(head,medfile1);
  fclose(medfile1);
  
  
  medfile=fopen("D:\\MEDFILE.dat","w+");
  lianbiaointofile(head,medfile);//1链表到文件 2释放旧的链表 3文件指针回头 4没有文件的打开和关闭    lianbiaointowenjian没有错!!!!! 
  fclose(medfile);
  
  medfile=fopen("D:\\MEDFILE.dat","r+"); 
  head=medfile_put(medfile);//1文件内容到链表 2文件的指针回头 3函数返回新链表的头指针 4没有文件的打开和关闭 
  fclose(medfile);
  
  
  list(head);   
  
}

void m2(void)
{
   int x=1; 
   while(x==1)
   {
   	    FILE *medfile;
	    int number;
	    struct medicine *head=NULL;
	
        medfile=fopen("D:\\MEDFILE.dat","r+"); 
        head=medfile_put(medfile);//1文件内容到链表 2文件的指针回头 3函数返回新链表的头指针 4没有文件的打开和关闭 
        fclose(medfile); 
    
        printf("\t请输入要删除的药品的编号:");
        scanf("%d",&number);
        head=del(head,number); 
    
        fopen("D:\\MEDFILE.dat","w+");
	    lianbiaointofile(head,medfile);//链表到文件 ,并回到头 
	    fclose(medfile);
	
     	medfile=fopen("D:\\MEDFILE.dat","r+"); 
    	head=medfile_put(medfile);
    	fclose(medfile);
    	list(head);
    	
    	sflianbiao(head); 
    	
    	printf("是否想要继续删除?按1继续,按0退出");
    	scanf("%d",&x); 
   }
}

void m3(void)
{
	int x=1;
    while(x==1)
    {
    	FILE* medfile; 
    	struct medicine *head=NULL;
    	int bianhao;  /*想要修改的编号*/
    	medfile=fopen("D:\\MEDFILE.dat","r+");
    	head=medfile_put(medfile);//文件到链表  
        fclose(medfile);  /*因为有rewind所以指回了开头*/ 
    	struct medicine* p=head;/*下面用p操作*/
	    
    	printf("请输入您想要修改药品的编号:");
	    scanf("%d",&bianhao);
    	while((p->number!=bianhao)&&(p!=NULL))
	    {
	        p=p->next;
	    }
	    if (p->number!=bianhao)
	    printf("该药品编号不存在!");  
    	if (p->number==bianhao)
	    {
		    printf("请输入新的进价:");        scanf("%d",&p->in_prince);           
	    	printf("请输入新的售价:");        scanf("%d",&p->out_price);	    	
	    	printf("请输入新的药品数量:");    scanf("%d",&p->amount);	    	
	    	printf("请输入新的药品名称:");    scanf("%s",p->name);   		
	    	printf("请输入新的药品厂家:");    scanf("%s",p->factory);	    	
	    	printf("输入新的生产年份");       scanf("%d",&p->year);
            printf("输入新的生产月份");       scanf("%d",&p->month);
            printf("输入新的生产日");         scanf("%d",&p->day);
            printf("输入新的保质年份");       scanf("%d",&p->bzyear);
            printf("输入新的保质月份");       scanf("%d",&p->bzmonth);
 	    } 
     	medfile=fopen("D:\\MEDFILE.dat","w+");
    	lianbiaointofile(head,medfile);//链表到文件 释放旧链表 
    	fclose(medfile);
     	medfile=fopen("D:\\MEDFILE.dat","r+");
    	head=medfile_put(medfile);  //1文件内容到链表 2文件的指针回头 3函数返回新链表的头指针 4没有文件的打开和关闭 
    	list(head);
    	fclose(medfile);
    	printf("您是否想继续修改?按1继续,按0退出");
    	scanf("%d",&x);
	}
	
}

void m4(void)
{
	int x=1;
	while(x==1)
	{
		int n;
	    FILE *medfile;
	    int bianhao;
	    struct medicine *head=NULL;
	
        medfile=fopen("D:\\MEDFILE.dat","r+"); 
        head=medfile_put(medfile);//1文件内容到链表 2文件的指针回头 3函数返回新链表的头指针 4没有文件的打开和关闭 
        fclose(medfile); 
    
        printf("\t请输入要销售的药品的编号:");
        scanf("%d",&bianhao);
        struct medicine* p=head;/*下面用p操作*/
        while((p->number!=bianhao)&&(p!=NULL))
	    {
	    	p=p->next;
	    }
	    if (p->number!=bianhao)
	    printf("该药品编号不存在!");  
    	if (p->number==bianhao)
	    {
	    	printf("请输入您要销售药品的数量:");
    		scanf("%d",&n);
	    	int flag=0;
	    	while(flag==0)
		    {
			    if(n>p->amount)
		        {   
	    		   printf("您输入的药品数量已超过库存,请重新输入") ;
			       flag=0;
		    	   scanf("%d",&n);
		        } 
		        if (n<=p->amount)
	     	    {
		        	flag=1;
		        	printf("销售成功!");
		        	p->amount=p->amount-n;
		        	printf("%d",p->amount);
			    }
	    	}
     	} 
 	
        fopen("D:\\MEDFILE.dat","w+");
    	lianbiaointofile(head,medfile);   //链表到文件 ,并回到头 
    	fclose(medfile);
	
    	medfile=fopen("D:\\MEDFILE.dat","r+"); 
    	head=medfile_put(medfile);
    	fclose(medfile);
	
    	list(head);
    	sflianbiao(head); 
    	
    	printf("您是否想继续销售?按1继续,按0退出");
    	scanf("%d",&x);
	}
}

void m5(void)
{
	int x=1;
	while(x==1)
	{
		printf("\t    1.按药品编号\n");
    	printf("\t    2.按药品名称\n");
    	printf("\t    3.按药品厂家\n");
    	int n;
    	printf("\t请输入您想按照什么方式进行查询:");   scanf("%d",&n);
	
	    switch(n)
    	{
    	    case 1:  h1();  break;
    		case 2:  h2();  break;
    		case 3:  h3();  break;
    		default:printf("按键错误\n");break;
	    }
	    printf("您是否想继续查询?按1继续查询,按0退出:");
	    scanf("%d",&x);
	}
}

void m6(void)
{
	int x=1;
	while(x==1)
	{
		FILE *medfile;
        FILE *medfile1;
    	struct medicine *head=NULL;
    	struct medicine *head1=NULL;
	
        medfile=fopen("D:\\MEDFILE.dat","r+"); 
        head=medfile_put(medfile);//1文件内容到链表 2文件的指针回头 3函数返回新链表的头指针 4没有文件的打开和关闭 
        fclose(medfile); 
    
        medfile1=fopen("D:\\MEDFILE1.dat","r+"); 
        head1=medfile_put(medfile1);//1文件内容到链表 2文件的指针回头 3函数返回新链表的头指针 4没有文件的打开和关闭
        fclose(medfile1); 
    
    
        struct medicine* p=head;/*下面用p操作*/
        struct medicine* p2=head;/*下面用p2操作*/
        struct medicine* p3=head;/*下面用p3操作*/
    
        struct medicine* p1=head1;/*下面用p1操作*/
    
        while (p!=NULL)
        {
        	int kk=p1->amount-p->amount;      //   销售量 
        	int ww=kk*(p->in_prince-p->out_price);                         //成交额 
        	printf("%d号药品的销售量是%d  成交额是%d\n\n\n",p->number,kk,ww);
        	p=p->next;
    	}
    
        while (p2!=NULL)
        {
        	if(p2->amount<20)
        	printf("库存警告!%d号药品存货量小于20,请及时补货\n\n\n",p2->number) ;
        	p2=p2->next;
    	}
	
    	while(p3!=NULL)
    	{
	    	if (p3->year==p3->bzyear)   //在同一年
	    	{
	    		if(p3->year-p3->bzyear<3)
	    		printf("警告! %d号药品即将过期!\n\n\n",p3->number); 
	    	} 
	    	if (p3->year!=p3->bzyear)      //相差一年 
	    	{
		    	if (p3->year-p3->bzyear==1)
		    	{
		    		if (p3->bzmonth-p3->month>9)
		    		printf("警告! %d号药品即将过期!\n\n\n",p3->number);
	    		}
	    	}
		p3=p3->next;
    	}
    	printf("您是否想重新统计?按1重新统计,按0退出:");
    	scanf("%d",&x);
	}
}

void m7(void)
{
  int x=1;
  while(x==1)
  {
  	 FILE *medfile;
     struct medicine *head=NULL;
     medfile=fopen("D:\\MEDFILE.dat","r+"); 
     head=medfile_put(medfile);//1文件内容到链表 2文件的指针回头 3函数返回新链表的头指针 4没有文件的打开和关闭 
     fclose(medfile);
     struct medicine *p0,*p1,*p2;
     p0=(struct medicine*)malloc(sizeof(struct medicine));
     p0->next=NULL; 
     printf("输入药瓶编号:");      scanf("%d",&p0->number); 
     printf("输入进价:");          scanf("%d",&p0->in_prince);
     printf("输入售价:");	       scanf("%d",&p0->out_price);
     printf("输入药品数量:");	   scanf("%d",&p0->amount);
     printf("输入药品名称:");	   scanf("%s",p0->name);
     printf("输入药品厂家:");	   scanf("%s",p0->factory);
     printf("输入生产年份");       scanf("%d",&p0->year);
     printf("输入生产月份");       scanf("%d",&p0->month);
     printf("输入生产日");         scanf("%d",&p0->day);
     printf("输入保质年份");       scanf("%d",&p0->bzyear);
     printf("输入保质月份");       scanf("%d",&p0->bzmonth);
     getchar();
     if(head==NULL) head=p0;
     else 
   	 {
        p1=head;
        while((p0->number>p1->number)&&(p1->next!=NULL))
	    {p2=p1;p1=p1->next;}
        if(p0->number<=p1->number)
	    {
	      if(head==p1) head=p0;
		  else p2->next=p0;
		  p0->next=p1;
	    }
        else p1->next=p0;
	 } 	
     medfile=fopen("D:\\MEDFILE.dat","w+");
     lianbiaointofile(head,medfile);//1链表到文件 2释放旧的链表 3文件指针回头 4没有文件的打开和关闭 
     fclose(medfile);
  
     medfile=fopen("D:\\MEDFILE.dat","r+"); 
     head=medfile_put(medfile);
     fclose(medfile);
     list(head);
     sflianbiao(head); 
     
     printf("您是否想继续添加药品信息?按1继续,按0退出:");
     scanf("%d",&x); 
   }
}

void h1 (void)    //按药品编号查询 
{
	int y=1;
	while (y==1)
	{
		FILE *medfile;
        int bianhao;
    	struct medicine *head=NULL;
	
        medfile=fopen("D:\\MEDFILE.dat","r+"); 
    
        head=medfile_put(medfile);//1文件内容到链表 2文件的指针回头 3函数返回新链表的头指针 4没有文件的打开和关闭 
    
        fclose(medfile); 
        struct medicine* p=head;/*下面用p操作*/
        printf("请输入您要查询药品的编号:") ;
        scanf("%d",&bianhao);
        while((p->number!=bianhao)&&(p!=NULL))
	    {
    		p=p->next;
    	}
    	if (p==NULL)
    	printf("该药品编号不存在!");  
    	if (p->number==bianhao)
    	{
     		printf("以检索到该药品:");
     		printf("药品编号:%d 药品进价:%d 药品售价:%d 药品数量:%d 药品名称:%s 药品厂家:%s 生产年:%d 生产月:%d 生产日:%d 保质年:%d 保质月:%d",p->number,p->in_prince,p->out_price,p->amount,p->name,p->factory,p->year,p->month,p->day,p->bzyear,p->bzmonth);                                         
     	} 
     	printf("您是否想继续按照药品编号查询?按1继续,按0退出:");
		scanf("%d",&y); 
 	}
}

void h2 (void)    //按药品名称查询 
{
	int s=1;
	while(s==1)
	{
		int m;
    	printf("1精准查询"); 
    	printf("2模糊查询"); 
    	printf("请输入您要精准查询还是模糊查询:");
    	scanf("%d",&m);
    	switch(m)
    	{
    		case 1:  z1();break;
    		case 2:  z2();break; 
    		default:printf("按键错误\n");break;
    	}
    	printf("您是否想继续按照药品名称查询?按1继续,按0退出");
    	scanf("%d",&s);
	}
}

void z1(void)        //按药品名称精准查询 
{
	char b[100];
	FILE *medfile;
    
	struct medicine *head=NULL;
	
    medfile=fopen("D:\\MEDFILE.dat","r+"); 
    
    head=medfile_put(medfile);//1文件内容到链表 2文件的指针回头 3函数返回新链表的头指针 4没有文件的打开和关闭 
    
    fclose(medfile); 
    struct medicine* p=head;/*下面用p操作*/
    
    printf("请输入您要精准查询的药品名称:");
    scanf("%s",b);
    
    while((strcmp(p->name,b)!=0)&&(p!=NULL))
	{
		p=p->next;
	}
	if (strcmp(p->name,b)!=0)
	printf("该药品编号不存在!");  
	if (strcmp(p->name,b)==0)
	{
		printf("以检索到该药品:");
		printf("药品编号:%d 药品进价:%d 药品售价:%d 药品数量:%d 药品名称:%s 药品厂家:%s 生产年:%d 生产月:%d 生产日:%d 保质年:%d 保质月:%d",p->number,p->in_prince,p->out_price,p->amount,p->name,p->factory,p->year,p->month,p->day,p->bzyear,p->bzmonth);
 	} 
    
    
}

void z2(void)   //按药品名称模糊查询 
{
	char c[100];
	int flag;
	FILE *medfile;
    
	struct medicine *head=NULL;
	
    medfile=fopen("D:\\MEDFILE.dat","r+"); 
    
    head=medfile_put(medfile);//1文件内容到链表 2文件的指针回头 3函数返回新链表的头指针 4没有文件的打开和关闭 
    
    fclose(medfile); 
    struct medicine* p=head;/*下面用p操作*/
    
    printf("请输入模糊查询的药品名称:");
    scanf("%s",c);
    
    while(p!=NULL)
    {
    	int flag=0;
    	int i=0; 
    	for (int t=0;c[t]!='\0';t++)
    	{
    		if(strchr (p->name,c[t])!=NULL)
    	    {
    		    i++;
		    }
		}
		if(i>3)
		{
			printf("该药品名称已被检索到:");
		    printf("药品编号:%d 药品进价:%d 药品售价:%d 药品数量:%d 药品名称:%s 药品厂家:%s 生产年:%d 生产月:%d 生产日:%d 保质年:%d 保质月:%d",p->number,p->in_prince,p->out_price,p->amount,p->name,p->factory,p->year,p->month,p->day,p->bzyear,p->bzmonth);
		    flag=1;
		    break;
		}
    	p=p->next;
	}
    
    if (flag==0)
    printf("模糊查询未检测到该药品");


} 

void h3 (void)   //按药品厂家精准查询 
{
	int g=1;
	while(g==1)
	{
		char u[100];
    	FILE *medfile;
    
    	struct medicine *head=NULL;
	
        medfile=fopen("D:\\MEDFILE.dat","r+"); 
    
        head=medfile_put(medfile);//1文件内容到链表 2文件的指针回头 3函数返回新链表的头指针 4没有文件的打开和关闭 
    
        fclose(medfile); 
        struct medicine* p=head;/*下面用p操作*/
    
        printf("请输入您要精准查询的药品厂家:");
        scanf("%s",u);
      
        while((strcmp(p->factory,u)!=0)&&(p!=NULL))
	    {
    		p=p->next;
	    }
    	if (strcmp(p->factory,u)!=0)
    	printf("该药品编号不存在!");  
    	if (strcmp(p->factory,u)==0)
    	{
     		printf("以检索到该药品:");
    		printf("药品编号:%d 药品进价:%d 药品售价:%d 药品数量:%d 药品名称:%s 药品厂家:%s 生产年:%d 生产月:%d 生产日:%d 保质年:%d 保质月:%d",p->number,p->in_prince,p->out_price,p->amount,p->name,p->factory,p->year,p->month,p->day,p->bzyear,p->bzmonth);
     	} 
     	printf("您是否想继续按照药品厂家精准查询?按1继续,按0退出:");
     	scanf("%d",&g);
  	}
}

void lianbiaointofile111(struct medicine *head,FILE *medfile) //1链表到文件 3文件指针回头 4没有文件的打开和关闭 ,不释放 
{
	struct medicine *p;
	p=head;
    while(p!=NULL)
    {
		fwrite(p,sizeof(struct medicine),1,medfile);        
        p=p->next;
	}
	rewind(medfile);
}

void list(struct medicine *head)    /*输出链表中的所有节点*/
{
    struct medicine *p;           /*存储*/
    if (head == NULL)
    printf("药品信息为不存在!\n");
    else
    {
        printf("药品信息如下:\n");
        p=head;               
        while (p != NULL)   /*循环的赋值然后输出*/
        {
            printf("编号:%d 进价:%d 售价:%d 库存数量:%d 名称:%s 厂家:%s 生产年:%d 生产月:%d 生产日:%d 保质年:%d 保质月:%d\n\n",p->number, p->in_prince, p->out_price,p->amount,p->name,p->factory,p->year,p->month,p->day,p->bzyear,p->bzmonth);
            p = p->next;
        }
    }
}

struct medicine *medfile_put(FILE *medfile)  //1文件内容到链表 2文件的指针回头 3函数返回新链表的头指针 4没有文件的打开和关闭   有问题!!!!!!!!! 
{
    struct medicine *p1=NULL,*p2=NULL,*head=NULL; 
    while(!feof(medfile))
    {
        p1=(struct medicine*)malloc(sizeof(struct medicine));
        fread(p1,sizeof(struct medicine),1,medfile);               
        if(head==NULL) head=p1;
        else p2->next=p1;
        p2=p1;
    }   
    p1->next=NULL; 
    rewind(medfile);        
	return(head);
}

void lianbiaointofile(struct medicine *head,FILE *medfile) //1链表到文件 2释放旧的链表 3文件指针回头 4没有文件的打开和关闭 
{
	struct medicine *p;
	p=head;
    while(p!=NULL)
    {
		fwrite(p,sizeof(struct medicine),1,medfile);        
        p=p->next;
	}
	sflianbiao(head);
	rewind(medfile);
}

struct medicine* del(struct medicine* head, int number)    /*只是删除了结点*/ 
{
    struct medicine* p1=NULL;
    struct medicine* p2=NULL;
    if (head == NULL)
        printf("链表为空无法进行删除!\n");
    else
    {
        p1 = head;
        while ((number != p1->number) && (p1->next != NULL))
        {
            p2 = p1;
            p1 = p1->next;
        }
        if (p1->number==number)
        {
            if (p1 == head)
                head = p1->next;
            else
                p2->next = p1->next;
            free(p1);
            printf("该节点已经删除\n");
        }
        else
            printf("链表中不存在该节点,无法删除!\n");
    }
    return head;
}

void sflianbiao(struct medicine *head)   //释放链表 
{
    struct medicine *p1=head,*p2 ;  
    while(p1!=NULL)
    {  p2=p1;
       p1=p1->next;
       free(p2);
	} 
} 
posted @ 2020-01-20 11:16  panghushalu  阅读(442)  评论(0编辑  收藏  举报