容器+文件版本的通讯录

本文介绍了一个使用C++编写的简易通讯录系统的实现过程。系统功能包括添加、删除、查看、搜索和更新联系人信息,并能将联系人数据保存到文件中。文章详细展示了各个功能的具体代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
#include <string.h>
#include <fstream>
#include <list>

using namespace std;
typedef struct node
{
	char name[20];
	char sex[20];
	char tel[20];
	char QQ[20];
	char address[20];
	char addition[20];
}Per;

bool Cominfo(Per first,Per second)
{
	if(strcmp(first.name,second.name) >= 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}
int menu()                                                                         //菜单
{
	int choice = 0;
	cout<<"\t\t\t\t******************************************"<<endl;
	cout<<"\t\t\t\t*           通讯录c++简易版本            *"<<endl;
	cout<<"\t\t\t\t*                                        *"<<endl;
	cout<<"\t\t\t\t******************************************"<<endl;
	cout<<"\t\t\t\t*   1、添加联系人    2、删除联系人       *"<<endl;
	cout<<"\t\t\t\t******************************************"<<endl;
	cout<<"\t\t\t\t*   3、查看联系人    4、搜索联系人       *"<<endl;
	cout<<"\t\t\t\t******************************************"<<endl;
	cout<<"\t\t\t\t*   5、更新联系人    6、保存并退出       *"<<endl;
	cout<<"\t\t\t\t******************************************"<<endl;
	cout<<"\t\t\t\t请输入选择:";
	cin>>choice;
	while(!(choice >= 1&&choice <= 6))
	{
		while(getchar()!='\n');
		cout<<"输入有误,请重新输入!";
		cin>>choice;
	}
	return choice;
}

void add_person(list<Per> &link)                                                   //添加联系人
{
	Per new_person;
	cout<<"请填写添加的联系人信息"<<endl;
	cout<<"姓名: ";
	cin>>new_person.name;
	cout<<"性别:";
	cin>>new_person.sex;
	cout<<"电话:";
	cin>>new_person.tel;
	cout<<"QQ: ";
	cin>>new_person.QQ;
	cout<<"地址:";
	cin>>new_person.address;
	cout<<"备注:";
	cin>>new_person.addition;
	
	link.push_back(new_person);
	cout<<"添加成功!"<<endl;
	cout<<"是否继续添加(Y/N)";
	char relay;
	cin>>relay;
	while(!(relay == 'Y'||relay == 'y'||relay == 'n'||relay == 'N'))
	{
		cout<<"输入错误,请重新输入!";
		cin>>relay;
	}
	if(relay == 'y'||relay == 'Y')
	{
		system("clear");
		add_person(link);
	}
}

void del_person(list<Per> &link)                                               //删除联系人
{
	char name[20];
	cout<<"请输入你要删除的人的姓名: ";
	cin>>name;
	int flag = 0;
	
	list<Per>::iterator p = link.begin();
	while(p != link.end())
	{
		if(strcmp(p->name,name) == 0)
		{
			link.erase(p);
			flag = 1;
			break;
		}
		p++;
	}
	if(flag == 1)
	{
		cout<<"\n删除成功!"<<endl;
	}
	else
	{
		cout<<"删除的联系人不存在,删除失败!"<<endl;
	}
	
}

void display(list<Per> &link)                                                  //显示联系人
{
	link.sort(Cominfo);
	list<Per>::iterator p = link.begin();
	while(p != link.end())
	{
		cout<<endl<<"======================================="<<endl;
		cout<<"姓名:"<<p->name<<endl;
		cout<<"性别:"<<p->sex<<endl;
		cout<<"电话:"<<p->tel<<endl;
		cout<<"QQ:"<<p->QQ<<endl;
		cout<<"地址:"<<p->address<<endl;
		cout<<"备注:"<<p->addition<<endl;	
		p++;
	}
}

void search(list<Per> &link)                                                     //搜索联系人
{
	char name[20];
	cout<<"请输入你要搜索的人的姓名: ";
	cin>>name;
	int flag = 0;
	
	list<Per>::iterator p = link.begin();
	while(p != link.end())
	{
		if(strcmp(p->name,name) == 0)
		{
			cout<<endl<<"======================================="<<endl;
			cout<<"姓名:"<<p->name<<endl;
			cout<<"性别:"<<p->sex<<endl;
			cout<<"电话:"<<p->tel<<endl;
			cout<<"QQ: "<<p->QQ<<endl;
			cout<<"地址:"<<p->address<<endl;
			cout<<"备注:"<<p->addition<<endl;	
			flag = 1;
		}
		p++;
	}
	if(flag == 1)
	{
		cout<<"\n搜索成功!"<<endl;
	}
	else
	{
		cout<<"该联系人不存在!"<<endl;
	}
}

void update(list<Per> &link)                                           //修改联系人
{
	int flag = 0;
	char m_name[20];
	cout<<"请输入你要更新的姓名:";
	cin>>m_name;
	list<Per>::iterator p = link.begin();
	
	while(p != link.end())
	{
		if(strcmp(p->name,m_name) == 0)
		{
			cout<<"请更新性别:";
			cin>>p->sex;
			cout<<"请更新电话:";
			cin>>p->tel;
			cout<<"请更新QQ:";
			cin>>p->QQ;
			cout<<"请更新住址:";
			cin>>p->address;
			cout<<"请更新备注:";
			cin>>p->addition;
			flag = 1;
			break;
		}
		p++;
	}
	if(flag == 1)
	{
		cout<<"\n更新成功"<<endl;
	}
	else
	{
		cout<<"查无此人,更新失败!"<<endl;
	}
}

void save_file(list<Per> &link)                                    //保存联系人
{
	ofstream outfile("per.txt",ios::out|ios::trunc);
	if(!outfile)
	{
		cerr<<"file open fail"<<endl;
		return ;
	}
	list<Per>::iterator p = link.begin();
	while(p != link.end())
	{
		outfile<<p->name<<endl;
		outfile<<p->sex<<endl;
		outfile<<p->tel<<endl;
		outfile<<p->QQ<<endl;
		outfile<<p->address<<endl;
		outfile<<p->addition<<endl;	
		p++;
	}
	sleep(1);
	cout<<"\n保存成功"<<endl;
	outfile.close();
}

void read_file(list<Per> &link)                                   //读取文件至链表
{
	ifstream infile("per.txt",ios::in);
	Per new_per;
	if(!infile)
	{
		cout<<"read fail"<<endl;
		return ;
	}
	while(!infile.eof())
	{
		infile.getline(new_per.name,'\n');
		infile.getline(new_per.sex,'\n');
		infile.getline(new_per.tel,'\n');
		infile.getline(new_per.QQ,'\n');
		infile.getline(new_per.address,'\n');
		infile.getline(new_per.addition,'\n');
		if(infile.eof())
		{
			break;
		}
		link.push_back(new_per);	
	}
	infile.close();
}

int main()
{
	list<Per> link;
	int choice = 0;
	read_file(link);
	while(1)
	{
		system("clear");
		choice = menu();
		switch(choice)
		{
			case 1:
			{
				system("clear");
				add_person(link);
				break;
			}
			case 2:
			{
				system("clear");
				del_person(link);
				break;
			}
			case 3:
			{
				system("clear");
				display(link);
				break;
			}
			case 4:
			{
				system("clear");
				search(link);
				break;
			}
			case 5:
			{
				system("clear");
				update(link);
				break;
			}
			case 6:
			{
				cout<<"正在保存,请稍后..."<<endl;
				save_file(link);
				exit(0);
			}
		}
		cout<<"\n\n\n按任意键返回......";
		getchar();
		getchar();
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值