Guest User

Problem Set 5, Dictionary.c

a guest
Nov 2nd, 2017
77
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2. * Implements a dictionary's functionality.
  3. */
  4.  
  5. #include <stdbool.h>
  6. #include <stdio.h>
  7. #include "dictionary.h"
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include <ctype.h>
  11.  
  12. int index = 0;
  13.  
  14. typedef struct node
  15. {
  16. char word[LENGTH + 1];
  17. struct node *next;
  18. }
  19. node;
  20.  
  21. node *hashtable[26];
  22.  
  23. /**
  24. * Returns true if word is in dictionary else false.
  25. */
  26. bool check(const char *word)
  27. {
  28. // TODO
  29. /*
  30. hash the word
  31. then, with the hashed function, search the desired hashtable for the word using string compare strcmp through linked list
  32.  
  33. 1. hash word
  34. 2. search through linked list (loop) and strcmp every node
  35. 3. if not found till end, then return false
  36.  
  37. */
  38.  
  39.  
  40.  
  41. int len = strlen(word);
  42. char word_copy[LENGTH + 1];
  43.  
  44. // convert word to lowercase and store it in word_copy
  45. for (int i = 0; i < len; i++)
  46. {
  47. word_copy[i] = tolower(word[i]);
  48. }
  49.  
  50. // add null terminator to end of char array
  51. word_copy[len] = '\0';
  52.  
  53.  
  54.  
  55. int hashy;
  56.  
  57. if (word_copy[0] == 'a')
  58. {
  59. hashy= 0;
  60. }
  61.  
  62. else if (word_copy[0] == 'b')
  63. {
  64. hashy=1;
  65. }
  66.  
  67. else if (word_copy[0] == 'c')
  68. {
  69. hashy=2;
  70. }
  71.  
  72. else if (word_copy[0] == 'd')
  73. {
  74. hashy=3;
  75. }
  76.  
  77. else if (word_copy[0] == 'e')
  78. {
  79. hashy=4;
  80. }
  81.  
  82. else if (word_copy[0] == 'f')
  83. {
  84. hashy=5;
  85. }
  86.  
  87. else if (word_copy[0] == 'g')
  88. {
  89. hashy=6;
  90. }
  91.  
  92.  
  93. else if (word_copy[0] == 'h')
  94. {
  95. hashy=7;
  96. }
  97.  
  98. else if (word_copy[0] == 'i')
  99. {
  100. hashy=8;
  101. }
  102.  
  103. else if (word_copy[0] == 'j')
  104. {
  105. hashy=9;
  106. }
  107.  
  108. else if (word_copy[0] == 'k' )
  109. {
  110. hashy=10;
  111. }
  112.  
  113. else if (word_copy[0] == 'l')
  114. {
  115. hashy=11;
  116. }
  117.  
  118. else if (word_copy[0] == 'm')
  119. {
  120. hashy=12;
  121. }
  122.  
  123. else if (word_copy[0] == 'n')
  124. {
  125. hashy=13;
  126. }
  127.  
  128. else if (word_copy[0] == 'o')
  129. {
  130. hashy=14;
  131. }
  132.  
  133. else if (word_copy[0] == 'p')
  134. {
  135. hashy=15;
  136. }
  137.  
  138. else if (word_copy[0] == 'q')
  139. {
  140. hashy=16;
  141. }
  142.  
  143. else if (word_copy[0] == 'r')
  144. {
  145. hashy=17;
  146. }
  147.  
  148. else if (word_copy[0] == 's')
  149. {
  150. hashy=18;
  151. }
  152.  
  153. else if (word_copy[0] == 't')
  154. {
  155. hashy=19;
  156. }
  157.  
  158. else if (word_copy[0] == 'u')
  159. {
  160. hashy=20;
  161. }
  162.  
  163. else if (word_copy[0] == 'v')
  164. {
  165. hashy=21;
  166. }
  167.  
  168. else if (word_copy[0] == 'w')
  169. {
  170. hashy=22;
  171. }
  172.  
  173. else if (word_copy[0] == 'x')
  174. {
  175. hashy=23;
  176. }
  177.  
  178. else if (word_copy[0] == 'y')
  179. {
  180. hashy=24;
  181. }
  182.  
  183. else if (word_copy[0] == 'z')
  184. {
  185. hashy=25;
  186. }
  187.  
  188. else
  189. {
  190. return false;
  191. }
  192.  
  193.  
  194. node* cursor = hashtable[hashy];
  195.  
  196. while (cursor != NULL)
  197. {
  198. if (strcmp(word_copy, cursor->word) == 0)
  199. {
  200. // word is in dictionary
  201. return true;
  202. }
  203.  
  204. // check next node
  205. cursor = cursor->next;
  206.  
  207. }
  208.  
  209. return false;
  210. }
  211.  
  212. /**
  213. * Loads dictionary into memory. Returns true if successful else false.
  214. */
  215. bool load(const char *dictionary)
  216. {
  217. // TODO
  218. //Hashtables
  219. //sort dictionary into alphabetic order. a - z
  220.  
  221. FILE *file = fopen(dictionary, "r");
  222.  
  223. if (file == NULL)
  224. {
  225. printf("Failed to load dictionary");
  226. return false;
  227. }
  228.  
  229. //25 means 0-24
  230.  
  231. for (int i = 0; i < 26; i++)
  232. {
  233. hashtable[i] = NULL;
  234. hashtable[i] = malloc(sizeof(node));
  235. hashtable[i]-> next = NULL;
  236. }
  237. // malloc hashtable
  238.  
  239. char word[LENGTH + 1];
  240.  
  241. while (fscanf(file, "%s", word) != EOF)
  242. {
  243. /* if word starts with "a" then put it in node for hashtable[0]
  244. else if word starts with "b" then put it in node for hashtable[1]
  245. else if word starts with "c" then put it in node for hashtable[2]
  246. else if word starts with "d" then put it in node for hashtable[3]
  247. else if word starts with "e" then put it in node for hashtable[4]
  248. else if word starts with "f" then put it in node for hashtable[5]
  249. else if word starts with "g" then put it in node for hashtable[6]
  250. else if word starts with "h" then put it in node for hashtable[7]
  251. else if word starts with "i" then put it in node for hashtable[8]
  252. else if word starts with "j" then put it in node for hashtable[9]
  253. else if word starts with "k" then put it in node for hashtable[10]
  254. else if word starts with "l" then put it in node for hashtable[11]
  255. else if word starts with "z" then put it in node for hashtable[25]
  256. */
  257.  
  258. if (word[0] == 'a' || word[0] == 'A')
  259. {
  260. strcpy(hashtable[0]-> word, word);
  261. index++;
  262. }
  263.  
  264. else if (word[0] == 'b' || word[0] == 'B' )
  265. {
  266. strcpy(hashtable[1]-> word, word);
  267. index++;
  268. }
  269.  
  270. else if (word[0] == 'c' || word[0] == 'C' )
  271. {
  272. strcpy(hashtable[2]-> word, word);
  273. index++;
  274. }
  275.  
  276. else if (word[0] == 'd' || word[0] == 'D' )
  277. {
  278. strcpy(hashtable[3]-> word, word);
  279. index++;
  280. }
  281.  
  282. else if (word[0] == 'e' || word[0] == 'E' )
  283. {
  284. strcpy(hashtable[4]-> word, word);
  285. index++;
  286. }
  287.  
  288. else if (word[0] == 'f' || word[0] == 'E' )
  289. {
  290. strcpy(hashtable[5]-> word, word);
  291. index++;
  292. }
  293.  
  294. else if (word[0] == 'g' || word[0] == 'G' )
  295. {
  296. strcpy(hashtable[6]-> word, word);
  297. index++;
  298. }
  299.  
  300.  
  301. else if (word[0] == 'h' || word[0] == 'H' )
  302. {
  303. strcpy(hashtable[7]-> word, word);
  304. index++;
  305. }
  306.  
  307. else if (word[0] == 'i' || word[0] == 'I' )
  308. {
  309. strcpy(hashtable[8]-> word, word);
  310. index++;
  311. }
  312.  
  313. else if (word[0] == 'j' || word[0] == 'J' )
  314. {
  315. strcpy(hashtable[9]-> word, word);
  316. index++;
  317. }
  318.  
  319. else if (word[0] == 'k' || word[0] == 'K' )
  320. {
  321. strcpy(hashtable[10]-> word, word);
  322. index++;
  323. }
  324.  
  325. else if (word[0] == 'l' || word[0] == 'L' )
  326. {
  327. strcpy(hashtable[11]-> word, word);
  328. index++;
  329. }
  330.  
  331. else if (word[0] == 'm' || word[0] == 'M' )
  332. {
  333. strcpy(hashtable[12]-> word, word);
  334. index++;
  335. }
  336.  
  337. else if (word[0] == 'n' || word[0] == 'N' )
  338. {
  339. strcpy(hashtable[13]-> word, word);
  340. index++;
  341. }
  342.  
  343. else if (word[0] == 'o' || word[0] == 'O' )
  344. {
  345. strcpy(hashtable[14]-> word, word);
  346. index++;
  347. }
  348.  
  349. else if (word[0] == 'p' || word[0] == 'P')
  350. {
  351. strcpy(hashtable[15]-> word, word);
  352. index++;
  353. }
  354.  
  355. else if (word[0] == 'q' || word[0] == 'Q' )
  356. {
  357. strcpy(hashtable[16]-> word, word);
  358. index++;
  359. }
  360.  
  361. else if (word[0] == 'r' || word[0] == 'R' )
  362. {
  363. strcpy(hashtable[17]-> word, word);
  364. index++;
  365. }
  366.  
  367. else if (word[0] == 's' || word[0] == 'S' )
  368. {
  369. strcpy(hashtable[18]-> word, word);
  370. index++;
  371. }
  372.  
  373. else if (word[0] == 't' || word[0] == 'T' )
  374. {
  375. strcpy(hashtable[19]-> word, word);
  376. index++;
  377. }
  378.  
  379. else if (word[0] == 'u' || word[0] == 'U' )
  380. {
  381. strcpy(hashtable[20]-> word, word);
  382. index++;
  383. }
  384.  
  385. else if (word[0] == 'v' || word[0] == 'V' )
  386. {
  387. strcpy(hashtable[21]-> word, word);
  388. index++;
  389. }
  390.  
  391. else if (word[0] == 'w' || word[0] == 'W' )
  392. {
  393. strcpy(hashtable[22]-> word, word);
  394. index++;
  395. }
  396.  
  397. else if (word[0] == 'x' || word[0] == 'X' )
  398. {
  399. strcpy(hashtable[23]-> word, word);
  400. index++;
  401. }
  402.  
  403. else if (word[0] == 'y' || word[0] == 'Y' )
  404. {
  405. strcpy(hashtable[24]-> word, word);
  406. index++;
  407. }
  408.  
  409. else if (word[0] == 'z' || word[0] == 'Z' )
  410. {
  411. strcpy(hashtable[25]-> word, word);
  412. index++;
  413. }
  414.  
  415. else
  416. {
  417. return false;
  418. }
  419.  
  420. // strcpy(new_node-> word, word)
  421. }
  422. fclose(file);
  423. return true;
  424. }
  425.  
  426. /**
  427. * Returns number of words in dictionary if loaded else 0 if not yet loaded.
  428. */
  429. unsigned int size(void)
  430. {
  431. // TODO
  432. return index;
  433. }
  434.  
  435. /**
  436. * Unloads dictionary from memory. Returns true if successful else false.
  437. */
  438. bool unload(void)
  439. {
  440. // TODO
  441. for (int i = 0; i < 26; i++)
  442. {
  443. node* cursor = hashtable[i];
  444.  
  445. while (cursor != NULL)
  446. {
  447. node *temp = cursor;
  448. cursor = cursor->next;
  449. free(temp);
  450. }
  451. }
  452. return true;
  453. }
RAW Paste Data Copied