C++项目-通讯录管理系统

项目要求:

通讯录最多存放1000人,实现功能:添加、显示、查找、删除、修改、清空联系人,退出通讯录

下面给出代码及部分注释

#include 
using namespace std;

#define MAX 1000

/*---------------------------------------结构体----------------------------------------*/
//联系人结构体
struct Person {
	string m_name;
	int m_sex;//1男2女
	int m_age;
	string m_phone;
	string m_address;
};

//通讯录结构体
struct Books {
	struct Person personArray[MAX];//通讯录中保存的联系人数组
	int m_size;//通讯录联系人个数
};

/*----------------------------------------函数----------------------------------------*/
//菜单页面
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;
}

//1.添加联系人
void addPerson(struct Books* abs) {
	//判断通讯录是否满
	if (abs->m_size >= MAX) {
		cout << "通讯录已满,无法添加!" << endl;
		return;
	}
	else {
		//添加实际联系人
		
		//姓名
		string name;
		cout << "请输入姓名:" << endl;
		cin >> name;
		abs->personArray[abs->m_size].m_name = name;

		//性别
		int sex = 0;
		cout << "请输入性别(1-男,2-女):" << endl;
		while (true) {
			cin >> sex;
			if (sex == 1 || sex == 2) {
				abs->personArray[abs->m_size].m_sex = sex;
				break;
			}
			cout << "输入有误,请重新输入:" << endl;
		}

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

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

		//地址
		string address;
		cout << "请输入地址:" << endl;
		cin >> address;
		abs->personArray[abs->m_size].m_address = address;

		abs->m_size++;
		cout << "添加成功" << endl;
		system("pause");
		system("cls");//清屏操作
	}
}

//2.显示联系人
void showPerson(struct Books* abs) {
	if (abs->m_size == 0) {
		cout << "当前记录为空" << endl;
	}
	else {
		for (int i = 0; i < abs->m_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_address << endl;
		}
	}
	system("pause");
	system("cls");
}

//检测联系人是否存在,如存在返回所在位置,如不存在返回-1
int isExist(struct Books* abs,string name) {
	for (int i = 0; i < abs->m_size; i++) {
		if (abs->personArray[i].m_name == name) {
			return i;
		}
	}
	return -1;
}

//3.删除联系人
void deletePerson(struct Books* 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->personArray[i] = abs->personArray[i + 1];
		}
		abs->m_size--;//更新通讯录人员数
		cout << "删除成功!" << endl;
	}
	else {
		cout << "不存在该联系人" << endl;
	}
	system("pause");
	system("cls");
}

//4.查找指定联系人信息
void findPerson(struct Books* abs) {
	cout << "请输入要查找联系人的姓名:" << endl;
	string name;
	cin >> name;
	int ret = isExist(abs, name);
	if (ret != -1) {
		cout << "姓名:" << abs->personArray[ret].m_name << "\t";
		cout << "性别:" << (abs->personArray[ret].m_sex == 1 ? "男" : "女") << "\t";
		cout << "年龄:" << abs->personArray[ret].m_age << "\t";
		cout << "电话:" << abs->personArray[ret].m_phone << "\t";
		cout << "地址:" << abs->personArray[ret].m_address << endl;
	}
	else {
		cout << "不存在该联系人" << endl;
	}
	system("pause");
	system("cls");
}

//5.修改联系人信息
void modifyPerson(struct Books* abs) {
	cout << "请输入要修改联系人的姓名:" << endl;
	string name;
	cin >> name;
	int ret = isExist(abs, name);
	if (ret != -1) {
		cout << "请输入你要修改信息的选项(1-姓名,2-性别,3-年龄,4-电话,5-地址):" << endl;
		int chose = 0;
		cin >> chose;
		switch (chose) {
			case 1:
			{
				cout << "请输入姓名:" << endl;
				string name;
				cin >> name;
				abs->personArray[ret].m_name = name;
			}
				break;
			case 2:
			{
				cout << "请输入性别(1-男,2-女):" << endl;
				int sex = 0;
				while (true) {
					cin >> sex;
					if (sex == 1 || sex == 2) {
						abs->personArray[ret].m_sex = sex;
						break;
					}
					cout << "输入有误,请重新输入:" << endl;
				}
			}
				break;
			case 3:
			{
				cout << "请输入年龄:" << endl;
				int age = 0;
				cin >> age;
				abs->personArray[ret].m_age = age;
			}
				break;
			case 4:
			{
				cout << "请输入电话:" << endl;
				string phone;
				cin >> phone;
				abs->personArray[ret].m_phone = phone;
			}
				break;
			case 5:
			{
				cout << "请输入地址:" << endl;
				string address;
				cin >> address;
				abs->personArray[ret].m_address = address;
			}
				break;
			default:
				break;
		}
		cout << "修改成功" << endl;
	}
	else {
		cout << "不存在该联系人" << endl;
	}
	system("pause");
	system("cls");
}

//6.清空通讯录
void cleanPerson(struct Books* abs) {
	abs->m_size = 0;
	cout << "通讯录已清空" << endl;
	system("pause");
	system("cls");
}

int main() {
	
	struct Books abs;//创建通讯录结构体变量
	abs.m_size = 0;//初始化通讯录联系人个数

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

	while (true) {
		//菜单显示
		showMenu();

		cin >> select;
		switch (select) {
		case 1:
			addPerson(&abs);//利用地址传递,可以修饰实参
			break;
		case 2:
			showPerson(&abs);
			break;
		case 3:
		/*{
			cout << "请输入要删除联系人的姓名:" << endl;
			string name;
			cin >> name;
			if (isExist(&abs, name) == -1) {
				cout << "不存在" << endl;
			}
			else {
				cout << "存在" << endl;
			}
		}*/
			deletePerson(&abs);  
			break;
		case 4:
			findPerson(&abs);
			break;
		case 5:
			modifyPerson(&abs);
			break;
		case 6:
		{
			cout << "确认是否清空通讯录(1-是,0-否):" << endl;
			int conf = 0;
			cin >> conf;
			switch (conf) {
			case 1:
				cleanPerson(&abs);
				break;
			case 0:
				cout << "已取消清空操作" << endl;
				system("pause");
				system("cls");
				break;
			}
		}
			break;
		case 0:
			cout << "欢迎下次使用!" << endl;
			system("pause");
			return 0;
			break;
		default:
			break;
		}
	}  
}

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