案例---通讯录

#include <iostream>
using namespace std;
#include<string>;
#define max 1000
/*
编写一个通讯录管理系统
可以添加联系人,显示联系人,删除联系人,查找联系人,
修改联系人,清空联系人,退出通讯录,当没有查找的信息时,弹出查无此人
*/

//显示菜单界面
void show()
{
	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 people {
	string name;
	int  sex;//1代表男	2代表女
	int age;
	string phone;
	string address;
};
//通讯录结构体
struct phonebook {
	//通讯录中保存的联系人数组
	struct people peoplearr[max];
	//当前记录的人数
	int size;
};
//添加联系人
void addperson(phonebook * pb) 
{
	//判断通讯录是否已满
	if (pb->size == max)
	{
		cout << "人数已满" << endl;
		return;
	}
	else {
		//添加联系人
		//姓名
		string name;
		cout << "请输入姓名" << endl;
		cin >> name;
		pb->peoplearr[pb->size].name = name;
		//性别
		int sex;
		cout << "输入性别,1代表男,2代表女" << endl;
		while (true)
		{
			cin >> sex;
			if (sex == 1 || sex == 2)
			{
				pb->peoplearr[pb->size].sex = sex;
				break;
			}
		}
		//年龄
		int age;
		cout << "输入年龄" << endl;
		cin >> age;
		pb->peoplearr[pb->size].age = age;
		//电话
		string phone;
		cout << "输入电话" << endl;
		cin >> phone;
		pb->peoplearr[pb->size].phone = phone;
		//地址
		string address;
		cout << "输入地址" << endl;
		cin >> address;
		pb->peoplearr[pb->size].address = address;
		//更新通讯录人数
		pb->size++;
		//清屏
		system("pause");//按任意键继续
		system("cls");//清屏
	}
}

//显示所有联系人
void showpeople(phonebook * pb)
{
	if (pb->size == 0)
	{
		cout << "通讯录为空" << endl;
	}
	else {
		for (int i = 0; i < pb->size; i++)
		{
			cout << pb->peoplearr[i].name << " " << (pb->peoplearr[i].sex == 1?"男":"女") << " " << pb->peoplearr[i].age
				<< " " << pb->peoplearr[i].phone << " " << pb->peoplearr[i].address << endl;
		}
	}
	system("pause");
	system("cls");
}
//确认联系人是否存在,如果存在返回具体位置,不存在返回-1
int exist(phonebook* pb, string name)
{
	for (int i = 0; i < pb->size; i++)
	{
		if (pb->peoplearr[i].name == name)
		{
			return i;
		}
	}
	return -1;
}
//删除联系人,只是逻辑上的删除,并不是真的删除,实现真正的删除要使用动态数组
void deletepeople(phonebook * pb)
{
	cout << "输入要删除的联系人姓名" << endl;
	string name;
	cin >> name;
	//调用函数确定要删除的人名是否存在
	int a=exist(pb,name);
	if (a == -1)
	{
		cout << "联系人不存在" << endl;
	}
	else if (a == pb->size - 1)
	{
		pb->size--;
	}
	else
	{
		for (int i = a; i < pb->size; i++)
		{
			pb->peoplearr[i] = pb->peoplearr[i + 1];
		}
		pb->size--;
	}
	system("pause");
	system("cls");
}
//查找联系人
void findpeople(phonebook* pb)
{
	cout << "输入要查找的联系人" << endl;
	string name;
	cin >> name;
	int result=exist(pb, name);
	if (result == -1)
	{
		cout << "查无此人" << endl;
	}
	else
	{
		cout << "姓名" << pb->peoplearr[result].name << " " <<"性别" << (pb->peoplearr[result].sex == 1 ? "男" : "女") << " " <<"年龄" << pb->peoplearr[result].age
			<< " " <<"电话" << pb->peoplearr[result].phone << " " <<"住址" << pb->peoplearr[result].address << endl;
	}
	system("pause");
	system("cls");
}
//修改联系人,可以用switch选择结构来确定用户想要修改的项
void changepeople(phonebook* pb)
{
	cout << "请输入要修改的联系人" << endl;
	string name;
	cin >> name;
	int result = exist(pb, name);
	if (result == -1)
	{
		cout << "查无此人" << endl;
	}
	else
	{
		cout << "输入修改后姓名" << endl;
		cin >> pb->peoplearr[result].name;
		cout << "输入修改后年龄" << endl;
		cin >> pb->peoplearr[result].age;
		cout << "输入修改后性别,1代表男,2代表女" << endl;
		cin >> pb->peoplearr[result].sex;
		cout << "输入修改后电话" << endl;
		cin >> pb->peoplearr[result].phone;
		cout << "输入修改后地址" << endl;
		cin>>pb->peoplearr[result].address;
	}
	system("pause");
	system("cls");
}
//清空联系人
void clearpeople(phonebook* pb)
{
	cout << "是否确定要清空,y or n" << endl;
	char ch;
	cin >> ch;
	if (ch == 'y')
	{
		pb->size = 0;
		cout << "通讯录已清空" << endl;
	}
	else {
		return;
	}
	system("pause");
	system("cls");
}
int main()
{
	//创建通讯录
	phonebook pb;
	//初始化通讯录中的人数
	pb.size = 0;
	int select = 0;//创建用户选择输入的变量
	while (true) 
	{
		//调用显示菜单功能
		show();
		cin >> select;
		switch (select)
		{
		case 1://添加
			addperson(&pb);
			break;
		case 2://显示
			showpeople(&pb);
			break;
		case 3://删除
			deletepeople(&pb);
			break;
		case 4://查找
			findpeople(&pb);
			break;
		case 5://修改
			changepeople(&pb);
			break;
		case 6://清空
			clearpeople(&pb);
			break;
		case 0://退出通讯录
			cout << "欢迎下次使用" << endl;
			return 0;//结束函数
			break;
		default:
			break;
		}
	}
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

123。.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值