粥粥528 2022-11-30 12:47 采纳率: 84%
浏览 9

为啥菜单只能输入两次?感觉问题挺多却找不到.

个人电话号码查询系统
要求:1.用结构体输入电话号码结构,包括手机号和姓名两个数据
2.数据个数不少于10个
3.对数据进行二叉排序并实现平衡二叉树
4.实现查找功能

#include <iostream>

#include<malloc.h>
using namespace std;
struct stu_num
{
    char stu_name[30];
    int num;
    int number;
};
struct person_node
{
    person_node *lchild;
    person_node *rchild;
    int height;
    stu_num perfor_info;
};
person_node *request_person_node(const stu_num *value)
{
    person_node *new_node;
    //new_node=new person_node;
    new_node=(person_node*)malloc(sizeof(person_node));
    if(new_node==NULL)
    {
        cout<<"未申请到二叉树节点"<<endl;
    }
    new_node->perfor_info=*value;
    new_node->lchild=nullptr;
    new_node->rchild=nullptr;
    return new_node;
}
int get_tree_height(person_node *root)
{
    if(root=nullptr)
        return 0;
    return max(get_tree_height(root->lchild),get_tree_height(root->rchild))+1;
}
person_node *tree_node_rotate_left(person_node *root)
{
    person_node *tmp;
    tmp=root->lchild;
    root->lchild=tmp->rchild;
    tmp->rchild=root;
    tmp->height=get_tree_height(tmp);
    root->height=get_tree_height(root);
    return tmp;
}
person_node *tree_node_rotate_right(person_node *root)
{
    person_node *tmp;
    tmp=root->rchild;
    root->rchild=tmp->lchild;
    tmp->lchild=root;
    tmp->height=get_tree_height(tmp);
    root->height=get_tree_height(root);
    return tmp;
}
person_node *tree_node_rotate_left_right(person_node *root)
{

    root->lchild=tree_node_rotate_right(root->lchild);
    root=tree_node_rotate_left(root);
    return root;
}
person_node *tree_node_rotate_right_left(person_node *root)
{

    root->lchild=tree_node_rotate_left(root->lchild);
    root=tree_node_rotate_right(root);
    return root;
}
person_node *insert_node_to_tree(person_node *root,person_node *new_node)
{
    if(root==nullptr)
        return new_node;
    if(root->perfor_info.number>new_node->perfor_info.number)
    {
        root->lchild=insert_node_to_tree(root->lchild,new_node);
    }
    else
    {
        root->rchild=insert_node_to_tree(root->rchild,new_node);
    }
    if(get_tree_height(root->lchild)-get_tree_height(root->rchild)==2)
    {
        if(new_node->perfor_info.number<root->lchild->perfor_info.number)
        {
            root=tree_node_rotate_left(root);
        }
        else
        {
            root=tree_node_rotate_left_right(root);
        }
    }
    else if(get_tree_height(root->rchild)-get_tree_height(root->lchild)==2)
    {
        if(new_node->perfor_info.number>root->lchild->perfor_info.number)
        {
            root=tree_node_rotate_right(root);
        }
        else
        {
            root=tree_node_rotate_right_left(root);
        }
    }
    root->height=get_tree_height(root);
    return root;
}
person_node *find_tree_node(person_node *root,int input_value)
{
    if(root=nullptr)
        return nullptr;
    if(input_value<root->perfor_info.number)
    {
        return find_tree_node(root->lchild,input_value);
    }
    else if(input_value>root->perfor_info.number)
    {
        return find_tree_node(root->rchild,input_value);
    }
    return root;
}
void tree_for_each(person_node *root)
{
    if(root=nullptr) return ;
    tree_for_each(root->lchild);
    cout<<"number: "<<root->perfor_info.number<<"name: "<<root->perfor_info.stu_name<<"phone number:"<<root->perfor_info.num<<endl;
    tree_for_each(root->rchild);
}
int main(void)
{
    int choose,input_value,a;
    stu_num input_person_data;
    person_node *root=nullptr,*new_node,*find_node;
    while(1)
    {
        cout<<"****输入数字选择相应的指令****\n"<<endl;
        cout<<"****1.添加联系人**************\n"<<endl;
        cout<<"****2.查找联系人**************\n"<<endl;
        cout<<"****3.输出所有联系人**********\n"<<endl;
        cout<<"****4.退出本系统**************\n"<<endl;
        cin>>choose;
        switch(choose)
        {
        case 1:
            cout<<"input number: "<<endl;
            cin>>input_person_data.number;
            cout<<"input name: "<<endl;
            cin>>input_person_data.stu_name;
            cout<<"input phonenumber: " <<endl;
            cin>>input_person_data.num;
            new_node=request_person_node(&input_person_data);
            root=insert_node_to_tree(root,new_node);
            break;
        case 2:
            cout<<"input number: "<<endl;
            cin>>input_value;
            find_node=find_tree_node(root,input_value);
            cout<<"number: "<<find_node->perfor_info.number<<"name: "<<find_node->perfor_info.stu_name<<"phone number:"<<find_node->perfor_info.num<<endl;
            break;
        case 3:
            cout<<"All members message: "<<endl;
            tree_for_each(root);
            break;
        case 4:
            goto log_out;
        }

    }
    return 0;
    log_out:
        return 0;
}


  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-11-30 14:29
    关注
    评论

报告相同问题?

问题事件

  • 创建了问题 11月30日

悬赏问题

  • ¥30 STM32 INMP441无法读取数据
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境