
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<string.h>
#include<process.h>

struct q { char num[8];
	   char name[21];
	   int count;
	   float price;
	   struct q *p;
	  } n;

void newmem()
{	struct q *p=&n;
	while(p->p) p=p->p;
	p->p=(struct q *)malloc(sizeof(n));
	if(p->p)
      {	p=p->p;
	p->num[0]=NULL;p->p=NULL;p->count=0;
      }
	else printf("\n NO MEMORY");
}
//-----NEW MEM
void memfree()
{	struct q *p=NULL;

	while(p!=&n)
       {
	if(n.p==NULL) break;
	p=&n;
	while((p->p)->p) p=p->p;
	free(p->p);p->p=NULL;
       }
}
//---MEM FREE
void prns(struct q *p)
{
  printf("\n||  %s  ||  %d  ||  %.2f  ||  %s  ||\n",p->name,p->count,p->price,p->num);
}
void prnz()
{       clrscr();
	printf("\n||  NAME  ||  COUNT  ||  PRICE  ||  NUMBER  ||\n");
}
//-----PRINT
void cutz()
{	struct q *p=&n,*p1;

	while((p1=p->p)&&p1->count) p=p->p;

	if(p1) {   p->p=p1->p;
		   free(p1);
		}
}
//-----CUT ZERO COUNT
int numok(struct q *p)
{	int i=0,r=0;
	while(p->num[i]) {r+=(p->num[i]<'0'||p->num[i]>'9');i++;}
	return r;
}
//------numok
void newpr()
{
	struct q *p=&n;

	do{
	printf("\nTYPE NUMBER = ");
	scanf("%7s",&n.num);}
	while(numok(&n));

	printf("\nTYPE NAME : ");
	scanf("%s20",&n.name);

	do{
	printf("\nTYPE PRICE = ");
	scanf("%f",&n.price);}
	while(n.price<=0);

	do{
	printf("\nTYPE COUNT = ");
	scanf("%d",&n.count);}
	while(n.count<0);

	while(p) {p=p->p;if (p&&!strcmp(p->num,n.num)) break;}

	if(!p) {newmem();p=&n;while(p->p) p=p->p;}
	else   {  prnz();prns(p);getch();}

	strcpy(p->num,n.num);
	p->count=n.count;p->price=n.price;
	strcpy(p->name,n.name);
	cutz();
}
//------NEW PRODUCT
void decrem()
{
	struct q *p=n.p;
	char cn[8];
	printf("\n ENTER NUMBER OF PRODUCT TO DECREMENT : ");
	scanf("%7s",cn);

	while(p&&strcmp(p->num,cn)) p=p->p;

	if(p) (p->count)--;
	else {printf("\n NUMBER NOT FOUND ");getch();}
	cutz();
}
//---------------
void info()
{
	struct q *p=n.p;
	prnz();
	while(p) {prns(p);p=p->p;getch();}
}
//----INFORMATION ABOUT PRODUCTS
void load()
{       struct q *p=&n;FILE *fh;char fname[15];
	int r=sizeof(n);
	memfree();
	n.p=0;

	printf("\nTYPE FILE NAME TO LOAD : ");
	scanf("%s",fname);
	if((fh=fopen(fname,"rb"))==NULL) {printf("\n ERROR OPEN");exit(0);}

	while(r&&(!feof(fh)))
     {  newmem();
	r=fread(p->p,sizeof(n),1,fh);
	if(r) p=p->p; else free(p->p);
	p->p=NULL;
      }
	if(fclose(fh)==EOF) {printf("ERROR CLOSE");exit(0);}
}
//---LOAD
void save()
{
	struct q *p=n.p;FILE *fh;char fname[15];
	printf("\nTYPE FILE NAME TO SAVE : ");
	scanf("%s",fname);
	if((fh=fopen(fname,"wb"))==NULL) {printf("ERROR OPEN");exit(0);}

	while(p)
     {	fwrite(p,sizeof(n),1,fh);
	p=p->p;
     }
	if(fclose(fh)==EOF) {printf("ERROR CLOSE");exit(0);}
}
//----SAVE
void make()
{         char c=0;

	while(c!='6')
   {
	clrscr();
	printf("\n\n\n     1:   NEW  PRODUCT   ");
	printf("\n\n     2:  INFO ABOUT PRODUCTS");
	printf("\n\n     3:   DECREMENT PRODUCT");
	printf("\n\n     4:        LOAD");
	printf("\n\n     5:        SAVE");
	printf("\n\n     6:        QUIT\n\n");

	c=getch();
	switch(c)
      {
	case '1':newpr();break;
	case '2':info();break;
	case '3':decrem();break;
	case '4':load();break;
	case '5':save();break;
	case '6':memfree();break;
       }

    }
}
//-------MAKE ALL
int main()
{
	make();
}
//-------END OF PROGRAMM