cs50 pset5拼写错误的单词过多

时间:2020-06-04 15:05:29

标签: c hashtable cs50

这是CS50课程的一个预设。任务是将单词加载到链接列表(哈希表)数组中,然后检查文本以查看单词是否拼写错误。 Check()函数似乎可疑,但无法找出原因。任何帮助表示赞赏。 (单词的比较不区分大小写。)

我的输出:

WORDS MISSPELLED:     5440(TOO MUCH)
WORDS IN DICTIONARY:  143091
WORDS IN TEXT:        17756
TIME IN load:         0.02
TIME IN check:        0.06
TIME IN size:         0.00
TIME IN unload:       0.01
TIME IN TOTAL:        0.09

类解决方案:

WORDS MISSPELLED:     955
WORDS IN DICTIONARY:  143091
WORDS IN TEXT:        17756
TIME IN load:         0.04
TIME IN check:        0.01
TIME IN size:         0.00
TIME IN unload:       0.02
TIME IN TOTAL:        0.07
// Implements a dictionary's functionality

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "dictionary.h"
#include <math.h>
#include <string.h>
#include <strings.h>

int n = 0;


// 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 = 5000;

// Hash table
node *table[N];

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

    if (table[index] == NULL)//nothing loaded here (no link to the head)
    {
        return false;
    }

    node *iter = table[index];//iterator pointer to jump on linked list = root

    while (iter != NULL )// it could not find the word, keep searching
    {
        if (strcasecmp(iter -> word, word))
        {
            iter = iter ->next;
        }

        else if (!strcasecmp(iter -> word, word))
        {
            return true;
        }
    }
    return false;
}

// Hashes word to a number
unsigned int hash(const char *word)
{
    //between 0 N - 1 N = 5000
    int count = strlen(word);

    int sum = 0;

    for ( int i = 0; i < count; i++)
    {
        sum += (int)word[i];
    }

    return round(sum % 5000);
}

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

    char wordx[LENGTH+1];

    FILE *f = fopen(dictionary, "r");//opens the dictionary for reading

    for (int i = 0; i < N ; i++)//appending all the nuls to the table
    {
        table[i] = NULL;
    }

    while (f == NULL)
    {
        printf("File could not opened.");//Checking file
        return false;
    }
    while (fscanf(f, "%s", wordx) != EOF )//Until coming to the end of the file
    {
        node* p = (node *)malloc(sizeof(node));//creating a new node for each word

        if (p == NULL)
        {
            printf("Memory could not allocated.");
            return false;
        }

        strcpy(p -> word, wordx);//putting buffers word in to the node

        int numindex = hash(wordx);

        memset(wordx,0,LENGTH);//cleaning the buffer

        node* iter = table[numindex]; //iterator pointer = root of the hash table

        if (table[numindex] == NULL)//first appending to the hash table
        {

            table[numindex] = p; //appending "hashed" index function to the START of linked list

            p -> next = NULL; // appended function now points null

            n++;
        }
        else
        {

        table[numindex] = p;//head pointing new node

        p-> next = iter;//new node pointing to the old node

        n++;
        }
    }
    fclose(f);
    return true;
}

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

// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
    for (int i = 0; i < N; i++)
    {
        node* iterf; //forward iteration
        node* iterb; //pointer that stays behind of iterf

        iterf = table[i];
        iterb = iterf;
        while (iterb != NULL)
        {
            iterf = iterb -> next;
            free(iterb);
            iterb = iterf;
        }
    }

    return true;
}

1 个答案:

答案 0 :(得分:0)

这是我的实际代码,在我们开发代码的cs50 IDE中,此页面用作另一个文件的heather文件。因此,它可能无法自行编译。主要功能和所有内容都在其他文件之一中。但是我发现了问题。确实是sum += tolower(word[i]);解决了我的问题的哈希函数。 @ user3629249和@Johnny Mopp感谢您的漫游器的帮助。 @ user3629249也感谢您对代码设计的警告,这非常有帮助。 :)