C++实现简易通讯录

功能和效果图

功能
通讯录实现联系人查找,添加联系人,删除联系人,联系人信息修改
效果图
在这里插入图片描述

实现代码

创建结构体

typedef struct News 
{
	string name;		//姓名
	string sex;			//性别
	string address;		//地址
	int tell;			//电话
}News;

查找联系人

int search(News *a) 
{
	string name;
	int x;
	cout << "请输入要查找人的姓名:" << endl;
	cin >> name;
	for (int i = 0; i <= number; i++)
	{
		if (a[i].name == name)								//查找成功
		{
			cout << "查找结果为:" << endl;
			cout <<"姓名为:"<< a[i].name <<"  性别为:"<< a[i].sex << "  地址为:" << a[i].address << "  电话为:" << a[i].tell << endl;
			cout << "按任意数字键后,按enter结束" << endl;
			cin >> x;
			system("cls");    //清屏,返回主界面
			return 1;
		}

	}
	cout << "没有该联系人,查找结束" << endl;				//查找失败
	cout << "按任意数字键后,按enter结束" << endl;
	cin >> x;
	system("cls");    //清屏,返回主界面
	return -1;
}

添加联系人

void  Add(News *a)
{
	int x;
	int i = number;
	int tell1;
	cout << "请输入要插入联系人的姓名:" << endl;   //输入添加联系人的信息
	cin >> a[i].name;
	cout << "请输入要插入联系人的性别:" << endl;
	cin >> a[i].sex;
	cout << "请输入要插入联系人的地址:" << endl;
	cin >> a[i].address;
	cout << "请输入要插入联系人的电话:" << endl;
	cin >> a[i].tell;
	number++;
	cout << "信息已经保存,按任意数字键后,按enter结束" << endl;
	cin >> x;
	system("cls");     //清屏,返回主界面

}

删除联系人

int Delete(News *a)
{
	int i = number;
	int x;
	string name;
	cout<<"请输入要删除联系人的姓名"<<endl;
	cin >> name;
	for (int i = 0; i <= number; i++)
	{
		if (a[i].name == name)						//根据姓名查找需要删除的联系人
		{
			number--;
			cout << "删除成功!" << endl;
			cout << "按任意数字键后,按enter结束" << endl;
			cin >> x;
			system("cls");    //清屏,返回主界面

			return 1;
		}
	}
	cout << "联系人不存在" << endl;
	cout << "按任意数字键后,按enter结束" << endl;
	cin >> x;
	system("cls");    //清屏,返回主界面

	return 0;
}

修改联系人的信息

int Alter(News *a)
{
	int x;
	int i = number;
	string oldname;
	string newname;
	string newsex;
	string newaddress;
	int newtell;
	cout<<"请输入需要修改联系人的姓名:"<<endl;
	cin >> oldname;
	for (int i = 0; i <= number; i++)
	{
		if (a[i].name == oldname)
		{
			cout<<"请输入该联系人新信息"<<endl;
			cout << "请输入联系人的姓名:" << endl;   //输入联系人的新信息
			cin >> newname;
			a[i].name = newname;
			cout << "请输入联系人的性别:" << endl;
			cin >> newsex;
			a[i].sex = newsex;
			cout << "请输入联系人的地址:" << endl;
			cin >> newaddress;
			a[i].address = newaddress;
			cout << "请输入联系人的电话:" << endl;
			cin >> newtell;
			a[i].tell = newtell;
			cout << "联系人信息已经更改!" << endl;
			cout << "按任意数字键后,按enter结束" << endl;
			cin >> x;
			system("cls");   //清屏,返回主界面
			return 1;
		}
	}
	cout << "联系人不存在" << endl;
	cout << "按任意数字键后,按enter结束" << endl;
	cin >> x;
	system("cls");    //清屏,返回主界面
	return -1;
}

服务选择

int choose()
{
	int choice;
	cout << "				**********简易通讯录**********" << endl;
	cout << "					1.按姓名查找联系人" << endl;
	cout << "					2.添加联系人" << endl;
	cout << "					3.删除联系人" << endl;
	cout << "					4.修改联系人" << endl;
	cout << "					5.退出" << endl;
	cout << "				  请输入数字1-5选择服务" << endl;
	cin >> choice;
	system("cls");    //清屏
	return choice;
}

测试函数

int main()
{
	News a[50];					//初始化允许最大输入联系人为50
	int choice = 0;
	while (choice != 5)
	{
		choice = choose();
		switch (choice)			//选择服务
		{
		case 1: search(a); break;
		case 2: Add(a); break;
		case 3: Delete(a); break;
		case 4: Alter(a); break;
		case 5: exit(0); break;
		default:break;
		}

	}

【注】在这个程序里,联系人信息是保存在数组里面的,所以每次打开都是一个新的通讯录(暂时没有写到文件里面),所以称为简易通讯录

发布了9 篇原创文章 · 获赞 10 · 访问量 3553

猜你喜欢

转载自blog.csdn.net/weixin_44480968/article/details/104402537