2201_75616673 2022-12-03 11:20 采纳率: 100%
浏览 34
已结题

能帮我看看哪错了吗?

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node{
char code[8];
struct node *next;
};
int main(void)
{
char str[8];
int count, i, n, size = sizeof(struct node);
struct node *head, *p;

head = NULL;
gets(str);
/* 按输入数据的逆序建立链表 */
while(strcmp(str, "#") != 0){
    p = (struct node *)malloc(size);
    strcpy(p->code, str);
    head = p->next;
    head = p; 
    gets(str);
}
count = 0;
for(p = head; p->next != NULL; p = p->next) {
    if(p->code[1] == '0' && p->code[2] == '2') {
        count++ ;
    }
}
printf("%d\n", count);

return 0;

}

  • 写回答

1条回答 默认 最新

  • Huazie 优质创作者: 编程框架技术领域 2022-12-03 15:10
    关注

    修改两处,看截图

    img

    代码参考如下:

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    struct node{
        char code[8];
        struct node *next;
    };
    int main(void)
    {
        char str[8];
        int count, i, n, size = sizeof(struct node);
        struct node *head, *p;
    
        head = NULL;
        gets(str);
        /* 按输入数据的逆序建立链表 */
        while(strcmp(str, "#") != 0){
            p = (struct node *)malloc(size);
            strcpy(p->code, str);
            p->next = head;
            head = p; 
            gets(str);
        }
    
        count = 0;
        for(p = head; p != NULL; p = p->next) {
            if(p->code[1] == '0' && p->code[2] == '2') {
                count++ ;
            }
    
        }
        printf("%d\n", count);
    
        return 0;
    }
    
    

    如有帮助,欢迎采纳哈!

    在这里插入图片描述

    本人的开源项目,欢迎star支持下!!!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 10月20日
  • 已采纳回答 10月12日
  • 创建了问题 12月3日