C++作业通讯录管理系统

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

//菜单界面
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;
}

struct Person
{
	string name;
	string sex;
	int age;
	string phone;
	string address;
};

struct AddressBooks
{
	Person personArr[MAX];
	int peron_number;
};

void addPerson(AddressBooks* a1)
{
	//判断通讯录是否已满
	if (a1->peron_number==MAX)
	{
		cout << "通讯录已满,无法添加!" << endl;
		return;
	}
	else
	{
		cout << "请输入姓名:" << endl;
		cin >> a1->personArr[a1->peron_number].name;

		cout << "请输入性别:" << endl;
		cin >> a1->personArr[a1->peron_number].sex;

		cout << "请输入年龄:" << endl;
		cin >> a1->personArr[a1->peron_number].age;

		cout << "请输入联系电话:" << endl;
		cin >> a1->personArr[a1->peron_number].phone;

		cout << "请输入家庭住址:" << endl;
		cin >> a1->personArr[a1->peron_number].address;

		a1->peron_number++;
		cout << "添加成功!" << endl;

		system("pause");
		system("cls");
	}
}

//显示联系人
void showPerson(AddressBooks* a1)
{
	if (a1->peron_number==0)
	{
		cout << "当前记录为空" << endl;
	}
	else
	{
		for (int i = 0; i < a1->peron_number; i++)
		{
			cout << "姓名: " << a1->personArr[i].name << "\t";
			cout << "性别: " << a1->personArr[i].sex << "\t";
			cout << "年龄: " << a1->personArr[i].age << "\t";
			cout << "联系电话: " << a1->personArr[i].phone << "\t";
			cout << "家庭住址: " << a1->personArr[i].address << endl;
		}
	}
	system("pause");
	system("cls");
}

int isExist(AddressBooks* a1,string name)
{
	for (int i = 0; i < a1->peron_number; i++)
	{
		if (a1->personArr[i].name == name)
		{
			return i;
		}
	}
	return -1;
}

void deletePerson(AddressBooks* a1)
{
	cout << "请输入你要删除的联系人" << endl;
	string now_name;
	cin >> now_name;

	int ret=isExist(a1, now_name);
	if (ret!=-1)
	{
		for (int i = ret; i < a1->peron_number; i++)
		{
			a1->personArr[i] = a1->personArr[i + 1];
		}
		a1->peron_number--;
		cout << "删除成功!" << endl;
	}
	else
	{
		cout << "查无此人!" << endl;
	}

	system("pause");
	system("cls");
}

void findPerson(AddressBooks* a1)
{
	cout << "请输入你要查找的联系人" << endl;
	string now_name;
	cin >> now_name;

	int ret = isExist(a1, now_name);
	if (ret != -1)
	{
		cout << "姓名: " << a1->personArr[ret].name << "\t";
		cout << "性别: " << a1->personArr[ret].sex << "\t";
		cout << "年龄: " << a1->personArr[ret].age << "\t";
		cout << "联系电话: " << a1->personArr[ret].phone << "\t";
		cout << "家庭住址: " << a1->personArr[ret].address << endl;
	}
	else
	{
		cout << "查无此人!" << endl;
	}

	system("pause");
	system("cls");
}

void modifyPerson(AddressBooks* a1)
{
	cout << "请输入你要修改的联系人" << endl;
	string now_name;
	cin >> now_name;

	int ret = isExist(a1, now_name);
	if (ret != -1)
	{
		cout << "请输入姓名:";
		cin >> a1->personArr[ret].name;
		cout << endl;

		cout << "请输入性别:";
		cin >> a1->personArr[ret].sex;
		cout << endl;

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

		cout << "请输入联系电话:";
		cin >> a1->personArr[ret].phone;
		cout << endl;

		cout << "请输入家庭住址:";
		cin >> a1->personArr[ret].address;
		cout << endl;

		cout << "修改成功!" << endl;
	}
	else
	{
		cout << "查无此人!" << endl;
	}

	system("pause");
	system("cls");
}

void clearPerson(AddressBooks* a1)
{
	a1->peron_number = 0; 
	cout << "已清空通讯录" << endl;
	system("pause");
	system("cls");
}

int main()
{
	//创建通讯录结构体变量
	AddressBooks a1;
	a1.peron_number = 0;

	int select = 0;
	while (true)
	{
		//菜单
		showMenu();
		cin >> select;
		switch (select)
		{
		case 1://1、添加联系人
			addPerson(&a1);
			break;
		case 2://2、显示联系人
			showPerson(&a1);
			break;
		case 3://3、删除联系人
			deletePerson(&a1);
			break;
		case 4://4、查找联系人
			findPerson(&a1);
			break;
		case 5://5、修改联系人 
			modifyPerson(&a1);
			break;
		case 6:// 6、清空联系人
			clearPerson(&a1);
			break;
		case 0:// 0、退出通讯录
			cout << "欢迎下次使用" << endl;
			system("pause");
			return 0;
			break;
		default:
			break;
		}
	}
	
	system("pause");
	return 0;
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值