通讯录管理系统c++代码实现

一.分析

1.需求

我们运行运行代码,需要打印出功能选择界面以便于我们操作通讯录,然后输入对应的数字实现各功能的操作,然后将我们的通讯录数据保存在电脑上,下次运行通讯录不会丢失之前数据。

2.各操作功能的实现

实现通讯录管理系统的各种操作,需要利用c++的封装性,将各类功能利用函数封装,在函数体内利用代码实现功能

3.通讯录数据持久化

利用IO流保存每次运行通讯录修改的数据,使得数据持久化!

二.代码实现(有详细注释!)

#include
#include
using namespace std;
const int 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 Name;//姓名
	int Sex;//性别:1男 2女
	int Age;//年龄
	string Phone;//电话
	string Addr;//住址

};
//通讯录结构体
struct AddressBooks {
	struct Person personArray[MAX];//保存的联系人数组
	int Size;//通讯录人员个数
};
//1.添加联系人
void addPerson(AddressBooks* abs) {
	//判断通讯录是否已满
	if (abs->Size == MAX)
	{
		cout << "通讯录已满,无法添加!" << endl;
	}
	else//添加联系人
	{
		//姓名
		string name;
		cout << "请输入姓名:" << endl;
		cin >> name;
		abs->personArray[abs->Size].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->Size].Sex = sex;
				break;
			}
			cout << "输入有误,请重新输入!" << endl;
		}
		//年龄
		cout << "请输入年龄:" << endl;
		int age = 0;
		cin >> age;
		abs->personArray[abs->Size].Age = age;
		
		//电话
		cout << "请输入联系电话:" << endl;
		string phone;
		cin >> phone;
		abs->personArray[abs->Size].Phone = phone;
		//住址
		cout << "请输入家庭住址:" << endl;
		string addr;
		cin >> addr;
		abs->personArray[abs->Size].Addr = addr;

		//更新通讯录人数个数
		abs->Size++;
		cout << "添加成功!" << endl;
		system("pause");
		system("cls");
	}
}
//2.显示所有联系人
void showPerson(AddressBooks* abs) {
//判断通讯录中人数是否为0
	if (abs->Size == 0)
	{
		cout << "当前记录为空" << endl;

	}
	else
	{
		for (int i = 0; i < abs->Size; i++)
		{
			cout << "姓名:" << abs->personArray[i].Name<<"\t";
			cout << "性别:" << (abs->personArray[i].Sex==1?"男":"女" )<< "\t";
			cout << "年龄:" << abs->personArray[i].Age << "\t";
			cout << "联系电话:" << abs->personArray[i].Phone << "\t";
			cout << "家庭住址:" << abs->personArray[i].Addr<< endl;

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

//检测联系人
int isExist(AddressBooks* abs, string name)
{
	for (int i = 0; i < abs->Size; i++)
	{
		//遍历一遍
		if (abs->personArray[i].Name == name)
		{
			return i;
		}
	}
	return -1;
}
//3.删除联系人
void deletePerson(AddressBooks* abs) {
cout << "请输入删除联系人姓名:" << endl;
string name;
cin >> name;
int ret = isExist(abs, name);

if (ret != -1)
{
	for (int i = ret; i < abs->Size; i++)
	{
		//数据前移
		abs->personArray[i] = abs->personArray[i + 1];
	}
	abs->Size--;//更新人员数目
	cout << "删除成功!" << endl;
}
else
{
	cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
//4.查找联系人
void findPerson(AddressBooks* abs) {
	cout << "请输入需要查找的联系人" << endl;
	string name;
	cin >> name;

	//判断指定联系人是否存在
	int ret = isExist(abs, name);
	if (ret != -1)
	{
		cout << "姓名:" << abs->personArray[ret].Name << "\t";
		cout << "性别:" << (abs->personArray[ret].Sex == 1 ? "男" : "女") << "\t";
		cout << "年龄:" << abs->personArray[ret].Age << "\t";
		cout << "联系电话:" << abs->personArray[ret].Phone << "\t";
		cout << "家庭住址:" << abs->personArray[ret].Addr << endl;;
	}
	else
	{
		cout << "查无此人" << endl;
	}
	system("pause");
	system("cls");

}
//5.修改指定联系人
void modifyPerson(AddressBooks* abs) {
	cout << "请输入需要修改的联系人" << endl;
	string name;
	cin >> name;
	//判断指定联系人是否存在
	int ret = isExist(abs, name);
	if (ret != -1)
	{
		cout << "请输入你需要修改的信息:" << endl;
		cout << "1.姓名" << endl;
		cout << "2.性别" << endl;
		cout << "3.年龄" << endl;
		cout << "4.联系电话" << endl;
		cout << "5.家庭住址" << endl;
		cout << "0.退出" << endl;
		while (true) {
			int k;
			cin >> k;
			if (k == 0)
				break;
			switch (k)
			{
			case 1:
			{
				string name;
				cout << "请输入姓名:" << endl;
				cin >> name;
				abs->personArray[ret].Name = name;
			}break;
			case 2:
			{
				cout << "请输入性别:" << endl;
				cout << "1--男" << endl;
				cout << "2--女" << endl;
				int sex = 0;
				while (true)
				{
					cin >> sex;
					if (sex == 1 || sex == 2)
					{
						abs->personArray[ret].Sex = sex;
						break;
					}
					cout << "输入有误,请重新输入!" << endl;
				}
			}break;
			case 3:
			{
				cout << "请输入年龄:" << endl;
				int age = 0;
				cin >> age;
				abs->personArray[ret].Age = age;
			}break;
			case 4:
			{
				cout << "请输入联系电话:" << endl;
				string phone;
				cin >> phone;
				abs->personArray[ret].Phone = phone;
			}break;
			case 5:
			{
				cout << "请输入家庭住址:" << endl;
				string addr;
				cin >> addr;
				abs->personArray[ret].Addr = addr;
			}break;
			}
			cout << "修改成功!输入0退出或者继续修改。" << endl;
		}

	}
	system("pause");
	system("cls");
}
//6.清空联系人
void cleanPerson(AddressBooks* abs)
{
	abs->Size = 0;
	cout << "通讯录已清空" << endl;
	system("pause");
	system("cls");
}


	int main()
{

	//创建一个通讯录结构体变量
	AddressBooks abs;
	abs.Size = 0;
	int select = 0;//创建用户选择初始变量
	while (1) {
		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;
	

}

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