通讯管理系统

通讯管理系统

1、系统需求

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

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

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

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

2功能及其实现

1.输出菜单功能

#include<iostream>
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<iostream>
#include<string>
#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 << "查无此人" <<endl;
		cout << "请从新选择菜单" << endl;
		int select = 0;
		cin >> 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<iostream>
#include<string>
#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 << "*******请输入相应数字*****"<<endl;
}
//创建联系人结构体
struct Person
{
	string Name;//姓名
	int Sex = 0;//性别
	int Age = 0;//年龄
	string Phone;//电话
	string Addr;//住址
};

//通讯录结构体声明
struct Addressbooks
{
	struct Person personArray[MAX];
	int Size=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:
		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 << "查无此人" <<endl;
		cout << "请从新选择菜单" << endl;
		int select = 0;
		cin >> 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;
}
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
#define _CRT_SECURE_NO_WARNINGS 1 #ifndef __CONTACT_H_ #define __CONTACT_H_ #include<stdio.h> #include<stdlib.h> #include<string.h> #define PEO_MAX 30 #define STUID_MAX 10 #define NAME_MAX 20 #define SEX_MAX 10 #define TEL_MAX 15 typedef struct People { char id[STUID_MAX]; char name[NAME_MAX]; char sex[SEX_MAX]; char tel[TEL_MAX]; }*peo; typedef struct Contact { int count; struct People people[PEO_MAX]; }*pCon; void add_peo(pCon pcon); //添加联系人信息 void show_peo(pCon pcon); //显示指定联系人信息 void find_peo(pCon pcon); //查找联系人信息 void modify_peo(pCon pcon); //修改指定联系人信息 void clear_peo(pCon pcon); //清空联系人信息 void show_menu(); //菜单显示 #endif int search(pCon pcon, char *name) { int i = 0; for (i=0; i < pcon->count; i++) { if (strcmp(name, pcon->people[i].name) == 0) return i; } return -1; } void add_peo(pCon pcon) //添加联系人 { if (pcon->count == PEO_MAX) { printf("The contact has fullen."); return; } printf("please input studentID: "); scanf("%s",(pcon->people[pcon->count]).id); printf("please input name: "); scanf("%s", (pcon->people[pcon->count]).name); printf("please input sex : "); scanf("%s", (pcon->people[pcon->count]).sex); printf("please input tel: "); scanf("%s", (pcon->people[pcon->count]).tel); pcon->count++; } void show_peo(pCon pcon)//显示联系人 { int i = 0; for (; i < pcon->count; i++) { printf("studentID name sex tel \n"); printf("%s\t%s\t%s\t%s\n", pcon->people[i].id, pcon->people[i].name, pcon->people[i].sex, pcon->people[i].tel); } } void find_peo(pCon pcon) //查找联系人信息 { int i; char name[NAME_MAX]; printf("please input the people you want to find:"); scanf("%s", name); i = search(pcon, name); if (i == -1) printf("The people doesn't exsit.\n"); else printf("%s\t%s\t%s\t%s\n", pcon->people[i].id, pcon->people[i].name, pcon->people[i].sex, pcon->people[i].tel); } void modify_peo(pCon pcon) //修改指定联系人信息 { int i; char name[NAME_MAX]; printf("please input the people you want to modify:"); scanf("%s", name); i = search(pcon, name); if (i == -1) printf("The people doesn't exsit.\n"); else printf("please input studentID: "); scanf("%s", (pcon->people[i]).id); printf("please input name: "); scanf("%s", (pcon->people[i]).name); printf("please input sex : "); scanf("%s", (pcon->people[i]).sex); printf("please input tel: "); scanf("%s", (pcon->people[i]).tel); } void clear_peo(pCon pcon) //清空联系人 { pcon->count = 0; } void show_menu() //菜单显示 { printf("==========通讯录==========\n"); printf(" \n"); printf("*******[1]显示通讯录******\n"); printf("*******[2]查 询******\n"); printf("*******[3]修 改******\n"); printf("*******[4]结 束******\n"); } int main() { struct Contact my_contact; int input = 1; my_contact.count = 0; while (input) { show_menu(); printf("please input:"); scanf("%d", &input;); switch (input) { case 1: add_peo(&my;_contact); break; case 2: show_peo(&my;_contact); break; case 3: find_peo(&my;_contact); break; case 4: modify_peo(&my;_contact); break; case 5: clear_peo(&my;_contact); break; default: break; } } return 0; }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值