【问题标题】:My if statement is doing the opposite of what I want it to do我的 if 语句与我希望它做的相反
【发布时间】:2019-12-05 14:15:40
【问题描述】:

我有一个程序要求用户输入一个单词,他们输入的每个单词都被添加到一个链表中。当用户输入"END" 时,程序应该列出所有节点。

我的问题是程序只将单词"END"添加到列表中,当用户输入其他任何内容时,触发else条件:打印出列表中的所有项目,但所有这些单词都只是"END".

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

struct node {
  char word[32];
  struct node *next;
};

int main() {
  struct node *head = NULL, *cur = NULL;
  char input[32];

  while(1) {
    cur = malloc(sizeof(struct node));

    printf("Enter words: ");
    scanf("%s", input);

    if (strcmp(input, "END") == 0) {
      cur->next = head;
      strcpy(cur->word, input);
      head = cur;
    } else {
      struct node *iter = head;

      while (iter != NULL) {
        printf("Contents: %s\n", iter->word);
        iter = iter->next;
      }
    }
  }
}

通过 if 语句检查条件 == 1 是否只是让用户继续输入单词,而不管用户输入什么,例如"END"

任何帮助将不胜感激。

【问题讨论】:

  • 然后反转ifstrcmp 不保证返回 1 或 0。它可以返回 0 或任何其他值。检查strcmp(...) != 0
  • strcmp 在字符串匹配时返回0。当用户输入“END”时,你需要break退出循环。
  • @CodeCharmander 谢谢,那行得通。但我仍然不明白为什么。你能解释一下吗?
  • strcmp() 如果字符串匹配,则返回零,如果第一个字符串小于第二个字符串,则返回负数,如果第一个字符串大于第二个字符串,则返回正数。这是一个字典比较,而不仅仅是一个相等比较。
  • 啊,我没有意识到它是如何工作的。我也刚google了一下。我只是认为它要么相等要么不相等,而不是它返回一个值。

标签: c if-statement while-loop linked-list strcmp


【解决方案1】:

if 语句中的条件

if (strcmp(input, "END") == 0)

表示存储在数组input 中的字符串等于字符串文字"END"。因此,如果数组包含字符串 "END",则您将在列表中插入一个新节点。

此外,您必须在此检查之后而不是之前为新节点分配内存。否则会出现内存泄漏。

注意你有一个无限循环。

你需要释放所有分配的内存。

你的意思好像是这样的

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

#define N   32

struct node 
{
    char word[N];
    struct node *next;
};

int main(void) 
{
    struct node *head = NULL;
    char input[N];

    printf( "Enter words: " );

    while ( scanf( "%31s", input ) == 1 )
    {
        char tmp[N];

        size_t i = 0;

        while ( ( tmp[i] = toupper( ( unsigned char )input[i] ) ) != '\0' ) ++i;

        if ( strcmp( tmp, "END" )  == 0 ) break;

        struct node *current = malloc( sizeof( struct node ) );
        strcpy( current->word, input );
        current->next = head;
        head = current;
    }

    for ( struct node *current = head; current != NULL; current = current->next )
    {
        printf( "%s -> ", current->word );
    }

    puts( "NULL" );

    while ( head != NULL )
    {
        struct node *current = head;
        head = head->next;
        free( current );
    }

    return 0;
}

程序输出如下所示

Enter words: Jackson Jake Hello end
Hello -> Jake -> Jackson -> NULL

【讨论】:

    【解决方案2】:

    首先,您需要在比较非零时使用strcmp() != 0 将字符串添加到列表中,直到输入字符串为END

    其次,创建当前cur 节点,其中包含新字符串添加需要有一个尾指针用于添加新字符串,如下所示使用next 节点。同样对于节点,您需要确保 head 始终位于列表的开头。

    第三,当列表打印成功时你需要break,否则你将永远结束while(1)循环。

    int main() {
      struct node *head = NULL, *next = NULL, *cur = NULL;
      char input[32];
    
      while(1) {
        printf("Enter words: ");
        scanf("%s", input);
    
        if (strcmp(input, "END") != 0) {
          cur = malloc(sizeof(struct node));
          cur->next = NULL;
          strcpy(cur->word, input);
          if (head == NULL) {
            head = cur;
            next = head;
          }
          next->next = cur;
        } else {
          struct node *iter = head;
          while (iter != NULL) {
            printf("Contents: %s\n", iter->word);
            iter = iter->next;
          }
          break;
        }
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-16
      • 2017-03-25
      • 1970-01-01
      • 2021-12-19
      • 1970-01-01
      • 1970-01-01
      • 2018-11-02
      • 1970-01-01
      相关资源
      最近更新 更多