#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <conio.h>
#define  M    30
#define  N    10000


struct Lnode
 {
    char            code[20];
    char            name[80];
    int                price;
    Lnode    *next;
 };

typedef Lnode *SqList;

int sum = 0;
int goodnum = 0;
int NUMBER[N];
char  str[N][80];



int  Listname(char str[][], char name[]);
void Listrecord(Lnode *head);
int Listsearch(SqList L, char code2[]);
void Listfoundinformation(SqList &L);
void Listsert(Lnode *head);
void ListDelete(Lnode *head, char code[]);
void ListDelete2(SqList &L, char code[]);
void Listpint(Lnode *head);
void Listallpint(SqList L);



void main()
{
    char code2[20];
    char ch;
    SqList L;
    int x,i,s;
    Lnode *head;

    head=new struct Lnode;
    if(head==NULL)
    {
        printf("the memory is not enough!");
        return;
    }
    else
        head->next=NULL;
    
    printf("Please input the information of the good:\n");


    
    while(1)   //输入初始商品信息
    {
        Listfoundinformation(head);

        printf("Do you want to continue(YES 1 or NO 0):");
        scanf("%d", &s);

        if(s==0)
           break;
    }
   
    
    printf("output print 1 or 0 :");// 查找商品信息
    ch=getchar();
    while(ch=='1') 
    {
        printf("please input the good's code:");
        scanf("%s",code2);
        Listsearch(L,code2);
        printf("if you want to continue please 1 ,other 0:");
        ch=getchar();
   }
    system("cls");
   

   printf("if you want to delete good information(input 1 or 0):");//删除商品信息
   scanf("%d", &x);
   while (x==1)
   {
        printf("\ninput code:");
        scanf("%s", code2);
        ListDelete(head, code2);
        
   }


   printf("\nif want to insert the good information 1 0r 0:");//插入商品信息
   scanf("%d", &x);
   while(x==1)
   {
    Listsert(head);
    printf("\nCONTINUE OR NOT(1 or 0):");
    scanf("%d", &x);
   }


   printf("\nif you want to sell the good please input 1 other 0 :");//出售商品
   ch=getchar();
   if(ch =='1')
      Listpint(L);
   printf("\nthe sum price of all good is %d", sum);
   printf("\nthe number of all goods is %d",goodnum);
   system("cls");


   printf("if you want to print the all information(1 or 0):");//商品总货清单
   scanf("%d", &x);
   if(x==1)
       Listallpint(L);


   printf("if you want to record the information please input 1 or 0:");//信息入文件
   scanf("%d", &x);
   if(x==1)
       Listrecord(head);
}



void Listfoundinforation(SqList &L)
{
    char name[80];
    char code[20];
    int price,i;
    Lnode *pnew;

    printf("please input the code:");
    scanf("%s",code);
    printf("\nplease input the name:");
    scanf("%s",name);
    fflush(stdin);
    printf("\nplease input the price:");
    scanf("%d", &price);

    pnew=new struct Lnode;
    if(pnew==NULL)
    {
        printf("the room is not enough");
        exit;
    }
    strcpy(pnew->code,code);
    strcpy(pnew->name,name);
    pnew->price=price;

    Listsert(L,code);

    i=Listsearch(L,name);
    if(i>N)
    {
        NUMBER[i++]++;
    }
    else
        NUMBER[i]++;

    Listrecord(L);
}


void Listsert(Lnode *head)
{
    char name[80];
    char code[20];
    int price;
    int i;
    Lnode *pnew,*p;

    printf("CODE:");
    scanf("%s",code);
    printf("\nNAME:");
    scanf("%s",name);
    fflush(stdin);
    printf("\nPRICE:");
    scanf("%d",&price);

    pnew=new struct Lnode;
    if(pnew=NULL)
    {
        printf("the memory is not enough!");
    }
    strcpy(pnew->code,code);
    strcpy(pnew->name,name);
    pnew->price=price;

    p=head;
    while(p->next!=NULL)
        p=p->next;
    p->next=pnew;
    pnew->next=NULL;
    

    i=Listname(str,name);
    NUMBER[i]++;
}

void Listsearch(SqList L,char code2[])
{
    int i,j;
    char name[80];
    Lnode *q;
    for(q=head;q->next!=NULL;q=q->next)
        if(strcmp(q->code, code2)==0)
            break;

    strcpy(name,p->name);
    i=Listname(str, name);
    if(q->next==NULL)
    {
        printf("the good is not exist\n");
        return;
    }
    printf("编号        名称        价格        数量\n");
    printf("%-10c%-10c%-10d%-10d\n", q->code, q->name, q->price,    NUMBER[i]);
    printf("------------------------------------------------------------------\n");
    printf("\n");

    if(NUMBER[i]<M)
    {
        printf("the good is not enough!");
    }
 }

int Listname(char str[][],char name[])
{
    int i=0;
    while((strcmp(str[i],name)!= 0) )
        i++;
    return (i);
    
}


void  ListDelete(SqList &L,char code[])
{
    Lnode *q,*h;
    int i;

    q=head;
    h=q->next;
    while(h->code!=code)
    {    
            q=q->next;
         h=q ->next;
    }
    
    i=Listname(str,h->name);
    if(i==-1)
    {
        printf("the code is not exist!\n");
        return;
    }
    NUMBER[i]--;
    free(h);
}


void ListDelete2(SqList &L,char code[])
{
    int i;
    Lnode *q,*p;
    q=head;
    p=q->next;

    while(q->next->code!=code)
    {    
           q=q->next;
        p=q ->next;
    }

    i=Listname(str,p->name);
    NUMBER[i]--;

    
    printf("%-10c%-10d%-10d\n",p->name,p->price,NUMBER[i]);
    printf("\n");

    sum+=p->price;
    goodnum++;

    q->next=p->next;
    free(p);
    
}

void Listrecord(Lnode *head)//退出并把数据写入文件//
{
    FILE *fp;
    Lnode *pnew, *q;
    
    fp=fopen("data.txt","w");        
    for(pnew=head->next;pnew!=NULL;pnew=pnew->next)
    {
        
    if(pnew->next!=NULL)
    {
        fputs (pnew->name,fp);
        fputs ("\n",fp);
        fprintf(fp,"%f",pnew->price);
        fputs ("\n",fp);
        fprintf(fp,"%d",pnew->code);
        fputs ("\n",fp);
    }

    else
    {
        fputs (pnew->name,fp);
        fputs(pnew->price,fp);
        fputs ("\n",fp);
        fprintf(fp,"%d",pnew->code);

    }
    
    }

void Listpint()
{

    int x=1;
    int price;
    char name[80],code[20];
    
    printf("\t\t\t\t\t编号        名称        价格        数量\n");
    while(x)
    {
     printf("编号:");
     scanf("%s",code);
     ListDelete(L,code);
     printf("\ncontinue or not (1 or 0):");
     scanf("%d",&x);
     }
    return;
}


void Listallprint(Lnode *head)
{
    Lnode *q;
    int i;

    q=head->next;
    printf("编号        名称        价格        数量\n");
    while(q->next!=NULL)
    { 
      i=Listname(str, q->name);
      
      printf("%-10c%-10c%-10.2f%-10d\n",q->code,q->name,q->price,NUMBER[i]);
         printf("------------------------------------------------------------------\n");
      printf("\n");
    }

    printf("\n");
    printf("\n");
    printf("\n");
    printf("\t\t\t\t\");
    printf("商品存货信息");
    printf("------------------------------------------------------");

    q=head->next;
    while(q->next!=NULL)
    {
        i=Listname(str,q->name);
        if(i<M)
            printf("the good of %d is not enough ,please buy some\n", str[i]);
    }
}