rendered paste body#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//Mircs
unsigned int hashf(const char *buf, size_t len) {
unsigned int h = 0;
size_t i;
for (i = 0; i < len; i++) {
h += buf[i];
h += h << 10;
h ^= h >> 7;
}
h += h << 3;
h ^= h >> 11;
h += h << 15;
return h;
}
int main(int argc, char **argv)
{
char FileName[100];
int HashSize = 0;
if(argc > 2)
{
sscanf(argv[1],"%s",FileName);
sscanf(argv[2],"%d",&HashSize);
}
char *HashTable[HashSize];
unsigned int j = 0;
while(j < HashSize)
{
HashTable[j] = NULL;
j++;
}
FILE *pFile;
pFile = fopen(FileName,"r");
while (1)
{
char tempWord[100];
int a = fscanf(pFile," %s ",tempWord);
if(a == EOF || a < 1)
break;
if(HashTable[hashf(tempWord,strlen(tempWord) )%HashSize] != NULL)
{
unsigned int k = 1;
while(k < HashSize)
{
if(HashTable[(hashf(tempWord,strlen(tempWord))+k*k)%HashSize] == NULL)
{
HashTable[(hashf(tempWord,strlen(tempWord))+k*k)%HashSize] = malloc(strlen(tempWord)+1);
strcpy(HashTable[(hashf(tempWord,strlen(tempWord))+k*k)%HashSize],tempWord);
break;
}
k++;
}
if(k >= HashSize)
{
int k = 0;
while(k < HashSize)
{
if(HashTable[k] == NULL)
{
}
else
{
free(HashTable[k]);
}
k++;
}
fclose(pFile);
printf("Error: table full\n");
return 1;
}
/*printf("%d %s\n",hashf(tempWord,strlen(tempWord))%HashSize,tempWord);*/
/*return 1;*/
}
else
{
HashTable[hashf(tempWord, strlen(tempWord) )%HashSize] = malloc(strlen(tempWord)+1);
strcpy(HashTable[hashf(tempWord,strlen(tempWord))%HashSize],tempWord);
/*printf("%d %s\n",hashf(tempWord,strlen(tempWord))%HashSize,tempWord);*/
}
}
int i = 0;
while(i < HashSize)
{
if(i == HashSize-1)
{
if(HashTable[i] == NULL)
printf(" \n");
else
{
printf("%s\n",HashTable[i]);
free(HashTable[i]);
}
break;
}
else
{
if(HashTable[i] == NULL)
printf(" ,");
else
{
printf("%s,",HashTable[i]);
free(HashTable[i]);
}
}
i++;
}
fclose(pFile);
return 0;
}