【问题标题】:C increment counter in struct结构中的C递增计数器
【发布时间】:2013-04-03 22:41:45
【问题描述】:

我正在做一个项目,我从文件中读取单词,将它们添加到链接列表中,然后计算单词出现的频率。我的程序正在将单词读入链接列表,但它不会在每次出现重复单词时增加计数 - 计数保持在 1。我不会粘贴我的整个代码,只粘贴适用的部分。

struct node {
    struct node *next;
    char word[60];
    int wordCount;
};

以及推送功能:

void push_front (struct node **list, char * n) {

assert (list);
int count = 0;

struct node *temp = (struct node *)malloc (sizeof (struct node));

if (!temp) {
    fprintf (stderr, "Out of memory\n");
    exit (1);
}
if (list == NULL) {
    temp->next = *list;
    strcpy(temp->word, n);
    temp->wordCount+=1;
    *list = temp;
} else {
    while (list) {
        if (temp->word == n) {
            temp->wordCount+=1;
            exit(1);
        } else {
            temp->next = *list;
            strcpy(temp->word, n);
            temp->wordCount+=1;
            *list = temp;
            break;
        }
    }
}
return;
}

这个程序的一个示例运行是:

Word [0] = you, 1
Word [1] = you, 1
Word [2] = are, 1
Word [3] = a, 1
Word [4] = whipped, 1
Word [5] = what, 1
Word [6] = what, 1
Word [7] = you, 1
Word [8] = world, 1
Word [9] = you, 1
Word [10] = hello, 1

现在,您可以看到每行末尾的计数器保持在 1,但是对于每个重复的单词,它应该递增,并且重复的单词也不应该添加到链接列表中。对不起,我是 C 新手!

问候

【问题讨论】:

  • 不是完全重复的,但也许这会让人思考? stackoverflow.com/questions/3655728/…
  • 谢谢,我已经使用该技术进行增量,但仍然没有乐趣。我不知道我在这里错过了什么!

标签: c pointers auto-increment


【解决方案1】:

以下对比

if (temp->word == n) { 

是指针(地址)的比较,而不是字符串的比较

C 中的字符串比较不应该以上述方式进行

您可以使用#include <string.h> 中的strcmp

if (strcmp(temp->word, n)==0) {

您的函数包含一些需要修复的错误。我重新设计了你的功能:

void push_front (struct node **list, char * n) {    
    assert (list);
    struct node *temp = *list;

    while (temp) {
        if (strcmp(temp->word, n) == 0) {
            temp->wordCount+=1;
            return;
        }
        temp = temp->next;
    }

    temp = (struct node *)malloc (sizeof (struct node));
    if (!temp) {
        fprintf (stderr, "Out of memory\n");
        exit (1);
    }
    temp->next = *list;
    strcpy(temp->word, n);
    temp->wordCount=1;
    *list = temp;
    return;

}

在 main() 中,你的函数应该以这种方式调用

void main() {

    node *head = NULL;

    push_front (&head, "toto");
    push_front (&head, "titi");
    push_front (&head, "toto");
    push_front (&head, "titi");

    node *tmp;
    int i=0;
    for (tmp=head; tmp!=NULL; tmp = tmp->next)
        printf("Word[%d] = %s, %d\n", i++, tmp->word, tmp->wordCount);

}

我对其进行了测试,执行结果如下:

$ ./test
Word[0] = titi, 2
Word[1] = toto, 2

【讨论】:

  • 谢谢,我已经试过了。不过感谢您的指点。
  • @drizzy 我试图重写你的函数它包含一些错误
  • 非常感谢,帮了大忙!
【解决方案2】:
if (temp->word == n)

不会有帮助,因为您无法使用 == 运算符比较字符串值,请使用库函数 strcmp 并且您会得到正确的结果。另外,在这里:

else {
    while (list) {
        if (temp->word == n) {
            temp->wordCount+=1;
            exit(1);
        } else {
            temp->next = *list;
            strcpy(temp->word, n);
            temp->wordCount+=1;
            *list = temp;
            break;
        }
    }
}

当您进入 else 案例时,您尚未将 templist 链接。去做。 我也觉得应该是:while(*list)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-01
    • 2021-03-04
    • 2021-05-22
    • 1970-01-01
    • 1970-01-01
    • 2021-10-31
    • 2013-03-27
    • 1970-01-01
    相关资源
    最近更新 更多