动不了一点 2024-03-12 23:24 采纳率: 17.2%
浏览 1
已结题

输入2输入父亲后就系统报错

输入2找父亲时报错


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

struct node { //结构体变量
    char data[20];//家族成员
    int n;//辈分
    char married[5];
    node* lchild, * rchild;//左孩子和右孩子
};
struct node nodetree;
struct node* head = &nodetree;

void showmenu() {//显示页面
    puts("*******二叉树家谱管理系统*************************************");
    puts("**************************************************************");
    puts("*********菜单如下: *******************************************");
    puts("*********1.创 建 家 谱 祖 先                        **********");
    puts("*********2.添 加 家 族 成 员 (孩子)               *********");
    puts("*********3.查 找 指 定 成 员 及 辈 分               *********");
    puts("*********4.删 除 家 庭 成 员                        *********");
    puts("*********5.打 印 一 系 成 员                        *********");
    puts("*********6.清 空 族 谱 管 理 系 统                 *********");
    puts("*********0.退 出 族 谱 管 理 系 统                 *********");
    puts("*************************************************************");
    puts("*************************************************************");
    puts("*************************************************************");
    puts("*************************************************************");
    puts("提示:请按数字进行操作^-^");
}

void  create() { //建立祖先

    printf("输入家谱祖先名字:");
    scanf("%s", nodetree.data);
    puts("是否已婚(yes or no)");
    scanf("%s", nodetree.married);
    nodetree.n = 1;
    nodetree.lchild = NULL;
    nodetree.rchild = NULL;
    puts("祖先载入成功!");
    system("pause");
    system("cls");
}

int search(struct node* root, char* target) {
    if (root == NULL) {
        return 0;
    }
    if (strcmp(root->data, target) == 0) {
        printf("已找到%s,他为第%d代子孙!\n", root->data, root->n);
        return 1;
    }
    return search(root->lchild, target) || search(root->rchild, target);
    system("pause");
    system("cls");
}

int add(struct node* newhead1, struct node* onechild, char s[20]) {

    if (strcmp(newhead1->data, s) == 0) {
        if (newhead1->lchild == NULL) {
            newhead1->lchild = onechild;
            newhead1->lchild->n = newhead1->n + 1;
        }
        else {
            newhead1->rchild = onechild;
            newhead1->rchild->n = newhead1->n + 1;
        }
        return 1;
    }
    return add(newhead1->lchild, onechild, s) || add(newhead1->rchild, onechild, s);
    system("pause");
    system("cls");
}

void print(struct node* newhead, char x[]) {

    if (strcmp(newhead->data, x) == 0) {
        if (strcmp(newhead->data, "0") != 0)
            printf("%s-", newhead->data);
    }
    print(newhead->lchild, x);
    print(newhead->rchild, x);
}
void clearall(struct node* head) {
    strcpy(head->data, "0");
    head->lchild = NULL;
    head->rchild = NULL;
}

void add_child() {
    char s[20];
    struct node* newhead1 = head;
    struct node* onechild;
    printf("请输入需要插入进的父亲节点:");
    scanf("%s", s);
    int n = search(newhead1, s);
    if (n == 0) {
        printf("查无此人!!\a\n");
        system("pause");
        system("cls");
    }
    else {
        printf("请输入孩子名字:");
        scanf("%s", onechild->data);
        printf("是否结婚(yes or no)");
        scanf("%s", onechild->married);
        onechild->lchild = NULL;
        onechild->rchild = NULL;

        int mm = add(newhead1, onechild, s);
    }
}

int main() {

    int select;
    char s[20];
    while (true) {
        showmenu();//显示菜单页面
        printf("请输入您需要选择的功能:");
        scanf("%d", &select);

        switch (select) {
        case 1: {//创建祖先 (已完成)
            create();
            break;
        }
        case 2: {//添加孩子(yiwancheng)
            add_child();
            break;
        }
        case 3: {// 输出 指 定 成员
            struct node* newhead = head;
            printf("请输入需要查找的节点:");
            scanf("%s", s);
            search(newhead, s);
            int m = search(newhead, s);
            if (m == 0)printf("查无此人!!\a\n");
            system("pause");
            system("cls");
            break;
        }
              //            case 4 : {
              //                    printf("请输入要删除的成员:");
              //                    scanf("%s",&s);
              //                    shanchu(head,s);
              //                    break;
              //                }
        case 5: {//打印一系
            char ss[20];
            struct node* newnewhead = head;
            printf("请输入打印一系的父代名字:");
            scanf("%s", ss);
            print(newnewhead, ss);
            break;
        }
        case 6: {//清空家谱(已完成)
            clearall(head);
            break;
        }
        case 0: {//退出(已完成)
            printf("感谢使用本系统");
            return 0;
        }
        }

    }
    return 0;
}

img

```

  • 写回答

3条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2024-03-12 23:24
    关注

    引自免费微信小程序:皆我百晓生

    在您的代码中,问题出在add_child()函数的实现上。当您尝试从用户输入读取孩子信息时,onechild指针并没有被初始化就直接使用了。这导致了运行时错误,提示变量未初始化。

    为了解决这个问题,请在调用scanf之前为onechild分配内存。由于onechild是一个指向struct node的指针,您需要使用malloc来动态分配内存空间:

    void add_child() {
        char s[20];
        struct node* newhead1 = head;
        struct node* onechild = (struct node*)malloc(sizeof(struct node)); // 添加这一行,为onechild分配内存
    
        printf("请输入需要插入进的父亲节点:");
        scanf("%s", s);
        int n = search(newhead1, s);
        if (n == 0) {
            printf("查无此人!!\a\n");
            system("pause");
            system("cls");
        }
        else {
            printf("请输入孩子名字:");
            scanf("%s", onechild->data);
            printf("是否结婚(yes or no)");
            scanf("%s", onechild->married);
            onechild->lchild = NULL;
            onechild->rchild = NULL;
    
            int mm = add(newhead1, onechild, s);
        }
    
        // 当不再需要onechild时,记得释放内存以防止内存泄漏
        free(onechild); 
    }
    

    同时,在成功添加子节点后,为了保持程序的良好习惯和避免内存泄漏,我在上面的代码末尾添加了free(onechild)来释放为onechild分配的内存。请确保在实际应用中根据程序逻辑适当地进行内存管理。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

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

悬赏问题

  • ¥15 QQ邮箱过期怎么恢复?
  • ¥15 (标签-android|关键词-app)
  • ¥15 微信小程序web-view嵌套H5页面IOS左滑会出现相同的页面,有什么解决方法吗?
  • ¥60 如何批量获取json的url
  • ¥15 comsol仿真压阻传感器
  • ¥15 Python线性规划函数optimize.linprog求解为整数
  • ¥15 llama3中文版微调
  • ¥15 pg数据库导入数据序列重复
  • ¥15 三分类机器学习模型可视化分析
  • ¥15 本地测试网站127.0.0.1 已拒绝连接,如何解决?(标签-ubuntu)