通讯录管理系统

一:菜单设计:showMenu()

二:退出通讯录

      相关内容:利用switch语句来选择要进行的步骤

      注意;除了0可以退出通讯录:在case 0时后加system("pause");return 0;

      按任意的数字都继续显示菜单:利用while循环。

三:添加联系人:void addPerson(AddressBook *abs)//注意:传指针,因为传值不改变实参。

     1.设计联系人结构体

     2.设计通讯录结构体

     3.main函数中创建通讯录:创建通讯录结构体变量abs,初始化当前人员个数。

     4.封装添加联系人函数

     5.测试添加联系人功能

      注意:首先判满,其次进行添加,联系人数组不能写死,则写m_size,其次,在输入联系人年龄是,利用while循环来防止用户出错。

              system("pause");//请按任意键继续

              system("cls");//清屏

四:显示联系人  void showPerson(Addressbooks * abs)

1.封装显示联系函数

2.测试显示联系功能

注意:首先判断该通讯录中是否有记录,有则查找,无则显示记录为空。(利用if语句)

          ”/t“//8个空格

           注意判断句注意优先级

五:删除联系人

        1.封装检测联系人是否存在//是否存在,存在返回下标i,不存在返回-1. void isexit(Addressbooks * abs,string name)

       2.封装删除联系人函数

       3.测试删除联系人功能

实现功能:

1.添加联系人

void addperson(AddressBooks* abs)
{
	//判断通讯录容量是否满了
	if (abs->m_size == MAX)
	{
		cout << "通讯录已满,添加失败!" << endl;
		return;
	}
	else
	{
		//姓名
		string name;
		cout << "请输入姓名:" << endl;
		cin >> name;
		abs->perArray[abs->m_size].m_name = name;
		//性别
		cout << "请输入性别:" << endl;
		cout << "1.男" << endl;
		cout << "2.女" << endl;
		int sex = 0;
		while (true)
		{
			//如果输入1和2,则跳出循环,否则重新输入
			cin >> sex;
			if (sex == 1 || sex == 2)
			{
				abs->perArray[abs->m_size].m_sex = sex;
				break;
			}
			cout << "输入值错误,请重新输入:" << endl;
			
		}
		//年龄
		cout << "请输入年龄:" << endl;
		int age = 0;
		cin >> age;
		abs->perArray[abs->m_size].m_age = age;
		//电话
		cout << "请输入电话:" << endl;
		string phone;
		cin >> phone;
		abs->perArray[abs->m_size].m_phone = phone;
		//住址
		cout << "请输入住址:" << endl;
		string addr;
		cin >> addr;
		abs->perArray[abs->m_size].m_addr= addr;
		//更新通讯录人数
		abs->m_size++;
		cout << "添加成功!" << endl;
		system("pause");//按任意键继续
		system("cls");//清屏

	}
}

实现结果:

2.显示联系人

void showperson(AddressBooks* abs)
{
	//首先判断是否有数据,有则显示,无则显示记录为空
	if (abs->m_size == 0)
	{
		cout << "该记录为空!" << endl;
	}
	else
	{
		for (int i = 0; i < abs->m_size; i++)
		{
			cout << "姓名:" << abs->perArray[i].m_name << "\t";
			cout << "性别:" <<( abs->perArray[i].m_sex==1?"男":"女")<< "\t";
			cout << "年龄:" << abs->perArray[i].m_age << "\t";
			cout << "电话:" << abs->perArray[i].m_phone << "\t";
			cout << "住址:" << abs->perArray[i].m_addr<< "\t";

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

}

实现结果:

3.删除联系人

int isexit(AddressBooks* abs,string name)
{
	//如果找到了,返回下标i,找不到返回-1
	for (int i = 0; i < abs->m_size; i++)
	{
		if (abs->perArray[i].m_name == name)
		{
			return i;
		}
		
	}
	return -1;
}
void deleteperson(AddressBooks* abs)
{
	cout << "请输入要删除人的名字:" << endl;
	string name;
	cin >> name;
	int i=isexit(abs, name);
	if (i != -1)
	{
		for (int j = i; j < abs->m_size; j++)
		{
			abs->perArray[j] = abs->perArray[j + 1];
		}
	}
	else
	{
		cout << "没有此人,删除失败!" << endl;
	}
	abs->m_size--;
	system("pause");
	system("cls");


}

实现结果:

 

4.查找联系人:

void findperson(AddressBooks* abs)
{
	cout << "输入需要查找人的姓名:" << endl;
	string name;
	cin >> name;
	int ret = isexit(abs, name);
	if (ret != -1)
	{
		cout << "姓名:" << abs->perArray[ret].m_name << "\t";
		cout << "性别:" << (abs->perArray[ret].m_sex == 1 ? "男" : "女") << "\t";
		cout << "年龄:" << abs->perArray[ret].m_age << "\t";
		cout << "电话:" << abs->perArray[ret].m_phone << "\t";
		cout << "住址:" << abs->perArray[ret].m_addr << "\t";

	}
	else
	{
		cout << "查无此人!" << endl;
	}
	system("pause");
	system("cls");
}

实现结果:

 5.修改联系人:

void modifyperson(AddressBooks* abs)
{
	cout << "输入需要修改人的姓名:" << endl;
	string name;
	cin >> name;
	int ret = isexit(abs, name);
	if (ret != -1)
	{
		cout << "输入修改后联系人的姓名:" << endl;
		string name;
		cin >> name;
		abs->perArray[ret].m_name = name;
		cout << "输入修改后联系人的性别:" << endl;
		cout << "1----男" << endl;
		cout << "1----女" << endl;
		int sex = 0;
		while (true)
		{
			cin >> sex;
			if (sex == 1 || sex == 2)
			{
				abs->perArray[ret].m_sex = sex;
				break;
			}
			cout << "输入有误,请重新输入!";
					
		}
		cout << "输入修改后联系人的年龄:" << endl;
		int age = 0;
		cin >> age;
		abs->perArray[ret].m_age = age;
		cout << "输入修改后联系人的电话:" << endl;
		string phone;
		cin >> phone;
		abs->perArray[ret].m_phone = phone;
		cout << "输入修改后联系人的地址:" << endl;
		string addr;
		cin >> addr;
		abs->perArray[ret].m_addr = addr;
		cout << "修改成功!" << endl;
		system("pause");
		system("cls");
	}
	else
	{
		cout << "查无此人,修改失败!";
	}

}

实现结果:

 6.清空联系人:

void cleanperson(AddressBooks* abs)//将m_size置成0
{
	abs->m_size = 0;
	cout << "通讯录为空!" << endl;
	system("pause");
	system("cls");
}

 所有代码:

#include"txlg.h"
#include<iostream>
#include<string>
using namespace std;
#define MAX 1000

//设计联系人结构体
struct person
{
	string m_name;
	int m_sex;//1男,2女
	int m_age;
	string m_phone;
	string m_addr;
};
//设计通讯录结构体
struct AddressBooks
{
	//创建一个存放联系人的数组
	struct person perArray[MAX];
	int m_size;
};

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;
}

void addperson(AddressBooks* abs)
{
	//判断通讯录容量是否满了
	if (abs->m_size == MAX)
	{
		cout << "通讯录已满,添加失败!" << endl;
		return;
	}
	else
	{
		//姓名
		string name;
		cout << "请输入姓名:" << endl;
		cin >> name;
		abs->perArray[abs->m_size].m_name = name;
		//性别
		cout << "请输入性别:" << endl;
		cout << "1.男" << endl;
		cout << "2.女" << endl;
		int sex = 0;
		while (true)
		{
			//如果输入1和2,则跳出循环,否则重新输入
			cin >> sex;
			if (sex == 1 || sex == 2)
			{
				abs->perArray[abs->m_size].m_sex = sex;
				break;
			}
			cout << "输入值错误,请重新输入:" << endl;
			
		}
		//年龄
		cout << "请输入年龄:" << endl;
		int age = 0;
		cin >> age;
		abs->perArray[abs->m_size].m_age = age;
		//电话
		cout << "请输入电话:" << endl;
		string phone;
		cin >> phone;
		abs->perArray[abs->m_size].m_phone = phone;
		//住址
		cout << "请输入住址:" << endl;
		string addr;
		cin >> addr;
		abs->perArray[abs->m_size].m_addr= addr;
		//更新通讯录人数
		abs->m_size++;
		cout << "添加成功!" << endl;
		system("pause");//按任意键继续
		system("cls");//清屏

	}
}
void showperson(AddressBooks* abs)
{
	//首先判断是否有数据,有则显示,无则显示记录为空
	if (abs->m_size == 0)
	{
		cout << "该记录为空!" << endl;
	}
	else
	{
		for (int i = 0; i < abs->m_size; i++)
		{
			cout << "姓名:" << abs->perArray[i].m_name << "\t";
			cout << "性别:" <<( abs->perArray[i].m_sex==1?"男":"女")<< "\t";
			cout << "年龄:" << abs->perArray[i].m_age << "\t";
			cout << "电话:" << abs->perArray[i].m_phone << "\t";
			cout << "住址:" << abs->perArray[i].m_addr<< "\t";

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

}
int isexit(AddressBooks* abs,string name)
{
	//如果找到了,返回下标i,找不到返回-1
	for (int i = 0; i < abs->m_size; i++)
	{
		if (abs->perArray[i].m_name == name)
		{
			return i;
		}
		
	}
	return -1;
}
void deleteperson(AddressBooks* abs)
{
	cout << "请输入要删除人的名字:" << endl;
	string name;
	cin >> name;
	int i=isexit(abs, name);
	if (i != -1)
	{
		for (int j = i; j < abs->m_size; j++)
		{
			abs->perArray[j] = abs->perArray[j + 1];
		}
	}
	else
	{
		cout << "没有此人,删除失败!" << endl;
	}
	abs->m_size--;
	system("pause");
	system("cls");


}
void findperson(AddressBooks* abs)
{
	cout << "输入需要查找人的姓名:" << endl;
	string name;
	cin >> name;
	int ret = isexit(abs, name);
	if (ret != -1)
	{
		cout << "姓名:" << abs->perArray[ret].m_name << "\t";
		cout << "性别:" << (abs->perArray[ret].m_sex == 1 ? "男" : "女") << "\t";
		cout << "年龄:" << abs->perArray[ret].m_age << "\t";
		cout << "电话:" << abs->perArray[ret].m_phone << "\t";
		cout << "住址:" << abs->perArray[ret].m_addr << "\t";

	}
	else
	{
		cout << "查无此人!" << endl;
	}
	system("pause");
	system("cls");
}
void modifyperson(AddressBooks* abs)
{
	cout << "输入需要修改人的姓名:" << endl;
	string name;
	cin >> name;
	int ret = isexit(abs, name);
	if (ret != -1)
	{
		cout << "输入修改后联系人的姓名:" << endl;
		string name;
		cin >> name;
		abs->perArray[ret].m_name = name;
		cout << "输入修改后联系人的性别:" << endl;
		cout << "1----男" << endl;
		cout << "1----女" << endl;
		int sex = 0;
		while (true)
		{
			cin >> sex;
			if (sex == 1 || sex == 2)
			{
				abs->perArray[ret].m_sex = sex;
				break;
			}
			cout << "输入有误,请重新输入!";
					
		}
		cout << "输入修改后联系人的年龄:" << endl;
		int age = 0;
		cin >> age;
		abs->perArray[ret].m_age = age;
		cout << "输入修改后联系人的电话:" << endl;
		string phone;
		cin >> phone;
		abs->perArray[ret].m_phone = phone;
		cout << "输入修改后联系人的地址:" << endl;
		string addr;
		cin >> addr;
		abs->perArray[ret].m_addr = addr;
		cout << "修改成功!" << endl;
		system("pause");
		system("cls");
	}
	else
	{
		cout << "查无此人,修改失败!";
	}

}
void cleanperson(AddressBooks* abs)//将m_size置成0
{
	abs->m_size = 0;
	cout << "通讯录为空!" << endl;
	system("pause");
	system("cls");
}
int main()
{
	//创建通讯录变量
     AddressBooks abs;
	//初始化通讯录
	abs.m_size = 0;
	int select = 0;
	while (true)
	{
		showMenu();

		cin >> select;
		switch (select)
		{
		case 1://添加联系人
			addperson(&abs);
			break;
		case 2://显示联系人
			showperson(&abs);
			break;
		case 3://删除联系人	
			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;
		default:
			break;

		}
	}
	
	system("pause");
	return 0;
}

 

 

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值