欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

通讯录管理系统

程序员文章站 2022-07-04 20:07:01
...

通讯录管理系统

1.系统需求

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

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

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

1.添加联系人:向通讯录中添加新人,信息包括(姓名、性别、年龄、联系电话、家庭住址)最多纪录1000人

2.显示联系人:显示通讯录中所有联系人的信息

3.删除联系人:按照姓名进行删除指定联系人

4.查找联系人:按照姓名查看指定联系人信息

5.修改联系人:按照姓名重新修改指定联系人

6.清空联系人:清空通讯录中所有信息

7.退出通讯录:退出当前使用的通讯录

2.创建项目

新建项目

3.菜单功能

功能描述:用户选择功能的界面

步骤:封装函数显示该界面, 如void showMenu()

在main函数中调用封装好的函数

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

4.退出功能

功能描述:退出通讯录系统

思路:根据用户的不同选择,进入不同的功能,可以选择switch分支结构。将整个构架进行搭建

当用户选择0时,执行退出,选择其他先不做操作,也会退出程序。

5.添加联系人

功能实现:实现添加联系人功能,联系人上限为1000人,联系人信息包括(姓名、年龄、联系电话、家庭住址)

添加联系人实现步骤:

1、设计联系人结构体

2、设计通讯录结构体

3、main函数中创建通讯录

4、封装添加联系人函数

5、测试添加联系人函数

void addPerson(Addressbooks *abs) {
	if (abs->m_size == MAX_N) {
		cout << "通讯录已满,无法添加!";
		return;
	}
	else {
		string name;
		cout << "请输入姓名:" << endl;
		cin >> name;
		abs->personArray[abs->m_size].name = name;
		cout << "请输入性别:" << endl;
		cout << "1——男" << endl;
		cout << "2——女" << endl;
		int sex = 0;
		while (true) {
			cin >> sex;
			if (sex == 1 || sex == 2) {
				abs->personArray[abs->m_size].m_Sex = sex;
				break;
			}
			cout << "输入有误,请重新输入!" << endl;
		}
		cout << "请输入年龄:" << endl;
		cin >> abs->personArray[abs->m_size].m_Age;
		cout << "请输入联系电话:" << endl;
		cin >> abs->personArray[abs->m_size].m_phone;
		cout << "请输入家庭住址:" << endl;
		cin >> abs->personArray[abs->m_size].m_adder;
		abs->m_size++;
		cout << "添加成功!" << endl;
		system("pause");//请按任意键继续
		system("cls");//清屏操作
	}
	return;
}

6.显示联系人

功能描述:显示通讯录中已有的联系人信息

显示联系人实现步骤:

封装显示联系人函数

测试显示联系人功能

void showperson(Addressbooks *abs) {
	if (abs->m_size == 0) {
		cout << "当前记录为空" << endl;
	}
	else {
		for (int i = 0; i < abs->m_size; i++) {
			cout << "姓名:" << abs->personArray[i].name << "\t";
			cout << "性别:" << (abs->personArray[i].m_Sex == 1 ? "男" : "女") << "\t";
			cout << "年龄:" << abs->personArray[i].m_Age<< "\t";
			cout << "电话:" << abs->personArray[i].m_phone<< "\t";
			cout << "住址:" << abs->personArray[i].m_adder << endl;
		}
		cout << "信息显示完成!" << endl;
	}
	system("pause");//请按任意键继续
	system("cls");//清屏操作
	return;
}

7.删除联系人

功能描述:按照姓名进行删除指定联系人

删除联系人实现步骤:

1.封装检测联系人是否存在

2.封装删除联系人函数

3.测试删除联系人功能

int isExist(Addressbooks *abs, string name) {
	for (int i = 0; i < abs->m_size; i++) {
		if (abs->personArray[i].name == name) {
			return i;
		}
	}
	return -1;
}

void selectperson(Addressbooks *abs) {
	string name;
	cout << "请输入删除的联系人姓名:" << endl;
	cin >> name;
	int ret = isExist(abs, name);
	if (ret == -1){
		cout << "查无此人!" << endl;
	}
	else {
		for (int i = ret; i < abs->m_size; i++) {
			abs->personArray[i] = abs->personArray[i + 1];
		}
		abs->m_size--;
		cout << "删除成功!" << endl;
	}
	system("pause");//请按任意键继续
	system("cls");//清屏操作
	return;
}

8.查找联系人

功能描述:按照姓名查看指定联系人信息

查找联系人实现步骤:

1.封装查找联系人函数

2.测试查找指定联系人

void findperson(Addressbooks *abs) {
	string name;
	cout << "请输入要查找的联系人:" << endl;
	cin >> name;
	int ret = isExist(abs, name);
	if (ret != -1) {
		cout << "姓名:" << abs->personArray[ret].name << "\t" << "性别:" << (abs->personArray[ret].m_Sex == 1 ? "男" : "女") << "\t"
			<< "年龄:" << abs->personArray[ret].m_Age << "\t" << "电话:" << abs->personArray[ret].m_phone << "\t"
			<< "地址:" << abs->personArray[ret].m_adder << endl;
	}
	else {
		cout << "未找到此人!" << endl;
	}
	system("pause");//请按任意键继续
	system("cls");//清屏操作
	return;
}

9.修改联系人

功能描述:按照姓名重新修改指定联系人

修改联系人实现步骤:

1.封装修改联系人功能

2.测试修改联系人功能

void modifyperson(Addressbooks *abs) {
	cout << "请输入要修改的联系人姓名:" << endl;
	string name;
	cin >> name;
	int ret = isExist(abs, name);
	if (ret != -1) {
		string name1;
		cout << "请输入姓名:" << endl;
		cin >> name1;
		abs->personArray[ret].name = name1;
		cout << "请输入性别:" << endl;
		cout << "1——男" << endl;
		cout << "2——女" << endl;
		int sex = 0;
		while (true) {
			cin >> sex;
			if (sex == 1 || sex == 2) {
				abs->personArray[ret].m_Sex = sex;
				break;
			}
			cout << "输入有误,请重新输入!" << endl;
		}
		cout << "请输入年龄:" << endl;
		cin >> abs->personArray[ret].m_Age;
		cout << "请输入联系电话:" << endl;
		cin >> abs->personArray[ret].m_phone;
		cout << "请输入家庭住址:" << endl;
		cin >> abs->personArray[ret].m_adder;
		cout << "修改成功!" << endl;
	}
	else {
		cout << "查无此人!" << endl;
	}
	system("pause");//请按任意键继续
	system("cls");//清屏操作
	return;
}

10.清空联系人

功能实现:清空通讯录中所有信息

清空联系人实现步骤:

1.封装清空联系人函数

2.测试清空联系人

#include <iostream>
#include <string>
using namespace std;
#define MAX_N 1000

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;
	return;
}
//联系人结构体
struct person
{
	string name;
	int m_Sex; //性别:1男 2女
	int m_Age;
	string m_phone;
	string m_adder;
};
//通讯录结构体
struct Addressbooks
{
	struct person personArray[MAX_N];
	int m_size;
};

void addPerson(Addressbooks *abs) {
	if (abs->m_size == MAX_N) {
		cout << "通讯录已满,无法添加!";
		return;
	}
	else {
		string name;
		cout << "请输入姓名:" << endl;
		cin >> name;
		abs->personArray[abs->m_size].name = name;
		cout << "请输入性别:" << endl;
		cout << "1——男" << endl;
		cout << "2——女" << endl;
		int sex = 0;
		while (true) {
			cin >> sex;
			if (sex == 1 || sex == 2) {
				abs->personArray[abs->m_size].m_Sex = sex;
				break;
			}
			cout << "输入有误,请重新输入!" << endl;
		}
		cout << "请输入年龄:" << endl;
		cin >> abs->personArray[abs->m_size].m_Age;
		cout << "请输入联系电话:" << endl;
		cin >> abs->personArray[abs->m_size].m_phone;
		cout << "请输入家庭住址:" << endl;
		cin >> abs->personArray[abs->m_size].m_adder;
		abs->m_size++;
		cout << "添加成功!" << endl;
		system("pause");//请按任意键继续
		system("cls");//清屏操作
	}
	return;
}

void showperson(Addressbooks *abs) {
	if (abs->m_size == 0) {
		cout << "当前记录为空" << endl;
	}
	else {
		for (int i = 0; i < abs->m_size; i++) {
			cout << "姓名:" << abs->personArray[i].name << "\t";
			cout << "性别:" << (abs->personArray[i].m_Sex == 1 ? "男" : "女") << "\t";
			cout << "年龄:" << abs->personArray[i].m_Age<< "\t";
			cout << "电话:" << abs->personArray[i].m_phone<< "\t";
			cout << "住址:" << abs->personArray[i].m_adder << endl;
		}
		cout << "信息显示完成!" << endl;
	}
	system("pause");//请按任意键继续
	system("cls");//清屏操作
	return;
}

int isExist(Addressbooks *abs, string name) {
	for (int i = 0; i < abs->m_size; i++) {
		if (abs->personArray[i].name == name) {
			return i;
		}
	}
	return -1;
}

void selectperson(Addressbooks *abs) {
	string name;
	cout << "请输入删除的联系人姓名:" << endl;
	cin >> name;
	int ret = isExist(abs, name);
	if (ret == -1){
		cout << "查无此人!" << endl;
	}
	else {
		for (int i = ret; i < abs->m_size; i++) {
			abs->personArray[i] = abs->personArray[i + 1];
		}
		abs->m_size--;
		cout << "删除成功!" << endl;
	}
	system("pause");//请按任意键继续
	system("cls");//清屏操作
	return;
}

void findperson(Addressbooks *abs) {
	string name;
	cout << "请输入要查找的联系人:" << endl;
	cin >> name;
	int ret = isExist(abs, name);
	if (ret != -1) {
		cout << "姓名:" << abs->personArray[ret].name << "\t" << "性别:" << (abs->personArray[ret].m_Sex == 1 ? "男" : "女") << "\t"
			<< "年龄:" << abs->personArray[ret].m_Age << "\t" << "电话:" << abs->personArray[ret].m_phone << "\t"
			<< "地址:" << abs->personArray[ret].m_adder << endl;
	}
	else {
		cout << "未找到此人!" << endl;
	}
	system("pause");//请按任意键继续
	system("cls");//清屏操作
	return;
}

void modifyperson(Addressbooks *abs) {
	cout << "请输入要修改的联系人姓名:" << endl;
	string name;
	cin >> name;
	int ret = isExist(abs, name);
	if (ret != -1) {
		string name1;
		cout << "请输入姓名:" << endl;
		cin >> name1;
		abs->personArray[ret].name = name1;
		cout << "请输入性别:" << endl;
		cout << "1——男" << endl;
		cout << "2——女" << endl;
		int sex = 0;
		while (true) {
			cin >> sex;
			if (sex == 1 || sex == 2) {
				abs->personArray[ret].m_Sex = sex;
				break;
			}
			cout << "输入有误,请重新输入!" << endl;
		}
		cout << "请输入年龄:" << endl;
		cin >> abs->personArray[ret].m_Age;
		cout << "请输入联系电话:" << endl;
		cin >> abs->personArray[ret].m_phone;
		cout << "请输入家庭住址:" << endl;
		cin >> abs->personArray[ret].m_adder;
		cout << "修改成功!" << endl;
	}
	else {
		cout << "查无此人!" << endl;
	}
	system("pause");//请按任意键继续
	system("cls");//清屏操作
	return;
}

void clearperson(Addressbooks *abs) {
	abs->m_size = 0;
	cout << "通讯录已经清空" << endl;
	system("pause");//请按任意键继续
	system("cls");//清屏操作
	return;
}

int main() {
	int select = 0;
	Addressbooks abs;
	abs.m_size = 0;
	while (true) {
		showMenu();
		cin >> select;
		switch (select) {
		case 1: {
			addPerson(&abs);
		} break;
		case 2: {
			showperson(&abs);
		} break;
		case 3: {
			selectperson(&abs);
		} break;
		case 4: {
			findperson(&abs);
		} break;
		case 5: {
			modifyperson(&abs);
		} break;
		case 6: {
			clearperson(&abs);
		} break;
		case 0: {
			cout << "欢迎下次使用!" << endl;
			system("pause");
			return 0;
		} break;
		default: {
			cout << "输入有误请重新输入:" << endl;
		} break;
		}
	}
	system("pause");
	return 0;
}
相关标签: B站课程学习笔记

上一篇: html样式

下一篇: POJ-3176 Cow Bowling