C++入门——通讯录管理系统

参考链接

  1. https://www.bilibili.com/video/BV1et411b73Z?p=72

系统需求

系统需要实现的功能如下:

  1. 添加联系人:向通讯录中添加信任,信息包括姓名、性别、年龄、联系电话、家庭住址。最多记录1000人;
  2. 显示联系人:显示通讯录中所有联系人信息;
  3. 删除联系人:按照姓名进行删除指定联系人;
  4. 查找联系人:按照姓名查看指定联系人信息;
  5. 修改联系人:按照姓名重新修改指定联系人;
  6. 清空联系人:清空通讯录中所有信息;
  7. 退出通讯录:退出当前使用的通讯录。

菜单功能

简单打印菜单界面。

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;
}

退出功能

系统使用switch分支结构,当用户选择0的时候,执行退出。

while (true)
	{
		showMenu();
		cin >> select;

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

添加联系人

首先需要判断通讯录是否已满,如果未满,则按提示输入联系人信息。对性别、年龄的输入需要排除非法输入。

void addPerson(Addressbooks *abs)
{
	// 判断通讯录是否已满
	if (abs->m_Size == MAX)
	{
		cout << "通讯录已满,无法添加" << endl;
		return;
	}
	else
	{
		// 添加具体联系人
		
		// 姓名
		string name;
		cout << "请输入姓名:" << endl;
		cin >> name;
		abs->personArray[abs->m_Size].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[abs->m_Size].m_Sex = sex;
				break;
			}
			cout << "输入有误,请重新输入" << endl;
		}

		// 年龄
		cout << "请输入年龄:" << endl;
		int age = 0;
		while (true)
		{
			cin >> age;
			if (age >= 0 && age <= 150)
			{
				abs->personArray[abs->m_Size].m_Age = age;
				break;
			}
			else
			{
				cout << "输入有误,请重新输入" << endl;
			}
		}

		// 电话
		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;

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

		cout << "添加成功" << endl;

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

显示联系人

首先要判断通讯录是否为空,如果非空,则遍历联系人数组进行输出。需要对性别输出做处理,提高可读性。

void showPerson(Addressbooks *abs)
{
	// 判断通讯录中人数是否为0
	if (abs->m_Size == 0)
	{
		cout << "当前记录为空" << endl;
	}
	else
	{
		for (int i = 0; i < abs->m_Size; i++)
		{
			cout << "姓名:" << abs->personArray[i].m_Name << "\t";
			cout << "性别:" << (abs->personArray[i].m_Sex == 1 ? "男" : "女") << "\t";
			cout << "年龄:" << abs->personArray[i].m_Age << "\t";
			cout << "电话:" << abs->personArray[i].m_Phone << "\t";
			cout << "住址:" << abs->personArray[i].m_Addr << endl;
		}
	}
	system("pause");
	system("cls");
}

删除联系人

首先要判断联系人是否存在,如果存在,则删除。具体地,将联系人数组中指定联系人后面的数据前移一位。

void deletePerson(Addressbooks *abs)
{
	cout << "请输入您要删除的联系人" << endl;

	string name;
	cin >> name;

	int res = isExist(abs, name);
	if (res != -1)
	{
		for (int i = res; i < abs->m_Size - 1; i++)
		{
			abs->personArray[i] = abs->personArray[i + 1];
		}
		abs->m_Size--;
		cout << "删除成功" << endl;
	}
	else
	{
		cout << "查无此人" << endl;
	}

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

查找联系人

先判断指定联系人是否存在,如果存在则输出对应的信息。

void findPerson(Addressbooks *abs)
{
	cout << "请输入您要查找的联系人" << endl;
	string name;
	cin >> name;

	// 判断联系人是否存在
	int res = isExist(abs, name);
	if (res != -1)
	{
		cout << "姓名:" << abs->personArray[res].m_Name << "\t";
		cout << "性别:" << (abs->personArray[res].m_Sex == 1 ? "男" : "女") << "\t";
		cout << "年龄:" << abs->personArray[res].m_Age << "\t";
		cout << "电话:" << abs->personArray[res].m_Phone << "\t";
		cout << "住址:" << abs->personArray[res].m_Addr << endl;
	}
	else
	{
		cout << "查无此人" << endl;
	}
	system("pause");
	system("cls");
}

修改联系人

首先判断通讯录是否存在指定联系人,如果存在,则按类似添加联系人的操作,按提示输入修改后的联系人信息。

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

	int res = isExist(abs, name);
	if (res != -1)
	{
		// 姓名
		string name;
		cout << "请输入姓名:" << endl;
		cin >> name;
		abs->personArray[res].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[res].m_Sex = sex;
				break;
			}
			cout << "输入有误,请重新输入" << endl;
		}

		// 年龄
		cout << "请输入年龄:" << endl;
		int age = 0;
		while (true)
		{
			cin >> age;
			if (age >= 0 && age <= 150)
			{
				abs->personArray[res].m_Age = age;
				break;
			}
			else
			{
				cout << "输入有误,请重新输入" << endl;
			}
		}

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

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

		cout << "修改成功" << endl;
	}
	else
	{
		cout << "查无此人" << endl;
	}
	system("pause");
	system("cls");
}

清空联系人

只需要将通讯录中的联系人数量置为0,做逻辑清空。

void cleanPerson(Addressbooks *abs)
{
	abs->m_Size = 0;
	cout << "通讯录已清空" << endl;
	system("pause");
	system("cls");
}

完整代码

#include <iostream>
using namespace std;
#include <string>
#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;
};

void addPerson(Addressbooks *abs)
{
	// 判断通讯录是否已满
	if (abs->m_Size == MAX)
	{
		cout << "通讯录已满,无法添加" << endl;
		return;
	}
	else
	{
		// 添加具体联系人
		
		// 姓名
		string name;
		cout << "请输入姓名:" << endl;
		cin >> name;
		abs->personArray[abs->m_Size].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[abs->m_Size].m_Sex = sex;
				break;
			}
			cout << "输入有误,请重新输入" << endl;
		}

		// 年龄
		cout << "请输入年龄:" << endl;
		int age = 0;
		while (true)
		{
			cin >> age;
			if (age >= 0 && age <= 150)
			{
				abs->personArray[abs->m_Size].m_Age = age;
				break;
			}
			else
			{
				cout << "输入有误,请重新输入" << endl;
			}
		}

		// 电话
		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;

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

		cout << "添加成功" << endl;

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

void showPerson(Addressbooks *abs)
{
	// 判断通讯录中人数是否为0
	if (abs->m_Size == 0)
	{
		cout << "当前记录为空" << endl;
	}
	else
	{
		for (int i = 0; i < abs->m_Size; i++)
		{
			cout << "姓名:" << abs->personArray[i].m_Name << "\t";
			cout << "性别:" << (abs->personArray[i].m_Sex == 1 ? "男" : "女") << "\t";
			cout << "年龄:" << abs->personArray[i].m_Age << "\t";
			cout << "电话:" << abs->personArray[i].m_Phone << "\t";
			cout << "住址:" << abs->personArray[i].m_Addr << endl;
		}
	}
	system("pause");
	system("cls");
}

// 检测联系人是否存在
int isExist(Addressbooks *abs, string name)
{
	for (int i = 0; i < abs->m_Size; i++)
	{
		if (abs->personArray[i].m_Name == name)
		{
			return i;
		}
		return -1;
	}
}

// 删除指定联系人
void deletePerson(Addressbooks *abs)
{
	cout << "请输入您要删除的联系人" << endl;

	string name;
	cin >> name;

	int res = isExist(abs, name);
	if (res != -1)
	{
		for (int i = res; i < abs->m_Size - 1; i++)
		{
			abs->personArray[i] = abs->personArray[i + 1];
		}
		abs->m_Size--;
		cout << "删除成功" << endl;
	}
	else
	{
		cout << "查无此人" << endl;
	}

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

// 查找指定联系人
void findPerson(Addressbooks *abs)
{
	cout << "请输入您要查找的联系人" << endl;
	string name;
	cin >> name;

	// 判断联系人是否存在
	int res = isExist(abs, name);
	if (res != -1)
	{
		cout << "姓名:" << abs->personArray[res].m_Name << "\t";
		cout << "性别:" << (abs->personArray[res].m_Sex == 1 ? "男" : "女") << "\t";
		cout << "年龄:" << abs->personArray[res].m_Age << "\t";
		cout << "电话:" << abs->personArray[res].m_Phone << "\t";
		cout << "住址:" << abs->personArray[res].m_Addr << endl;
	}
	else
	{
		cout << "查无此人" << endl;
	}
	system("pause");
	system("cls");
}

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

	int res = isExist(abs, name);
	if (res != -1)
	{
		// 姓名
		string name;
		cout << "请输入姓名:" << endl;
		cin >> name;
		abs->personArray[res].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[res].m_Sex = sex;
				break;
			}
			cout << "输入有误,请重新输入" << endl;
		}

		// 年龄
		cout << "请输入年龄:" << endl;
		int age = 0;
		while (true)
		{
			cin >> age;
			if (age >= 0 && age <= 150)
			{
				abs->personArray[res].m_Age = age;
				break;
			}
			else
			{
				cout << "输入有误,请重新输入" << endl;
			}
		}

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

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

		cout << "修改成功" << endl;
	}
	else
	{
		cout << "查无此人" << endl;
	}
	system("pause");
	system("cls");
}

// 清空所有联系人
void cleanPerson(Addressbooks *abs)
{
	abs->m_Size = 0;
	cout << "通讯录已清空" << endl;
	system("pause");
	system("cls");
}

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;
}

int main()
{
	// 创建通讯录结构体变量
	Addressbooks abs;
	// 初始化通讯录中当前人员个数
	abs.m_Size = 0;

	int select = 0; // 创建用户选择输入的变量

	while (true)
	{
		showMenu();
		cin >> select;

		switch (select)
		{
		case 1:  // 1.添加联系人
			addPerson(&abs);
			break;
		case 2:  // 2.显示联系人
			showPerson(&abs);
			break;
		case 3:  // 3.删除联系人
			deletePerson(&abs);
			break;
		case 4:  // 4.查找联系人
			findPerson(&abs);
			break;
		case 5:  // 5.修改联系人
			modifyPerson(&abs);
			break;
		case 6:  // 6.清空联系人
			cleanPerson(&abs);
			break;
		case 0:  // 0.退出通讯录
			cout << "欢迎下次使用" << endl;
			system("pause");
			return 0;
			break;
		default:
			break;
		}
	}
	
	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值