课程设计作业-通讯录(树形状)

#include <iostream>
#include <stdio.h>
#include <map>
#include <cstring>
#include <algorithm>
#include <string>
using namespace std;
const int maxn=105;
typedef struct node* point_node;
struct node
{
    string address,tel,name;
    point_node point_relation[maxn];
    point_node point_ans;
};
map <string ,int > table;
int cnt=0;
point_node Creat_node()
{
    point_node happy=new node;
    for(int i=0;i<maxn;i++)
    {
        happy->point_relation[i]=0;
    }
    return happy;
}
int fget(string name)
{
    if(!table.count(name))
    {
        table[name]=++cnt;
    }
    return table[name];
}

void Creat_new_people(point_node root,string name,string tel,int fir)
{
    string f_name;
    f_name=name.substr(fir*2, 2);
    //cout<<f_name<<endl;
    int table_name=fget(f_name);
    //cout<<table_name<<endl;
    if( root->point_relation[table_name]==NULL )
    {
        root->point_relation[table_name]=Creat_node();
    }
    if(name.size()-2 > fir*2)
    {
        Creat_new_people(root->point_relation[table_name],name,tel,fir+1);
    }
    else
    {
        root->name=name;
        root->tel=tel;
        cout<<"建立联系人成功"<<endl;
    }
}
void Find_new_people( point_node root,string name ,int fir,int op)
{
    string f_name;
    f_name=name.substr(fir*2, 2);
    //cout<<f_name<<endl;
    int table_name=fget(f_name);
    //cout<<table_name<<endl;
   // cout<<root->point_relation[table_name]<<endl;

    if(name.size()-2 > fir*2)
    {
        Find_new_people(root->point_relation[table_name],name ,fir+1,op);
    }
    else if(!root->point_relation[table_name])
        cout<<"查无此人"<<endl;
    else
    {
        if(op==3)
        {
        if(root->name!="查无此人")
        {
        cout<<"通讯录中存在此人,查找到联系人"<<endl;
        cout<<"联系人姓名     :"<<root->name<<endl;
        cout<<"联系人手机号   :"<<root->tel<<endl;
        }
        else
            cout<<"查无此人"<<endl;
        }
        else if(op==2)
        {
        if(root->name=="查无此人")
            {
            cout<<"查无此人"<<endl;
            }
        else
            {
            root->name="查无此人",root->tel="";
            cout<<"删除联系人成功"<<endl;
            }
        }

        return ;
    }

}
void Delete_people(point_node root)
{
    for(int i=0;i<maxn;i++)
    {
        if(root->point_relation[i])
        {
            Delete_people(root->point_relation[i]);
        }
    }
    delete root;
    return ;
}
int main()
{
    cout<<"             欢迎使用自制通讯录第二版o(∩_∩)o..."<<endl;
    cout<<"         正在建立通讯录..."<<endl;
    cout<<"         完成o(∩_∩)o..."<<endl;
    point_node root =Creat_node() ;

    while(1)
    {
        printf(" 1.新建联系人请按一\n 2.删除联系人请按二\n 3.查找联系人请按三\n 4.删除通讯录请按四\n");
        int op;
        cin>>op;
        getchar();
        if(op==1)
        {
            printf("  请输入联系人姓名:格式:xxx \n");
            string name,tel;
            cin>>name;
            printf("  请输入联系人手机号:格式:xxx \n");
            cin>>tel;
            Creat_new_people( root, name ,tel,0);
            getchar();
        }
        else if(op==2)
        {
            printf("  请输入联系人姓名:格式:xxx \n");
            string name,tel;
            getline(cin,name);
            Find_new_people(root,name,0,2);
        }
        else if(op==3)
        {

            printf("  请输入联系人姓名:格式:xxx \n");
            string name,tel;
            getline(cin,name);
            Find_new_people(root,name,0,3);

        }
        else if(op==4)
        {
            cout<<"不要阿木木了么T-T"<<endl;
            Delete_people(root);
	    break;
        }
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值