欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C++通讯录管理系统

程序员文章站 2024-03-14 18:28:04
...

直接上代码:

#include<iostream>
using namespace std;
#include<string>
#define MAX 1000
void showMenu()
{
	cout << "***** 1:添加联系人 *****" << endl;
	cout << "***** 2:显示联系人 *****" << endl;
	cout << "***** 3:删除联系人 *****" << endl;
	cout << "***** 4:查找联系人 *****" << endl;
	cout << "***** 5:修改联系人 *****" << endl;
	cout << "***** 6:清空联系人 *****" << endl;
	cout << "***** 0:退出通讯录 *****" << endl;
}
struct person 
{
	string m_name;
	int m_sex;
	int m_age;
	string m_phone;
	string m_addr;

};
struct addressBooks
{
	struct person personArray[MAX];
	int size;
};
void addPerson(addressBooks * abs )
{
	if (abs->size > MAX)
	{
		cout << "通讯录已满" << endl;
	}
	else
	{
		string name, phone, addr;
		cout << "请输入姓名:";
		cin >> name;
		abs->personArray[abs->size].m_name = name;
		int sex, age;
		cout << "请输入性别,1代表男性,2代表女性:";		
		cin >> sex;
		abs->personArray[abs->size].m_sex = sex;
		cout << "请输入年龄:";
		cin >> age;
		abs->personArray[abs->size].m_age = age;
		cout << "请输入电话号码:";
		cin >> phone;
		abs->personArray[abs->size].m_phone = phone;
		cout << "请输入住址:";
		cin >> addr;
		abs->personArray[abs->size].m_addr = addr;
		abs->size++;
		cout << "添加成功" << endl;
		system("pause");
		system("cls");
	}
	
}
void showPerson(addressBooks* abs)
{
	if (abs->size== 0)
	{
		cout << "通讯录为空!" << endl;
	}
	else
	{
		for (int i = 0; i < abs->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 ifExit(addressBooks * abs,string name)
{
	for (int i = 0; i < abs->size; i++)
	{
		if (abs->personArray[i].m_name == name)
		{
			return i;
		}
	}
	return -1;
}
void delectPerson(addressBooks* abs)
{
	string name;
	cout << "请输入您想删除的联系人姓名:";
	cin >> name;
	int a= ifExit(abs, name);
	if (a == -1)
	{
		cout << "通讯录中无此人!" << endl;
	}
	else
	{
		for (int i = a; i < abs->size; i++)
		{
				abs->personArray[i] = abs->personArray[i + 1];
		}
		abs->size--;
		cout << "删除成功!" << endl;
	}
	system("pause");
	system("cls");
}
void searchPerson(addressBooks* abs, string name)
{
	int a = ifExit(abs, name);
	if (a == -1)
	{
		cout << "通讯录中无此人!" << endl;
	}
	else
	{
			cout << "姓名:" << abs->personArray[a].m_name << '\t';
			cout << "性别:" << (abs->personArray[a].m_sex == 1 ? "男" : "女") << '\t';
			cout << "年龄:" << abs->personArray[a].m_age << '\t';
			cout << "号码:" << abs->personArray[a].m_phone << '\t';
			cout << "住址:" << abs->personArray[a].m_addr << endl;
	}
	system("pause");
	system("cls");
}
void revisePerson(addressBooks* abs, string name)
{
	int a = ifExit(abs, name);
	if (a == -1)
	{
		cout << "通讯录中无此人!" << endl;
	}
	else
	{
		string name, phone, addr;
		cout << "请重新输入姓名:";
		cin >> name;
		abs->personArray[a].m_name = name;
		int sex, age;
		cout << "请重新输入性别,1代表男性,2代表女性:";
		cin >> sex;
		abs->personArray[a].m_sex = sex;
		cout << "请重新输入年龄:";
		cin >> age;
		abs->personArray[a].m_age = age;
		cout << "请重新输入电话号码:";
		cin >> phone;
		abs->personArray[a].m_phone = phone;
		cout << "请重新输入住址:";
		cin >> addr;
		abs->personArray[a].m_addr = addr;
		cout << "修改成功!" << endl;
	}
        system("pause");
		system("cls");
}
void cleanPerson(addressBooks* abs)
{
	
	abs->size = 0;
	cout << "通讯录已清空!" << endl;
	system("pause");
	system("cls");
}
int main()
{
	addressBooks abs;
	abs.size = 0;
	int select;
	while (1)
	{
		showMenu();
		cout << "请输入你的选择:";
		cin >> select;
		switch (select)
		{
		case 1:
			addPerson(&abs);
			break;
		case 2:
			showPerson(&abs);
			break;
		case 3:
			delectPerson(&abs);
			break;
		case 4:
		{
			string name;
			cout << "请输入您要查找的联系人姓名:";
			cin >> name;
			searchPerson(&abs, name);
			break;
		}
		case 5:
		{
			string name;
			cout << "请输入您要修改的联系人姓名:";
			cin >> name;
			revisePerson(&abs, name);
			break;
		}
			break;
		case 6:
			cleanPerson(&abs);
			break;
		case 0:
			cout << "欢迎下次使用" << endl;
			return 0;
			break;
		}
	}
}
相关标签: 恰饭