rahulb5

Untitled

Feb 20th, 2015
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 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.  
  10. #include <stdbool.h>
  11. #include<stdio.h>
  12. #include "dictionary.h"
  13. #include<stdlib.h>
  14. #include<string.h>
  15. /**
  16. * Returns true if word is in dictionary else false.
  17. */
  18.  
  19. typedef struct node
  20. {
  21. int check ;
  22. struct node* letter[10];
  23. }node;
  24.  
  25. struct node* root = NULL;
  26.  
  27. struct node* createnode()
  28. {
  29. struct node* new_node = malloc(sizeof(node));
  30. for(int i = 0 ; i < 10 ;i++)
  31. new_node->letter[i] = NULL;
  32. return new_node;
  33. }
  34.  
  35. int i = 0 ;
  36.  
  37. bool check(const char* word)
  38. {
  39. // TODO
  40. struct node* check = root;
  41. for(i = 0 ; i < strlen(word) ; i++)
  42. {
  43. int temp = atoi(&word[i]);
  44. if(check->letter[i] != NULL)
  45. check = check->letter[temp];
  46. else
  47. break;
  48. }
  49. if(check->check==1)
  50. return true;
  51.  
  52. return false;
  53.  
  54. }
  55.  
  56. /**
  57. * Loads dictionary into memory. Returns true if successful else false.
  58. */
  59. bool load(const char* dictionary)
  60. {
  61. // TODO
  62.  
  63.  
  64. FILE* fp = NULL;
  65.  
  66. fp = fopen(dictionary,"r");
  67.  
  68. if(fp == NULL)
  69. return false;
  70.  
  71. char word[20];
  72.  
  73. while(fscanf(fp,"%s",word) != EOF)
  74. {
  75. if(root == NULL)
  76. root = createnode();
  77.  
  78. struct node* next_node = root;
  79. for(i= 0; i < strlen(word) ; i++)
  80. {
  81. int temp = atoi(&word[i]) - 'a' ;
  82. if(next_node->letter[temp] == NULL)
  83. next_node->letter[temp] = createnode();
  84. next_node = next_node->letter[temp];
  85. }
  86. next_node->check = 1;
  87.  
  88. }
  89. fclose(fp);
  90. return true;
  91. }
  92.  
  93. /**
  94. * Returns number of words in dictionary if loaded else 0 if not yet loaded.
  95. */
  96. unsigned int size(void)
  97. {
  98. // TODO
  99. return 0;
  100. }
  101.  
  102. /**
  103. * Unloads dictionary from memory. Returns true if successful else false.
  104. */
  105. bool unload(void)
  106. {
  107. // TODO
  108. return false;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment