渴望变聪明的小笨蛋
2021-12-27 09:31
采纳率: 85.7%
浏览 36

请问显示getch会出现问题是为什么啊

问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include<conio.h> 
typedef char TElemType;
typedef int status;
typedef struct BiTPNode {
    TElemType data[10];
    struct BiTPNode* parent, * lchild, * rchild; //父亲及左右孩子指针
}BiTPNode, * BiPTree;
BiPTree P;
BiPTree T;

//家谱的创建
int Cre()
{
    system("cls");
    FILE* fp; //声明指向文件的指针
    char filename[40], str[10];
    printf("请输入家谱名称:");
    getchar();
    gets(filename); //输入家谱名称
    while (filename[0] == NULL)
    {
        printf("家谱名不能为空,请重新输入:");
        gets(filename);
    }
    if ((fp = fopen(filename, "w")) == NULL)
    {
        printf("%s家谱创建失败!\n", filename);
        return 0;
    }
    printf("请输入家谱内容:\n");
    while (strlen(gets(str)) > 0)
    {
        fputs(str, fp); //向文件写入字符串
        putc('\n', fp);
    }
    fclose(fp); //关闭文件
    printf("按任一键继续!");
    getch();
    return 1;
}

status loc(BiPTree T, BiPTree* P, TElemType name[10]) {
    if (T)
    {
        P = T;
        //字符串的比较
        if (!strcmp(name, T->data)) return 1;
        if (loc(T->lchild, P, name)) return 1;
        if (loc(T->rchild, P, name)) return 1;
    }
    else
        return 0;
}

//构造二叉树
status inittree(BiPTree T) 
{
    T = (BiTPNode*)malloc(sizeof(BiTPNode));
    if (T)
        return 0;
    T->lchild = NULL;
    T->rchild = NULL;
    T->parent = NULL;
    return 1;
}


//载入家谱
status Crt(BiPTree  T) 
{
    FILE* fp;
    BiPTree Q, R, M, N;
    char filename[40], name[10];
    system("cls"); //清屏
    R = (BiTPNode*)malloc(sizeof(BiTPNode)); //分配存储空间
    M = (BiTPNode*)malloc(sizeof(BiTPNode));
    N = (BiTPNode*)malloc(sizeof(BiTPNode));
    printf("请输入家谱名:");
    getchar();
    gets(filename);
    while (filename[0] == NULL)
    {
        printf("家谱名不能为空,请重新输入:");
        gets(filename);
    }
    if ((fp = fopen(filename, "r")) == NULL)
    {
        printf("%s家谱打开失败!\n", filename);
        return 0;
    }
    inittree(T);
    fscanf(fp, "%s", name); //从文件读入姓名
    strcpy(T->data, name);
    T->lchild = NULL;
    T->rchild = NULL;
    T->parent = NULL;
    fclose(fp);
    if ((fp = fopen(filename, "r")) == NULL)
    {
        printf("%家谱打开失败!\n", filename);
        return 0;
    }
    fscanf(fp, "%s", name);
    while (!feof(fp)) {
        if (loc(T, P, name)) {
            fscanf(fp, "%s", name);
            Q = (BiTPNode*)malloc(sizeof(BiTPNode));
            strcpy(Q->data, name);
            P->lchild = Q; //构建孩子
            Q->parent = P;
            Q->lchild = NULL;
            Q->rchild = NULL;
            N = P;
        }
        else if (!loc(T, P, name)) {
            Q = (BiTPNode*)malloc(sizeof(BiTPNode));
            R = N;
            R = R->lchild;
            while (R) {
                M = R;
                R = R->rchild;
            }
            strcpy(Q->data, name);
            M->rchild = Q;
            Q->parent = M;
            Q->lchild = NULL;
            Q->rchild = NULL;
        }
        fscanf(fp, "%s", name);
    }
    printf("信息载入成功,按任一键继续!");
    getch();
    return 1;
}

//添加成员
status in(BiPTree* T)
{
    char father[10], name[10];
    BiPTree Q, M;
    system("cls");
    printf("请输入要添加到该家谱中的人的父亲姓名:");
    getchar();
    gets(father);
    while (!loc(T, P, father)) {
        printf("%s不在该家谱中!请重新输入:", father);
        gets(father);
    }
    printf("请输入要添加到该家谱中的人的姓名:");
    gets(name);
    Q = (BiTPNode*)malloc(sizeof(BiTPNode));
    M = (BiTPNode*)malloc(sizeof(BiTPNode));
    strcpy(Q->data, name);
    Q->lchild = NULL;
    Q->rchild = NULL;
    if (!P->lchild) {
        P->lchild = Q;
        Q->parent = P;
    }
    else {
        P = P->lchild;
        while (P) {
            M = P;
            P = P->rchild;
        }
        M->rchild = Q;
        Q->parent = M;
    }
    printf("成员添加成功,按任一键继续!");
    getch();
    return 1;
}


//删除成员
status de(BiPTree* T) {
    char name[10];
    system("cls");
    printf("请输入要删除的人的姓名:");
    getchar();
    gets(name);
    while (!loc(T, P, name)) {
        printf("%s不在该家谱中!请重新输入:", name);
        gets(name);
    }
    if (!P->rchild) {
        if (P->parent->lchild == P)
            P->parent->lchild = NULL;
        else
            P->parent->rchild = NULL;
        free(P);
    }
    else if (P->rchild) {
        if (P->parent->lchild == P)
            P->parent->lchild = P->rchild;
        else
            P->parent->rchild = P->rchild;
        free(P);
    }
    printf("成员删除成功,按任一键继续!");
    getch();
    return 1;
}


status Show(TElemType e[10]) {
    printf("%s  ", e);
    return 1;
}


//二叉树的遍历
status pre(BiPTree T, status(*visit)(TElemType[10])) {
    if (T) {
        if ((*visit)(T->data))
            if (pre(T->lchild, visit))
                if (pre(T->rchild, visit)) return 1;
        return 0;
    }
    else return 1;
}


//家族成员查询
status Sea(BiPTree T) {
    char name[10];
    BiPTree N;
    N = (BiTPNode*)malloc(sizeof(BiTPNode));
    system("cls");
    printf("请输入要查寻的人的姓名:");
    getchar();
    gets(name);
    while (!loc(T, P, name)) {
        printf("%s不在该家谱中!请重新输入:", name);
        gets(name);
    }
    N = P;
    if (P == T)
        printf("%s的父亲在该家谱中没有记载!\n", P->data);
    else {
        while (N->parent->rchild == N)
            N = N->parent;
        printf("%s的父亲是:%s\n", P->data, N->parent->data);
    }
    N = P;
    if (P == T)
        printf("%s没有兄弟!\n", P->data);
    else if (!P->rchild && P->parent->rchild != P)
        printf("%s没有兄弟!\n", P->data);
    else {
        printf("%s的兄弟有:\n", name);
        while (N->rchild) {
            printf("%s ", N->rchild->data);
            N = N->rchild;
        }
        N = P;
        while (N->parent->rchild == N) {
            printf("%s ", N->parent->data);
            N = N->parent;
        }
        printf("\n");
    }
    if (P == T)
        printf("%s的祖先在该家谱中没有记载!\n", name);
    else
        printf("%s的祖先是:%s\n", name, T->data);
    N = P;
    if (!P->lchild) {
        printf("%s没有孩子!\n", name);
        printf("%s没有后代\n", name);
    }
    else {
        printf("%s的孩子有:\n", name);
        printf("%s ", P->lchild->data);
        N = N->lchild;
        while (N->rchild) {
            printf("%s ", N->rchild->data);
            N = N->rchild;
        }
        printf("\n");
        printf("%s的后代有:\n", name);
        pre(P->lchild, Show);
        printf("\n");
    }
    printf("按任一键继续!");
    getch();
    return 1;
}

//文件的创建
status write(BiPTree T, char filename[40]) {
    FILE* fp;
    if ((fp = fopen(filename, "a+")) == NULL)
    {
        printf("%s文件创建失败!\n", filename);
        return 0;
    }
    fprintf(fp, "%s ", T->data);
    T = T->lchild;
    while (T) {
        fprintf(fp, "%s ", T->data);
        T = T->rchild;
    }
    fprintf(fp, "\n"); //输出
    fclose(fp);
    return 1;
}

status prewrite(BiPTree T, status(*visit)(BiPTree, char[40]), char filename[40]) {
    if (T) {
        if (T->lchild)
            (*visit)(T, filename);
        prewrite(T->lchild, visit, filename);
        prewrite(T->rchild, visit, filename);
        return 1;
    }
    else return 1;
}




status wrong()
{
    char a;
    scanf("%c", &a);
    printf("无此选项,请重新选择!(按任一键继续!)");
    getch();
    return 1;
}


//家谱的存储
status Sav(BiPTree T) {
    FILE* fp;
    char filename[40];
    system("cls");
    printf("请输入新的文件名:");
    getchar();
    gets(filename);
    while (filename[0] == NULL)
    {
        printf("家谱名不能为空,请重新输入:");
        gets(filename);
    }
    prewrite(T, write, filename);
    printf("%s家谱保存成功,按任一键继续!", filename);
    getch();
    return 1;
}

//修改家谱
status Upd() {
    system("cls");
    int xz;
    while (1)
    {
        system("cls");
        printf("\n\n\n\n");
        printf("   家族成员的添加与删除操作  \n");
        printf("      请选择           \n");
        printf("    1.添加成员.        \n");
        printf("    2.删除成员.        \n");
        printf("    3.返回上一级.      \n");
        printf("       请选择:");
        scanf("%d", &xz);
        switch (xz)
        {
        case 1: in(T); break;
        case 2: de(T); break;
        case 3: return 0;
        default:
            wrong();
            break;
        }
    }
}


main()
{
    P = (BiTPNode*)malloc(sizeof(BiTPNode));
    int xz;
    while (1)
    {
        system("cls");
        printf("\n\n\n\n");
        printf("     家族关系查询系统     \n");
        printf("     具体操作如下         \n");
        printf("     1.创建家谱.          \n");
        printf("     2.载入家谱.          \n");
        printf("     3.修改家谱.          \n");
        printf("     4.查寻成员.          \n");
        printf("     5.保存家谱.          \n");
        printf("     6.退出程序.          \n");
        printf("     请选择操作:");
        scanf("%d", &xz);
        switch (xz)
        {
        case 1:
            Cre();
            break;
        case 2:
            Crt(T);
            break;
        case 3:
            Upd();
            break;
        case 4:
            Sea(T);
            break;
        case 5:
            Sav(T);
            break;
        case 6:
            return 0;
        default:
            wrong();
            break;
        }
    }
}


运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果
严重性    代码    说明    项目    文件    行    禁止显示状态
错误    C4996    'getch': The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name: _getch. See online help for details.    hello    D:\VS code\C语言学习\hello\hello\源2.c    42    
错误    C4996    'getch': The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name: _getch. See online help for details.    hello    D:\VS code\C语言学习\hello\hello\源2.c    136    
错误    C4996    'getch': The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name: _getch. See online help for details.    hello    D:\VS code\C语言学习\hello\hello\源2.c    174    
205 290 332 352也都是这样




  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题