C++基础入门-------通讯录管理系统

通讯录管理系统

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

//设计联系人结构体
struct person
{
	string Name;
	int sex;
	int age;
	string number;
	string addr;
};

//设计通讯录结构体
struct addressbooks
{
	//通讯录中保存的联系人数组
	struct person personarray[MAX];
	//通讯录中当前联系人个数
	int size;
};

//1.添加联系人
void addperson(addressbooks* abs)
{
	if (abs -> size == MAX)
	{
		cout << "通讯录已满,无法添加" << endl;
		return;
	}
	else
	{
		//姓名
		string name;
		cout << "请输入姓名:" << endl;
		cin >> name;
		abs->personarray[abs->size].Name = name;
		//性别
		cout << "请输入性别:" << endl;
		cout << "1-----男:" << endl;
		cout << "2-----女:" << endl;
		int sex = 0;
		while (true)
		{
			cin >> sex;
			if (sex == 1||sex==2)
			{
				abs->personarray[abs->size].sex = sex;
				break;
			}
			cout << "输入错误,请重新输入" << endl;
		}
		//年龄
		cout << "请输入年龄:" << endl;
		int age;
		cin >> age;
		abs->personarray[abs->size].age = age;

		//电话
		cout<< "请输入联系电话:" << endl;
		string number;
		cin >> number;
		abs->personarray[abs->size].number = number;
		//家庭住址
		cout << "请输入家庭住址:" << endl;
		string addr;
		cin >> addr;
		abs->personarray[abs->size].addr = addr;

		//更新通讯录人数
		abs->size++;

		cout << "添加成功" << endl;
		system("pause");//按任意键继续
		system("cls");//清屏操作
	}
}
//2.显示所有联系人
void showperson(addressbooks* abs)
{
	if (abs->size == 0)
	{
		cout << "当前记录为空" << endl;
	}
	else
	{
		for (int i = 0; i < abs->size; i++)
		{
			cout << "姓名:" << abs->personarray[i].Name << "\t";
			cout << "性别:" << (abs->personarray[i].sex == 1 ? "男" : "女") << "\t";
			cout << "年龄:" << abs->personarray[i].age << "\t";
			cout << "电话:" << abs->personarray[i].number << "\t";
			cout << "住址:" << abs->personarray[i].addr << endl;		
		}
	}
	system("pause");//按任意键继续
	system("cls");//清屏操作
}


//检测联系人是否存在,如果存在返回联系人所在数组的具体位置
//不存在返回-1
int isexist(addressbooks * abs, string name)
{
	for (int i = 0; i < abs->size; i++)
	{
		//找到用户输入的姓名
		if (abs->personarray[i].Name == name)
		{
			return i;//找到了,返回这个人在数组的下标
		}
	}
	return -1;//遍历结束没有找到返回-1
}
//3.删除联系人
void deleteperson(addressbooks * abs)
{
	cout << "请输入您要删除的联系人" << endl;
	string name;
	cin >> name;
	int ret = isexist(abs, name);
	if (ret != -1)
	{
		//找到人,要进行删除操作
		for (int i = ret; i < abs->size; i++)
		{
			//数据前移
			abs->personarray[i] = abs->personarray[i + 1];
			abs->size--;//更新通讯录中的人数
		}
		cout << "删除成功" << endl;	
	}
	else
	{
		cout << "查无此人" << endl;
	}
	system("pause");//按任意键继续
	system("cls");//清屏操作
}

//4.查到指定联系人信息
void findperson(addressbooks * abs)
{
	cout << "请输入您要查找的联系人" << endl;
	string name;
	cin >> name;
	//判断指定的联系人是否存在通讯录中
	int ret = isexist(abs, name);
	if (ret != -1)//找到联系人
	{
		cout << "姓名:" << abs->personarray[ret].Name << "\t";
		cout << "性别:" << abs->personarray[ret].sex << "\t";
		cout << "年龄:" << abs->personarray[ret].age << "\t";
		cout << "电话:" << abs->personarray[ret].number << "\t";
		cout << "住址:" << abs->personarray[ret].addr << endl;
	}
	else//未找到联系人
	{
		cout << "查无此人" << endl;
	}
	system("pause");//按任意键继续
	system("cls");//清屏操作
}

//5.修改指定联系人信息、
void modifyperson(addressbooks* abs)
{
	cout << "请输入要修改的联系人" << endl;
	string name;
	cin >> name;
	//判断指定的联系人是否存在通讯录中
	int ret = isexist(abs, name);
	if (ret != -1)//找到联系人
	{
		//姓名
		string name;
		cout << "请输入姓名" << endl;
		cin >> name;
		abs->personarray[ret].Name = name;
		//性别
		cout << "请输入性别" << endl;
		cout << "1------男" << endl;
		cout << "2------女" << endl;
		while (true)
		{
			int sex;
			cin >> sex;
			if (sex == 1 || sex == 2)
			{
				abs->personarray[ret].sex = sex;
				break;
			}
			else
			{
				cout << "输入错误请重新输入" << endl;
			}
		}
		//年龄
		cout << "请输入年龄" << endl;
		int age;
		cin >> age;
		abs->personarray[ret].age = age;
		//电话
		cout << "请输入联系电话" << endl;
		string number;
		cin >> number;
		abs->personarray[ret].number = number;
		//住址
		cout << "请输入家庭住址" << endl;
		string addr;
		cin >> addr;
		abs->personarray[ret].addr = addr;
		cout << "修改成功" << endl;
	}
	else//未找到联系人
	{
		cout << "查无此人" << endl;
	}
	system("pause");//按任意键继续
	system("cls");//清屏操作
}

//清空联系人
void clearperson(addressbooks* abs)
{
	abs->size = 0;
	cout << "通讯录已清空" << endl;
	system("pause");//按任意键继续
	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()
{
	//创建通讯录结构体变量
	addressbooks abs;
	//初始化通讯录中当前人员个数
	abs.size = 0;
	int select;//创建用户选择输入的变量
	while ( true )
	{
		//菜单调用
		showMenu();
		cin >> select;
		if (select == 1)//添加联系人
		{
			addperson(&abs);
		}
		else if (select == 2)
		{
			showperson(&abs);
		}
		else if (select == 3)
		{
			deleteperson(&abs);
		}
		else if (select == 4)
		{
			findperson(&abs);
		}
		else if (select == 5)
		{
			modifyperson(&abs);
		}
		else if (select == 6)
		{
			clearperson(&abs);
		}
		else if (select == 0)
		{
			cout << "欢迎下次使用" << endl;
			break;
		}
	}
	
	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值