C++ :用multimap编写简单的通讯录小程序

#include
#include
#include
#define FALSE 0
#define TRUE 1
using namespace std;


class User
{
public:
User(int id,string name ,string tel)
{
this->id =id;
this->name=name;
this->tel = tel;
}


void show()
{
printf("id=%d,name=%s,tel=%s\n",id,name.c_str(),tel.c_str());


}


string getName()
{
return name;
}
int getid()
{
return id;
}
string gettel()
{
return tel;
}
private:
int id;
string name;
string tel;






};


class UserManage
{
public:
void add(int id,string name,string tel,string group)
{
User *p=new User(id,name,tel);   //创建新用户


m.insert(make_pair(group,p));
}


void show()
{
multimap::iterator it=m.begin();
while(it != m.end())
{
printf("分组:%-10s",it->first.c_str());
it->second->show();
it++;
}
if(m.begin() == m.end())
{
cout<<"通讯录里无记录,请添加用户!"< }
}


void showGroup(string group)
{
pair::iterator,multimap::iterator> ret=m.equal_range(group);
multimap::iterator it1=ret.first;
multimap::iterator it2=ret.second;
cout< while(it1 != it2)
{
it1->second->show();
it1++;
}




}


void Seek(string name)
{
int flag;
flag=FALSE;
multimap::iterator it=m.begin();
while(it != m.end())
{
if(name == it->second->getName())
{
cout<first<<": ";
it->second->show();
flag=TRUE;
break;
}
it++;
}
if(flag== FALSE)
{
cout<<"查无此人!"< }
}


bool del(string name )
{
int flag;
flag=FALSE;
multimap::iterator it=m.begin();
while(it != m.end())
{
if(name == it->second->getName())
{
m.erase(it);
cout<<"删除成功"< flag=TRUE;
return true;
}
it++;
}
if(flag == FALSE)
{
cout<<"不存在该用户,重新选择!"< return false;
}
}



private:
multimap m;    // 用户需要分组:家庭成员、同学、同事、陌生人、骚扰电话...
                              // 键:分组
                              // 值:用户
};




void menu()
{
printf("\t\t\t1.添加用户:\n");
printf("\t\t\t2.查看全部用户:\n");
printf("\t\t\t3.查看分组用户:\n");
printf("\t\t\t4.搜索用户:\n");
printf("\t\t\t5.删除用户:\n");
printf("\t\t\t6.修改用户:\n");
printf("\t\t\t0.退出:\n");
}


void add(UserManage &um)
{
int id;
string name;
string tel;
string group;
cout<<"请输入id:";
cin>>id;
cout<<"请输入用户名:";
cin>>name;
cout<<"请输入电话号码:";
cin>>tel;
cout<<"请输入用户组名:";
cin>>group;
um.add(id,name,tel,group);
}


void del(UserManage &um)
{
string name;
cout<<"请输入要删除用户的用户名:";
cin>>name;
um.del(name);




}


void showGroup(UserManage &um)
{
string group;
cout<<"请输入组名:";
cin>>group;
um.showGroup(group);
}


void Seek(UserManage &um)
{
string name;
cout << "请输入要搜索的用户名:";
cin>>name;
um.Seek(name);





}


void change(UserManage &um)
{
string name;
cout<<"请输入要修改用户的用户名:";
cin>>name;
if(um.del(name))
{
add(um);
}
}









int main ()
{
UserManage um;
char cmd[20];
//um.Rfile();



while(1)
{
menu();
printf("请输入您的选择(0-6):\n");
scanf("%s",cmd);
getchar();
if(cmd[0] >= '0' &&  cmd[0]<='6' && (strlen(cmd)==1) )
{
switch(cmd[0])
{
case '1' :add(um);break;
case '2':um.show();break;
case '3':showGroup(um);break;
case '4':Seek(um);break;
case '5':del(um);break;
case '6':change(um);break;
case '0':printf("谢谢使用本软件,按回车键退出...\n");getchar();exit(0);break;

}
}
else
{
printf("输入选择有误:\n\n");



}




}






return 0;
}

你可能感兴趣的:(C++ :用multimap编写简单的通讯录小程序)