通讯录管理系统

通讯录管理统统

功能实现

  • 添加联系人
  • 显示联系人
  • 删除联系人
  • 查找联系人
  • 修改联系人
  • 退出系统

添加联系人

功能描述:实现添加联系人功能,联系人上限为1000人,联系人信息包括(姓名、性别、年龄、联系电话、家庭住址)

添加联系人实现步骤:

  • 设计联系人结构体
  • 设计通讯录结构体
  • main函数中创建通讯录
  • 封装添加联系人函数
  • 测试添加联系人功能
#include
using namespace std;
#include
#define MAX 1000

//菜单显示
void showMenu()
{
     
	cout << "******************************" << endl;
	cout << "*****   1.添加联系人   *****" << endl;
	cout << "*****   2.显示联系人   *****" << endl;
	cout << "*****   3.删除联系人   *****" << endl;
	cout << "*****   4.查找联系人   *****" << endl;
	cout << "*****   5.修改联系人   *****" << endl;
	cout << "*****   0.退出通讯录   *****" << endl;
	cout << "******************************" << endl;
}

//联系人结构体
struct Person
{
     
	//姓名
	string m_Name;

	//性别  1:男  2:女
	int m_Sex;

	//年龄
	int m_Age;

	//联系电话
	string m_Phone;

	//家庭住址
	string m_Addr;
};

//通讯录结构体
struct Addrbooks
{
     
	struct Person personArray[MAX];	//通讯录保存的联系人数组
	int m_Size;	  //通讯录中的人数
};

//1.添加联系人
void addPerson(Addrbooks *abs)
{
     
	//判断电话本是否满了
	if (abs->m_Size >= MAX)
	{
     
		cout << "通讯录人数已满" << endl;
	}

	else
	{
     
		//姓名
		cout << "请输入姓名:" << endl;
		string name;
		cin >> name;
		abs->personArray[abs->m_Size].m_Name = name;

		//性别
		cout << "请输入性别:" << endl;
		cout << "1 --- 男" << endl;
		cout << "2 --- 女" << endl;
		int sex;
		while (true)
		{
     
			cin >> sex;
			if (sex == 1 || sex == 2)
			{
     
				abs->personArray[abs->m_Size].m_Sex = sex;
				break;
			}
			cout << "您输入的不对,请您重新输入"<<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 address;
		cin >> address;
		abs->personArray[abs->m_Size].m_Addr = address;
	}
	cout << "添加成功" << endl;

	//更新通讯录人数
	abs->m_Size++;

	system("pause");  //请按任意键继续
	system("cls");  //清屏
}

//2.显示联系人
void showPerson(Addrbooks *abs)
{
     
	//判断通讯录中是否添加成员
	if (abs->m_Size == 0)
	{
     
		cout << "当前没有记录" << endl;
	}
	else
	{
     
		for (int i = 0; i < abs->m_Size; i++)
		{
     
			cout << "姓名:" << abs->personArray[i].m_Name
				<< "  性别:" << (abs->personArray[i].m_Sex==1 ? "男":"女")
				<< "  年龄:" << abs->personArray[i].m_Age
				<< "  联系电话:" << abs->personArray[i].m_Phone
				<< "  家庭住址:" << abs->personArray[i].m_Addr << endl;
		}
	}
	

	system("pause");
	system("cls");
}

//检测联系人是否存在,如果存在,返回联系人所在数组中的具体位置,不存在返回-1
//参数1 通讯录  参数2 对比姓名
int isExist(Addrbooks *abs,string name)
{
     
	for (int i = 0; i < abs->m_Size; i++)
	{
     
		if (abs->personArray[i].m_Name == name)
		{
     
			return i;  //如果找到,返回数组中元素所在位置的下标
		}
		return -1;
	}
}

//3.删除联系人
void delPerson(Addrbooks *abs)
{
     
	cout << "请输入您要删除的联系人:" << endl;
	string name;
	cin >> name;

	//ret == -1 未找到
	//ret != -1 查到了
	int ret = isExist(abs, name);

	if (ret != -1)
	{
     
		for (int i = ret; i < abs->m_Size; i++)
		{
     
			//数据前移
			abs->personArray[i] = abs->personArray[i + 1];
		}
		cout << "删除成功!" << endl;
		abs->m_Size--;//更新通讯录人数
	}
	else
	{
     
		cout << "查无此人" << endl;
	}

	system("pause");
	system("cls");
}

//4.查找联系人
void selectPerson(Addrbooks *abs)
{
     
	cout << "请输入要查找的联系人" << endl;
	string name;
	cin >> name;
	int ret = isExist(abs, name);

	if (ret != -1)
	{
     
		cout << "姓名:" << abs->personArray[ret].m_Name
			<< "  性别:" << (abs->personArray[ret].m_Sex == 1 ? "男" : "女")
			<< "  年龄:" << abs->personArray[ret].m_Age
			<< "  联系电话:" << abs->personArray[ret].m_Phone
			<< "  家庭住址:" << abs->personArray[ret].m_Addr << endl;
	}
	else
	{
     
		cout << "未找到" << endl;
	}

	system("pause");
	system("cls");
}

//5.修改联系人
void modifyPerson(Addrbooks *abs)
{
     
	cout << "请输入您要修改的联系人" << endl;
	string name;
	cin >> name;

	int ret = isExist(abs, name);
	if (ret != -1)
	{
     
		//姓名
		string name;
		cout << "请输入姓名:" << endl;
		cin >> name;
		abs->personArray[ret].m_Name = name;

		cout << "请输入性别:" << endl;
		cout << "1 -- 男" << endl;
		cout << "2 -- 女" << endl;

		//性别
		int sex = 0;
		while (true)
		{
     
			cin >> sex;
			if (sex == 1 || sex == 2)
			{
     
				abs->personArray[ret].m_Sex = sex;
				break;
			}
			cout << "输入有误,请重新输入";
		}

		//年龄
		cout << "请输入年龄:" << endl;
		int age = 0;
		cin >> age;
		abs->personArray[ret].m_Age = age;

		//联系电话
		cout << "请输入联系电话:" << endl;
		string phone ;
		cin >> phone;
		abs->personArray[ret].m_Phone = phone;

		//家庭住址
		cout << "请输入家庭住址:" << endl;
		string address;
		cin >> address;
		abs->personArray[ret].m_Addr = address;

		cout << "修改成功" << endl;
	}
	else
	{
     
		cout << "查无此人" << endl;
	}

	system("pause");
	system("cls");

}

int main()
{
     
	int select;
	//创建通讯录结构体
	Addrbooks abs;
	abs.m_Size = 0;  //初始化通讯录人数

	//调用显示菜单函数
	while (true)
	{
     
		showMenu();

		cin >> select;

		//使用switch结构让用户按菜单实现选择操作
		switch (select)
		{
     
		case 1: //添加联系人
			addPerson(&abs);
			break;
		case 2: //显示联系人
			showPerson(&abs);
			break;
		case 3: //删除联系人
			delPerson(&abs);
			break;
		case 4: //查找联系人
			selectPerson(&abs);
			break;
		case 5: //修改联系人
			modifyPerson(&abs);
			break;
		case 0: //退出系统
			cout << "欢迎下次使用" << endl;
			system("pause");  //请按任意键继续
			return 0;  //让程序正常退出
			break;
		default:
			break;
		}
	}
	

	system("pause");  //请按任意键继续
	return 0;
}

改进版

#include
using namespace std;
#include
#define MAX 1000

void showMenu()
{
     
	cout << "******************************" << endl;
	cout << "*****   1.添加联系人   *****" << endl;
	cout << "*****   2.显示联系人   *****" << endl;
	cout << "*****   3.删除联系人   *****" << endl;
	cout << "*****   4.查找联系人   *****" << endl;
	cout << "*****   5.修改联系人   *****" << endl;
	cout << "*****   6.清空联系人   *****" << endl;
	cout << "*****   0.退出通讯录   *****" << endl;
	cout << "******************************" << endl;
}

//联系人结构体
struct Person
{
     
	//姓名
	string p_Name;

	//性别 1--男 2--女
	int p_Sex;

	//年龄
	int p_Age;

	//联系电话
	string p_Phone;

	//家庭住址
	string p_Addr;
};

//通讯录结构体
struct Addrbooks
{
     
	Person personArray[MAX];//通讯录中用来保存联系人的数组
	int p_Size = 0;//通讯录中保存的的人数
};

//添加联系人
void addPerson(Addrbooks *abs)
{
     
	//判断人数是否已满
	if (abs->p_Size >= MAX)
	{
     
		cout << "成员已满,无法添加" << endl;
	}
	else
	{
     
		//姓名
		string name;
		cout << "请输入姓名:" << endl;
		cin >> name;
		abs->personArray[abs->p_Size].p_Name = name;

		//性别
		int sex;
		cout << "请输入性别" << endl;
		cout << "1---男" << endl;
		cout << "2---女" << endl;
		while (true)
		{
     
			cin >> sex;
			if (sex == 1 || sex == 2)
			{
     
				abs->personArray[abs->p_Size].p_Sex = sex;
				break;
			}
			cout << "输入有误,请重新输入!" << endl;
		}


		//年龄
		int age;
		cout << "请输入年龄(不能超过120岁):" << endl;
		while (true)
		{
     
			cin >> age; //年龄不能超过120
			if (age >= 1 && age <= 120)
			{
     
				abs->personArray[abs->p_Size].p_Age = age;
				break;
			}
			cout << "输入有误,请重新输入" << endl;
		}
		
		//联系电话
		string phone;
		cout << "请输入联系电话:" << endl;
		cin >> phone;
		abs->personArray[abs->p_Size].p_Phone = phone;

		//家庭地址
		string address;
		cout << "请输入家庭住址:" << endl;
		cin >> address;
		abs->personArray[abs->p_Size].p_Addr = address;

		cout << "添加成功" << endl;
		abs->p_Size++;  //更新通讯录人数
	}

	system("pause");
	system("cls");
}

//显示联系人
void showPerson(Addrbooks *abs)
{
     
	//判断是否存在联系人
	if (abs->p_Size == 0)
	{
     
		cout << "当前没有联系人为空,无记录" << endl;
	}
	else
	{
     
		for (int i = 0; i < abs->p_Size; i++)
		{
     
			cout << "姓名:" << abs->personArray[i].p_Name
				<< "  性别:" << (abs->personArray[i].p_Sex == 1 ? "男" : "女")
				<< "  年龄:" << abs->personArray[i].p_Age
				<< "  联系电话:" << abs->personArray[i].p_Phone
				<< "  家庭住址:" << abs->personArray[i].p_Addr << endl;
		}
	}

	system("pause");
	system("cls");
}

//判断联系人是否存在
int isExist(Addrbooks *abs,string name)
{
     
	for (int i = 0; i < abs->p_Size; i++)
	{
     
		if (abs->personArray[i].p_Name == name)
		{
     
			return i;
		}
		return -1;
	}
}

//删除联系人  删除联系人之前需要判断联系人是否存在,
//故可以封装一个函数来判断联系人是否存在
void delPerson(Addrbooks *abs)
{
     
	if (abs->p_Size == 0)
	{
     
		cout << "当前联系人为空,无记录" << endl;
	}
	else
	{
     
		string name;
		cout << "请输入您要删除的联系人的姓名:" << endl;
		cin >> name;

		//ret == -1,查无此人
		//ret != -1,找到了并删除
		int ret = isExist(abs, name);

		if (ret != -1)
		{
     
			//数据前移(后面的盖住前面的来实现删除操作)
			for (int i = ret; i < abs->p_Size; i++)
			{
     
				abs->personArray[i] = abs->personArray[i + 1];
			}
			cout << "删除成功" << endl;

			abs->p_Size--; //更新通讯录人数
		}
		else
		{
     
			cout << "查无此人" << endl;
		}
	}
	
	system("pause");
	system("cls");
}

//查找联系人
void searchPerson(Addrbooks *abs)
{
     
	if (abs->p_Size == 0)
	{
     
		cout << "当前联系人为空,无记录" << endl;
	}
	else
	{
     
		cout << "请输入您要查找的联系人的姓名:" << endl;
		string name;
		cin >> name;

		int ret = isExist(abs, name);
		if (ret != -1)
		{
     
			cout << "姓名:" << abs->personArray[ret].p_Name
				<< "  性别:" << (abs->personArray[ret].p_Sex == 1 ? "男" : "女")
				<< "  年龄:" << abs->personArray[ret].p_Age
				<< "  联系电话:" << abs->personArray[ret].p_Phone
				<< "  家庭住址:" << abs->personArray[ret].p_Addr << endl;
		}
		else
		{
     
			cout << "查无此人" << endl;
		}
	}
	

	system("pause");
	system("cls");
}

//修改联系人
void modifyPerson(Addrbooks *abs)
{
     
	if (abs->p_Size == 0)
	{
     
		cout << "当前记录为空" << endl;
	}
	else
	{
     
		cout << "请输入您要修改的联系人的姓名:" << endl;
		string name;
		cin >> name;
		int flag;

		int ret = isExist(abs, name);
		if (ret != -1)
		{
     
			//姓名
			string name;
			cout << "是否修改姓名(1:是  0:否)" << endl;
			cin >> flag;
			if (flag == 1)
			{
     
				cout << "输入姓名:" << endl;
				cin >> name;
				abs->personArray[ret].p_Name = name;
			}

			//性别
			int sex;
			cout << "是否修改性别(1:是  0:否)" << endl;
			cin >> flag;
			if (flag == 1)
			{
     
				cout << "输入性别:" << endl;
				cout << "1---男" << endl;
				cout << "2---女" << endl;
				while (true)
				{
     
					cin >> sex;
					if (sex == 1 || sex == 2)
					{
     
						abs->personArray[ret].p_Sex = sex;
						break;
					}
					cout << "输入有误,重新输入" << endl;
				}
			}

			//年龄
			int age;
			cout << "是否修改年龄(1:是  0:否)" << endl;
			cin >> flag;
			if (flag == 1)
			{
     
				cout << "请输入年龄(不超过120岁):" << endl;
				while (true)
				{
     
					cin >> age;
					if (age > 0 & age <= 120)
					{
     
						abs->personArray[ret].p_Age = age;
						break;
					}
					cout << "输入有误,请重新输入" << endl;
				}
			}

			//联系电话
			string phone;
			cout << "是否修改联系电话(1:是  0:否)" << endl;
			cin >> flag;
			if (flag == 1)
			{
     
				cout << "输入联系电话:" << endl;
				cin >> phone;
				abs->personArray[ret].p_Phone = phone;
			}

			//家庭住址	
			string address;
			cout << "是否修改家庭住址(1:是  0:否)" << endl;
			cin >> flag;
			if (flag == 1)
			{
     
				cout << "输入家庭住址:" << endl;
				cin >> address;
				abs->personArray[ret].p_Addr = address;
			}
		}
		else
		{
     
			cout << "查无此人" << endl;
		}
	}
	
	system("pause");
	system("cls");
}

//清空联系人
void clearPerson(Addrbooks *abs)
{
     
	abs->p_Size = 0;
	cout << "联系人已清空" << endl;
	system("pause");
	system("cls");
}

int main()
{
     
	int select;

	//创建通讯录结构体
	Addrbooks abs;

	while (true)
	{
     
		//菜单显示
		showMenu();
		cout << "请输入您要执行的功能选项:" <<" ";
		cin >> select;

		switch (select)
		{
     
		case 1://1.添加联系人
			addPerson(&abs);
			break;
		case 2://2.显示联系人
			showPerson(&abs);
			break;
		case 3://3.删除联系人
			delPerson(&abs);
			break;
		case 4://4.查找联系人
			searchPerson(&abs);
			break;
		case 5://5.修改联系人
			modifyPerson(&abs);
			break;
		case 6://6.清空联系人
			clearPerson(&abs);
			break;
		case 0://0.退出系统
			cout << "欢迎下次使用" << endl;
			system("pause");
			return 0;
			break;
		default:
			break;
		}
	}
	
	system("pause");
	return 0;
}

你可能感兴趣的:(C++)