/* BUSQUEDA (HASH) DESPUES DE LEER UN ARCHIVO
Objetivo:
Lee un archivo de texto y lo almacena
en una tabla hash para efectuar bUsquedas
J. Rafael R. Ochoa
*/
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#define valmax 300
struct cadena
{char palabra[16];
int frec;
struct cadena *psig;
};
typedef struct cadena *LISTA;
struct
{int totp;
LISTA pc;
}tabla[valmax];
void agrega_palabra(char *, int);
LISTA search(LISTA, char *);
LISTA getnode();
void insfin(LISTA, char *);
void pausa(void);
void libera_tabla(void);
void free_cad(LISTA);
int gen_hash(char *);
void bushash(char []);
void main()
{
int i, ip, vp=0, cp=0;
char letra, palabra[16], bfin[16], patron[16];
FILE *fpt;
clrscr();
for (i=0; i < valmax; i++)
{tabla[i].totp = 0;
tabla[i].pc = NULL;
}
if ((fpt = fopen("diccesp.txt","r")) == NULL)
pausa();
}
else
do
{letra = getc(fpt);
if (isalpha(letra))
{ip=0;
palabra[ip]=letra;
while (isalpha(letra = getc(fpt)) && ip<15)
{ip++;palabra[ip]=letra;}
if (ip < 15)
{ip++;palabra[ip]='\0';}
else palabra[ip]='\0';
vp=gen_hash(palabra);
agrega_palabra(palabra, vp);
}
cp++;
}
while (feof(fpt) == 0 && cp < 1500);
fclose(fpt);
strcpy(bfin,"//");
while (strcmp(patron,bfin)!=0)
{clrscr();
printf("\nBUSQUEDA POR DISPERSION...");
printf("\n\nPalabra a buscar? (Salir: //)");
scanf("%s",patron);
if (strcmp(patron,bfin)!=0)
bushash(patron);
}
libera_tabla();
clrscr();
}
void bushash(char match[])
{ int pos;
LISTA p=NULL;
pos=gen_hash(match);
if (tabla[pos].totp != 0)
p=search(tabla[pos].pc, match);
if (p==NULL)
printf("\n ...No encontrada");
else
printf("\n ...Encontrada en la celda: %d",pos);
pausa();
}
int gen_hash(char *p)
{
int longp, ccod, valp=0, ptab;
longp = strlen(p);
for (ccod=0; ccod <longp ; ccod++)
valp = valp + toascii(p[ccod]);
ptab = fmod(valp,300);
return ptab;
}
void agrega_palabra(char *p, int ptab)
{
LISTA ppal;
if (tabla[ptab].totp == 0)
{tabla[ptab].pc=getnode();
strcpy(tabla[ptab].pc->palabra,p);
tabla[ptab].pc->frec = 1;
++tabla[ptab].totp;
tabla[ptab].pc->psig=NULL;
}
else
{ppal=search(tabla[ptab].pc,p);
if (ppal == NULL)
{insfin(tabla[ptab].pc,p);
ppal=search(tabla[ptab].pc,p);
ppal->frec = 1;
++tabla[ptab].totp;
}
else ++ppal->frec;
}
}
LISTA search(LISTA primera, char* plb)
{
LISTA l;
for (l = primera; l != NULL; l = l->psig)
if (strcmp(l->palabra, plb) == 0)
return (l);
return (NULL);
}
void insfin(LISTA primera, char *plb)
{
LISTA m,l;
l=getnode();
strcpy(l->palabra,plb);
l->psig = NULL;
if (primera == NULL)
{
printf("Error\n");
exit(1);
}
else
{for (m = primera; m->psig != NULL; m = m->psig);
m->psig = l;
}
}
LISTA getnode()
{
LISTA l;
l = (LISTA) malloc(sizeof(struct cadena));
if (l==NULL)
{printf("\nERROR: !memoria agotada!!");
exit(1);}
return l;
}
void libera_tabla()
{
int i;
for (i=0;i<valmax;i++)
if (tabla[i].totp != 0)
free_cad(tabla[i].pc);
}
void free_cad(LISTA l)
{
if (l==NULL)
return;
else
{
free_cad(l->psig);
free(l);
}
}
void pausa()
{
gotoxy(40,23);
printf("oprima una tecla: ");
getche();
}