C++学习记录---通讯录管理系统案例

一、通讯录管理系统案例

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<string>
#include<stdio.h>
#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 m_name;
	int m_sex;
	int m_age;
	int m_phonenumber;
	string m_address;
};
//定义通讯录结构体
struct adressbooks
{
	//通讯录中保存的联系人数组
	struct person personarray[MAX];
	//当前记录的人数
	int m_size;
};

//定义添加联系人函数(地址传递)
void addperson(adressbooks * p)
{
	//判断是否已经满了,如果满了就提示
	if (p -> m_size == MAX)
	{
		cout << "通讯录已满,无法添加!" << endl;
		return;
	}
	else
	{
		//添加具体联系人
		//姓名
		string name;
		cout << "添加联系人姓名:" << endl;
		cin >> name;
		p->personarray[p->m_size].m_name = name;
		//性别
		cout << "添加联系人性别:" << endl;
		cout << "1 --- 男" << endl;
		cout << "2 --- 女" << endl;
		int sex = 0;
		while (true)
		{
			cin >> sex;
			if (sex == 1 || sex == 2)
			{
				p->personarray[p->m_size].m_sex = sex;
				break;
			}
			cout << "输入有误,请重新输入" << endl;
		}
		//年龄
		cout << "添加联系人年龄:" << endl;
		int age;
		cin >> age;
		p->personarray[p->m_size].m_age = age;
		//电话
		cout << "添加联系人电话:" << endl;
		int phonenumber;
		cin >> phonenumber;
		p->personarray[p->m_size].m_phonenumber = phonenumber;
		//地址
		cout << "添加联系人地址:" << endl;
		string address;
		cin >> address;
		p->personarray[p->m_size].m_address = address;
		//更新
		p->m_size++;
		cout<< "添加成功" << endl;
		system("pause");
		system("cls");
	}
}

//显示联系人
void showperson(adressbooks * p)
{
	//判断通讯录中人数是否为0,若为0,提示记录为空;不为则显示所有联系人
	if (p->m_size == 0)
	{
		cout << "当前记录为空" << endl;
	}
	else
	{
		for (int i = 0; i < p->m_size; i++)
		{
			cout << "姓名:" << p->personarray[i].m_name << "\t";
			cout << "性别:" << (p->personarray[i].m_sex == 1?"男":"女") << "\t";
			cout << "年龄:" << p->personarray[i].m_age << "\t";
			cout << "电话:" << p->personarray[i].m_phonenumber << "\t";
			cout << "住址:" << p->personarray[i].m_address << endl;
		}
	}
	system("pause");
	system("cls");
}

//检测联系人是否存在,如果存在,返回联系人所在的数组中的具体位置,不存在返回-1
int isExist(adressbooks* p, string name)
{
	for (int i = 0; i < p->m_size; i++)
	{
		if (p->personarray[i].m_name == name)
		{
			return i;
		}
	}
	return -1;
}

//删除联系人
void deleteperson(adressbooks* p)
{
	cout << "输入需要删除的联系人" << endl;
	string name;
	cin >> name;
	
	int ret = isExist(p, name);
	if (ret != -1)
	{
		for (int i = 0; i < p->m_size; i++)
		{
			//数据迁移
			p->personarray[i] = p->personarray[i + 1];
		}
		p->m_size--;
		cout << "删除成功" << endl;
	}
	else
	{
		cout << "查无此人" << endl;
	}
	system("pause");
	system("cls");
}

//查找指定联系人
void findperson(adressbooks* p)
{
	cout << "请输入您要查找的联系人" << endl;
	string name;
	cin >> name;
	int ret = isExist(p, name);
	if (ret != -1)
	{
		cout << "姓名:" << p->personarray[ret].m_name << "\t";
		cout << "性别:" << p->personarray[ret].m_sex << "\t";
		cout << "年龄:" << p->personarray[ret].m_age << "\t";
		cout << "电话:" << p->personarray[ret].m_phonenumber << "\t";
		cout << "住址:" << p->personarray[ret].m_address << endl;
	}
	else
	{
		cout << "查无此人" << endl;
	}
	system("pause");
	system("cls");
}
//修改联系人
void modifyperson(adressbooks* p)
{
	cout << "输入修改联系人姓名" << endl;
	string name;
	cin >> name;
	int ret = isExist(p, name);
	if (ret != -1)
	{
		//姓名
		string name;
		cout << "请输入姓名:" << endl;
		cin >> name;
		p->personarray[ret].m_name = name;

		//性别
		cout << "请输入性别:" << endl;
		cout << "1 --- 男" << endl;
		cout << "2 --- 女" << endl;
		int sex = 0;
		while (true)
		{
			cin >> sex;
			if (sex == 1 || sex == 2)
			{
				p->personarray[ret].m_sex = sex;
				break;
			}
			cout << "输入有无,请重新输入" << endl;
		}
		
		//年龄
		int age;
		cout << "请输入年龄:" << endl;
		cin >> age;
		p->personarray[ret].m_age = age;
		//电话
		int phone;
		cout << "请输入电话:" << endl;
		cin >> phone;
		p->personarray[ret].m_phonenumber = phone;
		//地址
		string address;
		cout << "请输入家庭住址:" << endl;
		cin >> address;
		p->personarray[ret].m_address = address;
		cout << "修改成功" << endl;
	}
	else
	{
		cout << "查无此人" << endl;
	}
	system("pause");
	system("cls");
}

//清空联系人
void cleanperson(adressbooks* p)
{
	p->m_size = 0;
	cout << "通讯录已清空" << endl;
	system("pause");
	system("cls");
}

//main函数
int main()
{
	//创建通讯录结构体
	adressbooks abs;
	abs.m_size = 0; //初始化人数
	
	//switch语句的多分支选择
	int select = 0;
	while (true)
	{
		//调用函数展示界面
		showMenu();
		cin >> select; // 输入
		switch (select)
		{
		case 1: //添加
			addperson(&abs);
			break;
		case 2: //显示
			showperson(&abs);
			break;
		case 3:// 删除
/*		{
			cout << "请输入删除联系人姓名:" << endl;
			string name;
			cin >> name;
			if (isExist(&abs, name))
			{
				cout << "查无此人" << endl;
			}
			else
			{
				cout << "找到此人" << endl;
			}
		}*/	
			deleteperson(&abs);
			break;
		case 4: //查找
			findperson(&abs);
			break;
		case 5: //修改
			modifyperson(&abs);
			break;
		case 6: //清空
			cleanperson(&abs);
			break;
		case 0: //退出
			cout << "谢谢使用,欢迎下次光临!" << endl;
			system("pause");
			return 0;
			break;
		}
	}
	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值