CS50 Speller(所有单词拼写错误)

时间:2020-05-14 19:52:24

标签: c dictionary cs50

我一直想让脚本注册正确数目的拼写错误的单词,但是它会不断返回所有单词拼写错误的结果。我已经尝试在网上多次找到的其他脚本中引用和修复代码,但现在不确定哪个函数给我带来了问题。至少返回了词典和文本中正确数量的单词,所以我想知道问题出在我的check还是hash函数。

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <ctype.h>

#include "dictionary.h"

// Represents a node in a hash table
typedef struct node
{
    char word[LENGTH + 1];
    struct node *next;
}
node;

// Number of buckets in hash table
const unsigned int N = 1;

// Hash table (an array of node pointers; every element is a pointer to a node)
node *table[N];
// Returns true if word is in dictionary else false
bool check(const char *word)//4
{
    //take a word and check if word is in dicitionary or not with hash function
    //hash function to obtain hash value
    unsigned int index = hash(word);
    if (table[index] == NULL)
    {
        return false;
    }
    else if (table[index] != NULL)
    {
        node *cursor = table[index];
        //if cursor = NULL;(return false, reached end of list)
        while (cursor != NULL)
        {
            //case insensitive; strcasecmp (compares strings case-insensitively)
            //traverse linked list looking for word
            if (strcasecmp(cursor -> word, word) == true)
            {
                return true;
            }
            else
            {
                //access linked list at hash value(index) at hash table
                cursor = cursor -> next;
            }
        }
    }
    return false;
}
// Hashes word to a number
unsigned int hash(const char *word)//2
{
    unsigned int hash = 5381;
    int c;

    // Iterate through the characters of string
    while ((c = *word++))
    {
        // Calculate (hash * 33) + c
        hash = ((hash << 5) + hash) + tolower(c);
    }
    // returns a number for bucket index; existing range N
    return hash % N;
}
// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)//1
{
    // TODO
    //max length of a word
    char dict_word[LENGTH + 1];

    //open file
    FILE *file = fopen(dictionary, "r");
    //check if return valus is NULL to indicate success, otherwise false
    if (file == NULL)
    {
        unload();
        fprintf(stderr,"Dictionary not Available\n");
        return 1;
    }

    //read file
    //fscanf return EOF (end of file) when done (will want a loop to do so, word after word)
    while (fscanf(file, "%s", dict_word) != EOF)
    {
        //load data into hash table; call for hash function to determine which index in hash table should be used for new node
        unsigned int bucket = hash(dict_word);
        //create new node for words in the dictionary
        node *new_node = malloc(sizeof(node));

        //check for NULL return value from malloc
        if (new_node == NULL)
        {
            unload();
            printf("\nMessed Up Loading Node\n");
            return false;
        }

        //strcpy function
        strcpy(new_node -> word, dict_word);
        //prepare new node
        new_node -> next = table[bucket];
        table[bucket] = new_node;
    }

    fclose(file);
    return true;
}
// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)//3
{
    // TODO
    //return number of words in dictionary
    int count = 0;
    //as loading hash table, keep track of number of words added to dictionary, w = word
    //iterate over each bucket
    for (int w = 0; w < N; w++)
    {
        //prepare extra pointer 'temp' to track number of words
        node *temp = table[w];
        //iterate over each word in bucket
        while(temp != NULL)
        {
            count += 1;
            temp = temp -> next;
        }
    }
    //printf; return value in size function
    return count;
}
// Unloads dictionary from memory, returning true if successful else false
bool unload(void)//5
{
    // TODO
    //variable temp = cursor (points to same node)
    // go through all of the indexes in the hashtable
    for (int i = 0; i < N; i++)
    {
        node* cursor = table[i];

        //repeat until reaching end of list NULL and return true
        while (cursor != NULL)
        {
            //frees up what cursor is pointing to
            node* temp = cursor -> next;

            free(cursor);
            cursor = temp;
        }
    }
    return true;
}

1 个答案:

答案 0 :(得分:5)

这是不正确的

if (strcasecmp(cursor -> word, word) == true)

应该是

if (strcasecmp(cursor -> word, word) == 0)

strcasecmp如果两个字符串相等,则返回零。

Documentation