我以某种方式改变ctype.h?
问题描述:
#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;
}
当我运行我的代码,我收到此错误信息:我以某种方式改变ctype.h?
在文件中包含从/usr/include/ctype.h:28:0, 从speller.c:10: 在/ usr/include/bits/types.h:31:1:error:expected'=',',';','asm'或'attribute'before'typedef'
这是否意味着我有以某种方式设法改变ctype.h库?如果是这样,我该如何解决它?
答
啊哈!戈登,你解决了它!我发现在#include的正上方输入了一些导致问题的零散字符。谢谢!
+3
上面没有流浪的角色。当在问题中发布代码时,请请发布您使用的**实际代码**。 – 2012-07-31 02:19:23
你看过ctype.h,在第28行左右?你看到有什么不对吗? – abelenky 2012-07-31 01:03:13
嗯...我会查找如何打开它在ctype.h并让你知道... – hannah 2012-07-31 01:05:58
这是你使用的确切代码(它说'speller.c:10',但'ctype.h'看起来就像它在第5行)?当我在包含之前忘记了分号或其他东西时,我遇到了这种类型的错误。 – 2012-07-31 01:06:23