首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我不能理解为什么checkcs50正在保存":( speller编译了预期的退出代码0,而不是2“有人可以解释吗?

我不能理解为什么checkcs50正在保存":( speller编译了预期的退出代码0,而不是2“有人可以解释吗?
EN

Stack Overflow用户
提问于 2020-10-11 23:32:25
回答 1查看 255关注 0票数 0

如果有人能帮我找出我做错了什么,我将非常高兴。我已经在其他论坛上读到过,可能是因为somo拼写代码发生了变化。但是,只有我添加的“新”东西是函数和全局指针。我不明白为什么checkcs50要告诉我;

代码语言:javascript
复制
:) dictionary.c, dictionary.h, and Makefile exist
:( speller compiles
    expected exit code 0, not 2
:| handles most basic words properly
    can't check until a frown turns upside down
:| handles min length (1-char) words
    can't check until a frown turns upside down
:| handles max length (45-char) words
    can't check until a frown turns upside down
:| handles words with apostrophes properly
    can't check until a frown turns upside down
:| spell-checking is case-insensitive
    can't check until a frown turns upside down
:| handles substrings properly
    can't check until a frown turns upside down
:| program is free of memory errors
    can't check until a frown turns upside down
代码语言:javascript
复制
#include <stdlib.h>
#include <stdbool.h>
#include <string.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];

// i created that
node *fre;

// i created that function
void unl(int i, node *tmp);

// Returns true if word is in dictionary else false
bool check(const char *word)
{
   node *midle;
midle -> next = table[13];
   // TODO
     unsigned int w = hash(word);
     node * tmp = table[w];
     if (w > 12)
     {
         tmp = midle;
     }
     if (tmp == NULL)
     {
         return false;
     }

     while (tmp != NULL)
     {
         if (strcmp(tmp -> word, word) == 0)
         {
             return true;
         }
         else
         {
             tmp = tmp->next;
         }
     }



   return false;
}

// Hashes word to a number
unsigned int hash(const char *word)
{
   unsigned int index_hash = 0;
   int c = word[0];

   if ((90 - c) >= 0)
   {
       return (90 - c);
   }
   else
   {
       return (122 - c);
   }


}

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
   FILE * text;
   text = fopen(dictionary,"r");

   if (text == NULL)
   {
       fclose(text);
       return false;
   }

   char buffer[46];
   while (fscanf (text, "%s",buffer) != EOF)
   {
       char * word = buffer;
       unsigned int index_hash = hash(word);

       if (table[index_hash] == NULL)
       {
           node * n = malloc(sizeof(node));
           if (n == NULL)
           {
               return false;
           }
           table[index_hash] = malloc(sizeof(node));
           if (table[index_hash] == NULL)
           {
               return false;
           }
           // talvez ter que colocar o '&' no n
           table [index_hash] -> next = n;
           strcpy(n -> word, word);
           n -> next = NULL;
       }
       else
       {
           node * n  = malloc(sizeof(node));
           if (n == NULL)
           {
               return false;
           }
           int c= 0;
           while (n -> next != NULL)
           {
               c++;
           }
           for (int i = 0; i < c; i++)
           {
               if (i == c-1)
               {
                   strcpy(n -> word,word);
                   n -> next = NULL;
               }
           }

       }
   }
   return true;


   // TODO

}

// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
   int size_loaded = 0;
   int size_loaded_sp = 0;

     node * tmp = table[0];
     node * tmp_midle = table[22];
     if (tmp == NULL)
     {
         return 0;
     }
     if (tmp_midle == NULL)
     {
         return 0;
     }

     while (tmp != NULL && tmp_midle != NULL)
     {
         if (tmp != NULL)
         {
             size_loaded++;
             tmp = tmp->next;
         }
         if (tmp_midle != NULL)
         {
             size_loaded_sp++;
             tmp_midle = tmp_midle-> next;
         }

     }
   // TODO
   return (size_loaded + size_loaded_sp);
}

// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
   node *tmp;
   int j = 0;
   //usar esse while pra recursividade usando esse fre

       // talvez eu deva por uma funçao e usar recursividade
   for (int i = 0; i < 46; i++)
   {
       tmp = table[i];
       while (tmp != NULL)
       {
           tmp = tmp -> next;
           j++;
       }
       for (int k = 0; k > j; k++)
       {
           if (fre == NULL)
           {
               unl(i,tmp);
           }
           else
           {
               tmp = fre;
               unl(i,tmp);
           }
       }
   }

   // TODO
   return false;
}
void unl(int i, node *tmp)
{
   free(tmp);
   tmp = table[i];

   while (tmp != NULL)
       {
           fre = tmp;
           tmp = tmp -> next;
       }

}```
EN

回答 1

Stack Overflow用户

发布于 2021-02-05 22:49:41

确保您没有更改speller.cdictionary.h中的任何内容。我犯了这个错误,这就是这个错误的原因。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64305812

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档