[分享]存储电话号码的程序(小部分功能)
如题这只是个小部分的功能,不想再写了,就发上来,让大家指点一下#include <map>
#include <utility>
#include <iostream>
#include <iomanip>
class Tel
{
std::multimap<std::string,double> map_tel;
public:
void add();
void print();
void find(const std::string & name);
void change(const std::string & name);
};
void Tel::add()
{
//给电话本添加电话号码
std::cout<<"do you want to add num,please press 'y' or 'n' :";
char ch;
std::cin>>ch;
while(ch == 'y')
{
std::cout<<"please input the number_name and own telphone number :";
std::string num_name;
double number;
std::cin>>num_name>>number;
map_tel.insert(std::make_pair(std::string(num_name),number));
std::cout<<"please input home number :";
std::cin>>number;
map_tel.insert(std::make_pair(std::string(num_name),number));
std::cout<<"do you want to add,please press 'y' or 'n' :";
std::cin>>ch;
}
}
void Tel::print()
{
//输出电话本
std::multimap<std::string,double>::const_iterator iter = map_tel.begin();
while(iter != map_tel.end())
{
std::cout<<iter->first<<'\t'<<std::setprecision(11)<<iter->second<<std::endl;
iter++;
}
}
void Tel::find(const std::string & name)
{
//从电话本中查找指定名字的电话号码
typedef std::multimap<std::string,double>::iterator map_it;
std::pair<map_it,map_it> pos = map_tel.equal_range(name);
if(pos.first == pos.second)//电话本里面无此人
{
std::cout<<"no this name\n";
return ;
}
while(pos.first != pos.second)
{
std::cout<<std::setprecision(11)<<pos.first->second<<std::endl;
++pos.first;
}
}
void Tel::change(const std::string &name)
{
//更改指定名字的电话号码
typedef std::multimap<std::string,double>::iterator map_it;
std::pair<map_it,map_it> pos = map_tel.equal_range(name);
double number;
if(pos.first == pos.second)//电话本里面无此人
{
std::cout<<"no this name\n";
return ;
}
for(int i = 0;pos.first != pos.second;++pos.first,++i)
{
if(i == 0)
{
std::cout<<"please input the new own telphone number :";
std::cin>>number;
pos.first->second = number;
}
else
{
std::cout<<"please input the new home number :";
std::cin>>number;
pos.first->second = number;
}
}
}
int main()
{
Tel tel_book;
tel_book.add();
tel_book.print();
std::string name;
std::cout<<"please input the one who you want change :";
std::cin>>name;
tel_book.change(name);
tel_book.print();
std::cout<<"please input you want find :";
std::cin>>name;
tel_book.find(name);
system("pause");
return 0;
}
英语太难了,有些句子没写好
程序运行时:先输入名字,手机号,家庭电话号码
如下:
wang 13555888855 3986668
[此贴子已经被作者于2006-6-2 13:03:23编辑过]