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

#include <iostream>
#include <stdlib.h>
#include <string>
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 Addressbooks {
	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;
}

//0. 退出通讯录
void exitMenu() {
	cout << "欢迎下次使用" << endl;
	system("pause");
	return;
}

//1. 添加联系人
void writePerson(Addressbooks *abs, int pos) {
	//姓名
	string name;
	cout << "请输入姓名:" << endl;
	cin >> name;
	abs->personArray[pos].M_name = name;

	cout << "请输入性别" << endl;
	cout << "1 -- 男" << endl;
	cout << "2 -- 女" << endl;
	//性别
	int sex = 0;
	while(true) {
		cin >> sex;
		if(sex == 1 || sex == 2) {
			abs->personArray[pos].M_sex = sex;
			break;
		}
		cout << "输入有误,请重新输入";
	}
	//年龄
	cout << "请输入年龄:" << endl;
	int age = 0;
	cin >> age;
	abs->personArray[pos].M_age = age;
	//联系电话
	cout << "请输入联系电话:" << endl;
	string phone = "";
	cin >> phone;
	abs->personArray[pos].M_phone = phone;
	//家庭住址
	cout << "请输入家庭地址: " << endl;
	string address = "";
	cin >> address;
	abs->personArray[pos].M_address = address;

	if(!name.empty() && sex && age && !phone.empty() && !address.empty()) {
		cout << "添加成功" << endl;
		system("pause");
		system("cls");
	}
}
void addPerson(Addressbooks *abs) {
	//判断电话本是否满了
	int pos = abs ->M_size;
	if(pos == MAX) {
		cout << "通讯录已满,无法添加" << endl;
		return;
	} else {
		cout << pos << endl;
		writePerson(abs, pos);
		abs->M_size++; //更新通讯录人数
		//cout << abs->M_size << endl;
	}
}

//2. 显示联系人
void printPerson(Addressbooks *abs, int pos) {
	cout << "姓名:" << abs->personArray[pos].M_name << "\t";
	cout << "性别: " << (abs->personArray[pos].M_sex == 1 ? "男" : "女") << "\t";
	cout << "年龄: " << abs->personArray[pos].M_age << "\t";
	cout << "电话:" << abs->personArray[pos].M_phone << "\t";
	cout << "住址:" << abs->personArray[pos].M_address << endl;
}

void showPerson(Addressbooks *abs) {
	if(abs->M_size == 0) {
		cout << " 当前记录为空" << endl;
	} else {
		for(int i = 0; i < abs->M_size; i++) {
			printPerson(abs, i);
		}
	}
	system("pause");
	system("cls");
}

//3. 删除联系人
int isExist(Addressbooks *abs, string name) {
	for(int i = 0; i < i; i++) {
		if(abs->personArray[i].M_name == name) {
			return i;
		}
	}
	return -1;
}

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->personArray[i] = abs->personArray[i + 1];
		}
		abs->M_size--;
		cout << "删除成功" << endl;
	} else {
		cout << "查无此人" << endl;
	}

	system("pause");
	system("cls");
}
//4. 查找联系人
void findPerson(Addressbooks *abs) {
	cout << "请输入您要查找的联系人" << endl;
	string name;
	cin >> name;

	int ret = isExist(abs, name);
	if(ret != -1) {
		printPerson(abs, ret);
	} else {
		cout << "查无此人" << endl;
	}
	system("pause");
	system("cls");
}

//5. 修改联系人
void modifyPerson(Addressbooks *abs) {
	cout << "请输入您要修改的联系人" << endl;
	string name;
	cin >> name;

	int ret = isExist(abs, name);
	if(ret != -1) {
		writePerson(abs, ret);
		cout << "修改成功" << endl;
	} else {
		cout << "查无此人" << endl;
	}
	system("pause");
	system("cls");
}

//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;
		switch(select) {
			case 1: //添加联系人
				addPerson(&abs);
				break;
			case 2: //显示联系人
				showPerson(&abs);
				break;
			case 3:	//删除联系人
				deletePerson(&abs);
				break;
			case 4:	//查找联系人
				findPerson(&abs);
				break;
			case 5:	//修改联系人
				modifyPerson(&abs);
				break;
			case 6:	//清空联系人
				cleanPerson(&abs);
				break;
			case 0:	//退出通讯录
				exitMenu();
				break;
			default:
				exitMenu();
				break;
			}
		}

	system("pause");
	
	return 0;
}

posted @ 2020-03-03 22:45  哨音  阅读(3514)  评论(0编辑  收藏  举报