C++实现简单的通讯录管理系统

该通讯录管理系统黑马程序员C++课程下阶段练习题,该通讯录仅使用C/C++基础语法来实现通过提示对通讯录进行添加,查看,删除,查找,修改联系人以及清空通讯录功能,十分简单。适合用来对C/C++基础知识的巩固。

下面为代码片:

#include
#include

using namespace std;

#define MAX 100		//通讯录最大人数

void showMenu();
void addPerson(struct addressList* adl);
void showLinkman(struct addressList* adl);
int isExist(struct addressList* adl, string name);
void deleteLinkman(struct addressList* adl);
void findLinkman(struct addressList* adl);
void modifyLinkman(struct addressList* adl);
void cleanList(struct addressList* adl);

//联系人结构体
struct linkman {
     
	string Name;	//姓名
	int Sex;		//性别 1代表男 2代表女
	int Age;		//年龄
	string PhoneNum;//电话号码
};

//通讯录结构体
struct addressList {
     
	struct linkman linkmanArray[MAX];	//联系人数组
	int list_Size = 0;						//已存在联系人个数
};

int main()
{
     
	addressList adl;
	while (1) {
     
		showMenu();

		cout << "请输入您要进行的操作:  ";
		int choice = -1;
		cin >> choice;
		switch (choice) {
     
		case 1:		//1.添加联系人
			addPerson(&adl);
			break;
		case 2:		//2.显示联系人
			showLinkman(&adl);
			break;
		case 3: 	//3.删除联系人
			deleteLinkman(&adl);
			break;
		case 4:		//4.查找联系人
			findLinkman(&adl);
			break;
		case 5:		//5.修改联系人
			modifyLinkman(&adl);
			break;
		case 6:		//6.清空联系人
			cleanList(&adl);
			break;
		case 0:		//0.退出系统
			system("pause");
			return 0;
			break;
		default:
			cout << "非法输入, 请重新输入!!" << endl;
			break;
		}
		system("pause");
		system("cls");
	}
	system("pause");
	return 0;
}

//现实操作菜单信息
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;
}

//添加联系人
void addPerson(struct addressList* adl) {
     
	if (adl->list_Size < MAX) {
     
		cout << "请输入联系人姓名:";
		cin >> adl->linkmanArray[adl->list_Size].Name;
		int sex = -1;
		cout << "1 - 男, 2 - 女";
		cout << "\n请输入联系人性别:";
		while (sex != 1 && sex != 2) {
     	//若输入不为1或2,则重新输入
			cin >> sex;
			if (sex == 1 || sex == 2) {
     
				adl->linkmanArray[adl->list_Size].Sex = sex;
			}
			else {
     
				cout << "输入不合法,请重新输入!" << endl;
			}
		}
		cout << "\n请输入联系人年龄:";
		cin >> adl->linkmanArray[adl->list_Size].Age;
		cout << "\n请输入联系人电话号码:";
		cin >> adl->linkmanArray[adl->list_Size].PhoneNum;

		cout << "\n新加联系人成功!!" << endl;
		adl->list_Size++;
	}
	else {
     
		cout << "通讯录已满,无法添加新联系人。" << endl;
	}
}

//显示通讯录
void showLinkman(struct addressList* adl) {
     
	if (adl->list_Size != 0) {
     
		for (int i = 0; i < adl->list_Size; i++) {
     
			cout << "\t姓名:" << adl->linkmanArray[i].Name
				<< "\t\t性别:" << (adl->linkmanArray[i].Sex == 1 ? "男" : "女") 
				<< "\t\t年龄:"<<adl->linkmanArray[i].Age
				<< "\t\t电话号码:" << adl->linkmanArray[i].PhoneNum<<endl;
		}
	}
	else {
     
		cout << "通讯录为空!" << endl;
	}

}

//判断某姓名是否在通讯录中存在
int isExist(struct addressList* adl, string name) {
     
	int flag = -1;
	for (int i = 0; i < adl->list_Size;i++) {
     
		if (name == adl->linkmanArray[i].Name)
		{
     
			cout << "\t姓名:" << adl->linkmanArray[i].Name
				<< "\t\t性别:" << (adl->linkmanArray[i].Sex == 1 ? "男" : "女")
				<< "\t\t年龄:" << adl->linkmanArray[i].Age
				<< "\t\t电话号码:" << adl->linkmanArray[i].PhoneNum << endl;
			flag = 1;
		}
	}
	return flag;
}

//删除联系人
void deleteLinkman(struct addressList* adl) {
     
	string name;
	cout << "请输入要删除的联系人姓名:" << endl;
	cin >> name;
	int ret = isExist(adl, name);
	if (ret == 1) {
     
		for (int i = 0; i < adl->list_Size; i++) {
     
			if (adl->linkmanArray[i].Name == name) {
     
				for (int j = i; j < adl->list_Size; j++) {
     
					adl->linkmanArray[j] = adl->linkmanArray[j + 1];
				}
				adl->list_Size--;
				cout << "删除  "<<name<<"  成功" << endl;
			}
		}
	}
	else {
     
		cout << "不存在姓名为 - " << name << " - 的联系人!" << endl;
	}
}

//查找联系人
void findLinkman(struct addressList* adl) {
     
	string name;
	cout << "请输入要查找的联系人姓名:" << endl;
	cin >> name;
	int ret = isExist(adl, name);
	if (ret == 1) {
     
		for (int i = 0; i < adl->list_Size; i++) {
     
			if (adl->linkmanArray[i].Name == name) {
     
				cout << "查找完毕!" << endl;
			}
		}
	}
	else {
     
		cout << "不存在姓名为 - " << name << " - 的联系人!" << endl;
	}
}

//修改联系人信息
void modifyLinkman(struct addressList* adl) {
     
	string name;
	cout << "请输入要查找的联系人姓名:" << endl;
	cin >> name;
	int ret = isExist(adl, name);
	if (ret == 1) {
     
		for (int i = 0; i < adl->list_Size; i++) {
     
			if (adl->linkmanArray[i].Name == name) {
     
				int flag = -1;
				while(flag != 1){
     
					cout << "\n\n请输入要修改信息编号:\t1 - 姓名  \n\t\t\t2 - 性别 \n\t\t\t3 - 年龄 \n\t\t\t4 - 电话 \n\t\t\t0 - 完成修改\n ";
					int choice = -1;
					cin >> choice;
					switch (choice) {
     
					case 1:
						cout << "请输入新的联系人姓名:";
						cin >> adl->linkmanArray[i].Name;
						cout << "\n修改姓名成功!";
						break;
					case 2: {
     
						int sex = -1;
						cout << "1 - 男, 2 - 女";
						cout << "\n请输入新的联系人性别:";
						while (sex != 1 && sex != 2) {
     
							cin >> sex;
							if (sex == 1 || sex == 2) {
     
								adl->linkmanArray[i].Sex = sex;
								cout << "\n修改性别成功!";
							}
							else {
     
								cout << "输入不合法,请重新输入!" << endl;
							}
						}
					}
						break;
					case 3:
						cout << "\n请输入新的联系人年龄:";
						cin >> adl->linkmanArray[i].Age;
						cout << "\n修改年龄成功!";
						break;
					case 4:
						cout << "\n请输入新的联系人电话号码:";
						cin >> adl->linkmanArray[i].PhoneNum;
						cout << "\n修改电话号码成功!";
						break;
					case 0:
						cout << "\n完成修改操作。";
						flag = 1;
						break;
					default:
						cout << "\n非法输入,请重新输入!";
						break;
					}
				}
			}
		}
	}
	else {
     
		cout << "不存在姓名为 - " << name << " - 的联系人!" << endl;
	}
}

//清空通讯录
void cleanList(struct addressList* adl) {
     
	int choice = -1;
	while(1){
     
		cout << "确定删除全部联系人信息吗?\t1 - yes \t0 - no\n";
		cin >> choice;
		if (choice == 1) {
     
			adl->list_Size = 0;	//做逻辑清空操作
			cout << "通信录已清空!" << endl;
			break;
		}
		else if (choice == 0) {
     
			cout << "取消删除全部联系人信息!" << endl;
			break;
		}
		else {
     
			cout << "非法输入,请重新输入!" << endl;
		}
	}
}

你可能感兴趣的:(C++)