Pastebin is 300% more awesome when you are logged in. Sign Up, it's FREE!
Guest

Untitled

By: a guest on Feb 16th, 2013  |  syntax: None  |  size: 3.32 KB  |  hits: 33  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /****************************************************************************
  2.  * dictionary.c
  3.  *
  4.  * Computer Science 50
  5.  * Problem Set 5
  6.  *
  7.  * Implements a dictionary's functionality.
  8.  ***************************************************************************/
  9.  
  10. #include <stdbool.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <stdlib.h>
  14. #include <ctype.h>
  15. #include "dictionary.h"
  16.  
  17. int count = 0;
  18. bool opened = false;
  19. struct dict_node *wordArr[LLLength];
  20. struct dict_node *temp;
  21. struct dict_node *prev;
  22. struct dict_node *start[LLLength];
  23.  
  24. /**
  25.  * Returns true if word is in dictionary else false.
  26.  */
  27. bool check(const char* word){
  28.     char converted[LENGTH] = "";
  29.     bool found = true;
  30.     // TODO
  31.     if(strlen(word) == 0){
  32.         return false;
  33.     }
  34.     for(int i = 0; i < strlen(word); i++){
  35.         converted[i] = tolower(word[i]);
  36.     }
  37.     for(int i = 0; i < LLLength; i++){
  38.         wordArr[i] = start[i];
  39.        
  40.         while(wordArr[i]->next != NULL){
  41.             if(strcmp(wordArr[i]->word,converted) == 0){
  42.                 break;
  43.             }
  44.             else{
  45.                 wordArr[i] = wordArr[i]->next;
  46.             }
  47.         }
  48.     }
  49.     return found;
  50. }
  51.  
  52. /**
  53.  * Loads dictionary into memory.  Returns true if successful else false.
  54.  */
  55.  
  56. bool load(const char* dictionary){
  57.     // TODO
  58.     FILE *dict = fopen(dictionary, "r");
  59.     if(opened){
  60.         fclose(dict);
  61.         return false;
  62.     }
  63.     opened = true;
  64.     if(dict == NULL || !dict){
  65.         return false;
  66.     }
  67.  
  68.     char line[LENGTH];
  69.     bool filling;
  70.     for(int i = 0; i < LLLength; i++){
  71.         struct dict_node *newNode;
  72.         newNode = malloc(sizeof(dict_node));
  73.         wordArr[i] = newNode;
  74.         start[i] = wordArr[i];
  75.     }
  76.    
  77.     while(!feof(dict)){
  78.         for(int i = 0; i < LLLength; i++){
  79.             filling = true;
  80.             while(filling)
  81.                 fgets(line,LENGTH,dict);
  82.                 strcpy(wordArr[i]->word,line);
  83.                 prev = wordArr[i];
  84.                 wordArr[i]->next = malloc(sizeof(dict_node));
  85.                 wordArr[i] = wordArr[i]->next;
  86.                 fgets(line,LENGTH,dict);
  87.                 strcpy(wordArr[i]->word,line);
  88.         }
  89.     }
  90.     fclose(dict);
  91.     for(int i = 0; i < LLLength; i++){
  92.         wordArr[i] = start[i];
  93.         while(wordArr[i]->next != NULL){
  94.             wordArr[i] = wordArr[i]->next;
  95.             count++;
  96.         }
  97.     }
  98.     int test = 0;
  99.     wordArr[test] = start[test];
  100.     while(wordArr[test]->next != NULL){
  101.         printf("%s",wordArr[test]->word);
  102.         wordArr[test] = wordArr[test]->next;
  103.     }
  104.     return true;
  105. }
  106.  
  107. /**
  108.  * Returns number of words in dictionary if loaded else 0 if not yet loaded.
  109.  */
  110. unsigned int size(void){
  111.     // TODO
  112.     if(opened){
  113.         return count;
  114.     }
  115.     else{
  116.         return 0;
  117.     }
  118. }
  119.  
  120. /**
  121.  * Unloads dictionary from memory.  Returns true if successful else false.
  122.  */
  123. bool unload(void){
  124.     // TODO
  125.    
  126.     for(int i = 0; i < LENGTH; i++){
  127.         while(wordArr[i] != NULL){
  128.                 temp = wordArr[i];
  129.                 wordArr[i] = wordArr[i]->next;
  130.                 free(temp);
  131.         }        
  132.     }
  133.     for(int i = 0; i < LENGTH; i++){
  134.         if(wordArr[i] != NULL){
  135.             return false;
  136.         }
  137.     }
  138.         return true;
  139. }