实战:C++实现简易通讯录

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_42581477/article/details/82020059

“classmate.data”文件,存储数据

“map.cpp”,代码实现简易通讯录

//classmate.data

zhounian 1803001
leixiaowei 1803002
zhaozisha 1803003
wenbao 1803004
huangxukun 1803005
wangchenglin 1803006
liumiao 1803007
wanghcenggen 1803008
gaoxiaoqin 1803009
dongminglei 1803010
shixingyuan 1803011
#include<iostream>
#include<string>
//C语言的 输入输出
#include<stdio.h>
#include<list>
#include<map>
using namespace std;

//宏
#define FILENAME "classmate.data"

//人类
class people
{
	public:
		//构造器
		people();//无构造
		people(int ,const char*);//有参构造---没有无参
		//公有接口:获取ID
		int getId();
		//重载    强转运算符  operator 类型   (void);
		operator string()const;	// (类型)表达式   (int) 3.14
	protected:
		int id;
		string name;
};
people::people()
{
}
//构造函数
people::people(int i,const char* n):id(i),name(n)
{
}
//获取ID
int people::getId()
{
	return this->id;
}
//重载转运算符:
people::operator string() const
{
	//name="sb";  不允许修改
	return this->name;
}

//通讯录类
class recorde
{
	public://构造器
		recorde();
		//查找--查找姓名所在键值对
	map<char,list<people> >::iterator  find(const string name);
	//返回---返回该链表中所在的位置
	list<people>::iterator  find(int id=0);
	//插入信息
	bool add(const people);
	//显示
	void display();
	bool reloadData();
private:
	//关联容器:用于存放Key-Value对
	map<char,list<people> > m;
};
recorde::recorde()
{
	//对m中初始为26个Key-Value
	char ch='a';
	while(ch<='z')	
	{
		//向m发送消息:创建Key-Value对
		m.insert( pair<char,list<people> >(ch,list<people>())   );
		ch++;
	}
}
//查找姓名所对应的键值对
map<char,list<people> >::iterator recorde::find(const string name)
{
/*
	//迭代
	map<char,list<people> >::iterator it=this->m.begin();	
	while(it!=m.end())
	{
		if(name[0]==it->first)
			break;
		it++;
	}

	//返回地址
	return it;
*/
	//向m发送消息
	return this->m.find(name[0]);

}
//通过ID查找所在链表的位置
list<people>::iterator  recorde::find(int id)
{
	list<people>::iterator lit;	
	map<char,list<people> >::iterator mit=m.begin();
	while(mit!=m.end())//获取每一个Key-Value中的Value(list)
	{
		lit=mit->second.begin();
		while(lit!=mit->second.end())
		{	//people::id(procted)
			if(lit->getId()==id)//lit->people::id
				return lit;
			lit++;
		}

		mit++;
	}
	return lit;
}
//增加	
bool recorde::add(const people p)//p为空常空间:
{
	//lifei 1001 m 
	//people::naem为保护
	map<char,list<people> >::iterator it=this->find((string)p);
	if(it==this->m.end())//查找失败
		return false;
	it->second.push_back(p);//插入数据给的该链表
	return true;
}
//遍历
void recorde::display()
{
	//迭代器
	map<char ,list<people> >::iterator mit=this->m.begin();
	list<people>::iterator lit;
	while(mit!=this->m.end())
	{
		//姓氏输出
		if(mit->second.size()>0)//只输出有记录的姓氏
		{
			cout<<endl<<mit->first<<" :"<<endl;
		}
		lit=mit->second.begin();//获取链表首地址
		while(lit!=mit->second.end())
		{
			cout<<"姓名:"<<(string)*lit<<" 编号:"<<lit->getId()<<endl;
			lit++;
		}
		mit++;
	}	
}
bool recorde::reloadData()
{
//1打开文件
	FILE* fp=fopen(FILENAME,"r");
	if(NULL==fp)
		return false;

//2操作
	char name[100]="";
	int id;
	while(!feof(fp))
	{
		fscanf(fp,"%s %d",name,&id);
		this->add( people(id,name)/*无名对象*/  );
		//文件中文件的读写位置
	}

//3关闭
	fclose(fp);
}
int main()
{
//实例化
	recorde d;
	//插入
	d.reloadData();
	//遍历
	d.display();

	return 0;
}



/*
//...
int main()
{
//实例化:
	map<char,int> m;
//方法:发送消息
	m.insert(pair<char,int>('a',97));
	m.insert(pair<char,int>('b',98));
	m.insert(pair<char,int>('c',99));
	cout<<m.size()<<endl;	
//遍历:获取第一个节点
	map<char,int>:: iterator it=m.begin();
	while(it!=m.end())
	{
		cout<<it->first<<"  "<<it->second<<endl;
		it++;
	}


	return 0;
}
*/

猜你喜欢

转载自blog.csdn.net/weixin_42581477/article/details/82020059