Cs50 pset5 - 程序崩溃

发布于 2025-01-17 21:36:51 字数 3159 浏览 4 评论 0原文

我完成了拼写器代码的编写。它编译得很好,但唯一打印的输出是“拼写错误的单词”。拼写错误的单词、文本中的单词、字典中的单词不会被打印。我假设是因为程序在那之前崩溃了?这是我的代码。如果我知道我的问题出在哪个功能或领域,我也许就能解决它。另外,我的哈希函数是根据单词的前两个字母来建立哈希索引。

// Implements a dictionary's functionality

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

#include "dictionary.h"

int dsize = 0;

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

// TODO: Choose number of buckets in hash table
const unsigned int N = 676;

// Hash table
node *table[N];

// Returns true if word is in dictionary, else false
bool check(const char *word)
{
    // TODO
    // hash word
    int hashnumber = hash(word);
    node *cursor = table[hashnumber];
    // traversing linked list at that hash number
    while(cursor != NULL)
    {
        if((strcasecmp(cursor->word, word) == 0))
        {
            return true;
        }
    }
    return false;
}

// Hashes word to a number
unsigned int hash(const char *word)
{
    int hash;
    // TODO: Improve this hash function
    for( int i = 97; i < 123; i ++)
    {
        hash = (i - 97) * 26;
        // check asciivalue both uppercase and lowercase for first letter
         if(word[0] == (char)i || word[0] == (char)(i - 32) )
         {
             for ( int j = 97; j < 122; j++)
              {
                  // check asciivalue both uppercase and lowercase for second letter
                  if(word[1] == (char)j || word[1] == (char)(j - 32))
                  {
                      hash = hash + (j - 97);
                  }

              }
         }

    }


    return hash;
}

// Loads dictionary into memory, returning true if successful, else false
bool load(const char *dictionary)
{
    for( int i = 0; i < N; i++)
    {
        table[i] = NULL;
    }

     FILE *input = fopen(dictionary, "r");

    if (dictionary== NULL)
    {
        return false;
    }

    node *temp;
    char word[LENGTH + 1];
    while((fscanf(input, "%s", word)) != EOF)
    {

       temp = malloc(sizeof(node));
       if(temp == NULL)
       {
           return false;
       }


       strcpy(temp->word, word);
       int hashnumber = hash(word);
       if (table[hashnumber] == NULL)
       {
           table[hashnumber] = temp;
       }
       else
       {
           temp->next = table[hashnumber];
           table[hashnumber] = temp;
       }
       dsize++;
    }
    fclose(input);

return true;
}

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

// Unloads dictionary from memory, returning true if successful, else false
bool unload(void)
{
    // TODO
node *temp;
node *cursor;
    for ( int i = 0; i < N; i++ )
    {
        cursor = table[i];

        while(table[i] != NULL)
        {
            temp = cursor;
            cursor = cursor->next;
            free(temp);
        }
        if(cursor == NULL && i == (N - 1))
        {
            return true;
        }

    }
    return false;
}

I finished writing my code for speller. It compiles fine but the only output being printed is "Misspelled words". The words misspelled , words in text, words in dictionary does not get printed. I'm assuming its because the program crashes before then? Here is my code. If only I knew in which function or area my problem lies I might be able to fix it. Also, my hash function is to base hash indexes base on the first two letters of the word.

// Implements a dictionary's functionality

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

#include "dictionary.h"

int dsize = 0;

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

// TODO: Choose number of buckets in hash table
const unsigned int N = 676;

// Hash table
node *table[N];

// Returns true if word is in dictionary, else false
bool check(const char *word)
{
    // TODO
    // hash word
    int hashnumber = hash(word);
    node *cursor = table[hashnumber];
    // traversing linked list at that hash number
    while(cursor != NULL)
    {
        if((strcasecmp(cursor->word, word) == 0))
        {
            return true;
        }
    }
    return false;
}

// Hashes word to a number
unsigned int hash(const char *word)
{
    int hash;
    // TODO: Improve this hash function
    for( int i = 97; i < 123; i ++)
    {
        hash = (i - 97) * 26;
        // check asciivalue both uppercase and lowercase for first letter
         if(word[0] == (char)i || word[0] == (char)(i - 32) )
         {
             for ( int j = 97; j < 122; j++)
              {
                  // check asciivalue both uppercase and lowercase for second letter
                  if(word[1] == (char)j || word[1] == (char)(j - 32))
                  {
                      hash = hash + (j - 97);
                  }

              }
         }

    }


    return hash;
}

// Loads dictionary into memory, returning true if successful, else false
bool load(const char *dictionary)
{
    for( int i = 0; i < N; i++)
    {
        table[i] = NULL;
    }

     FILE *input = fopen(dictionary, "r");

    if (dictionary== NULL)
    {
        return false;
    }

    node *temp;
    char word[LENGTH + 1];
    while((fscanf(input, "%s", word)) != EOF)
    {

       temp = malloc(sizeof(node));
       if(temp == NULL)
       {
           return false;
       }


       strcpy(temp->word, word);
       int hashnumber = hash(word);
       if (table[hashnumber] == NULL)
       {
           table[hashnumber] = temp;
       }
       else
       {
           temp->next = table[hashnumber];
           table[hashnumber] = temp;
       }
       dsize++;
    }
    fclose(input);

return true;
}

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

// Unloads dictionary from memory, returning true if successful, else false
bool unload(void)
{
    // TODO
node *temp;
node *cursor;
    for ( int i = 0; i < N; i++ )
    {
        cursor = table[i];

        while(table[i] != NULL)
        {
            temp = cursor;
            cursor = cursor->next;
            free(temp);
        }
        if(cursor == NULL && i == (N - 1))
        {
            return true;
        }

    }
    return false;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文