通讯录管理系统——黑马程序员案例

// 封装函数显示该界面 void showMenu()
// 在main函数中调用封装好的函数

#include<iostream>
#include <string>
#include<cstdlib>
#define MAX 1000 //最大人数
using namespace std;

// 1.显示菜单
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 m_Name;
	int m_Sex;
	int m_Age;
	string m_Phone;
	string m_Add;
};
// 设计通讯录结构体
struct Adressbooks {
	struct Person personArray[MAX]; //通讯录中保存联系人数组;
	int m_Size; //通讯录中人员个数
};

void addperson(Adressbooks * 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 ---男,2 ---女" << endl;
		int sex = 0;
		// while无限循环,如果if为真,break跳出循环,否则重新输入
		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;
		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 add;
		cin >> add;
		abs->personArray[abs->m_Size].m_Add = add;
		//更新通讯录人数
		abs->m_Size++;
		cout << "添加成功" << endl;

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

// 显示联系人
void showperson(Adressbooks* abs)
{
	//判断通讯录中人数是否为0,如果为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"
				<< "性别:" << (abs->personArray[i].m_Sex == 1 ? "男" : "女" ) << "\t"
				<< "年龄:" << abs->personArray[i].m_Age << "\t"
				<< "电话:" << abs->personArray[i].m_Phone << "\t"
				<< "住址:" << abs->personArray[i].m_Add << "\t" << endl;
		}
	}
	system("pause");
	system("cls");
}

//检测联系人是否存在
int detectperson(Adressbooks* abs, string name)
{
	for (int i = 0; i < abs->m_Size; i++)
	{
		if (abs->personArray[i].m_Name == name)
		{
			//cout << "联系人存在" << endl;
			return i;
		}
	}
	return -1;
	//cout << "联系人不存在" << endl;
	system("pause");
	system("cls");
}

void deletperson(Adressbooks* abs)
{
	string name;
	cout << "请输入您要删除的联系人:" << endl;
	cin >> name;
	//调用是否有该联系人的函数;
	int ret = detectperson(abs, name);
	if (ret == -1)
	{
		cout << "查无此人请重新输入:" << endl;
	}
	else
	{
		char anser;
		cout << "你真的确定你要删除他吗?(Y or N ?)" << endl;
		cin >> anser;
		if (anser == 'Y')
		{
			for (int j = 0; j < abs->m_Size; j++)
			{
				abs->personArray[j] = abs->personArray[j + 1];  //将后面的联系人移动到前面就代表此联系人已经删除;
			}
			cout << "删除" << abs->personArray[abs->m_Size].m_Name << "的信息成功" << endl;
			abs->m_Size--; //更新通讯录中的人数;
		}
		system("pause");
		system("cls");
	}
}

void findperson(Adressbooks* abs)
{
	cout << "请输入您要查找的人的姓名:" << endl;
	string name;
	cin >> name;
	int ret = detectperson(abs, name);
	if (ret == -1)
	{
		cout << "查无此人请重新输入:" << endl;
	}
	else
	{
		cout << "姓名:" << abs->personArray[ret].m_Name << "\t"
			<< "性别:" << (abs->personArray[ret].m_Sex == 1 ? "男" : "女") << "\t"
			<< "年龄:" << abs->personArray[ret].m_Age << "\t"
			<< "电话:" << abs->personArray[ret].m_Phone << "\t"
			<< "住址:" << abs->personArray[ret].m_Add << "\t" << endl;
	}
}

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

int main() 
{
	// 创建通讯录结构体变量
	struct Adressbooks abs;
	abs.m_Size = 0;

	while (true)
	{
		showMenu();
		int select = 0;
		cin >> select;
		switch (select)
		{
		case 1: //添加联系人
			addperson(&abs);
			break;
		case 2:
			showperson(&abs); //显示联系人;
			break;
		case 3:
			deletperson(&abs); //删除联系人;
			break;
		case 4:
			findperson(&abs); //查找联系人;
			break;
		case 5:
			                 // 修改联系人(略)
			break;
		case 6:
			cleanperson(&abs); //清空联系人;
			break;
		case 0:
			cout << "欢迎下次使用" << endl;
			system("pause");
			return 0;
			break;
		default:
			break;
		}
	}
	system("pause");
	return 0;
}

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值