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;
} 

当我运行我的代码时,我收到以下错误消息:

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

这是否意味着我设法改变了 ctype.h 库?如果是这样,我该如何解决?

4

1 回答 1

3

啊哈!戈登,你解决了!我发现在导致问题的#include 上方键入了一些杂散字符。谢谢!

于 2012-07-31T01:14:47.323 回答