コード例 #1
0
int main()
{
    Hash* h = alocar_hash(11);
    int i,j;


        for(i=0;i<100;i++){
            inserir_hash(h,rand()/(float)RAND_MAX*100+0.5);
        }

        imprimir_hash(h);

    return 0;
}
コード例 #2
0
ファイル: pegaTitle.c プロジェクト: CaioDaoud/BMW-CS
void parse_Hash(FILE *arquivo, FILE *arq) {

     unsigned char texto[BLOCO_TAMANHO],palavra[NUMERO];
     long tamanho_arquivo;
     int i,j,tamanho_texto,busca_char_nulo=0;
     int tamanho_hash, tabela_letras[256];
	int cont = 0;


     //Calculando o numero de palavras existentes no arquivo
     //Obs: No. de Palavras = Tamanho da tabela HASH
     fseek(arquivo,0,SEEK_END);
     tamanho_arquivo = ftell(arquivo);
     tamanho_hash = (int) pow((float)tamanho_arquivo,0.7);
     tamanho_hash = tamanho_hash * 4;
     
     //Criando a Tabela Hash
     TipoNo *hash[tamanho_hash];
     for (i=0;i<tamanho_hash;i++) {
          hash[i] = NULL;
     }
     
     //Gerando a Tabela de valores de cada letra em cada posição da palavra
     GeraTabela();

     
     //Buscando palavras do texto para inserir no Hash
     fseek(arquivo,0,SEEK_SET);
     
     //Criando uma tabela de Letras
     for (i=0;i<256;i++) {
         tabela_letras[i] = 0;
     }
     for (i=97;i<123;i++) {
         tabela_letras[i] = 1;
     }

     //Buscando palavras do texto para inserir no Hash
     fseek(arquivo,0,SEEK_SET);
	
     
     //Lendo palavras de um arquivo... O algoritmo lê em memória parte por parte do arquivo, utilizando blocos...
	while (!feof(arquivo)) {
		memset(texto,'\0', BLOCO_TAMANHO);
		tamanho_texto = fread(texto,sizeof(char),BLOCO_TAMANHO,arquivo);
		busca_char_nulo = 0;	

          //Quando, ao ler o BLOCO_TAMANHO, chegar no fim do arquivo, não precisa utilizar todo o vetor... então, segue:
          if (feof(arquivo)) {
               break;
          }

		//Colocar as palavras em letras minusculas!
		for (i=0;i<strlen((const char*)texto);i++){
			texto[i]=tolower(texto[i]);
		}

		//Para não pegar pedaços de palavras e/ou palavra incompletas...	
		while (( tabela_letras [ texto [ tamanho_texto - busca_char_nulo -1]])) {
			busca_char_nulo++;
		}

		texto[tamanho_texto - busca_char_nulo] = '\0';
		fseek(arquivo,-(busca_char_nulo * sizeof(char)), SEEK_CUR);

		tamanho_texto = tamanho_texto - busca_char_nulo;
		i=0;

		while (i< (tamanho_texto)) {
			memset(palavra,'\0',NUMERO);
              	j=0;
              	while ((tabela_letras[texto[i]]) && (i<tamanho_texto)) {
				palavra[j] = texto[i];
                   	j++;
                   	i++;     
              	}
			i++;
      		if (strlen((const char*)palavra)>1) { //Palavra com um caracter não entra no hash
      			cont++;
				Inserir_Hash(&(hash[Pos_Hash(palavra, tamanho_hash)]), palavra);
			}
    	     }  
	}     
	

     //Lendo a última parte do texto
     texto[tamanho_texto] = '\0';
     
	
	for (i=0;i<strlen((const char*)texto);i++){
		texto[i]=tolower(texto[i]);
	}
	
	i=0;
	while (i< (tamanho_texto)) {
		memset(palavra,'\0',NUMERO);
         	j=0;
         	while ((tabela_letras[texto[i]]) && (i<tamanho_texto)) {
			palavra[j] = texto[i];
              	j++;
              	i++;     
         	}
		i++;
		if (strlen((const char*)palavra)>1) {
			cont++;
			Inserir_Hash(&(hash[Pos_Hash(palavra, tamanho_hash)]), palavra);
		}
	}     
	
	imprimir_hash(hash, tamanho_hash, arq);
	
	printf("palavras: %d\n", cont);
}