通讯录管理系统

205 阅读1分钟
#include <iostream>
using namespace std;
#include<string>
#define max 1000
//联系人结构体
struct person
{
	string m_name;
	int m_sex;//1表示男,2表示女
	int m_age;
	string m_phone;
	string m_addr;

};
//通讯录结构体
struct addressbooks
{   //通讯录中保存联系人的数组
	struct person perarray[max];
	//通讯录中的人数
	int m_size;
};
//1.添加联系人
void addperson(addressbooks* abs)
{
	if (abs->m_size == max)
	{//判断通讯录是否满了
		cout << "通讯录已满,无法添加" << endl;
		return;
	}
	else
	{//添加信息
		//姓名
		string name;
		cout << "请输入姓名" << endl;
		cin >> name;
		abs->perarray[abs->m_size].m_name = name;
		//性别
		cout << "请输入性别" << endl;
		cout << "1---man" << endl;
		cout << "2---woman" << endl;
		int sex = 0;
		while (true)
		{//如果输入1或者2推出循环
			cin >> sex;
			if (sex == 1 || sex == 2)
			{
				abs->perarray[abs->m_size].m_sex = sex;
				break;
			}
			//输入错误一直循环
			else
			{
				cout << "输入错误,请重新输入" << endl;
			}
		}

		//年龄
		cout << "请输入年龄" << endl;
		int age = 0;
		cin >> age;
		abs->perarray[abs->m_size].m_age = age;


		//电话
		cout << "请输入电话" << endl;
		string phone;
		cin >> phone;
		abs->perarray[abs->m_size].m_phone = phone;

		//地址
		cout << "请输入地址" << endl;
		string adress;
		cin >> adress;
		abs->perarray[abs->m_size].m_addr = adress;
		//更新通讯录人数
		abs->m_size++;
		cout << "添加成功" << endl;
		//清屏操作
		system("pause");//按任意键继续
		system("cls");//清屏


	}
}
//检测联系人是否存在,如果存在输出联系人在数组中的位置,不存在输出-1
int isExist(addressbooks* abs, string name)
{
	for (int i = 0; i < abs->m_size; i++)
	{//如果相等输出数组中的位置
		if (abs->perarray[i].m_name == name)
		{
			return i;
		}
		//查找不到输出-1
		else
		{
			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->m_size; i++)
		{
			//数据迁移
			abs->perarray[i] = abs->perarray[i+1];
		}
		abs->m_size--;
		cout << "删除成功" << endl;
	}
	else//查无此人
	{
		cout << "查无此人" << endl;
	}
		

}
//4.查找联系人
void findperson(addressbooks* abs)
{
	cout << "请输入您要查找的联系人" << endl;
	string name;
	cin >> name;
	int ret = isExist(abs, name);
	if (ret != -1)//查找到联系人
	{
		cout << "已查找到" << "\t";
		cout << "姓名:" << abs->perarray[ret].m_name << endl;
		cout << "性别:" << abs->perarray[ret].m_sex << endl;
		cout << "年纪:" << abs->perarray[ret].m_age << endl;
		cout << "电话:" << abs->perarray[ret].m_phone << endl;
		cout << "地址:" << abs->perarray[ret].m_addr << endl;
	}
	else
	{
		cout << "查无此人" << endl;
	}
	system("pause");
	system("cls");
}
//菜单界面
void showmenu()
{
	cout << "1、添加联系人" << endl;
	cout << "2、显示联系人" << endl;
	cout << "3、删除联系人" << endl;
	cout << "4、查找联系人" << endl;
	cout << "5、修改联系人" << endl;
	cout << "6、清空联系人" << endl;
	cout << "0、退出通讯录" << endl;
}
void showperson(addressbooks* abs)
{
	//判断通讯录人数是否为零
	if (abs->m_size == 0)
	{
		cout << "当前记录为空" << endl;
	}
	else
	{
		for (int i = 0; i < abs->m_size; i++)
		{
			cout << "姓名:" << abs->perarray[i].m_name << endl;
			cout << "性别:" << (abs->perarray[i].m_sex == 1 ? "男" : "女") << endl;
			cout << "年龄:" << abs->perarray[i].m_age << endl;
			cout << "电话:" << abs->perarray[i].m_phone << endl;
			cout << "地址" << abs->perarray[i].m_addr << endl;
		}
	}
	system("pause");//按任意键继续
	system("cls");//清屏
}
//5.修改联系人
void modifyperson(addressbooks* abs)
{
	cout << "请输入要修改的联系人" << endl;
	string name;
	cin >> name;
	int ret=isExist(abs, name);//查找联系人
	if (ret != -1)
	{
		//姓名
		string name;
		cout << "请输入姓名" << endl;
		cin >> name;
		abs->perarray[ret].m_name = name;
		//性别
		cout << "请输入性别" << endl;
		cout << "1---man" << endl;
		cout << "2---woman" << endl;
		int sex = 0;
		while (true)
		{//如果输入1或者2推出循环
			cin >> sex;
			if (sex == 1 || sex == 2)
			{
				abs->perarray[ret].m_sex = sex;
				break;
			}
			//输入错误一直循环
			else
			{
				cout << "输入错误,请重新输入" << endl;
			}
		}

		//年龄
		cout << "请输入年龄" << endl;
		int age = 0;
		cin >> age;
		abs->perarray[ret].m_age = age;


		//电话
		cout << "请输入电话" << endl;
		string phone;
		cin >> phone;
		abs->perarray[ret].m_phone = phone;

		//地址
		cout << "请输入地址" << endl;
		string adress;
		cin >> adress;
		abs->perarray[ret].m_addr = adress;
		system("pause");
		system("cls");
	}
	else
	{
		cout << "查无此人" << endl;
	}


}
//6.清空联系人
void cleanperson(addressbooks* abs)
{
	abs->m_size = 0;
	cout << "已清空通讯录" << endl;
	system("pause");
	system("cls");
}
int main()
{//创建通讯录结构体变量
	addressbooks abs;
	//初始化通讯录人数
	abs.m_size = 0;


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


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

		if (select == 1)//1、添加联系人
		{
			addperson(&abs);

			continue;
		}
		else if (select == 2)//2、显示联系人
		{
			showperson(&abs);
			continue;
		}
		else if (select == 3)//3、删除联系人
		{
			deleteperson(&abs);
			continue;
		}
		else if (select == 4)//4、查找联系人
		{
			findperson(&abs);
			continue;
		}
		else if (select == 5)//5、修改联系人
		{
			modifyperson(&abs);
			continue;
		}
		else if (select == 6)//6、清空联系人
		{
			cleanperson(&abs);
			continue;
		}
		else(select == 0);//0、退出通讯录
		{
			cout << "谢谢使用" << endl;
			system("pause");
			break;
		}
	};


	return 0;

}
C++