Advertisement
Guest User

dcadca

a guest
Feb 17th, 2010
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.91 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. unsigned int hashf(const char *buf, size_t len);
  6.  
  7. int main(int argc, char *argv[]) {
  8.  
  9.     unsigned int i, k, c, hash;
  10.  
  11.     if (argc < 3) {
  12.         puts("Too few arguments!");
  13.         return 1;
  14.     }
  15.    
  16.     // Take the filename given as a command line parameter
  17.     char * filename = argv[1];
  18.  
  19.     // Take the hash table size given as the second parameter
  20.     int hash_size = atoi(argv[2]);
  21.     char * table[hash_size];
  22.     char * empty = " ";
  23.     for (int m = 0; m < hash_size; m++) {
  24.         table[m] = empty;
  25.     }
  26. //  char table[hash_size][100];
  27.  
  28.     char word[100];
  29.  
  30.     // Open the file
  31.     FILE * file = fopen(filename, "r");
  32.  
  33.     if (!file) {
  34.         puts("Couldn't open the file!");
  35.         return 1;  
  36.     }
  37.     else {
  38.         int in_word = 0;
  39.         while ((c = fgetc(file)) != EOF) {
  40.             word[k] = c;
  41.             if (c == ' ' || c == '\n' || c == '\t') {
  42.                 if (in_word == 1) {
  43.                     word[k] = '\0';
  44.  
  45.                     char* word2 = (char*) malloc((k+1)*sizeof(char));
  46.                     strcpy(word2, word);
  47.                     hash = hashf(word2, k+1);
  48.                     printf ("word2: %s\n", word2);
  49.                     printf ("length: %d\n", k+1);
  50.                     i = 0;
  51.                     /*while (table[hash] != NULL) {
  52.                         hash += i*i;
  53.                         if (hash > hash_size) {
  54.                             i++;
  55.                         }
  56.                         else {
  57.                             printf("Error: table full");
  58.                         }
  59.                     }*/
  60.                     printf ("%u\n", hash);
  61.                     //table[hash] = word2;
  62.                 }
  63.                 k = 0;
  64.                 in_word = 0;
  65.             }
  66.             else if (!in_word) {
  67.                 word[k] = c;
  68.                 k++;
  69.                 in_word = 1;
  70.             }
  71.         }
  72.  
  73.         // Close the file  
  74.         fclose(file);
  75.  
  76.         // Print the hash table
  77.         for (int j = 0; j < hash_size; j++) {
  78.             printf("%s, ", table[j]);
  79.         }
  80.     }
  81.     return 0;
  82. }
  83.  
  84. // Given function that hashes the words
  85. unsigned int hashf(const char *buf, size_t len) {
  86.     unsigned int h = 0;
  87.     size_t i;
  88.     for (i = 0; i < len; i++) {
  89.         h += buf[i];
  90.         h += h << 10;
  91.         h ^= h >> 7;
  92.     }
  93.     h += h << 3;
  94.     h ^= h >> 11;
  95.     h += h << 15;
  96.     return h;
  97. }
  98.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement