#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;
}
课程设计作业-通讯录(树形状)
最新推荐文章于 2023-10-16 15:24:03 发布