Speller拼错了所有单词

时间:2020-09-24 16:53:12

标签: c cs50

运行程序时,我的所有单词拼写错误。它可以编译,但无法按预期工作。我相信这与hash / check /或load函数有关。我已经检查了很多次,但无法找出问题所在。我尝试输出newWord变量,以查看它是否以小写形式返回一个副本,但由于某种原因,它没有打印出来。也许这意味着所有值都是NULL?因此,这可能是我的load函数中的问题。我会再次检查。预先感谢。

编辑:运行调试器,我相信load函数将返回正确的值。我认为它只能是我的hashcheck函数。

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

const unsigned int N = 17577;

// Hash table
node *table[N];

//words global variable
int words = 0;

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    int hashValue = hash(word);
    if(hashValue == 17576){
        return false;
    }
    node *cursor = table[hashValue];
    printf("/%s", cursor->word);
    //review my notes for this while loop
    //basically tmp->next is pointing to the NEXT ITERATION
    //and tmp is our CURRENT ITERATION
    //so we want to check a value, if !true, continue
    while(cursor->next != NULL){
        if (strcasecmp(word, cursor->word) == 0){
            return true;
        }
        else{
            cursor = cursor->next;
        }
    }
    return false;
}


unsigned int hash(const char *word)
{
    const int ASCII_APOSTROPHE = 39;
    int alphaIndex;
    char *newWord = NULL;
    //make it lowercase for easier read
    for(int i = 0, n = strlen(word); i < n; i++){
        if(isalpha(word[i]) == 1){
            newWord[i] = tolower(word[i]);
        }
        else if(word[i] == ASCII_APOSTROPHE || isalpha(word[i]) == 2){
            newWord[i] = word[i];
        }
        else{
            //last index of array
            return 17576;
        }
    }
    
    if(strlen(newWord) >= 3){
        alphaIndex = (word[0] - 97) + (word[1] - 97) + (word[2] - 97);
    }
    else if(strlen(newWord) == 2){
         alphaIndex = (word[0] - 97) + (word[1] - 97);
    }
    else if(strlen(newWord) == 1){
        alphaIndex = (word[0] - 97);
    }
    else{
        alphaIndex = 0;
    }
    return alphaIndex;
}


// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    FILE *file = fopen(dictionary, "r");
    if(file == NULL){
        printf("Could not open file!\n");
        return false;
    }

  
   char *word = malloc(LENGTH + 1);

   while(fscanf(file, "%s", word) != EOF){
       node *n = malloc(sizeof(node));
       if(n == NULL){
           printf("Not enough memory!");
           return 1;
       }
       strcpy(n->word, word);
       n->next = table[hash(word)];
       table[hash(word)] = n;
       size();
   }
   free(word);
   fclose(file);
   return true;
}

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

// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
    for(int i = 0; i < N; i++){
        node *head = table[i];
        if(head != NULL){
            node *cursor = head->next;
            node *deleteCursor = cursor;
            while(cursor->next != NULL){
                free(deleteCursor);
                cursor = cursor->next;
                deleteCursor = cursor;
            }
        }
    }
    return true;
}

1 个答案:

答案 0 :(得分:1)

有两个问题。

第一个是哈希函数在某种程度上运行错误。我没有调试它,只是用一个非常简单的哈希函数(从https://stackoverflow.com/a/1469939/11000382复制)代替了它。

unsigned int hash(const char *word)
{
    char c = word[0];
    int n = -1;
    static const char *const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    char *p = strchr(alphabet, toupper((unsigned char)c));

    if (p)
    {
        n = p - alphabet;
    }

    return n;
}

此后,我检查了$ ./speller texts/lalaland.txt,但得到的拼写错误的单词更多(1738)。但是,应该是955。然后我回顾了check函数,发现了一个小错误。

这个

while(cursor->next != NULL){

应该是这个

while(cursor != NULL){

然后,它为lalaland.txt找到955个拼写错误的单词。