// programa desarrollado por: // avenda¤o , diaz vega, neyra roque, campos vasquez #include // para printf y manejo de archivos #include // para colores #include // para toupper #include // para strcmp #include // para exit struct DIREC { char nombre[35]; char tele[20]; } ; void lee_dir(); // lee del archivo los datos (al comienzo) void guarda_dir(); // guardar en el archivo los datos (al terminar) void imprime(); // lista todos los datos void nuevo(); // ingresa un nuevo dato y lo adiciona al direct void consulta(); // buscar en el directorio un nombre int busca_clave(char nombre[]); // busca en el directorio y retorna la posicion struct DIREC dir[200]; // arreglo con los datos del directorio int tdir; // total de datos en el directorio void main() { char opcion; lee_dir(); do { // textattr(0x17); textattr( (BLUE << 4) + WHITE ); // fondo (AZUL) y letra (BLANCA) clrscr(); cprintf(" DIRECTORIO TELEFONICO"); printf("\n\n"); textcolor(YELLOW); cprintf(" N"); printf("uevo"); cprintf(" L"); printf("ista"); cprintf(" C"); printf("onsulta"); cprintf(" S"); printf("alir"); gotoxy(1,25); printf(" *** PRESIONE LA LETRA RESALTADA PARA ESCOGER LA OPCION ***"); gotoxy(1,4); opcion= getch(); opcion = toupper( opcion ); // convierte a mayuscula la letra presionada switch(opcion) { case 'N': nuevo(); break; case 'L': imprime(); break; case 'C': consulta(); break; } // switch } // while while ( opcion != 'S' ); guarda_dir(); textattr( (BLACK << 4) + WHITE ); clrscr(); } /*********************************************************************/ void nuevo() { struct DIREC dato; printf("\n\n"); printf("Nombre : "); gets(dato.nombre); strupr(dato.nombre); // convierte a mayuscula el nombre ingresado if ( dato.nombre[0] == 0 ) return; if ( busca_clave(dato.nombre) == 0 ) { // no est en el directorio ? printf("Tel‚fono : "); gets(dato.tele); dir[tdir] = dato; tdir++; } else { printf("\n\nYa existen esos datos!!!"); getch(); } } /*********************************************************************/ void imprime() { int c; struct DIREC dato; for ( c = 0; c < tdir; c++ ) { if ( c % 5 == 0 ) { if ( c != 0 ) getch(); textattr( (BLUE << 4) + WHITE ); clrscr(); textcolor(YELLOW); cprintf("NOMBRE TELEFONO"); } dato = dir[c]; if ( (c%2) != 0 ) textattr( (GREEN << 4) + YELLOW ); else textattr( (LIGHTGRAY << 4) + YELLOW ); printf("\n"); cprintf("%2d)%-35s%-20s",c+1,dato.nombre,dato.tele); } getch(); } /*********************************************************************/ int busca_clave(char buscado[]) { struct DIREC r; int c; for( c = 0; c < tdir; c++ ) { r = dir[c]; if( strcmp(r.nombre,buscado) == 0 ) return c+1; } return 0; } /*********************************************************************/ void consulta() { int c; struct DIREC dato; printf("\n"); printf("Nombre a buscar : "); gets(dato.nombre); strupr(dato.nombre); // convierte a mayuscula el nombre ingresado if ( dato.nombre[0] == 0 ) //si no ingreso nada sale de la funcion return; c = busca_clave(dato.nombre); // devuelve la posicion que ocupa una persona if ( c != 0 ) // est el nombre en el directorio ? { dato = dir[c-1]; printf("\n\n\n"); textcolor(LIGHTGRAY); cprintf("NOMBRE TELEFONO\n"); printf("\n"); textattr( (GREEN << 4) + YELLOW ); cprintf("%2d)%-35s%-20s",c+1,dato.nombre,dato.tele); getch(); } else { printf("\n\nLa informaci˘n solicitada no existe ..."); getch(); } } /*********************************************************************/ void guarda_dir() { struct DIREC dato; FILE * arch; int c; arch = fopen("datos.dat","wb"); if ( arch == NULL ) { printf("No se puede guardar el directorio en el disco\n"); return; } for ( c = 0; c < tdir; c++ ) { dato = dir[c]; fwrite( &dato, sizeof(dato), 1, arch ); } fclose(arch); } /*********************************************************************/ // Apertura de Archivo para Lectura void lee_dir() { FILE * arch; struct DIREC dato; tdir = 0; arch = fopen("datos.dat","rb"); if ( arch == NULL ) return; while( fread( &dato, sizeof(dato), 1, arch ) > 0 ) dir[tdir++] = dato; fclose(arch); }