c++做通讯录的demo

#include <iostream>
#include <string>
#include<cstdlib>
using namespace std;
#define MAX 1000
struct Person
{
	string m_Name;
	int m_Sex;
	int m_Age;
	string m_Phone;
	string m_Address;
};
struct Contacts
{
	struct Person personArray[MAX];
	int m_Size;
};

int isExist(Contacts* cts, string name);



void addPerson(Contacts* cts)
{
	//判断是否满了
	if (cts->m_Size >= MAX)
	{
		cout << "通讯录满了" << endl;
		return;
	}
	else {
		string name;
		cout << "请输入姓名:" << endl;
		cin >> name;
		cts->personArray[cts->m_Size].m_Name = name;

		int sex;
		cout << "请输入性别:" << endl;
		cin >> sex;
		cts->personArray[cts->m_Size].m_Sex = sex;

		string phone;
		cout << "请输入手机号:" << endl;
		cin >> phone;
		cts->personArray[cts->m_Size].m_Phone = phone;

		string addr;
		cout << "请输入地址:" << endl;
		cin >> addr;
		cts->personArray[cts->m_Size].m_Address = addr;

		cts->m_Size++;
		system("pause");
		system("cls");
	}
}
void updatePerson(Contacts* cts, string name)
{
	int index = isExist(cts, name);
	if(index  != -1){
		string name;
		cout << "请输入姓名:" << endl;
		cin >> name;
		cts->personArray[index].m_Name = name;

		int sex;
		cout << "请输入性别:" << endl;
		cin >> sex;
		cts->personArray[index].m_Sex = sex;

		string phone;
		cout << "请输入手机号:" << endl;
		cin >> phone;
		cts->personArray[index].m_Phone = phone;

		string addr;
		cout << "请输入地址:" << endl;
		cin >> addr;
		cts->personArray[index].m_Address = addr;
	}
	else {
		cout << "查无此人" << endl;

	}
	system("pause");
	system("cls");
}
void showPerson(Contacts * cts)
{
	//判断人数
	if (cts->m_Size == 0)
	{
		cout << "人数为0" << endl;
	}
	else {
		for (int i = 0; i < cts->m_Size; i++)
		{
			cout << "姓名:" << cts->personArray[i].m_Name << "\t" 
				<< "电话:" << cts->personArray[i].m_Phone << "\t"
				<< "性别:" << (cts->personArray[i].m_Sex == 1 ? "男" : "女") << "\t"
				<< "地址:" << cts->personArray[i].m_Address << endl;
		}
	}
	system("pause");
	system("cls");
}
int isExist(Contacts* cts, string name)
{
	for (int i = 0; i < cts->m_Size; i++)
	{
		if (cts->personArray[i].m_Name == name)
		{
			return i;
		}
	}
	return -1;
}
void delPerson(Contacts* cts,string name)
{
	int index = isExist(cts, name);
	if (index != -1)
	{
		//删除index索引数据
		for (int  i = 0; i < cts->m_Size; i++)
		{
			cts->personArray[i] = cts->personArray[i + 1];
		}
		cts->m_Size--;
		cout << "删除成功" << endl;
	}
	else {
		cout << "查无此人" << endl;
	}
}
void findPersonByName(Contacts* cts, string name)
{
	int index = isExist(cts, name);
	if (index != -1)
	{
		cout << "姓名:" << cts->personArray[index].m_Name << "\t"
			<< "电话:" << cts->personArray[index].m_Phone << "\t"
			<< "性别:" << (cts->personArray[index].m_Sex == 1 ? "男" : "女") << "\t"
			<< "地址:" << cts->personArray[index].m_Address << endl;
	}
	system("pasue");
	system("cls");
}
void clearPerson(Contacts* cts)
{
	cts->m_Size = 0;//逻辑清空即可 访问不到就是清空
	cout << "清空成功" << endl;
	system("pasue");
	system("cls");
}
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;

}
int main()
{
	//创建通讯录
	struct Contacts cts;
	cts.m_Size = 0;
	int select = 0;//用户选择变量
	while (true) {
		showMenu();
		cin >> select;
		switch (select)
		{
		case 1:
			addPerson(&cts);
			break;
		case 2:
			showPerson(&cts);
			break;
		case 3: {
			cout << "请输入删除人姓名:" << endl;
			string name;
			cin >> name;
			delPerson(&cts, name);
			break;
		}
		case 4: {
			cout << "请输入查找的姓名:" << endl;
			string name;
			cin >> name;
			findPersonByName(&cts, name);
			break;
		}
		case 5:
		{
			cout << "请输入修改的姓名:" << endl;
			string name;
			cin >> name;
			updatePerson(&cts, name);
			break;
		}
		case 6:
			clearPerson(&cts);
			break;
		case 0:
			cout << "欢迎下次使用" << endl;
			system("pause");
			return 0;
			break;
		default:
			cout << "无命令" << endl;
			break;
		}
	}
	
	
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值