0% encontró este documento útil (0 votos)
98 vistas7 páginas

Archivos Índices

Este documento describe un programa que mantiene registros de datos con índices para búsqueda rápida. Los registros incluyen nombres, apellidos y números de teléfono. El programa permite insertar, buscar y listar registros. Usa archivos para almacenar los registros y los índices, y funciones como quicksort para ordenar los índices por número de teléfono.

Cargado por

neriovf
Derechos de autor
© Attribution Non-Commercial (BY-NC)
Nos tomamos en serio los derechos de los contenidos. Si sospechas que se trata de tu contenido, reclámalo aquí.
Formatos disponibles
Descarga como DOC, PDF, TXT o lee en línea desde Scribd
0% encontró este documento útil (0 votos)
98 vistas7 páginas

Archivos Índices

Este documento describe un programa que mantiene registros de datos con índices para búsqueda rápida. Los registros incluyen nombres, apellidos y números de teléfono. El programa permite insertar, buscar y listar registros. Usa archivos para almacenar los registros y los índices, y funciones como quicksort para ordenar los índices por número de teléfono.

Cargado por

neriovf
Derechos de autor
© Attribution Non-Commercial (BY-NC)
Nos tomamos en serio los derechos de los contenidos. Si sospechas que se trata de tu contenido, reclámalo aquí.
Formatos disponibles
Descarga como DOC, PDF, TXT o lee en línea desde Scribd

// [Link]: Ejemplo de ficheros de acceso aleatorio con ndices. #include <stdio.h> #include <stdlib.

h> struct char char char char }; stRegistro { valido; / / Campo que indica si el registro es valido nombre[20]; apellido[20]; telefono[10];

struct stIndice { char telefono[10]; long indice; }; int Menu(); void Capturar(stRegistro &reg); void EliminarRetornoLinea(char *cad); void Leer(FILE *fa, stRegistro &reg, char *telefono); void Insertar(FILE *fa, stRegistro &reg); void Mostrar(stRegistro &reg); void ListarPorTelefonos(FILE *fa); void ListarNatural(FILE *fa); void ReconstruirIndices(FILE *fa); void Intercambia(FILE *fa, long iz, long de); char *LeeCampo(FILE *fa, long n, char *buf); void QuickSort(FILE *fa, long inicio, long final); int main() { stRegistro reg; FILE *fa; int opcion; char telefono[10]; fa = fopen("[Link]", "r+b"); if (!fa) fa = fopen("[Link]", "w+b"); do { opcion = Menu();

switch (opcion) { case '1': // Insertar registro Capturar(reg); Insertar(fa, reg); break; case '2': // Buscar registro system("cls"); printf("Buscar registro: "); do { fgets(telefono, 10, stdin); EliminarRetornoLinea(telefono); } while(strlen(telefono) < 1); Leer(fa, reg, telefono); Mostrar(reg); break; case '3': // Indicar archivo system("cls"); printf("Indicando archive : "); ReconstruirIndices(fa); break; case '4': // Mostrar todo por orden de telfonos ListarPorTelefonos(fa); break; case '5': // Mostrar todo por orden natural ListarNatural(fa); break; } } while(opcion != '0'); fclose(fa); return 0; } Int Menu() { char resp[20];

do { system("cls"); printf("MENU PRINCIPAL\n"); printf("--------------\n\n"); printf("1- Insertar registro\n"); printf("2- Buscar registro\n"); printf("3- Reindicar archivo\n"); printf("4- Listar por orden de telfonos\n"); printf("5- Listar por orden natural\n"); printf("0- Salir\n"); fgets(resp, 20, stdin); } while (resp[0] < '0' && resp[0] > '5'); return resp[0]; } void Capturar(stRegistro &reg) { int i; char numero[6]; system("cls"); printf("Leer registro:\n\n"); [Link] = 'S'; printf("Nombre : "); fgets([Link], 34, stdin); EliminarRetornoLinea([Link]); printf("Primer apellido : "); fgets([Link][0], 34, stdin); EliminarRetornoLinea([Link][0]); printf("Segundo apellido : "); fgets([Link][1], 34, stdin); EliminarRetornoLinea([Link][1]); printf("Telfono : "); fgets([Link], 10, stdin); EliminarRetornoLinea([Link]); } void EliminarRetornoLinea(char *cad) { int i; for (i = strlen(cad) - 1; i >= 0 && cad[i] < ' '; i--) cad[i] = 0; }

void Mostrar(stRegistro &reg) { int i; if ([Link] == 'S') { printf("Nombre: %s %s %s\n", [Link], [Link][0], [Link][1]); printf("Nmero de telfono: %s\n", [Link]); } system("pause"); } void Leer(FILE *fa, stRegistro &reg, char *telefono) { FILE *fi; stIndice ind; long inf, sup, n, nRegs; fi = fopen("[Link]", "rb"); fseek(fi, 0, SEEK_END); nRegs = ftell(fi)/sizeof(stIndice); inf = 0; sup = nRegs - 1; do { n = inf + (sup - inf) / 2; fseek(fi, n * sizeof(stIndice), SEEK_SET); fread(&ind, sizeof(stIndice), 1, fi); if (strcmp(ind. telefono, telefono) < 0) inf = n + 1; else sup = n - 1; } while (inf <= sup && strcmp(ind. telefono, telefono)); If (!strcmp(ind. telefono, telefono)) { fseek(fa, [Link] * sizeof(stRegistro), SEEK_SET); fread(&reg, sizeof(stRegistro), 1, fa); } else { [Link] = 'N'; printf("Registro no encontrado\n"); } fclose(fi); }

void Insertar(FILE *fa, stRegistro & reg) { fseek(fa, 0, SEEK_END); fwrite(&reg, sizeof(stRegistro), 1, fa); ReconstruirIndices(fa); } void ListarPorTelefonos(FILE *fa) { FILE *fi; stIndice ind; stRegistro reg; system("cls"); fi = fopen("[Link]", "rb"); while(fread(&ind, sizeof(stIndice), 1, fi)) { fseek(fa, [Link] * sizeof(stRegistro), SEEK_SET); fread(&reg, sizeof(stRegistro), 1, fa); printf("%s %s %s %s\n", [Link], [Link][0], [Link][1], [Link]); } fclose(fi); system("pause"); } void ListarNatural(FILE *fa) { stRegistro reg; rewind(fa); system("cls"); while (fread(&reg, sizeof(stRegistro), 1, fa)) printf("%s %s %s %s\n", [Link], [Link][0], [Link][1], [Link]); system("pause"); } void ReconstruirIndices(FILE *fa) { long n = 0; FILE *fi; stRegistro reg; stIndice ind; fi = fopen("[Link]", "w+b");

rewind(fa); while (fread(&reg, sizeof(stRegistro), 1, fa)) { strcpy([Link], [Link]); [Link] = n++; fwrite(&ind, sizeof(stIndice), 1, fi); } QuickSort(fi, 0, n-1); fclose(fi); } void QuickSort(FILE *fi, long inicio, long final) { long iz, de; char mitad[10]; static char cad[10]; iz = inicio; de = final; strcpy(mitad, LeeCampo(fi, (iz + de) / 2, cad)); do { while (strcmp(LeeCampo(fi, iz, cad), mitad) < 0 && iz < final) iz++; while(strcmp(mitad, LeeCampo(fi, de, cad)) < 0 && de > inicio) de--; if (iz < de) Intercambia(fi, iz, de); if (iz <= de) { iz++; de--; } } while (iz <= de); If (inicio < de) QuickSort(fi, inicio, de); If (iz < final) QuickSort(fi, iz, final); } char *LeeCampo(FILE *fi, long n, char *buf) { stIndice ind; fseek(fi, n * sizeof(stIndice), SEEK_SET); fread(&ind, sizeof(stIndice), 1, fi); strcpy(buf, [Link]); return buf; }

void Intercambia(FILE *fi, long iz, long de) { stIndice reg1, reg2; fseek(fi, iz * sizeof(stIndice), SEEK_SET); fread(&reg1, sizeof(stIndice), 1, fi); fseek(fi, de * sizeof(stIndice), SEEK_SET); fread(&reg2, sizeof(stIndice), 1, fi); fseek(fi, iz * sizeof(stIndice), SEEK_SET); fwrite(&reg2, sizeof(stIndice), 1, fi); fseek(fi, de * sizeof(stIndice), SEEK_SET); fwrite(&reg1, sizeof(stIndice), 1, fi); }

También podría gustarte