如何计算节点中的单词数?

问题描述

我正在研究cs50 pset5拼写器,并且正在研究size函数,在这里您必须计算字典中的单词数。我试图计算节点的每个部分中有多少东西(请参阅What is a node in C?),然后将它们加在一起以得出总数。但是我不知道该怎么做。到目前为止,这是我的代码:

// Implements a dictionary's functionality

#include <ctype.h>
#include <stdio.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <string.h>
#include <stdlib.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 = 26;

// Hash table
node *table[N];

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    // TODO
    return false;
}

// Hashes word to a number
unsigned int hash(const char *word)
{
    // TODO
    for(int i = 65; i < N - 1 + 65; i++)
    {
        char c = i;
       if(word[0] == c)
       {
           return i;
       }

    }
    return 0;
}

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

     }
     FILE* word2 = NULL;
     char* wordread = NULL;
     fopen("r",dictionary);

     while(wordread)
     {

     fscanf(word2,"%s",wordread );
     node *n = malloc(sizeof(node));
     strcpy(n->word,wordread);
     n->next = NULL;
     const char *word = wordread;
     
    unsigned int hash(const char *word);
 

     }

    return false;
}

// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
   int count = 0;
    // TODO
     for(int i = 0; i < 26; i++)
    {
        for(int j = 0; j < strlen(table[i]); j++)
        {
            count++;
        }
    }
    return 0;
}

// Unloads dictionary from memory,returning true if successful else false
bool unload(void)
{
    
    // TODO
   return 1;
    
    return count;
}

关注大小功能。我仅添加了其他代码,因此您可以了解一些变量。我尝试使用谷歌搜索,但是这个问题非常具体,我什么也找不到。我还必须保持函数定义不变,这也是一个问题,例如,我不能从中更改size函数:

unsigned int size(void)

对此:

unsigned int size(const char *dictionary)

如果您对如何修改代码有任何想法,即使该错误位于其他功能中,也请告诉我。 谢谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)