Advertisement
Jodyone

Load

Apr 2nd, 2014
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.16 KB | None | 0 0
  1. /****************************************************************************
  2.  * dictionary.c
  3.  *
  4.  * Computer Science 50
  5.  * Problem Set 6
  6.  *
  7.  * Implements a dictionary's functionality.
  8.  ***************************************************************************/
  9. #include <stdio.h>
  10. #include <stdbool.h>
  11. #include <stdlib.h>
  12. #include "dictionary.h"
  13. #include <ctype.h>
  14. #include <string.h>
  15. #define SIZE  26
  16.  
  17.  
  18. typedef struct node
  19. {
  20.     char word[LENGTH + 1];  
  21.     struct node* next;
  22. }
  23. node;
  24.  
  25. // Make a hash table
  26. node* hashtable[SIZE];
  27.  
  28.  /**
  29.  * Returns true if word is in dictionary else false.
  30.  */
  31. bool check(const char* word)
  32. {
  33.     // TODO
  34.    
  35.     return false;
  36. }
  37.  
  38. /**
  39.  * Loads dictionary into memory.  Returns true if successful else false.
  40.  */
  41.  
  42. int table_pos(const char* word)
  43. {
  44.     int table_pos = 0;
  45.     int hash;
  46.     for (int i = 0; word[i] != '\0'; i++)
  47.     {
  48.        // check if word is alpha
  49.         if (isalpha(word[i]))
  50.         {
  51.             hash = word[i] - 'a';
  52.         }
  53.         else  
  54.         {
  55.             hash = 26;  
  56.         }
  57.      table_pos = hash % SIZE;
  58.     }
  59.     return table_pos;
  60. }
  61.    
  62. bool load(const char* dictionary)
  63. {
  64.     // TODO
  65.     // do some prep work
  66.     FILE* inptr = fopen(dictionary,"r");
  67.     if (inptr == NULL)
  68.     {
  69.         printf("Could not open %s.\n", dictionary);
  70.         return 2;
  71.     }
  72.     // Make a new word
  73.    char temp_word[LENGTH + 1];
  74.  
  75.     //while there are words available
  76.      while (fscanf(inptr,"%s\n",temp_word) != 0)
  77.      {
  78.           node* new_node = malloc(sizeof(node));
  79.           strcpy(new_node->word,temp_word);
  80.           int x = table_pos(new_node->word);
  81.  
  82.           // get it into that list
  83.           new_node->next = hashtable[x];
  84.           hashtable[x] = new_node;
  85.          
  86.      }
  87.  
  88.      //do some cleanup work
  89.      
  90.      fclose(inptr);
  91.      return true;
  92.  }
  93.  
  94. /**
  95.  * Returns number of words in dictionary if loaded else 0 if not yet loaded.
  96.  */
  97. unsigned int size(void)
  98. {
  99.     // TODO
  100.     return 0;
  101. }
  102.  
  103. /**
  104.  * Unloads dictionary from memory.  Returns true if successful else false.
  105.  */
  106. bool unload(void)
  107. {
  108.     // TODO
  109.     return false;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement