通讯录管理系统_C++

#include <iostream>
#include <string>
#include <string.h>
using namespace std;

#define MAX 1000

typedef struct _PERSON
{
	string name;
	int sex;
	int age;
	string phoneNum;
	string address;
} PERSON;

typedef struct _ADDRESS_BOOK
{
	PERSON personArray[MAX];
	int personNum;
} ADDRESS_BOOK;

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 personAdd(ADDRESS_BOOK *addressBook)
{
	string name = "";
	int sex = 0;
	int age = 0;
	string phoneNum = "";
	string address = "";

	if (addressBook->personNum >= MAX)
	{
		cout << "通讯录已满" << endl;
		return;
	}

	cout << "请输入姓名" << endl;
	cin >> name;
	addressBook->personArray[addressBook->personNum].name = name;

	cout << "请输入性别" << endl;
	cout << "1-男性" << endl;
	cout << "2-女性" << endl;
	cin >> sex;
	addressBook->personArray[addressBook->personNum].sex = sex;

	cout << "请输入年龄" << endl;
	cin >> age;
	addressBook->personArray[addressBook->personNum].age = age;

	cout << "请输入电话号码" << endl;
	cin >> phoneNum;
	addressBook->personArray[addressBook->personNum].phoneNum = phoneNum;

	cout << "请输入地址" << endl;
	cin >> address;
	addressBook->personArray[addressBook->personNum].address = address;

	addressBook->personNum++;

	cout << "添加完毕" << endl;
}

void personShow(ADDRESS_BOOK addressBook)
{
	int i = 0;

	if (0 == addressBook.personNum)
	{
		cout << "通讯录为空" << endl;
		return;
	}

	for (i = 0; i < addressBook.personNum; i++)
	{
		cout << "姓名:" << addressBook.personArray[i].name << "\t";
		cout << "性别:" << (addressBook.personArray[i].sex == 1 ? "男" : "女") << "\t";
		cout << "年纪:" << addressBook.personArray[i].age << "\t";
		cout << "电话号码:" << addressBook.personArray[i].phoneNum << "\t";
		cout << "地址:" << addressBook.personArray[i].address << endl;
	}
}

int _isExist(ADDRESS_BOOK addressBook, string name)
{
	int i = 0;

	for (i = 0; i < addressBook.personNum; i++)
	{
		if (addressBook.personArray[i].name == name)
		{
			return i;
		}
	}

	return -1;
}

void personDel(ADDRESS_BOOK *addressBook)
{
	string name = "";
	int ret = 0;
	int i = 0;

	cout << "请输入要删除的姓名" << endl;
	cin >> name;

	ret = _isExist(*addressBook, name);

	if (-1 == ret)
	{
		cout << "查无此人" << endl;
		return;
	}

	for (i = ret; i < addressBook->personNum - 1; i++)
	{
		addressBook->personArray[i] = addressBook->personArray[i + 1];
	}

	addressBook->personNum--;

	cout << "删除联系人成功!" << endl;
}

void personFind(ADDRESS_BOOK addressBook)
{
	string name = "";
	int ret = 0;

	cout << "请输入姓名" << endl;
	cin >> name;

	ret = _isExist(addressBook, name);

	if (-1 == ret)
	{
		cout << "未找到此人" << endl;
		return;
	}

	cout << "姓名:" << addressBook.personArray[ret].name << "\t";
	cout << "性别:" << (addressBook.personArray[ret].sex == 1 ? "男" : "女") << "\t";
	cout << "年纪:" << addressBook.personArray[ret].age << "\t";
	cout << "电话号码:" << addressBook.personArray[ret].phoneNum << "\t";
	cout << "地址:" << addressBook.personArray[ret].address << endl;
}

void personModify(ADDRESS_BOOK *addressBook)
{
	string name = "";
	int sex = 0;
	int age = 0;
	string phoneNum = "";
	string address = "";
	int ret = 0;

	cout << "请输入姓名" << endl;
	cin >> name;

	ret = _isExist(*addressBook, name);

	if (-1 == ret)
	{
		cout << "未找到此人" << endl;
		return;
	}

	cout << "请输入姓名" << endl;
	cin >> name;
	addressBook->personArray[ret].name = name;

	cout << "请输入性别" << endl;
	cout << "1-男性" << endl;
	cout << "2-女性" << endl;
	cin >> sex;
	addressBook->personArray[ret].sex = sex;

	cout << "请输入年龄" << endl;
	cin >> age;
	addressBook->personArray[ret].age = age;

	cout << "请输入电话号码" << endl;
	cin >> phoneNum;
	addressBook->personArray[ret].phoneNum = phoneNum;

	cout << "请输入地址" << endl;
	cin >> address;
	addressBook->personArray[ret].address = address;
}

void personClean(ADDRESS_BOOK *addressBook)
{
	addressBook->personNum = 0;
}

int main(void)
{
	int select = 0;
	ADDRESS_BOOK addressBook;

	memset(&addressBook, 0, sizeof(addressBook));
	cout << addressBook.personNum << endl;

	while (true)
	{
		showMenu();
		cin >> select;

		switch (select)
		{
		case 1:
			personAdd(&addressBook);
			break;
		case 2:
			personShow(addressBook);
			break;
		case 3:
			personDel(&addressBook);
			break;
		case 4:
			personFind(addressBook);
			break;
		case 5:
			personModify(&addressBook);
			break;
		case 6:
			personClean(&addressBook);
			break;
		case 0:
			cout << "欢迎下次使用" << endl;
			return 0;
			break;
		default:
			break;
		}
	}

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值