使用c++完成通信录管理系统

使用c++完成通信录管理系统,第1张

使用c++完成通信录管理系统
#include 
#include 
using namespace std;
#define MAX 1000

//1.创建一个showMenu函数,用来显示菜单
void showMenu() {
	cout << "****************************************" << endl;
	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;
	cout << "****************************************" << endl;
}

//定义联系人结构体
struct person {
	string name;//姓名
	int sex;//性别   1男   2女
	int age;//年龄
	string phone;//手机号码
	string adress;//住址

};

//定义通讯录结构体
struct listPerson {
	//通讯录保存联系人的数组
	struct person personArr[MAX];
	//通讯录中保存联系人的个数
	int size;
};



//创建添加联系人的函数
void addPerson(listPerson* abs) {
	//首先判断当前联系人数量是否已达最大值
	if (abs->size == MAX) {
		cout << "当前存储联系人数量已满,无法添加!" << endl;
		return;
	}
	else {
		//添加具体的联系人

		//姓名
		string name;
		cout << "请输入姓名:" << endl;
		cin >> name;
		abs->personArr[abs->size].name = name;

		//性别
		int sex=0;
		cout << "请输入性别:" <<"1代表男,2代表女" << endl;
		while (true) {
			cin >> sex;
			if (sex == 1 || sex == 2) {
				abs->personArr[abs->size].sex = sex;
				if (sex == 1) {
					cout << "您输入的性别为男" << endl;
				}
				else {
					cout << "您输入的性别为女" << endl;
				}
				break;
			}
			else {
				cout << "不好意思,请您重新输入" << endl;
			}
		}


		//年龄
		int age;
		cout << "请输入年龄:" << endl;
		cin >> age;
		abs->personArr[abs->size].age = age;


		//手机号码
		string phone;
		cout << "请输入11位手机号码:" << endl;
		cin >> phone;
		abs->personArr[abs->size].phone = phone;


		//家庭住址
		string adress;
		cout << "请输入家庭住址:" << endl;
		cin >> adress;
		abs->personArr[abs->size].adress = adress;
		


		//更新size
		abs->size++;

		//当前联系人数量为
		cout<<"添加成功," << "当前联系人数量为:" << abs->size << endl;

		system("pause");//请安任意键继续
		system("cls");//清屏幕
		
	}
}
 
//创建显示联系人的函数
void showMessage(listPerson* abs) {
	//首先判断保存的联系人是否为空,如果为空则提示
	if (abs->size == 0) {
		cout << "联系人信息为空" << endl;
	}
	for (int i = 0; i < abs->size; i++) {
		cout << "姓名: " << abs->personArr[i].name <<"t";
		cout << "  性别: " << (abs->personArr[i].sex == 1 ? "男" : "女") << "t";
		cout << " 年龄: " << abs->personArr[i].age << "t";
		cout << "电话: " << abs->personArr[i].phone << "t";
		cout << "地址: " << abs->personArr[i].adress << endl;
	}
	system("pause");
	system("cls");
}

//创建检测联系人是否存在的函数
int isExist(listPerson* abs, string name) {
	//循环遍历数组每一项中的name,进行比对
	for (int i = 0; i < abs->size; i++) {
		if (abs->personArr[i].name == name) {
			return i;
			cout << "此人存在" << endl;
		}
	}
	return -1;
}


//创建删除联系人的函数
void deletePerson(listPerson* abs) {
	cout << "请输入要删除的联系人" << endl;
	string name;
	cin >> name;
	int ret = isExist(abs, name);
	if (ret != -1) {
		for (int i = ret; i < abs->size; i++) {
			abs->personArr[i] = abs->personArr[i + 1];
		}
	}
	else {
		cout << "查无此人" << endl;
	}
	//更新personArr中的size
	abs->size--;
	cout << "删除完成" << endl;
	system("pause");
	system("cls");
}


//创建查找联系人的函数
void findPerson(listPerson* abs) {
	cout << "请输入要查找的联系人" << endl;
	string name;
	cin >> name;
	int ret = isExist(abs, name);
	if (ret != -1) {
		cout << "姓名: " << abs->personArr[ret].name << "t";
		cout << "  性别: " << (abs->personArr[ret].sex == 1 ? "男" : "女") << "t";
		cout << " 年龄: " << abs->personArr[ret].age << "t";
		cout << "电话: " << abs->personArr[ret].phone << "t";
		cout << "地址: " << abs->personArr[ret].adress << endl;
	}
	else {
		cout << "查无此人" << endl;
	}
	system("pause");
	system("cls");

}

//创建修改联系人的函数
void changePerson(listPerson* abs) {
	cout << "请输入您要修改的联系人" << endl;
	string name;
	cin >> name;
	int ret = isExist(abs, name);
	if (ret != -1) {
		//姓名
		string name;
		cout << "请输入姓名:" << endl;
		cin >> name;
		abs->personArr[ret].name = name;

		//性别
		int sex = 0;
		cout << "请输入性别:" << "1代表男,2代表女" << endl;
		while (true) {
			cin >> sex;
			if (sex == 1 || sex == 2) {
				abs->personArr[ret].sex = sex;
				if (sex == 1) {
					cout << "您输入的性别为男" << endl;
				}
				else {
					cout << "您输入的性别为女" << endl;
				}
				break;
			}
			else {
				cout << "不好意思,请您重新输入" << endl;
			}
		}


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


		//手机号码
		string phone;
		cout << "请输入11位手机号码:" << endl;
		cin >> phone;
		abs->personArr[ret].phone = phone;


		//家庭住址
		string adress;
		cout << "请输入家庭住址:" << endl;
		cin >> adress;
		abs->personArr[ret].adress = adress;

	}
	else {
		cout << "查无此人" << endl;
	}
	cout << "修改成功" << endl;
	system("pause");
	system("cls");
}


//创建清空联系人的函数
void qk(listPerson* abs) {
	abs->size = 0;
	cout << "联系人已清空" << endl;
	system("pause");
	system("cls");
}

//在main函数中调用封装好的函数

int main() {

	//创建通讯录结构体变量
	struct listPerson abs;
	//初始化通讯录保存人员的个数
	abs.size = 0;

	int user = 0;

	while (true) {
		showMenu();

		cin >> user;

		switch (user) {
		case 1://1.添加联系人
			addPerson(&abs);//利用地址传递可以修饰实参
			break;
		case 2://2.显示联系人
			showMessage(&abs);//显示保存的联系人信息
			break;
		case 3://3.删除联系人 
			deletePerson(&abs);
			break;
		case 4://4.查找联系人
			findPerson(&abs);
			break;
		case 5://5.修改联系人
			changePerson(&abs);
			break;
		case 6:// 6.清空联系人 
			qk(&abs);
			break;
		case 0:// 0.退出通讯录 
			cout << "欢迎下次使用" << endl;
			system("pause");
			return 0;
			break;

		}


	}

	
	system("pause");

	return 0;

}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5156376.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-18
下一篇 2022-11-18

发表评论

登录后才能评论

评论列表(0条)

保存