2012-07-31 86 views
0
#include <stdbool.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <ctype.h> 

#include "dictionary.h" 

#define HASH_SIZE 5 

// prototype 
int hash(char *word); 

// counter 
int counter; 

// node 
typedef struct node 
{ 
    char word[LENGTH + 1]; 
    struct node *next; 
} node; 

// hash table 
struct node *hashtable[HASH_SIZE]; 



/* 
* Returns true if word is in dictionary else false. 
*/ 

bool 
check(const char *word) 
{ 
    //make a writable word (palabra) 
    int len = strlen(word); 
    char palabra[len + 1]; 
    //int i; 

    // make la palabra all lowercase letters 
    for (int i = 0; i < len; i++) 
    { 
     if (isalpha(palabra[i]) || palabra[i] == '\'') 
      palabra[i] = tolower(word[i]); 
    } 

    //palabra[i] = (char) "\0"; 

    // hash the word 
    int value = hash(palabra); 

    // let's look at the first node in the bucket 
    struct node *n; 
    if (hashtable[value] == NULL) 
     return false; 
    else 
     n = hashtable[value]; 

    // iterate through the bucket to see if the word is there 
    while (strcmp(palabra, n->word) != 0 && n->next != NULL) 
    { 
     n = n->next; 
    } 


    // if the word is found, print true, else false 
    if (strcmp(palabra, n->word) ==0) 
     return true; 
    else 
     return false; 
} 


/* 
* Loads dictionary into memory. Returns true if successful else false. 
*/ 

bool 
load(const char *dictionary) 
{ 
    // open the dictionary 
    FILE *dict = fopen(dictionary, "r"); 
    if(dict == NULL) 
    { 
     printf("Could not open %s.\n", dictionary); 
     return false; 
    } 

    // set all values in the hash table to null 
    for(int i = 0; i < HASH_SIZE; i++) 
    { 
     hashtable[i] = NULL; 
    } 

    // set the counter to 0 
    counter = 0; 

    // iterate through the words in the dictionary 
    while (!feof(dict)) 
    { 
     //declare a node 
     node *n = malloc(sizeof(node)); 

     // copy the word into the node 
     fscanf(dict, "%s", n->word); 

     // hash the word, baby! 
     int hash_value = hash(n->word); 

     // start saving addresses to the hashtable 
     n->next = hashtable[hash_value]; 
     hashtable[hash_value] = n; 

     // that's one more! 
     counter++; 
    } 

    // testing 
    printf("Starting the test.\n"); 
    for (int i = 0; i < HASH_SIZE; i++) 
    { 
     struct node *q = hashtable[i]; 
     while (q != NULL) 
     { 
      printf("%s\n", q->word); 
      q = q->next; 
     } 
    } 
    printf("Ending the test.\n"); 

    fclose(dict); 

    return true; 
} 


/* 
* Returns number of words in dictionary if loaded else 0 if not yet loaded. 
*/ 

unsigned int 
size(void) 
{ 
    return counter; 
} 


/* 
* Unloads dictionary from memory. Returns true if successful else false. 
*/ 

bool 
unload(void) 
{ 
    // TODO 
    return false; 
} 


/* 
* Returns a hash value for a word. 
*/ 

int 
hash(char *word) 
{ 
    // hash value and length of word 
    int value = 0; 
    int len = strlen(word); 

    // iterate through letters, adding ASCII values 
    for(int i = 0; i < len; i++) 
    { 
     int letter = (int) word[i]; 
     value += letter; 
    } 

    // make sure the value is less than 100 & return 
    value = value%HASH_SIZE; 
    return value; 
} 

當我運行我的代碼,我收到此錯誤信息:我以某種方式改變ctype.h?

在文件中包含從/usr/include/ctype.h:28:0, 從speller.c:10: 在/ usr/include/bits/types.h:31:1:error:expected'=',',';','asm'或'attribute'before'typedef'

這是否意味着我有以某種方式設法改變ctype.h庫?如果是這樣,我該如何解決它?

+0

你看過ctype.h,在第28行左右?你看到有什麼不對嗎? – abelenky 2012-07-31 01:03:13

+0

嗯...我會查找如何打開它在ctype.h並讓你知道... – hannah 2012-07-31 01:05:58

+7

這是你使用的確切代碼(它說'speller.c:10',但'ctype.h'看起來就像它在第5行)?當我在包含之前忘記了分號或其他東西時,我遇到了這種類型的錯誤。 – 2012-07-31 01:06:23

回答

3

啊哈!戈登,你解決了它!我發現在#include的正上方輸入了一些導致問題的零散字符。謝謝!

+3

上面沒有流浪的角色。當在問題中發佈代碼時,請請發佈您使用的**實際代碼**。 – 2012-07-31 02:19:23

相關問題