#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<process.h>
#include<string.h>
	int j;
	FILE *f1,*f2;
	char ime_file[12];
		struct pat {
		char ustrois,papka[35],ime[12];
	};
	struct zagl {
		int poz,br;
		struct pat di;
		};
	struct duma {
		int ptr,nom;
	};
	struct spis {
		int du1,du2,no1,no2,broi;
	};
	struct masiv {
		char niz[16];
	};
	struct vazel {
		struct zagl yd;
		struct duma ac[10];
	};
	typedef struct masiv work;
	typedef struct vazel obect;
	typedef struct spis omd_ob;
	work mas[10];
	omd_ob desc;
	obect ob1,ob2;
	int form()
	{ 	char k;
		int i=0;
		printf("В║веждане поле▓а▓а на док│мен▓а\n");
		do{
			i++;
			printf("\n Поле %d:",i);
			scanf("%15s",&mas[i-1].niz);
			printf("\nЖелае▓е ли ново поле Y/N:\n");
			k=getch();
			getchar();
		} while ((k == 'y')&&(i<10));
		return i;
	};
	void fwrite_func(FILE *fl,void * buf,int size_buf,int n)
	{
		fwrite(buf,size_buf,n,fl);
	};
	void zapis()
	{	FILE *f;
		printf("В║веде▓е име▓о на ┤айла:");
		scanf("%s",&ime_file);
		if ((f=fopen (ime_file,"wb"))==NULL)
		{ printf ("\n Н┐ма до▒▓║п");
		  exit(1);};
		fwrite_func(f,(void*)&mas,sizeof mas,1);
		fclose(f);
	};
	int tar_poz()
	{	int po;
		FILE *f;
		if ((f=fopen ("ompod","rb"))!=NULL)
			{fseek (f,0l,SEEK_END);
			 fread (&ob2,sizeof ob2,1,f);
			 po=ob2.yd.poz;
			 po++;}
			else
			 po=0;
		fclose(f);
		return po;
	};
	void sazd()
	{	FILE *f;
		int i,poz1;
		strcpy(ob1.yd.di.ime,ime_file);
		ob1.yd.br=j;
		for(i=0;i<10;i++);
			{ob1.ac[i].ptr=0;
			 ob1.ac[i].nom=0;
			};
		poz1=tar_poz();
		ob1.yd.poz=poz1;
		if (poz1 !=0)
			{ f=fopen ("ompod","ab");
			fwrite_func(f,(void *)& ob1,sizeof ob1,1);
			fclose(f);
			};
	};
	void sazd_omd()
	{	int i;
		omd_ob ss;
		FILE * f1;
		f1=fopen("OMD","wb");
			 ss.du1=0;
			 ss.du2=0;
			 ss.no1=0;
			 ss.no2=0;
			 ss.broi=0;
		fwrite_func(f1,(void *)&ss,sizeof ss,970);
		fclose (f1);
	};
	int hash_func(int l)
	{	int k,preo,suma,hash;
		k=0;
		suma=0;
		hash=0;
		do{
			preo=(int)(mas[l].niz[k]);
			if (preo==0) break;
			if (preo<0) preo=256-preo;
			preo=preo<<1;
			suma=suma+preo;
			k++;
		} while (k<15);
		if (suma>970) hash=suma % 971;
			else hash=suma;
		return hash;
	};
	void now_desc(int k)
	{	desc.du1=ob1.yd.poz;
		desc.du2=ob1.yd.poz;
		desc.no1=k;
		desc.no2=k;
		desc.broi=1;
	};
	void dobw_desc(int k)
	{	int st;
		f2=fopen("OMPOD","rb");
		fseek(f2,desc.du2,SEEK_SET);
		fread(&ob2,sizeof ob2,1,f2);
		fclose(f2);
		st=desc.du2;
		ob2.ac[desc.no2].ptr=ob1.yd.poz;
		desc.du2=ob1.yd.poz;
		ob2.ac[desc.no2].nom=k;
		desc.no2=k;
		desc.broi++;
		f2=fopen("OMPOD","w+b");
		fseek(f2,st,SEEK_SET);
		fwrite(&ob2,sizeof ob2,1,f2);
		fclose(f2);
	};
	void main()
	{
		char simbol;
		int m=0,hh;
		if((f1=fopen("OMD","rb"))==NULL)
		sazd_omd();
		fclose(f1);
		do{	clrscr();
		j=form();
		zapis();
		sazd();
		do{
			hh=hash_func(m);
			f1=fopen("OMD","rb");
			fseek(f1,hh,SEEK_SET);
			fread(&desc,sizeof desc,1,f1);
			fclose(f2);
			if (desc.broi==0) now_desc(m);
				else dobw_desc(m);
			f1=fopen("OMD","w+b");
			fseek(f1,hh,SEEK_SET);
			fwrite(&desc,sizeof desc,1,f1);
			fclose(f1);
			m++;
		   } while (m<j);
		   simbol=getch();
		   printf("\n За к░ай на▓и▒не▓е ESC");
		} while (simbol != 27);
	}

