c++ |通讯录信息管理项目

可实现的功能

菜单显示,添加、查找、显示、修改联系人,通讯录清空功能。属于一次性通讯录。
参考学习于B站黑马程序员
#include 
#include 

using namespace std;
#define MAX_CAPACITY 1000
//联系人结构体
struct Person {
	string m_Name;
	int m_Age;
	string m_Number;
	string m_Addr;
};

//通讯录结构体
struct AddrBook {
	Person m_Addrbook[MAX_CAPACITY];
	int m_CurPerson;
};

void addPerson(AddrBook& m_book1_local)
{
	bool Continue_flag = 1;
	string name,number,addr;
	int age;
	//添加联系人,首先要判断通讯录容量是否超出
	while (Continue_flag)
	{
		if (m_book1_local.m_CurPerson == MAX_CAPACITY)
		{
			cout << "此次添加失败,通讯录人数已满!";
			system("pause");
			return;
		}
		else
		{
			cout << "请输入姓名:" << endl;
			cin >> name;
			m_book1_local.m_Addrbook[m_book1_local.m_CurPerson].m_Name = name;

			cout << "请输入年龄:" << endl;
			cin >> age;
			m_book1_local.m_Addrbook[m_book1_local.m_CurPerson].m_Age = age;

			cout << "请输入电话号码" << endl;
			cin >> number;
			m_book1_local.m_Addrbook[m_book1_local.m_CurPerson].m_Number = number;

			cout << "请输入家庭住址" << endl;
			cin >> addr;
			m_book1_local.m_Addrbook[m_book1_local.m_CurPerson].m_Addr = addr;

			++m_book1_local.m_CurPerson;

		}
		while(1)
		{
			cout << "请问是否继续添加新联系人?(1—是 or 0—否)" << endl;
			cin >> Continue_flag;
			if (Continue_flag != 1 && Continue_flag != 0)
				cout << "输入有误,请重新输入" << endl;
			else
				break;
		}		
	}
	system("pause");
	system("cls");
	
}

void showPerson(AddrBook* p_book)
{
	if (p_book->m_CurPerson)
	{
		for (int i = 0; i < p_book->m_CurPerson; ++i)
		{
			cout << "姓名:" << p_book->m_Addrbook[i].m_Name << "\t";
			cout << "年龄:" << p_book->m_Addrbook[i].m_Age << "\t";
			cout << "电话:" << p_book->m_Addrbook[i].m_Number << "\t";
			cout << "地址:" << p_book->m_Addrbook[i].m_Addr << "\n";
		}
	}
	else
	{
		cout << "此为空通讯录!";
	}
	system("pause");
	system("cls");
}

//查找某个联系人是否存在 存在返回位置 不存在返回-1
int is_Exist(AddrBook* p_book, string name)
{
	for (int i = 0; i < p_book->m_CurPerson; ++i)
	{
		if (p_book->m_Addrbook[i].m_Name == name)
			return i;
	}
	//cout << "查无此人!" << endl;
	//system("pause");
	//system("cls");
	return -1;
}

void deletePerson(AddrBook* p_book)
{
	string name;
	cout << "请输入想要删除的联系人:" << endl;
	cin >> name;
	if(is_Exist(p_book, name)==-1)
		cout << "查无此人!" << endl;
	else
	{
		for (int i = is_Exist(p_book, name); i < p_book->m_CurPerson; ++i)
			p_book->m_Addrbook[i] = p_book->m_Addrbook[i + 1];
	}
	--p_book->m_CurPerson;
	cout << "成功删除!" << endl;
	system("pause");
	system("cls");
}

void findPerson(AddrBook* p_book)
{
	string name;
	cout << "请输入想要查找的联系人:" << endl;
	cin >> name;
	int find_index = is_Exist(p_book, name);
	if (find_index == -1)
		cout << "查无此人!" << endl;
	else
	{
		cout << "查到此人,其信息如下:" << endl;
		cout << "姓名:" << p_book->m_Addrbook[find_index].m_Name << "\t";
		cout << "年龄:" << p_book->m_Addrbook[find_index].m_Age << "\t";
		cout << "电话:" << p_book->m_Addrbook[find_index].m_Number << "\t";
		cout << "地址:" << p_book->m_Addrbook[find_index].m_Addr << "\n";
	}
	system("pause");
	system("cls");
}

void modifyPerson(AddrBook* p_book)
{
	string name;
	bool error_flag=0;
	cout << "请输入想要修改的联系人:" << endl;
	cin >> name;
	int find_index = is_Exist(p_book, name);
	if (find_index == -1)
		cout << "查无此人!" << endl;
	else
	{
		int obj;
		while(1)
		{
			cout << "1-修改姓名" << "\t" << "2-修改年龄" << "\t" << "3-修改电话" << "\t" << "4-修改地址" << endl;
			cout << "请输入您想要修改内容对应数字:" << endl;
			cin >> obj;
			switch (obj)
			{
			case 1:
				cout << "姓名修改为:" << endl;
				cin >> p_book->m_Addrbook[find_index].m_Name;
				break;
			case 2:
				cout << "年龄修改为:" << endl;
				cin >> p_book->m_Addrbook[find_index].m_Age;
				break;
			case 3:
				cout << "电话:" << endl;
				cin >> p_book->m_Addrbook[find_index].m_Number;
				break;
			case 4:
				cout << "地址:" << endl;
				cin >> p_book->m_Addrbook[find_index].m_Addr;
				break;
			default:
				cout << "输入有误,请重新输入" << endl;
				error_flag = 1;
				break;
			}
			if (error_flag)
				continue;
			else
			{
				while (1)
				{
					cout << "是否需要修改该联系人的其他信息?(yes or no)" << endl;
					string anser;
					cin >> anser;
					if (anser == "yes" || anser == "YES")
					{
						break;
					}
					else if (anser == "no" || anser == "NO")
					{
						cout << "修改完成!" << endl;
						system("pause");
						system("cls");
						return;
					}
					else
					{
						cout << "输入有误请重新输入" << endl;
					}
				}
			}
		}
	}
	cout << "修改完成!" << endl;
	system("pause");
	system("cls");
}
void clearPeraon(AddrBook* p_book)
{
	string ackFlag;
	while(1)
	{
		cout << "是否确认清空通讯录?(yes or no)" << endl;
		cin >> ackFlag;
		if (ackFlag == "yes" || ackFlag == "YES")
		{
			p_book->m_CurPerson = 0;
			cout << "通讯录已清空!" << endl;
			system("pause");
			system("cls");
			return;
		}
		else if (ackFlag == "no" || ackFlag == "NO")
		{
			cout << "取消清空" << endl;
			system("pause");
			system("cls");
			return;
		}
		else
		{
			cout << "输入有误请重新输入" << endl;
		}
	}
}

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() {

	AddrBook m_book1;
	m_book1.m_CurPerson = 0;
	while(1)
	{
		showmenu();
		int select = 0;
		cout << "请输入您想要实现的功能:";
		cin >> select;
		switch (select)
		{
		case 1:
			addPerson(m_book1);
			break;
		case 2:
			showPerson(&m_book1);
			break;
		case 3:
			deletePerson(&m_book1);
			break;
		case 4:
			findPerson(&m_book1);
			break;
		case 5:
			modifyPerson(&m_book1);
			break;
		case 6:
			clearPeraon(&m_book1);
			break;
		case 0:
			cout << "欢迎下次使用,再见!" << endl;
			system("pause");
			return 0;
			break;
		default:
			cout << "输入有误请重新输入!" << endl;
			break;
		}
	}
	system("pause");
	return 0;
}

你可能感兴趣的:(C++学习记录,c++,开发语言)