通讯录管理系统

这是一个使用C++编写的个人通讯录管理系统,包括添加、显示、删除、查找和修改联系人等功能。用户可以方便地操作自己的联系人信息,系统还提供了清空通讯录的选项。代码中采用了结构体来存储联系人信息,并通过动态数组管理联系人列表。

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

 

 

张三负了李四,王五无情躺枪诶

咱就是说,要删,当初为何要加呢

#include <iostream>
#include <string>
using namespace std;
#define MAX 1000
struct person {
	string m_name;
	int m_sex;
	int m_age;
	string m_phone;
	string m_addr;
};
struct addressbooks {
	struct person personarray[MAX];
	int m_size;   //当前记录联系人个数
};

//1、菜单界面
void showmenu(void)
{
	cout << "       You're welcome        " << endl;
	cout << "************Mr.Qi************" << endl;
	cout << "*****   1、添加联系人   *****" << endl;
	cout << "*****   2、显示联系人   *****" << endl;
	cout << "*****   3、删除联系人   *****" << endl;
	cout << "*****   4、查找联系人   *****" << endl;
	cout << "*****   5、修改联系人   *****" << endl;
	cout << "*****   6、清空联系人   *****" << endl;
	cout << "*****   0、退出通讯录   *****" << endl;
	cout << "*****************************" << endl;
}
//2、添加联系人
void addperson(struct addressbooks* abs)
{
	//先判断是否满了
	if (abs->m_size == MAX)
	{
		cout << "通讯录已满,thankyou!" << endl;
		return ;
	}
	else
	{
		//姓名
		cout << "请输入姓名:" << endl;
		string name;
		cin >> name;
		abs->personarray[abs->m_size].m_name = name;
		//性别,1--男,2--女
		cout << "请输入性别(1--男,2--女):" << endl;
		while (1)
		{
			int sex;
			cin >> sex;
			if (sex == 1 || sex == 2)
			{
				abs->personarray[abs->m_size].m_sex = sex;
				break;
			}
			else {
				cout << "please input again!" << endl;
			}
		}
		//年龄
		cout << "请输入年龄:" << endl;
		int age;
		cin >> age;
		abs->personarray[abs->m_size].m_age = age;
		//电话
		cout << "请输入电话:" << endl;
		string phone;
		cin >> phone;
		abs->personarray[abs->m_size].m_phone = phone;
		//地址
		cout << "请输入地址:" << endl;
		string ad;
		cin >> ad;
		abs->personarray[abs->m_size].m_addr = ad;
		//更新通讯录
		(abs->m_size)++;
		cout << "添加成功哦~" << endl;
		system("pause");//请按任意键继续
		system("cls");//清屏函数,注意学
	}
}
//3、显示联系人
void showperson(struct addressbooks* abs)
{
	if (abs->m_size == 0)
	{
		cout << "Mr.Qi no one!" << endl;
	}
	else
	{
		cout << "name\t\tsex\t\tage\t\tphone\t\taddress" << endl;
		for (int i = 0; i < abs->m_size; i++)
		{
			cout << abs->personarray[i].m_name << "\t\t";
			cout << ((abs->personarray[i].m_sex==1)?"男":"女") << "\t\t";
			cout << abs->personarray[i].m_age << "\t\t";
			cout << abs->personarray[i].m_phone << "\t";
			cout << abs->personarray[i].m_addr << endl;
		}
	}
	system("pause");
	system("cls");
}
//4、检查联系人是否存在
int check(struct addressbooks* abs,string arr)
{
	for (int i = 0; i < abs->m_size; i++)
	{
		if (abs->personarray[i].m_name == arr)
		{   //注意哦,string定义的字符串,在某些使用上如+-
			//比C语言方便的太太,C中比较两个字符串只能用strcmp函数
			return i;
		}
	}
	return -1;
}
//5、删除联系人
void deleteperson(struct addressbooks* abs)
{
	cout << "为保留温情,留一份情谊,why删?" << endl;
	cout << "您若无情,那就删吧!please input who!!!" << endl;
	string name;
	cin >> name;
	int ret = check(abs, name);
	if (ret != -1)
	{
		for (int i = ret; i < abs->m_size; i++)
		{
			abs->personarray[i] = abs->personarray[i + 1];
		}
		(abs->m_size)--;
		cout << "congratulations!烦恼无了" << endl;
	}
	else {
		cout << "看来,您还没有他(她)电话,咋删呢!" << endl;
	}
	system("pause");
	system("cls");
}
//6、查找联系人
void findperson(struct addressbooks* abs)
{
	cout << "请输入您要查找的联系人,看来好久没联系了吧!" << endl;
	string name;
	cin >> name;
	int ret = check(abs, name);
	if (ret != -1)
	{
		cout << "您要查的联系人的信息:";
		cout << abs->personarray[ret].m_name << "\t";
		cout << ((abs->personarray[ret].m_sex == 1) ? "男" : "女") << "\t";
		cout << abs->personarray[ret].m_age << "\t";
		cout << abs->personarray[ret].m_phone << "\t";
		cout << abs->personarray[ret].m_addr << endl;
	}
	else {
		cout << "很遗憾,您未加该联系人" << endl;
	}
	system("pause");
	system("cls");
}
//7、修改联系人
void modifyperson(struct addressbooks* abs)
{
	cout << "请输入要修改的联系人" << endl;
	string name;
	cin >> name;
	int ret = check(abs, name);
	if (ret != -1)
	{
		cout << "请输入名字:" << endl;
		string mname;
		cin >> mname;
		abs->personarray[ret].m_name = mname;
		cout << "请输入性别(1--男,2--女):" << endl;
		while (1)
		{
			int sex;
			cin >> sex;
			if (sex == 1 || sex == 2)
			{
				abs->personarray[ret].m_sex = sex;
				break;
			}
			else {
				cout << "please input again!" << endl;
			}
		}
		cout << "请输入年龄:" << endl;
		int age;
		cin >> age;
		abs->personarray[ret].m_age = age;
		//电话
		cout << "请输入电话:" << endl;
		string phone;
		cin >> phone;
		abs->personarray[ret].m_phone = phone;
		//地址
		cout << "请输入地址:" << endl;
		string ad;
		cin >> ad;
		abs->personarray[ret].m_addr = ad;
		cout << "修改成功哦~" << endl;
	}
	else {
		cout << "很遗憾,您未加该联系人" << endl;
	}
	system("pause");
	system("cls");
}
//8、清空联系人
void cleanperson(struct addressbooks* abs)
{
	abs->m_size = 0;
	cout << "您的通讯录已清" << endl;
	system("pause");
	system("cls");
}
int main()
{
	struct addressbooks abs;  //创建通讯录abs
	abs.m_size = 0;
	while (1)
	{
		showmenu();
		int select;
		cin >> select;
		switch (select)
		{
		case 1:   //添加联系人
			addperson(&abs);
			break;
		case 2:    //显示联系人
			showperson(&abs);
			break;
		case 3:    //删除联系人
			deleteperson(&abs);
			break;
		case 4:    //查找联系人
			findperson(&abs);
			break;
		case 5:    //修改联系人
			modifyperson(&abs);
			break;
		case 6:    //清空通讯录
			cleanperson(&abs);
			break;
		case 0:
			cout << "欢迎下次使用Mr.Qi!" << endl;
			system("pause");//按任意键结束
			return 0;//提前结束main函数,即退出程序
			break;
		default:
			break;
		}
	}
	system("pause");//按任意键继续
	return 0;
}//咱就是说,要删,当初为何要加呢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值