cs50 Speller无法处理大多数单词,并且大小写也不敏感

时间:2020-07-09 23:24:20

标签: cs50

我已尽我所能解决此问题。我尝试过调整strcompcase()函数以反映它返回的整数,但是我可能不知道正确的方法,因此我尝试在哈希处理之前仅使用strcmp()将所有文本转换为小写。不仅不能解决问题,而且还有内存问题。

我假设我的检查功能存在问题,但可能不是。我真的可以使用实际上知道自己在做什么的人的建议,而我绝对不知道。

谢谢。

// Implements a dictionary's functionality

#include <stdbool.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.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 = 200000;

// Hash table
node *table[N];

unsigned int dict_size = 0;

// Returns true if word is in dictionary else false
bool check(const char *word) {
    // getting hash value of word
    int index = hash(word);

    // declare travel point to navigate table
    node *trav = table[index];

    while (trav != NULL) {

        // comparison to find if word is present in table
        if (strcasecmp (trav->word, word) == 0) {
            return true;
        } else {
            trav = trav->next;
        }
    }
    return false;
}

// Hashes word to a number
unsigned int hash(const char *word) {
    // hash function found https://www.reddit.com/r/cs50/comments/1x6vc8/pset6_trie_vs_hashtable/
    unsigned int index = 0;
    for (int i = 0, n=strlen(word); i < n; i++) {
        index = (index << 2) ^ word[i];
    }
    return index % N;
}

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary) {

    // declaring buffer array
    char dict_word [LENGTH + 1];

    // opening dictionary file
    FILE *dict = fopen(dictionary, "r");

    // double checking if file opened correctly
    if (dict == NULL) {
        printf("Dictionary did not open.\n");
        return false;
    }

    // looping through until end of file
    while (fscanf (dict, "%s\n", dict_word) != EOF) {

        // hashing word to check
        int index = hash(dict_word);
        node *new_node = malloc (sizeof(node));

        // making sure malloc worked
        if (new_node == NULL) return false;

        // checks if index is empty
        if (table [index] == NULL) {
            // points index to new node
            table [index] = new_node;

            // points next field to NULL
            new_node->next = NULL;

        // if not empty
        } else {
            // insert new node at front of list
            new_node->next = table [index];

            // makes new node head of list
            table [index] = new_node;
        }

        // insert word into new node
        strcpy(new_node->word, dict_word);
        dict_size ++;

    }
    fclose(dict);
    return true;
}
    
// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void) {
    return dict_size;
}

// Unloads dictionary from memory, returning true if successful else false
bool unload(void) {
    for (int i = 0; i < N; i++) {

        // creates new travel pointer
        node *cursor = table [i];

        while (cursor != NULL) {
            // creates temporary pointer, synch with cursor
            node *tmp = cursor;

            cursor = cursor->next;

            // free what tmp is pointing at
            free(tmp);
        }
    }
    return true;
}

1 个答案:

答案 0 :(得分:1)

在执行此操作时,我遇到了同样的问题...在将单词转换为小写字母之前,您先对其进行了哈希处理。这是您的检查功能:

// Returns true if word is in dictionary else false
bool check(const char *word) {
    //Just converting the word to lowercase
    char* lower_word = strcmp(word)
    // getting hash value of word
    int index = hash(lower_word);

    // declare travel point to navigate table
    node *trav = table[index];

    while (trav != NULL) {

        // comparison to find if word is present in table
        if (strcasecmp (trav->word, word) == 0) {
            return true;
        } else {
            trav = trav->next;
        }
    }
    return false;
}

我想知道的是我将作品转换为小写,所以ASCII值使用小写字母来计算您的单词 我希望这行得通!