gpt4 book ai didi

c - 我是否以某种方式更改了 ctype.h?

转载 作者:太空宇宙 更新时间:2023-11-04 07:33:01 24 4
gpt4 key购买 nike

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

#include "dictionary.h"

#define HASH_SIZE 5

// prototype
int hash(char *word);

// counter
int counter;

// node
typedef struct node
{
char word[LENGTH + 1];
struct node *next;
} node;

// hash table
struct node *hashtable[HASH_SIZE];



/*
* Returns true if word is in dictionary else false.
*/

bool
check(const char *word)
{
//make a writable word (palabra)
int len = strlen(word);
char palabra[len + 1];
//int i;

// make la palabra all lowercase letters
for ( int i = 0; i < len; i++)
{
if (isalpha(palabra[i]) || palabra[i] == '\'')
palabra[i] = tolower(word[i]);
}

//palabra[i] = (char) "\0";

// hash the word
int value = hash(palabra);

// let's look at the first node in the bucket
struct node *n;
if (hashtable[value] == NULL)
return false;
else
n = hashtable[value];

// iterate through the bucket to see if the word is there
while (strcmp(palabra, n->word) != 0 && n->next != NULL)
{
n = n->next;
}


// if the word is found, print true, else false
if ( strcmp(palabra, n->word) ==0 )
return true;
else
return false;
}


/*
* Loads dictionary into memory. Returns true if successful else false.
*/

bool
load(const char *dictionary)
{
// open the dictionary
FILE *dict = fopen(dictionary, "r");
if(dict == NULL)
{
printf("Could not open %s.\n", dictionary);
return false;
}

// set all values in the hash table to null
for(int i = 0; i < HASH_SIZE; i++)
{
hashtable[i] = NULL;
}

// set the counter to 0
counter = 0;

// iterate through the words in the dictionary
while (!feof(dict))
{
//declare a node
node *n = malloc( sizeof(node) );

// copy the word into the node
fscanf(dict, "%s", n->word);

// hash the word, baby!
int hash_value = hash(n->word);

// start saving addresses to the hashtable
n->next = hashtable[hash_value];
hashtable[hash_value] = n;

// that's one more!
counter++;
}

// testing
printf("Starting the test.\n");
for ( int i = 0; i < HASH_SIZE; i++)
{
struct node *q = hashtable[i];
while (q != NULL)
{
printf("%s\n", q->word);
q = q->next;
}
}
printf("Ending the test.\n");

fclose(dict);

return true;
}


/*
* Returns number of words in dictionary if loaded else 0 if not yet loaded.
*/

unsigned int
size(void)
{
return counter;
}


/*
* Unloads dictionary from memory. Returns true if successful else false.
*/

bool
unload(void)
{
// TODO
return false;
}


/*
* Returns a hash value for a word.
*/

int
hash(char *word)
{
// hash value and length of word
int value = 0;
int len = strlen(word);

// iterate through letters, adding ASCII values
for(int i = 0; i < len; i++)
{
int letter = (int) word[i];
value += letter;
}

// make sure the value is less than 100 & return
value = value%HASH_SIZE;
return value;
}

当我运行我的代码时,我收到此错误消息:

在/usr/include/ctype.h:28:0 包含的文件中, 来自 speller.c:10:/usr/include/bits/types.h:31:1: 错误:在 'typedef' 之前应为 '='、'、'、';'、'asm' 或 'attribute'

这是否意味着我以某种方式设法改变了 ctype.h 库?如果是这样,我该如何解决?

最佳答案

啊哈!戈登,你解决了!我发现在 #include 上方键入了一些杂散字符,这些字符导致了问题。谢谢!

关于c - 我是否以某种方式更改了 ctype.h?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11731741/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com