#include <stdio.h>
#include <conio.h>
#include <process.h>
#include <ctype.h>
#include <dir.h>
#include <io.h>

struct client
{
 int codi,edat;
 char nom[30],cog1[30],cog2[30];
 char sexe;
};

int comptar_clients()
{
 FILE *f;
 struct client r;
 int x,comp;
 comp=0;
 f=fopen("CLIENTS.DAT","rb");
 if (f==NULL)
 {
  f=fopen("CLIENTS.DAT","wb");
  comp=0;
 }
 else
 {
  x=fread(&r,sizeof(struct client),1,f);
  while (!feof(f))
  {
	comp++;
	x=fread(&r,sizeof(struct client),1,f);
  }
 }
 fclose(f);
 return comp;
}

void entradades(struct client *r)
{
 clrscr();
 printf("\nCodi.........: %d ",r->codi);
 fflush(stdin);
 printf("\nNom..........: ");scanf("%s",r->nom);fflush(stdin);
 printf("\n1er Cognom...: ");scanf("%s",r->cog1);fflush(stdin);
 printf("\n2on Cognom...: ");scanf("%s",r->cog2);fflush(stdin);
 printf("\nEdat.........: ");scanf("%d",&(r->edat));fflush(stdin);
 do
  {
	printf("\nSexe.........: ");scanf("%c",&(r->sexe));fflush(stdin);
  }
 while (r->sexe != 'H' && r->sexe != 'D');
 printf("\n\nPremi una tecla");getch();
}

int recerca_client(int codcli)
{
 FILE *f;
 int x, trobat;
 struct client r;
 f=fopen("CLIENTS.DAT","rb");
 do
 x=fread(&r,sizeof(struct client),1,f);
 while (!feof(f) && r.codi != codcli);
 if feof(f) trobat=0;
 else trobat=1;
 fclose(f);
 return trobat;
}


void insord1(struct client r,FILE *f2,FILE *f3)
{
 struct client r2,r3;
 int x2,x3;
 x2=fread(&r2,sizeof(struct client),1,f2);
 while (!feof(f2) && strcmp(r2.cog1,r.cog1) < 0)
 {
  x3=fwrite(&r2,sizeof(struct client),1,f3);
  x2=fread(&r2,sizeof(struct client),1,f2);
 }
 if (feof(f2))x3=fwrite(&r,sizeof(struct client),1,f3);
 else
 {
	while (!feof(f2) && strcmp(r2.cog1,r.cog1)==0 && strcmp(r2.cog2,r.cog2) < 0)
	{
	 x3=fwrite(&r2,sizeof(struct client),1,f3);
	 x2=fread(&r2,sizeof(struct client),1,f2);
	}
	x3=fwrite(&r,sizeof(struct client),1,f3);
	while (!feof(f2))
	{
	 x3=fwrite(&r2,sizeof(struct client),1,f3);
	 x2=fread(&r2,sizeof(struct client),1,f2);
	}
 }
}

void insord2(struct client r,FILE *f2,FILE *f3)
{
 struct client r2,r3;
 int x2,x3;
 x2=fread(&r2,sizeof(struct client),1,f2);
 while (!feof(f2) && r2.edat <= r.edat)
 {
  x3=fwrite(&r2,sizeof(struct client),1,f3);
  x2=fread(&r2,sizeof(struct client),1,f2);
 }
 x3=fwrite(&r,sizeof(struct client),1,f3);
 while (!feof(f2))
 {
  x3=fwrite(&r2,sizeof(struct client),1,f3);
  x2=fread(&r2,sizeof(struct client),1,f2);
 }
}

void main()
{
 FILE *f,*f1,*f2;
 struct client r,r1,r2;
 char siono;
 int comptador,comptador2,op,op2,x,x1,x2,codcli;
 comptador=comptar_clients();
 if (comptador==-1)
  {
	 printf("Error greu en els arxius.");
	 getch();
	 exit(1);
  }
 do
 {
	clrscr();
	printf("\n *** MANTENIMIENT DE CLIENTS *** \n\n");
	printf("\n 1 - Introduccio d'un client. ");
	printf("\n 2 - Baixa d'un client.");
	printf("\n 3 - Modificacio d'un client.");
	printf("\n 4 - Llistar els clients.");
	printf("\n 5 - Particio homes/dones.");
	printf("\n 6 - Importacio nous clients.");
	printf("\n 7 - Sortir del programa.");
	printf("\n\nIntroduiu opcio: ");
	do scanf("%d",&op); while (op < 1 || op > 7);
	switch (op)
	{
	 case 1:
		f=fopen("CLIENTS.DAT","ab");
		if (f==NULL) f=fopen("CLIENTS.DAT","wb");
		r.codi = comptador+1;
		entradades(&r);
		x=fwrite(&r,sizeof(struct client),1,f);
		if (x > 0)
			comptador++;
		fclose(f);
		break;
	 case 2:
		codcli=0;
		printf("Client que vol donar de baixa: ");scanf("%d",&codcli);fflush(stdin);
		printf("\n");
		if (recerca_client(codcli)==0)
		{
		 printf("El client no existeix.");
		 getch();
		}
		else
		{
		 f1=fopen("CLIENTS.DAT","rb");
		 f2=fopen("CLIENT2.DAT","wb");
		 x1=fread(&r1,sizeof(struct client),1,f1);
		 while (!feof(f1))
		 {
		  if (r1.codi != codcli) x2=fwrite(&r1,sizeof(struct client),1,f2);
		  x1=fread(&r1,sizeof(struct client),1,f1);
		 }
		 printf("\n\nS'ha donat la baixa.");
		 getch();
		 fclose(f1);fclose(f2);
		 unlink("CLIENTS.DAT");
       rename("CLIENT2.DAT","CLIENTS.DAT");
		}

		break;
	 case 3:
		printf("\nClient que vol modificar: ");scanf("%d",&codcli);fflush(stdin);
		printf("\n");
		if (recerca_client(codcli)==0)
		{
		 printf("El client no existeix");getch();
		}
		else
		{
		 f=fopen("CLIENTS.DAT","r+b");
		 x=fread(&r,sizeof(struct client),1,f);
		 while (!feof(f) && r.codi != codcli)
			 x=fread(&r,sizeof(struct client),1,f);
		 if (!feof(f))
		  {
			entradades(&r);
			fseek(f,-(long)sizeof(struct client),SEEK_CUR);
			x=fwrite(&r,sizeof(struct client),1,f);
		  }
		  fclose(f);
		}
		getch();break;
	 case 4:
		printf("\n\n 1 - Per cognoms  2 - Per edat   3 - Sortir ");
		do scanf("%d",&op2); while (op2 < 1 || op2 > 3);
		if (op2!=3)
		{
		 f=fopen("CLIENTS.DAT","rb");
		 x=fread(&r,sizeof(struct client),1,f);
		 while (!feof(f))
		 {
			f1=fopen("CLIENTS.ORD","rb");
			if (f1==NULL) f1=fopen("CLIENTS.ORD","wb");
			f2=fopen("CLIENTS.TEM","wb");
			if (op2==1) insord1(r,f1,f2);
			else insord2(r,f1,f2);
			fclose(f1);fclose(f2);
			unlink("CLIENTS.ORD");
			rename("CLIENTS.TEM","CLIENTS.ORD");
			x=fread(&r,sizeof(struct client),1,f);
		  }
		 fclose(f);
		 clrscr();
		 printf("\n\n LLISTAT DE CLIENTS\n\n");
		 f=fopen("CLIENTS.ORD","rb");
		 x=fread(&r,sizeof(struct client),1,f);
		 while (!feof(f))
		 {
		  printf("\n%s %s %s %d %c",r.nom,r.cog1,r.cog2,r.edat,r.sexe);
		  x=fread(&r,sizeof(struct client),1,f);
		 }
		 fclose(f);
		 unlink("CLIENTS.ORD");
		 printf("\n\nLlistat finalitzat. Premi una tecla.");
		 getch();
		}
		break;

	 case 5:
		f=fopen("CLIENTS.DAT","rb");
		f1=fopen("HOMES.DAT","wb");
		f2=fopen("DONES.DAT","wb");
		x=fread(&r,sizeof(struct client),1,f);
		while (!feof(f))
		{
		 if (r.sexe=='H') x=fwrite(&r,sizeof(struct client),1,f1);
		 else x=fwrite(&r,sizeof(struct client),1,f2);
		 x=fread(&r,sizeof(struct client),1,f);
		}
		fclose(f);fclose(f1);fclose(f2);
		printf("S'ha realitzat la particio. Vols veure  els arxius ? ");
		do scanf("%c",&siono); while (siono != 'S' && siono!='N');
		if (siono=='S')
		 {
		  clrscr();
		  printf("\n\n LLISTAT D'HOMES\n\n");
		  f=fopen("HOMES.DAT","rb");
		  x=fread(&r,sizeof(struct client),1,f);
		  while (!feof(f))
		  {
			printf("\n%s %s %s %d ",r.nom,r.cog1,r.cog2,r.edat);
			x=fread(&r,sizeof(struct client),1,f);
		  }
		  fclose(f);
		  printf("\n\nLlistat finalitzat. Premi una tecla.");
		  getch();
		  clrscr();
		  printf("\n\n LLISTAT DE DONES\n\n");
		  f=fopen("DONES.DAT","rb");
		  x=fread(&r,sizeof(struct client),1,f);
		  while (!feof(f))
		  {
			printf("\n%s %s %s %d ",r.nom,r.cog1,r.cog2,r.edat);
			x=fread(&r,sizeof(struct client),1,f);
		  }
		  fclose(f);
		  printf("\n\nLlistat finalitzat. Premi una tecla.");
		  getch();
		 }
		break;

	 case 6:
		f=fopen("CLIENTS.DAT","rb");
		f1=fopen("NOUS.DAT","rb");
		f2=fopen("TOTS.DAT","wb");
		x=fread(&r,sizeof(struct client),1,f);
		x1=fread(&r1,sizeof(struct client),1,f1);
		while (!feof(f) && !feof(f1))
		 {
		  if (strcmp(r.cog1,r1.cog1) < 0)
		  {
			r2=r;
			x=fread(&r,sizeof(struct client),1,f);
		  }
		  else if(strcmp(r.cog1,r1.cog1)>0)
		  {
			r2=r1;
			x1=fread(&r1,sizeof(struct client),1,f1);
		  }
		  else if(strcmp(r.cog1,r1.cog1)==0)
		  {
			if (strcmp(r.cog2,r1.cog2)< 1)
			  {
				 r2=r;
				 x=fread(&r,sizeof(struct client),1,f);
			  }
			else
			  {
				 r2=r1;
				 x1=fread(&r1,sizeof(struct client),1,f1);
			  }
		  }
		  x2=fwrite(&r2,sizeof(struct client),1,f2);
		 }
		while (!feof(f))
		 {
		  x2=fwrite(&r,sizeof(struct client),1,f2);
		  x=fread(&r,sizeof(struct client),1,f);
		 }
		while (!feof(f1))
		 {
		  x2=fwrite(&r1,sizeof(struct client),1,f2);
		  x1=fread(&r1,sizeof(struct client),1,f1);
		 }
		 fclose(f);fclose(f1);fclose(f2);
		 f=fopen("TOTS.DAT","r+b");
		 x=fread(&r,sizeof(struct client),1,f);
		 comptador2=1;
		 while (!feof(f))
		 {
		  r.codi=comptador2;
		  comptador2++;
		  fseek(f,-(long)sizeof(struct client),SEEK_CUR);
		  x=fwrite(&r,sizeof(struct client),1,f);
		  x=fread(&r,sizeof(struct client),1,f);
		 }
		 fclose(f);
		 unlink("CLIENTS.DAT");
       rename("TOTS.DAT","CLIENTS.DAT");
		 break;
	}
 }
 while (op!=7);
}


