/*                 ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
		   º    PROGRAMA DE LA AGENDA         º
		   ºÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄº
		   º Noms  :  - M¢nica Salas          º
		   º          - S¡lvia de la Orden    º
		   º Curs  :  95-96                   º
		   º Login :  EI00228                 º
		   º Data  :   9-1-96                 º
		   º Assignatura : F.INFORMATICA      º
		   ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ¼
*/
#include <dos.h>
#include <conio.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <graphics.h>


const unsigned char ENTRA=1,CONSU=2,BORRA=3,MODIF=4,LISTA=5,ORDENBR=6,
ORDEDNI=7,GENER=8,SALIR=0,NULO=255,MAX=101,FALSO=0,CIERTO=!FALSO,MAXLIN=20;

typedef char TNombre[30],TTelefo[20],TProfe[30];

typedef struct {
		TNombre       nom;
		unsigned long dni;
		TTelefo       tel;
		int          edad;
		TProfe        pro;
} TPersona;
typedef TPersona TListin[MAX];
typedef struct {
		TListin lista;
		int     cant;
} TBase;

unsigned char menu(void);
void entrada(TBase &ag);
void consultar(TBase ag);
void borrar(TBase &ag);
void modificar(TBase &ag);
void listar(TBase ag);
void ordnombre(TBase &ag);
void orddni(TBase &ag);
void generar(TBase &ag);
void inicializar(TBase &ag);
void ini_grafico(void);
void portada(void);

void main(void)
{
	TBase agenda;
	unsigned char op;

	clrscr();
	ini_grafico();
	portada();
	inicializar(agenda);
	while ((op = menu()) != SALIR)
	   switch (op) {
		   case ENTRA: entrada(agenda); break;
		   case CONSU: consultar(agenda); break;
		   case BORRA: borrar(agenda); break;
		   case MODIF: modificar(agenda); break;
		   case LISTA: listar(agenda); break;
		   case ORDENBR: ordnombre(agenda);break;
		   case ORDEDNI: orddni(agenda);break;
		   case GENER: generar(agenda); break;
			   }
	restorecrtmode();
}

void plantilla(unsigned char pos)
{
	setfillstyle(LINE_FILL,LIGHTBLUE);bar(0,0,700,700);
	setfillstyle(SOLID_FILL,BLACK);bar(25,120,600,400);
	_setcursortype(_NORMALCURSOR);settextstyle(7,0,1);
	setcolor(11);gotoxy(8,10); printf("Ficha n£mero  %d",pos);
	outtextxy(75,200,"Nombre   :                      ");
	outtextxy(410,200,"D.N.I.  :                      ");
	outtextxy(75,248,"Tel‚fono   :                      ");
	outtextxy(410,248,"Edad   :                      ");
	outtextxy(75,297,"Profesi¢n  :                      ");
}

void leernombre(TNombre n)
{
	fflush(stdin);gotoxy(26,14);gets(n);
}

void leerdni(unsigned long &d)
{
	fflush(stdin);gotoxy(64,14);scanf("%D",&d);
}

void leertelefono(TTelefo t)
{
	fflush(stdin);gotoxy(26,17);gets(t);
}

void leeredad(int &e)
{
	fflush(stdin);gotoxy(64,17);scanf("%D",&e);
}

void leerprofe(TProfe p)
{
	fflush(stdin);gotoxy(26,20);gets(p);
}

int buscar(TBase ag, TNombre n)
{
	int i;

	for(i=1;i<ag.cant;i++)
	{
		if(strcmp(ag.lista[i].nom,n)==0) return i;
	}return -1;
}

void mostrar(TPersona p)
{
	gotoxy(26,14); printf("%s",p.nom);gotoxy(64,14); printf("%ld",p.dni);
	gotoxy(26,17); printf("%s",p.tel);gotoxy(64,17); printf("%d",p.edad);
	gotoxy(26,20); printf("%s",p.pro);
}


void entrada(TBase &ag)
{
	clrscr();
	while (ag.cant < MAX)
	{
	   if (ag.cant==0) ag.cant++;
	   plantilla(ag.cant);
	   leernombre(ag.lista[ag.cant].nom);
	   if (strlen(ag.lista[ag.cant].nom) == 0) break;
	   ag.cant++;
	   leerdni(ag.lista[ag.cant].dni);leertelefono(ag.lista[ag.cant].tel);
	   leeredad(ag.lista[ag.cant].edad);leerprofe(ag.lista[ag.cant].pro);

	}
	if(ag.cant >= MAX)
	{
		_setcursortype(_NOCURSOR);clrscr();
		setfillstyle(LINE_FILL,LIGHTBLUE);bar(0,0,700,700);
		settextstyle(SANS_SERIF_FONT,0,1);setcolor(11);
		outtextxy(200,200,"La agenda est  llena");
		outtextxy(500,400,"Pulse una tecla para continuar");
		getch();
	}
}

void consultar(TBase ag)
{
	TNombre n;
	int pos;

	if (ag.cant==0)
	{
		clrscr();setcolor(11);
		setfillstyle(LINE_FILL,LIGHTBLUE);bar(0,0,700,700);
		settextstyle(SANS_SERIF_FONT,0,1);
		outtextxy(200,200,"OJO !! La agenda est  vac¡a");
		outtextxy(180,400,"Pulse una tecla para continuar...");
		getch();
	}
	else
	{
	 do
	  {
	   clrscr();plantilla(0);
	   leernombre(n);
	   if (strcmp(n,"")==0) break;
	   pos = buscar(ag,n);
	   if (pos<0)
	   {
		  clrscr();setcolor(11);
		  setfillstyle(LINE_FILL,LIGHTBLUE);bar(0,0,700,700);
		  settextstyle(SANS_SERIF_FONT,0,1);setcolor(11);
		  outtextxy(150,200,"La ficha no se encuentra en esta agenda");
		  outtextxy(180,400,"Pulse una tecla para continuar...");
	    }
	   else
	   {
		   plantilla(pos);mostrar(ag.lista[pos]);
	   }
	   getch();
	  }
	while(CIERTO);
	}
}

void borrar(TBase &ag)
{
   int pos,i;
   TNombre n;

   if (ag.cant==0)
	{
	   clrscr();setcolor(11);
	   setfillstyle(LINE_FILL,LIGHTBLUE);bar(0,0,700,700);
	   settextstyle(SANS_SERIF_FONT,0,1);
	   outtextxy(200,200,"OJO!! La agenda est  vac¡a");
	   outtextxy(180,400,"Pulse una tecla para continuar...");
	   getch();
	}
   else {
	do
		{
			plantilla(0);leernombre(n);
			if (strcmp(n,"")==0) break;
			pos=buscar(ag,n);
			if(pos==-1)
			{
			 clrscr();setcolor(11);
			 setfillstyle(LINE_FILL,LIGHTBLUE);bar(0,0,700,700);
			 settextstyle(SANS_SERIF_FONT,0,1);
			 outtextxy(200,200,"No se encuentra la ficha");
			 outtextxy(180,400,"Pulse una tecla para continuar...");
			 getch();
			}
		else
			{
			 plantilla(pos);mostrar(ag.lista[pos]);gotoxy(20,23);
			 printf("Desea borrar la ficha (s/n) ? ");
			 if(getch()=='s')
			  {
				for (i=pos ; i<ag.cant; i++)
				{
				 ag.lista[i]=ag.lista[i+1];
				}
				ag.cant--;
			   }
			  }
		}
	while(CIERTO);
	}
}

void modificar(TBase &ag)
{
   int pos;
   TNombre n;

   if (ag.cant==0)
   {
	   clrscr();setcolor(11);
	   setfillstyle(LINE_FILL,LIGHTBLUE);bar(0,0,700,700);
	   settextstyle(SANS_SERIF_FONT,0,1);
	   outtextxy(200,200,"OJO!! La agenda est  vac¡a");
	   outtextxy(180,400,"Pulse una tecla para continuar...");
	   getch();
   }
   else
	{
	  do
	    {
		plantilla(0);leernombre(n);
		if (strcmp(n,"")==0) break;
		pos=buscar(ag,n);
		if(pos==-1)
		 {
		  clrscr();setcolor(11);
		  setfillstyle(LINE_FILL,LIGHTBLUE);bar(0,0,700,700);
		  settextstyle(SANS_SERIF_FONT,0,1);
		  outtextxy(150,200,"La ficha no se encuentra en esta agenda");
		  outtextxy(180,400,"Pulse una tecla para continuar...");
		  getch();
		 }
		else
		 {
		  plantilla(pos);mostrar(ag.lista[pos]);gotoxy(20,23);
		  printf("Desea modificar esta ficha (s/n) ? ");
		  switch(getch())
		  {
		   case 's':
		   case 'S':
		   {
		      TPersona x;plantilla(0);leernombre(x.nom);
		      leerdni(x.dni);leertelefono(x.tel);leeredad(x.edad);
		      leerprofe(x.pro);
		      if(x.nom[1]==0) strcpy(x.nom+2,ag.lista[pos].nom+2);
		      if(x.dni ==0)x.dni=ag.lista[pos].dni;
		      if(x.tel[1]==0) strcpy(x.tel+2,ag.lista[pos].tel+2);
		      if(x.edad == 0)x.edad=ag.lista[pos].edad;
		      if(x.pro[1] == 0) strcpy(x.pro+2,ag.lista[pos].tel+2);
		      gotoxy(15,23); printf("Est  seguro de que quiere modificar la ficha (s/n)?");
		      switch(getch())
			{
			 case 's':
			 case 'S':
			 {
			 mostrar(x);ag.lista[pos] = x;
			 }
			 break;
			}
			break;
		   }
		  }
		 }
	    }
	    while(CIERTO);
	}
}

void listar(TBase ag)
{
	unsigned char i, x;

	clrscr();_setcursortype(_NOCURSOR);
	if (ag.cant == 0)
	{
	   setcolor(11);setfillstyle(LINE_FILL,LIGHTBLUE);
	   bar(0,0,800,800);settextstyle(SANS_SERIF_FONT,0,1);
	   outtextxy(200,200,"OJO !!! La agenda est  vac¡a");
	   outtextxy(180,400,"Pulse una tecla para continuar...");
	   getch();
	}
	else {

		x=0;clrscr();setfillstyle(SOLID_FILL,BLACK);
		bar(0,0,800,800);
		gotoxy(1,x);
		printf("N§     Nombre                    D.N.I.");
		printf("    Tel‚fono  Edad  Profesi¢n");
		x=3;
		for (i=1 ; i < ag.cant && i<MAX; i++)
		{
			gotoxy(1,x);printf("%d",i);
			gotoxy(8,x);printf("%s",ag.lista[i].nom);
			gotoxy(34,x);printf("%ld", ag.lista[i].dni);
			gotoxy(45,x);printf("%s", ag.lista[i].tel);
			gotoxy(54,x);printf("%d", ag.lista[i].edad);
			gotoxy(60,x);printf("%s",ag.lista[i].pro);
			x++;
			if (x==23)
			{
				printf("\n\nPulse una tecla para continuar...\n");
				getch();clrscr();
				setfillstyle(SOLID_FILL,BLACK);
				bar(0,0,700,700);
				printf("N§     Nombre                    D.N.I.");
				printf("    Tel‚fono  Edad  Profesi¢n");
				x=3;
			}
			putchar('\n');
		}
		printf("\n\nPulse una tecla para continuar \n");
		if (x != 0) getch();
	}
	_setcursortype(_NORMALCURSOR);
}

void ordnombre(TBase  &ag)
{
	unsigned int i,j;
	TPersona x;

	clrscr();_setcursortype(_NOCURSOR);
	if (ag.cant == 0)
	{
	   setcolor(11);setfillstyle(LINE_FILL,LIGHTBLUE);
	   bar(0,0,800,800);settextstyle(SANS_SERIF_FONT,0,1);
	   outtextxy(200,200,"OJO !!! La agenda est  vac¡a");
	   outtextxy(180,400,"Pulse una tecla para continuar...");
	   getch();
	}
	else
	{
	for(i=1; i < ag.cant; i++)
	{
		for(j=ag.cant-1; j > 1; j--)
		{
			if(stricmp(ag.lista[j-1].nom,ag.lista[j].nom)>0)
			{
				x=ag.lista[j-1];
				ag.lista[j-1]=ag.lista[j];
				ag.lista[j]=x;
			}
		}
	}
	}
}

void orddni(TBase &ag)
{
	int i,j;
	TPersona x;
	clrscr();_setcursortype(_NOCURSOR);
	if (ag.cant == 0)
	{
	   setcolor(11);setfillstyle(LINE_FILL,LIGHTBLUE);
	   bar(0,0,800,800);settextstyle(SANS_SERIF_FONT,0,1);
	   outtextxy(200,200,"OJO !!! La agenda est  vac¡a");
	   outtextxy(180,400,"Pulse una tecla para continuar...");
	   getch();
	}
	else
	{
	for(i=1; i<ag.cant; i++)
	{
		x=ag.lista[i];
		j=i-1;
		while(x.dni<ag.lista[j].dni && j>0)
			{
			  ag.lista[j+1]=ag.lista[j];
			  j--;
			}
		ag.lista[j+1]=x;
	}
	}
}

void generar(TBase &ag)
{
	clrscr();
	int i;
	for(i=1; ag.cant < MAX && i <= 11; ag.cant++, i++)
	{
		itoa(random(1000),ag.lista[ag.cant].nom,10);
		itoa(random(1000),ag.lista[ag.cant].tel,10);
		itoa(random(1000),ag.lista[ag.cant].pro,10);
		ag.lista[ag.cant].dni = random(1000);
		ag.lista[ag.cant].edad = random(1000);
	}
	if(ag.cant >= MAX)
	{
		_setcursortype(_NOCURSOR);
		clrscr();
		setfillstyle(LINE_FILL,LIGHTBLUE);
		bar(0,0,700,700);
		settextstyle(SANS_SERIF_FONT,0,1);
		setcolor(11);
		outtextxy(200,200,"La agenda est  llena");
		outtextxy(180,400,"Pulse una tecla para continuar...");
		getch();
		_setcursortype(_NORMALCURSOR);
	}
}

void inicializar(TBase &ag)
{
	ag.cant = 0;
}

void ini_grafico(void) // Permite operar en modo gr…fico.
{
	int gdriver = DETECT, gmode, errorcode;

	initgraph(&gdriver, &gmode,"c:\\borlandc\\borlandc\\bgi");
	errorcode = graphresult();
	if (errorcode != grOk)
	{
		printf("Error de gr…fics: %s\n", grapherrormsg(errorcode));
		printf("Prem una tecla per acabar... ");
		getch();exit(1);
	}
}


unsigned char menu(void)
{
	int x;
	unsigned char op;

	clearviewport();setfillstyle(1,4);
	floodfill(1,1,1);setcolor(14);
	// Tri ngulos superior e inferior.
	for (x=0;x<640;x+=3)
	{
		line(320,240,x,0);
		line(320,240,x,480);
	}
	// Tri ngulos laterales.
	for (x=0;x<480;x+=3)
	{
		line(320,240,0,x);
		line(320,240,640,x);
	}
	settextstyle(2,HORIZ_DIR,6);setcolor(3);
	outtextxy(255,157,"MENU PRINCIPAL");
	outtextxy(240,165,"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ");
	outtextxy(230,180,"1.- Altas");
	outtextxy(230,195,"2.- Consultas");
	outtextxy(230,210,"3.- Bajas");
	outtextxy(230,225,"4.- Modificaciones");
	outtextxy(230,240,"5.- Listado de fichas");
	outtextxy(230,255,"6.- Ord. por nombre");
	outtextxy(230,270,"7.- Ord. por D.N.I.");
	outtextxy(230,285,"8.- Generar 10 ");
	outtextxy(230,300,"0.- Salir");
	do {
	   gotoxy(43,24);
	   switch (getch()) {
		  case '0': op = SALIR; break;
		  case '1': op = ENTRA; break;
		  case '2': op = CONSU; break;
		  case '3': op = BORRA; break;
		  case '4': op = MODIF; break;
		  case '5': op = LISTA; break;
		  case '6': op = ORDENBR; break;
		  case '7': op = ORDEDNI; break;
		  case '8': op = GENER; break;
		  default : op = NULO;  break;
	   }
	}while (op == NULO);
	return op;
}

void portada(void)
{
	int x,color;

	// Estrellas aleatorias en pantalla.
	randomize();
	for (x=0;x<1500;x++)
	{
		color=random(15);
		while (color==0) color=random(15);
		putpixel(random(640),random(480),color);
		delay(3);
	}
	// Informaci¢n del programa.
	setcolor(14);settextstyle(4,HORIZ_DIR,6);
	outtextxy(10,150,"AGENDA PERSONAL");
	settextstyle(2,HORIZ_DIR,5);outtextxy(560,190,"Ver. 1.0");
	settextstyle(7,HORIZ_DIR,5);setcolor(12);
	outtextxy(200,240,"Autores : ");setcolor(10);
	settextstyle(7,HORIZ_DIR,4);
	outtextxy(200,290,"M•nica Salas");
	outtextxy(200,330,"S¡lvia de la Orden");
	setcolor(15);getch();
}
