Advertisement
Guest User

Untitled

a guest
Feb 13th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.42 KB | None | 0 0
  1. /**
  2.  * Implements a dictionary's functionality.
  3.  */
  4.  
  5. #include <stdbool.h>
  6. #include <stdio.h>
  7. #include "dictionary.h"
  8. #include <ctype.h>
  9. #include <stdlib.h>
  10. const int CASE = 'a';
  11.  
  12. typedef struct node{
  13.     bool is_word ;
  14.     struct node *children[27];
  15. }node;
  16.  
  17. node *rootNode;
  18.  
  19.  
  20.  
  21.  
  22. FILE *inFile = NULL;
  23.  
  24. /**
  25.  * Returns true if word is in dictionary else false.
  26.  */
  27. bool check(const char *word)
  28. {
  29.     if(rootNode != NULL){
  30.         while(*word != '\0'){
  31.             char wordChar = tolower(*word);
  32.             if(rootNode->children[wordChar - CASE] != NULL){
  33.                 rootNode = rootNode->children[wordChar - CASE];
  34.             }
  35.             word++;
  36.         }
  37.         if(rootNode->is_word != true){
  38.             return false;
  39.         }
  40.     }
  41.     return true;
  42. }
  43.  
  44.  
  45. // void insertNode(char *word){
  46. //     node *currentNode = rootNode;
  47.  
  48. //     while(*word != '\0'){
  49.  
  50. //         if(currentNode->children[*word - CASE] == NULL){
  51. //             currentNode->children[*word - CASE] = malloc(sizeof(node));
  52. //         }
  53. //         currentNode = currentNode->children[*word - CASE];
  54. //         word++;
  55. //     }
  56. //     currentNode->is_word = true;
  57.  
  58. // }
  59. /**
  60.  * Loads dictionary into memory. Returns true if successful else false.
  61.  */
  62. bool load(const char *dictionary)
  63. {
  64.     if(rootNode == NULL){
  65.         rootNode = malloc(sizeof(node));
  66.     }
  67.  
  68.     inFile = fopen(dictionary , "r");
  69.     char *word = malloc(sizeof(char));
  70.  
  71.     if(!inFile){
  72.         printf("Could not find file");
  73.         return false;
  74.     }
  75.     while(fscanf(inFile, "%s", word) != EOF){
  76.  
  77.     }
  78.  
  79.  
  80.     return true;
  81. }
  82.  
  83. /**
  84.  * Returns number of words in dictionary if loaded else 0 if not yet loaded.
  85.  */
  86. unsigned int size(void)
  87. {
  88.     if(inFile == NULL){
  89.         return 0;
  90.     }
  91.     rewind(inFile);
  92.  
  93.     char *store = malloc(sizeof(char));
  94.     unsigned int counter = 0;
  95.  
  96.     if(rootNode == NULL){
  97.         return 0;
  98.     }
  99.  
  100.     while(fscanf(inFile, "%s", store) != EOF){
  101.         if(check(store)){
  102.             counter++;
  103.         }
  104.     }
  105.     return counter;
  106. }
  107.  
  108. /**
  109.  * Unloads dictionary from memory. Returns true if successful else false.
  110.  */
  111. bool unload(void)
  112. {
  113.     // TODO
  114.     return false;
  115. }
  116.  
  117. int main(){
  118.  
  119.  
  120.     load("dictionaries/small");
  121.     if(check("zealousness")){
  122.         printf("word found");
  123.     }else{
  124.         printf("Not found");
  125.     }
  126.  
  127.  
  128.     printf(" \nnumber of words %d\n", size());
  129.  
  130.  
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement