通讯录 c++ list版

        使用容器进行链表的增删查改的确是方便了很多,减少了使用指针可能出现的地址空间分配问题。每一个结点都包 括一个信息块 、一个前驱指针、一个后驱指针。可以不分配必须的内存大小方便的进行添加和删除操作,且使用的是非连续的内存空间进行存储。相比vector,list不能使用下标[ ]和vector.at()进行随机的内部访问,只能从头至尾或从后向前,且list占用了更多的 内存空间。

#include <iostream>
#include <list>
#include <string>
using namespace std;

class People{
	public:
		char name[10];
		char sex[10];
		char telephone[20];
		char QQ[20];
		char address[20];
		char note[20];
};
class List:public People{
	private:
		list<People> info;
		list<People>::iterator p;
		int m_number;
	public:
		int showscreen();
		void insert();
		void search();
		void dele();
		void alter();
		void display();
};

int List::showscreen()
{
	int number = 0;;
    cout<<"*+=================================================+*"<<endl;
    cout<<"*|                                                 |*"<<endl;
    cout<<"*|                多功能电子通讯录                 |*"<<endl;
    cout<<"*|                                                 |*"<<endl;
	cout<<"*|                                                 |*"<<endl;
    cout<<"*|                                        版本:1.0 |*"<<endl;
    cout<<"*|-------------------------------------------------|*"<<endl;
    cout<<"*|      1.添加好友信息       2.查看好友信息        |*"<<endl;
    cout<<"*|-------------------------------------------------|*"<<endl;
    cout<<"*|      3.删除好友信息       4.修改好友信息        |*"<<endl;
    cout<<"*|-------------------------------------------------|*"<<endl;
    cout<<"*|      5.显示好友信息       6.退出                |*"<<endl;
    cout<<"*+=================================================+*"<<endl;
    cout<<"                                                     "<<endl;
    cout<<"请选择你要实现的功能:"<<endl;
    cin>>number;
    if(number == 1||number == 2||number ==3||number == 4||number == 5||number == 6)
    {
		m_number = number;
        return m_number;
    }
}

void List::insert()
{
	char ch = 0;
	while(1)
	{
		//p = info.begin();        //初始化
		People peo;
		cout<<"姓名:"<<endl;
		cin>>peo.name;
		cout<<"性别:"<<endl;
		cin>>peo.sex;
		cout<<"电话:"<<endl;
		cin>>peo.telephone;
		cout<<"QQ:"<<endl;
		cin>>peo.QQ;
		cout<<"地址:"<<endl;
		cin>>peo.address;
		cout<<"备注:"<<endl;
		cin>>peo.note;
		info.push_back(peo);
		
		cout<<"添加成功。"<<endl;
			
		cout<<"是否继续? y/n:"<<endl;
		getchar();
		cin>>ch;
		while(ch != 'n' && ch != 'y')
		{
			while(getchar() != '\n');
			cout<<"输入错误!请重新输入 y/n:"<<endl;
			cin>>ch;
		}
		if(ch == 'n')
		{
			break;
		}
	}
}

void List::search()
{
	p = info.begin();
	char ch = 0;
	char a[10] = {0};
	int flag = 0;
	
	while(1)
	{
		cout<<"请输入查找人的姓名:";
		cin>>a;
		for(p = info.begin(); p != info.end(); p++)
		{
			if(strcmp(a,p->name) == 0)         
			{
				flag = 1;
				cout<<"姓名:"<<p->name<<endl;
				cout<<"性别:"<<p->sex<<endl;
				cout<<"电话:"<<p->telephone<<endl;
				cout<<"QQ:"<<p->QQ<<endl;
				cout<<"地址:"<<p->address<<endl;
				cout<<"备注:"<<p->note<<endl;
				cout<<"***************************************"<<endl;
			}
		}
		if(flag = 0)
		{
			cout<<"查无此人!"<<endl;
		}
		cout<<"是否继续? y/n:"<<endl;
		getchar();
		cin>>ch;
		while(ch != 'n' && ch != 'y')
		{
			while(getchar() != '\n');
			cout<<"输入错误!请重新输入 y/n:"<<endl;
			cin>>ch;
		}
		if(ch == 'n')
		{
			break;
		}
	}
}

void List::dele()
{
	char a[20] = {0};
	int len = 0;       //显示该用户所在位置
	int count = 0;     //显示人数下标
	int len_node[20] = {0};
	int num = 0;
	char ch = 0;
	int i = 0;
	
	while(1)
	{
		len = 0;
		count = 0;
		p = info.begin();
		
		cout<<"根据姓名删除:"<<endl;
		cin>>a;
		
		for(p = info.begin(); p != info.end(); p++)
		{
			len++;
			if(strcmp(a,p->name) == 0)
			{
				cout<<"第 "<<count<<"人:"<<endl;
				len_node[count] = len;
				count++;
				cout<<"姓名:"<<p->name<<endl;
				cout<<"性别:"<<p->sex<<endl;
				cout<<"电话:"<<p->telephone<<endl;
				cout<<"QQ:"<<p->QQ<<endl;
				cout<<"地址:"<<p->address<<endl;
				cout<<"备注:"<<p->note<<endl;
				cout<<"***************************************"<<endl;
			}
		}
		if(count == 0)
		{
			cout<<"查无此人!"<<endl;
		}
		else
		{
			cout<<"请输入删除联系人额度编号:"<<endl;
			cin>>num;
			while(num < 0 || num >= count)
			{
				while(getchar() != '\n');
				cout<<"输入错误! 请重新输入!"<<endl;
				cin>>num;
			}
			i = 0;
			for(p = info.begin(); i < len_node[num]-1; p++)			
			{
				i++;
			}
			info.erase(p);    ///删除结点
			cout<<"删除成功!"<<endl;
		}

		cout<<"是否继续? y/n:"<<endl;
		getchar();
		cin>>ch;
		while(ch != 'n' && ch != 'y')
		{
			while(getchar() != '\n');
			cout<<"输入错误!请重新输入 y/n:"<<endl;
			cin>>ch;
		}
		if(ch == 'n')
		{
			break;
		}
	}
}

void List::alter()
{
	char a[20] = {0};
	int len = 0;       //显示该用户所在位置
	int count = 0;     //显示人数下标
	int len_node[20] = {0};
	int num = 0;
	char ch = 0;
	int i = 0;
	
	while(1)
	{
		len = 0;
		count = 0;
		p = info.begin();
		
		cout<<"根据姓名修改:"<<endl;
		cin>>a;		
		p = info.begin();
		
		for(p = info.begin(); p != info.end(); p++)
		{
			len++;
			if(strcmp(a,p->name) == 0)
			{
				cout<<"第 "<<count<<"人:"<<endl;
				len_node[count] = len;
				count++;
				cout<<"姓名:"<<p->name<<endl;
				cout<<"性别:"<<p->sex<<endl;
				cout<<"电话:"<<p->telephone<<endl;
				cout<<"QQ:"<<p->QQ<<endl;
				cout<<"地址:"<<p->address<<endl;
				cout<<"备注:"<<p->note<<endl;
				cout<<"***************************************"<<endl;
			}
		}
		if(count == 0)
		{
			cout<<"查无此人!"<<endl;
		}
		else
		{
			cout<<"请输入修改联系人额度编号:"<<endl;
			cin>>num;
			while(num < 0 || num >= count)
			{
				while(getchar() != '\n');
				cout<<"输入错误! 请重新输入!"<<endl;
				cin>>num;
			}

			i = 0;
			for(p = info.begin(); i < len_node[num]-1; p++)			
			{
				i++;
			}
			cout<<"更新的信息:"<<endl;
			cout<<"姓名:"<<endl;
			cin>>p->name;
			cout<<"性别:"<<endl;
			cin>>p->sex;
			cout<<"电话:"<<endl;
			cin>>p->telephone;
			cout<<"QQ:"<<endl;
			cin>>p->QQ;
			cout<<"地址:"<<endl;
			cin>>p->address;
			cout<<"备注:"<<endl;
			cin>>p->note;
			cout<<"修改成功!"<<endl;
		}
		
		cout<<"是否继续? y/n:"<<endl;
		getchar();
		cin>>ch;
		while(ch != 'n' && ch != 'y')
		{
			while(getchar() != '\n');
			cout<<"输入错误!请重新输入 y/n:"<<endl;
			cin>>ch;
		}
		if(ch == 'n')
		{
			break;
		}		
	}
}

void List::display()
{
	char ch = 0;
	p = info.begin();        //初始化
	
	while(1)
	{
	for(p = info.begin();p != info.end(); p++)
	{
		cout<<"姓名:"<<p->name<<endl;
		cout<<"性别:"<<p->sex<<endl;
		cout<<"电话:"<<p->telephone<<endl;
		cout<<"QQ:"<<p->QQ<<endl;
		cout<<"地址:"<<p->address<<endl;
		cout<<"备注:"<<p->note<<endl;
		cout<<""<<"***********************"<<endl;
	}
	cout<<"请输入 'q' 退出!"<<endl;;
	cin>>ch;
	while(ch != 'q')
	{
		cout<<"输入错误!  请输入 'q' 退出!"<<endl;
		cin>>ch;
	}
    if(ch == 'q')
	{
		break;
	}
	
	}
}

int main()
{
	 List list;
	 int number = 0;
	 char ch = 0;
	 
	 while(1)
	 {
		system("reset");
		number = list.showscreen();
		system("reset");
		
		switch(number)
		{
			case 1:    //添加
			{
				list.insert();
				break;
			}
			case 2:    //查看
			{
				list.search();
				break;
			}
			case 3:    //删除  
			{
				list.dele();
				break;
			}
			case 4:    //修改
			{
				list.alter();	
				break;
			}
			case 5:
			{
				list.display();
				break;
			}
			case 6:    //退出
			{
				cout<<"退出成功!"<<endl;
				exit(0);
			}
			default:
			{
				break;
			}
		}
	 }
	 
	 
	return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值