我的程序通过getchar()读取输入,使用二叉树结构保存每个单词的出现次数,然后在最后打印它们。任何只包含数字、下划线和字母且不以数字开头的非空字符集都算作一个单词。当我把First_word First_word First_word Second_word Third_word Forth_word
这一行放在一个名为file.input
的文件中,并通过命令./a.out <file.input
运行程序时,我得到的输出是:3 First_word
虽然应该有其他的词。
我已经测试过了,getword
和print
工作正常。这是我写的代码:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
struct bt_node {
char *word;
int count;
struct bt_node *left;
struct bt_node *right;
};
int main()
{
extern struct bt_node *first;
int getword(char *, int);
struct bt_node *search(char *word, struct bt_node *node), *node = NULL;
void print(struct bt_node *node);
int lim = 100;
char word[lim];
while (getword(word, lim) != EOF) {
if (!(isalpha(word[0]) || word[0] == '_'))
continue;
search(word, node)->count++;
node = first;
}
print(first);
return 0;
}
void print(struct bt_node *node)
{
if (node->left != NULL)
print(node->left);
printf("%4d\t%s\n", node->count, node->word);
if (node->right != NULL)
print(node->right);
}
struct bt_node *search(char *word, struct bt_node *node)
{
struct bt_node *generate_node(char *word);
if (node == NULL)
node = generate_node(word);
int comp = strcmp(word, node->word);
if (comp == 0)
return node;
else
return search(word, 0 < comp ? node->right : node->left);
}
int getword(char *word, int lim)
{
int c, temp, getch(void);
void ungetch(int);
char *w = word;
pass_space: /* if encountered a comment or string skip it and go back to this step */
while (isspace(c = getch()))
;
if (c == '/') {
if ((temp = getch()) == '*') {
while ((c = getch()) != EOF)
if (c == '*' && (c = getch()) == '/')
goto pass_space;
else if (c == '*')
ungetch(c);
} else
ungetch(temp);
} else if (c == '\"') {
while ((c = getch()) != EOF)
if (c == '\"')
goto pass_space;
}
if (c != EOF)
*w++ = c;
if (!isalpha(c) && c != '_') {
*w = '\0';
return c;
}
for ( ; --lim > 0; w++)
if (!isalnum(*w = getch()) && *w != '_') {
ungetch(*w);
break;
}
*w = '\0';
return word[0];
}
#define BUFSIZE 100
int buf[BUFSIZE];
char bufp = 0;
int getch(void)
{
return bufp > 0 ? buf[--bufp] : getchar();
}
void ungetch(int c)
{
if (bufp < BUFSIZE)
buf[bufp++] = c;
else
printf("ungetch: too many characters, didn't unread %c\n", c);
}
struct bt_node *generate_node(char *word)
{
struct bt_node *bt_alloc(void);
char *alloc_char(int);
struct bt_node *node = bt_alloc();
node->word = alloc_char(strlen(word)+1);
strcpy(node->word, word);
node->count = 0;
node->left = NULL;
node->right = NULL;
return node;
}
#define BTALLOCSIZE 10000
static struct bt_node bt_allocbuf[BTALLOCSIZE];
static struct bt_node *bt_allocp = bt_allocbuf;
struct bt_node *first = bt_allocbuf;
struct bt_node *bt_alloc(void)
{
if (bt_allocp <= bt_allocbuf + BTALLOCSIZE)
return bt_allocp++;
else {
printf("bt_alloc - error: can't allocate\n");
return NULL;
}
}
#define ALLOCSIZE 10000
static char char_allocbuf[ALLOCSIZE];
static char *char_allocp = char_allocbuf;
char *alloc_char(int n)
{
if (char_allocbuf + ALLOCSIZE - char_allocp >= n) {
char_allocp += n;
return char_allocp - n;
} else {
printf("alloc_char - error: can't allocate\n");
return NULL;
}
}
1条答案
按热度按时间0ejtzxu11#
但问题是,在这个函数中:
我将返回的指针
generate_node
赋给变量node
,因为传递值的关系,我认为它会将指针赋给node->left或node->right。结果,生成并保存了结构,但它们的地址没有分配给子节点指针,node->left
和node->right
仍然是NULL
。通过这种方式更改函数有助于:现在我只需要修改函数声明,使其与新定义相对应,并将main中的调用
search(word, node)->count++;
更改为search(word, &node)->count++;