c++通讯录管理系统

系统需求

在这里插入图片描述

代码

#include<iostream>
#include<string>
#define MAX 1000
using namespace std;


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


//通讯录的结构体
struct AddressBook {
	Person personArray[MAX];
	int currentNum;
};


//显示菜单的函数
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(AddressBook* p_book) {
	if (p_book->currentNum == MAX) {
		cout << "通讯录已存满,添加失败" << endl;
		return;
	}
	else {
		//添加姓名
		cout << "请输入联系人的姓名" << endl;
		cin >> p_book->personArray[p_book->currentNum].m_name;
		//性别
		int sex = 0;
		while (sex!=1 && sex!=2)
		{
			cout << "请输入联系人的性别(1:男/2:女)" << endl;
			cin >> sex;
		}
		p_book->personArray[p_book->currentNum].m_sex = sex;
		//年龄
		int age = 0;
		do
		{
			cout << "请输入联系人的年龄" << endl;
			cin >> age;
		} while (age<=0 || age>150);
		p_book->personArray[p_book->currentNum].m_age = age;
		//电话
		cout << "请输入联系人的电话" << endl;
		cin >> p_book->personArray[p_book->currentNum].m_phone;
		//地址
		cout << "请输入联系人的地址" << endl;
		cin >> p_book->personArray[p_book->currentNum].m_address;

		cout << "添加成功" << endl;
		p_book->currentNum++;
		system("pause");
		system("cls");
	}
}


//显示联系人
void showPerson(AddressBook* p_book) {
	if (p_book->currentNum == 0) {
		cout << "通讯录为空" << endl;
		system("pause");
		system("cls");
		return;
	}
	else {
		for (int i = 0; i < p_book->currentNum; i++) {
			cout << "姓名:" << p_book->personArray[i].m_name
				<< "\t性别:" << (p_book->personArray[i].m_sex==1?"男":"女")
				<< "\t年龄:" << p_book->personArray[i].m_age
				<< "\t电话:" << p_book->personArray[i].m_phone
				<< "\t住址:" << p_book->personArray[i].m_address << endl;
		}
		system("pause");
		system("cls");
	}
}


//检测联系人是否存在
int isExist(AddressBook* p_book, string name) {
	for (int i = 0; i < p_book->currentNum; i++) {
		if (p_book->personArray[i].m_name == name) {
			return i;
		}
	}
	return -1;
}


//删除联系人
void deletePerson(AddressBook* p_book) {
	string name;
	cout << "请输入要删除的联系人" << endl;
	cin >> name;
	int index = isExist(p_book, name);
	if (index == -1) {
		cout << "查无此人" << endl;
		system("pause");
		system("cls");
		return;
	}
	else {
		for (int i = index; i < p_book->currentNum; i++) {
			p_book->personArray[i] = p_book->personArray[i + 1];
		}
		p_book->currentNum--;
		cout << "删除成功" << endl;
		system("pause");
		system("cls");
	}
}


//查找联系人
void searchPerson(AddressBook* p_book) {
	string name;
	cout << "请输入待查找的联系人的姓名" << endl;
	cin >> name;
	int index = isExist(p_book, name);
	if (index != -1) {
		cout << "查找成功" << endl;
		cout << "姓名:" << p_book->personArray[index].m_name
			<< "\t性别:" << (p_book->personArray[index].m_sex == 1 ? "男" : "女")
			<< "\t年龄:" << p_book->personArray[index].m_age
			<< "\t电话:" << p_book->personArray[index].m_phone
			<< "\t住址:" << p_book->personArray[index].m_address << endl;
	}
	else {
		cout << "查无此人" << endl;
	}
	system("pause");
	system("cls");
}


//修改联系人
void modifyPerson(AddressBook* p_book) {
	string name;
	cout << "请输入待修改的联系人的姓名" << endl;
	cin >> name;
	int index = isExist(p_book, name);

	if (index != -1) {
		cout << "请输入修改后联系人的姓名" << endl;
		cin >> p_book->personArray[index].m_name;
		cout << "请输入修改后联系人的性别(1男2女)" << endl;
		int sex = 0;
		while (true) {
			cin >> sex;
			if (sex == 1 || sex == 2) {
				p_book->personArray[index].m_sex = sex;
				break;
			}
			cout << "请输入正确的性别" << endl;
		}
		cout << "请输入修改后联系人的年龄" << endl;
		cin >> p_book->personArray[index].m_age;
		cout << "请输入修改后联系人的电话" << endl;
		cin >> p_book->personArray[index].m_phone;
		cout << "请输入修改后联系人地址" << endl;
		cin >> p_book->personArray[index].m_address;

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


//清空联系人
void cleanPerson(AddressBook* p_book) {
	cout << "是否确认清空(y/n)" << endl;
	char select;
	cin >> select;

	if (select == 'y') {
		p_book->currentNum = 0;
		cout << "清空成功" << endl;
	}
	else if (select == 'n') {
		cout << "退出清空界面" << endl;
		system("pause");
		system("cls");
		return;
	}
	else {
		cout << "输入错误,请重新输入" << endl;
	}
	system("pause");
	system("cls");
}


int main() {
	int select = 0;
	AddressBook book;
	book.currentNum = 0;

	while (true) {
		ShowMenu();
		cin >> select;
		switch (select)
		{
		case 1://1.添加联系人
			addPerson(&book);
			break;
		case 2://2.显示联系人
			showPerson(&book);
			break;
		case 3://3.删除联系人
			deletePerson(&book);
			break;
		case 4://4.查找联系人
			searchPerson(&book);
			break;
		case 5://5.修改联系人
			modifyPerson(&book);
			break;
		case 6://6.清空联系人
			cleanPerson(&book);
			break;
		case 0://0.退出通讯录
			cout << "欢迎下次使用" << endl;
			system("pause");
			return 0;
		default:
			break;
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值