通讯管理系统

通讯管理系统,第1张

通讯管理系统 通讯管理系统 1、系统需求

通讯录是一个可以记录亲人、好友信息的工具。

本教程主要利用C++来实现一个通讯录管理系统

系统中需要实现的功能如下:

添加联系人:向通讯录中添加新人,信息包括(姓名、性别、年龄、联系电话、家庭住址)最多记录1000人显示联系人:显示通讯录中所有联系人信息删除联系人:按照姓名进行删除指定联系人查找联系人:按照姓名查看指定联系人信息修改联系人:按照姓名重新修改指定联系人清空联系人:清空通讯录中所有信息退出通讯录:退出当前使用的通讯录 2功能及其实现 1.输出菜单功能

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

int main() {

	showMenu();

	system("pause");

	return 0;
}
2.退出功能

先输出菜单,对利用循环体,只有输入“0”时退出,可重复选择各种功能

在头文件下方添加一个宏定义一个全局变量并赋值为1000来定义通讯录上限,方便后期修改通讯录上限。

#include
#include
#define MAX 1000//宏定义一个数1000
using namespace std;

退出功能:先输出菜单,对利用循环体,只有输入“0”时退出,可重复选择各种功能。

int main()
{
	showMenu();//输出菜单
	int select = 0;
	cin >> select;
	if (select == 0)
		cout << "欢迎下次使用" << endl;
	else
	{
		while (select<=MAX)
		{
			if (select == 0)
			{
				cout << "欢迎下次使用" << endl;
				break;
			}		
}
3.将菜单功能封装

​ 利用switch函数来选择功能每一次使用完功能都输出菜单

菜单函数顾名思义就是调用下面的功能函数。

//菜单函数
void Menu(int select, Addressbooks* abs)
{
t
	switch (select)
	{
	case 1://添加联系人
		addPerson(abs);
		showMenu();//输出菜单
		break;
	case 2://显示联系人
		showPerson(abs);
		showMenu();//输出菜单
		break;
	case 3://删除联系人
		delPerson(abs);
		showMenu();//输出菜单
		break;
	case 4://查找联系人
		findPerson(abs);
		showMenu();//输出菜单
		break;
	case 5://修改联系人
		modifyPerson(abs);
		showMenu();//输出菜单
		break;
	case 6://清空联系人
		cleamPerson(abs);
		showMenu();//输出菜单
		break;
	}
}
4.定义联系人结构体

​ 创建一个联系人结构体,结构体里面包含联系人的姓名,性别,年龄,电话,住址的信息。

​ 在定义一个通讯录结构体,内嵌一个联系人结构体的数组,容量为"MAX",和人数的变量“Size”

//创建联系人结构体
struct Person
{
	string Name;//姓名
	int Sex = 0;//性别
	int Age = 0;//年龄
	string Phone;//电话
	string Addr;//住址
};

//通讯录结构体声明
struct Addressbooks
{
	struct Person personArray[MAX];
	int Size=0;//初始化,不进行初始化编译器报了警告
};
5.添加联系人功能

​ 定义一个结构体变量“abs”,Addressbooks abs;并对其进行初始化

并将参数"abs",和选择的功能传递给菜单函数

int main()
{
	Addressbooks abs;//创建通讯录
	abs.Size = 0;//通讯录初始化
	showMenu();//输出菜单
	int select = 0;
	cin >> select;
	if (select == 0)
		cout << "欢迎下次使用" << endl;
	else
	{
		while (select<=6)
		{
			if (select == 0)
			{
				cout << "欢迎下次使用" << endl;
				break;
			}
			else
			{
				Menu(select, &abs);//传参
				cin >> select;//功能选择
			}
		}
		system("pause");
		
	}
	
	return 0;
			
}

​ 首先要判断通讯录是否满了,其次才能进行添加,

//添加联系人
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;
	leable://goto函数返回处;;只能输入1/2
		cin >> sex;
		if (sex == 1 || sex == 2)
			abs->personArray[abs->Size].Sex = sex;
		else
		{
			cout << "输入错误" << endl;
			cout << "输入性别" << endl;
			cout << "1 -- 男" << endl;
			cout << "2 -- 女" << endl;

			goto leable;

		}

		cout << "输入年龄" << endl;
		int age = 0;
		cin >> age;
		abs->personArray[abs->Size].Age = age;

		cout << "输入电话号" << endl;
		string phone = "";
		cin >> phone;
		abs->personArray[abs->Size].Phone = phone;

		cout << "输入住址" << endl;
		string addr = "";
		cin >> addr;
		abs->personArray[abs->Size].Addr = addr;
		abs->Size++;
		cout << "添加成功"<< endl;
	}
}
6.显示所有联系人功能
//显示联系人
void showPerson(Addressbooks* abs)
{
	if (abs->Size == 0)
	{
		cout << "当前通讯录为空" << endl;
	}
	else
	{
		int i;
		for (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].Phone << "t";
			cout << "住址:" << abs->personArray[i].Addr << endl;
		}
	}
	
}
7.删除联系人功能
//删除联系人
//判断是否存在查询的人员,存在返回在数组中索引位置,不存在返回-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;
}

//删除指定联系人
void delPerson(Addressbooks* abs)
{	
	cout << "前请输入要删除的联系人姓名" << endl;
	string name;
	cin >> name;
	int Return=isExist(abs, name);
	if (Return != -1)
	{
		int i;
		i= Return; 
		abs->personArray[i] = abs->personArray[i + 1];
		abs->Size--;
		cout << "????" << endl;
		cout << "删除成功" << endl;
		cout << "????" << endl << endl << endl;
	}
	else
	{
		cout << "????" << endl;
		cout << "查无此人" << endl; 
		cout << "????" << endl << endl << endl;
		
	}
}
8.查找联系人功能
void findPerson(Addressbooks* abs)
{
	cout << "请输入需要查找的联系人姓名" << endl;
	string name;
	cin >> name;
	int Return = isExist(abs, name);
	if(Return!=-1)
		cout << Return;
	else
	{
		cout << "????" << endl;
		cout << "查无此人" << endl;
		cout << "????" << endl << endl << endl;
	}

}
9.修改联系人功能
//修改联系人
void modifyPerson(Addressbooks* abs)
{
	cout << "请输入需要修改的联系人" << endl;
	string name;
	cin >> name;
	int Return = isExist(abs, name);
	if (Return != -1)
	{
		string name;
		cout << "输入姓名" << endl;
		cin >> name;
		abs->personArray[abs->Size].Name = name;

		cout << "输入性别" << endl;
		cout << "1 -- 男" << endl;
		cout << "2 -- 女" << endl;
		int sex = 0;
		cin >> sex;
		abs->personArray[abs->Size].Sex = sex;

		cout << "输入年龄" << endl;
		int age = 0;
		cin >> age;
		abs->personArray[abs->Size].Age = age;

		cout << "输入电话号" << endl;
		string phone = "";
		cin >> phone;
		abs->personArray[abs->Size].Phone = phone;

		cout << "输入住址" << endl;
		string addr = "";
		cin >> addr;
		abs->personArray[abs->Size].Addr = addr;

		abs->Size=Return;
		cout << "修改成功" << endl;
	}
	else
	{
		showMenu();//输出菜单
		cout << "查无此人" <> select;
		Menu(select, abs);
	}
10.清空联系人功能
//清空联系人
void cleamPerson(Addressbooks* abs)

{
	cout << "!!!!!!!!!!!" << endl;
	cout << "您确定要清空通讯录吗?" << endl;
	cout << "!!!!!!!!!!!" << endl;
	cout << "清空请输入“1”,返回请输入“0”" << endl;
	int i = 0;
	cin >> i;
	if (i == 1)
		abs->Size = 0;
}
附录:完整代码
#include
#include
#define MAX 1000
using namespace std;

//输出菜单功能
void showMenu()
{
	cout << "**************************n" << endl;
	cout << "****  1、添加联系人  *****n" << endl;
	cout << "****  2、显示联系人  *****n" << endl;
	cout << "****  3、删除联系人  *****n" << endl;
	cout << "****  4、查找联系人  *****n" << endl;
	cout << "****  5、修改联系人  *****n" << endl;
	cout << "****  6、清空联系人  *****n" << endl;
	cout << "****  0、退出通讯录  *****n" << endl;
	cout << "**************************n" << endl;
	cout << "*******请输入相应数字*****"<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;
	leable:
		cin >> sex;
		if (sex == 1 || sex == 2)
			abs->personArray[abs->Size].Sex = sex;
		else
		{
			cout << "输入错误" << endl;
			cout << "输入性别" << endl;
			cout << "1 -- 男" << endl;
			cout << "2 -- 女" << endl;

			goto leable;

		}

		cout << "输入年龄" << endl;
		int age = 0;
		cin >> age;
		abs->personArray[abs->Size].Age = age;

		cout << "输入电话号" << endl;
		string phone = "";
		cin >> phone;
		abs->personArray[abs->Size].Phone = phone;

		cout << "输入住址" << endl;
		string addr = "";
		cin >> addr;
		abs->personArray[abs->Size].Addr = addr;
		abs->Size++;
		cout << "添加成功"<< endl;
	}
}


//显示联系人
void showPerson(Addressbooks* abs)
{
	if (abs->Size == 0)
	{
		cout << "当前通讯录为空" << endl;
	}
	else
	{
		int i;
		for (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].Phone << "t";
			cout << "住址:" << abs->personArray[i].Addr << endl;
		}
	}
	
}


//删除联系人
//判断是否存在查询的人员,存在返回在数组中索引位置,不存在返回-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;
}

//删除指定联系人
void delPerson(Addressbooks* abs)
{	
	cout << "前请输入要删除的联系人姓名" << endl;
	string name;
	cin >> name;
	int Return=isExist(abs, name);
	if (Return != -1)
	{
		int i;
		i= Return; 
		abs->personArray[i] = abs->personArray[i + 1];
		abs->Size--;
		cout << "????" << endl;
		cout << "删除成功" << endl;
		cout << "????" << endl << endl << endl;
	}
	else
	{
		cout << "????" << endl;
		cout << "查无此人" << endl; 
		cout << "????" << endl << endl << endl;
		
	}
}
//修改联系人    函数声明
void modifyPerson(Addressbooks* abs);

//查找联系人 
void findPerson(Addressbooks* abs)
{
	cout << "请输入需要查找的联系人姓名" << endl;
	string name;
	cin >> name;
	int Return = isExist(abs, name);
	if(Return!=-1)
		cout << Return;
	else
	{
		cout << "????" << endl;
		cout << "查无此人" << endl;
		cout << "????" << endl << endl << endl;
	}

}

//清空联系人    函数声明
void cleamPerson(Addressbooks* abs);



//菜单函数
void Menu(int select, Addressbooks* abs)
{

	switch (select)
	{
	case 1://添加联系人
		addPerson(abs);
		showMenu();//输出菜单
		break;
	case 2://显示联系人
		showPerson(abs);
		showMenu();//输出菜单
		break;
	case 3://删除联系人
		delPerson(abs);
		showMenu();//输出菜单
		break;
	case 4://查找联系人
		findPerson(abs);
		showMenu();//输出菜单
		break;
	case 5://修改联系人
		modifyPerson(abs);
		showMenu();//输出菜单
		break;
	case 6://清空联系人
		cleamPerson(abs);
		showMenu();//输出菜单
		break;
	}
}








//主函数
int main()
{
	Addressbooks abs;//创建通讯录
	abs.Size = 0;//通讯录初始化
	showMenu();//输出菜单
	int select = 0;
	cin >> select;
	if (select == 0)
		cout << "欢迎下次使用" << endl;
	else
	{
		while (select<=6)
		{
			if (select == 0)
			{
				cout << "欢迎下次使用" << endl;
				break;
			}
			else
			{
				Menu(select, &abs);
				cin >> select;
			}
		}
		system("pause");
		
	}
	
	return 0;
}

//修改联系人
void modifyPerson(Addressbooks* abs)
{
	cout << "请输入需要修改的联系人" << endl;
	string name;
	cin >> name;
	int Return = isExist(abs, name);
	if (Return != -1)
	{
		string name;
		cout << "输入姓名" << endl;
		cin >> name;
		abs->personArray[abs->Size].Name = name;

		cout << "输入性别" << endl;
		cout << "1 -- 男" << endl;
		cout << "2 -- 女" << endl;
		int sex = 0;
		cin >> sex;
		abs->personArray[abs->Size].Sex = sex;

		cout << "输入年龄" << endl;
		int age = 0;
		cin >> age;
		abs->personArray[abs->Size].Age = age;

		cout << "输入电话号" << endl;
		string phone = "";
		cin >> phone;
		abs->personArray[abs->Size].Phone = phone;

		cout << "输入住址" << endl;
		string addr = "";
		cin >> addr;
		abs->personArray[abs->Size].Addr = addr;

		abs->Size=Return;
		cout << "修改成功" << endl;
	}
	else
	{
		showMenu();//输出菜单
		cout << "查无此人" <> select;
		Menu(select, abs);
	}




}
//清空联系人
void cleamPerson(Addressbooks* abs)

{
	cout << "!!!!!!!!!!!" << endl;
	cout << "您确定要清空通讯录吗?" << endl;
	cout << "!!!!!!!!!!!" << endl;
	cout << "清空请输入“1”,返回请输入“0”" << endl;
	int i = 0;
	cin >> i;
	if (i == 1)
		abs->Size = 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5713579.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存