C++模拟通讯录管理系统-----C++学习笔记

C++学习日常(源码在底部)

C++模拟通讯录管理系统各大功能需求

❓ 通讯录管理系统登录操作需求
1️⃣ 注册用户功能
2️⃣ 登录用户功能
3️⃣ 找回密码功能
4️⃣ 退出系统功能

❓ 通讯录管理系统管理操作需求
1️⃣ 添加联系人功能
2️⃣ 删除联系人功能
3️⃣ 显示联系人功能
4️⃣ 查找联系人功能
5️⃣ 清空联系人功能
6️⃣ 修改联系人功能
7️⃣ 返回登录界面功能

C++模拟通讯录函数封装分解

声明以及头文件

#include 
using namespace std;
#include 
#define LOGIN_NUM 3		//登录失败次数
#define MAX_SIZE 50		//最大账号存储数
#define MAX_PERSON 200	//通讯录最大存储人数

void RegisteUser(DataBase* data);
void LoginUser(DataBase* data);
void FindPasswd(DataBase* data);
void InManage(AddressList* abs);
void AddPerson(AddressList* abs);
void DeletePerson(AddressList* abs);
void PrintPerson(AddressList* abs);
void FindPerson(AddressList* abs);
void ClearPerson(AddressList* abs);
void Modify(AddressList* abs, int i, string name);
void ModifyPerson(AddressList* abs);
void ShowMenu1();
void ShowMenu2();

注册用户

注册用户,首先需要的是用户名、密码、和找回密码的密保,创建一个User结构体存储,再创建一个数据库DataBase结构体存储信息,如下:

struct User {
	string Account;	//账号
	string Passwd;	//密码
	string Encrypted;	//密保
};

struct DataBase {
	struct User dataBases[MAX_SIZE];	//账号数据库数组
	int d_Size;
};

注册用户函数封装如下:
C++模拟通讯录管理系统-----C++学习笔记_第1张图片

//注册用户
void RegisteUser(DataBase* data) {
	string account, passwd, encrypted;
	cout << "请输入创建的账户:";
	cin >> account;
	data->dataBases[data->d_Size].Account = account;
	cout << "请输入创建的密码:";
	cin >> passwd;
	data->dataBases[data->d_Size].Passwd = passwd;
	cout << "请输入密保(找回密码):";
	cin >> encrypted;
	data->dataBases[data->d_Size].Encrypted = encrypted;
	data->d_Size++;

	cout << "创建账户成功" << endl;
	system("pause");
	system("cls");
}

登录用户

登录用户,根据输入的用户和密码与数据库匹配,匹配成功则登陆成功,否则登录不成功,显示重新登录,并有3次登录机会
C++模拟通讯录管理系统-----C++学习笔记_第2张图片
登录用户函数封装如下:

//登录用户
void LoginUser(DataBase* data,AddressList* abs) {
	string account, passwd;
	int flag = 0,j = 1;
	while(true){
		if (j > LOGIN_NUM) {
			cout << "登录失败次数过3,请重新尝试" << endl;
			break;
		}
		else if (flag == 1) {
			break;
		}
		cout << "请输入登录账户:";
		cin >> account;
		cout << "请输入登录密码:";
		cin >> passwd;
		for (int i = 0; i < data->d_Size; i++) {
			if (data->dataBases[i].Account == account && data->dataBases[i].Passwd == passwd) {
				flag = 1;
				cout << "登陆成功" << endl;
				system("pause");
				system("cls");
				InManage(abs);
			}
		}
		if (flag == 0 && j <= LOGIN_NUM) {
			cout << "登录失败,请重新输入账户以及密码" << endl;
			system("pause");
			system("cls");
			j++;
		}
	}
}

找回密码

如需找回密码,需要根据输入的用户以及密保才能找回,若匹配则返回密码,否则显示提示信息。
C++模拟通讯录管理系统-----C++学习笔记_第3张图片
找回密码函数封装如下:

//找回密码
void FindPasswd(DataBase* data){
	int flag = 0;
	if (data->d_Size == 0){
		cout << "没有任何账户,找回密码无效" << endl;
	}
	else {
		string account, encrypted;
		cout << "请输入该账户:";
		cin >> account;
		cout << "请输入该账户的密保:";
		cin >> encrypted;
		for (int i = 0; i < data->d_Size; i++) {
			if (data->dataBases[i].Account == account && data->dataBases[i].Encrypted == encrypted){
				cout << "该账户的密码 = " << data->dataBases[i].Passwd << endl;
				flag = 1;
				break;
			}
		}
		if (flag == 1){
			cout << "成功找回密码,请记住" << endl;
			system("pause");
			system("cls");
		}
		else {
			cout << "找回密码失败,账户错误或者密保错误,请重试" << endl;
			system("pause");
			system("cls");
		}
	}
}

退出系统

只需在main函数中定义即可
C++模拟通讯录管理系统-----C++学习笔记_第4张图片

1️⃣ 添加联系人功能

构建联系人列表结构体,定义相关的结构体变量如下:

struct Person {
	string p_Name;	//姓名
	int p_Age;		//年龄
	string p_Phone;	//电话
	string p_Sex;	//性别
	string p_Addr;	//地址
};

struct AddressList {
	struct Person personArray[MAX_PERSON];	//通讯录保存信息的数组
	int p_Size;		//记录通讯录中的人数
};

C++模拟通讯录管理系统-----C++学习笔记_第5张图片
添加联系人函数封装如下:

//添加联系人函数
void AddPerson(AddressList* abs) {
	if (abs->p_Size == MAX_PERSON) {
		cout << "抱歉,空间已满,无法添加,请选择其他操作" << endl;
	}
	string name, sex, phone, addr;
	int age;
	cout << "请输入添加联系人的姓名:";
	cin >> name;
	abs->personArray[abs->p_Size].p_Name = name;
	cout << "请输入添加联系人的年龄:";
	cin >> age;
	abs->personArray[abs->p_Size].p_Age = age;
	cout << "请输入添加联系人的电话:";
	cin >> phone;
	abs->personArray[abs->p_Size].p_Phone = phone;
	cout << "请输入添加联系人的性别:";
	cin >> sex;
	abs->personArray[abs->p_Size].p_Sex = sex;
	cout << "请输入添加联系人的住址:";
	cin >> addr;
	abs->personArray[abs->p_Size].p_Addr = addr;

	abs->p_Size++;	//人数++

	cout << "添加成功" << endl;
	system("pause");
	system("cls");
}

2️⃣ 删除联系人功能

通过匹配联系人的姓名进行删除,(可完善为通过姓名和其他信息验证删除C++模拟通讯录管理系统-----C++学习笔记_第6张图片
删除联系人函数封装如下:

//删除联系人函数
void DeletePerson(AddressList* abs) {
	if (abs->p_Size == 0) {
		cout << "通讯录为空" << endl;
		system("pause");
		system("cls");
	}else {
		int flag = 0;
		string name;
		cout << "请输入删除的联系人姓名:";
		cin >> name;
		for (int i = 0; i < abs->p_Size; i++) {
			if (abs->personArray[i].p_Name == name) {
				for (int j = i; j <=abs->p_Size-1; j++) {	//删除联系人(实质信息前移覆盖)
					abs->personArray[j] = abs->personArray[j+1];
				}
				abs->p_Size--;
				flag = 1;
				cout << "删除完成" << endl;
				system("pause");
				system("cls");
			}
		}
		if (flag == 0){
			cout << "没有此联系人" << endl;
			system("pause");
			system("cls");
		}
	}
}

3️⃣ 显示联系人功能

循环输出即可C++模拟通讯录管理系统-----C++学习笔记_第7张图片
显示联系人函数封装如下:

//显示联系人函数
void PrintPerson(AddressList* abs) {
	if (abs->p_Size == 0) {
		cout << "当前通讯录为空" << endl;
		system("pause");
		system("cls");
	}
	else {
		cout << "=============================================== 通讯录 ================================================" << endl;
		for (int i = 0; i < abs->p_Size; i++) {
			cout << "姓名:" << abs->personArray[i].p_Name << "\t";
			cout << "年龄:" << abs->personArray[i].p_Age << "\t";
			cout << "电话:" << abs->personArray[i].p_Phone << "\t";
			cout << "性别:" << abs->personArray[i].p_Sex << "\t";
			cout << "住址:" << abs->personArray[i].p_Addr << endl;
		}
		cout << "=======================================================================================================" << endl;
		cout << "显示成功" << endl;
		system("pause");
		system("cls");
	}
}

4️⃣ 查找联系人功能

通过姓名查找,可查找到同名联系人C++模拟通讯录管理系统-----C++学习笔记_第8张图片
查找联系人函数封装如下:

//查找联系人函数
void FindPerson(AddressList* abs) {
	int flag = 0;
	string name;
	cout << "请输入查找的联系人姓名:";
	cin >> name;
	for (int i = 0; i < abs->p_Size; i++) {
		if (abs->personArray[i].p_Name == name) {
			cout << "===================================== 查找的联系人如下 ===========================================" << endl;
			cout << "姓名:" << abs->personArray[i].p_Name << "\t";
			cout << "年龄:" << abs->personArray[i].p_Age << "\t";
			cout << "电话:" << abs->personArray[i].p_Phone << "\t";
			cout << "性别:" << abs->personArray[i].p_Sex << "\t";
			cout << "住址:" << abs->personArray[i].p_Addr << endl;
			cout << "========================================== 查找完成 ==============================================" << endl;
			system("pause");
			system("cls");
			flag = 1;
		}
	}
	if (flag == 0) {
		cout << "查无此人" << endl;
		system("pause");
		system("cls");
	}
}

5️⃣ 清空联系人功能

清空联系人需要提示是否执行此操作,避免误删
C++模拟通讯录管理系统-----C++学习笔记_第9张图片
清空联系人函数封装如下:

//清空联系人函数
void ClearPerson(AddressList* abs) {
	string select;
	while (true) {
		cout << "请问是否执行清空?(y || Y || n || N):";
		cin >> select;
		if (select == "Y" || select == "y") {
			if (abs->p_Size == 0) {
				cout << "当前通讯录为空,清空无效" << endl;
				system("pause");
				system("cls");
			}
			else {
				abs->p_Size = 0;
				cout << "清空完成" << endl;
				system("pause");
				system("cls");
			}
			break;
		}
		else if (select == "N" || select == "n") {
			cout << "清空操作无效,退出操作" << endl;
			system("pause");
			system("cls");
			break;
		}
		else {
			cout << "输入有误,请重新输入" << endl;
		}
	}
}

6️⃣ 修改联系人功能

通过匹配姓名修改,(可完善为修改指定的其中的信息C++模拟通讯录管理系统-----C++学习笔记_第10张图片
修改联系人函数封装如下:

//修改信息函数
void Modify(AddressList* abs,int i,string name) {
	string M_name,M_sex,M_phone,M_addr;
	int M_age;
	cout << "请输入" << name << "修改后的姓名:";
	cin >> M_name;
	abs->personArray[i].p_Name = M_name;
	cout << "请输入" << name << "修改后的年龄:";
	cin >> M_age;
	abs->personArray[i].p_Age = M_age;
	cout << "请输入" << name << "修改后的电话:";
	cin >> M_phone;
	abs->personArray[i].p_Phone = M_phone;
	cout << "请输入" << name << "修改后的性别:";
	cin >> M_sex;
	abs->personArray[i].p_Sex = M_sex;
	cout << "请输入" << name << "修改后的住址:";
	cin >> M_addr;
	abs->personArray[i].p_Addr = M_addr;
	cout << "修改成功" << endl;
}

源码

#include 
using namespace std;
#include 
#define LOGIN_NUM 3		//登录失败次数
#define MAX_SIZE 50		//最大账号存储数
#define MAX_PERSON 200	//通讯录最大存储人数

struct User {
	string Account;	//账号
	string Passwd;	//密码
	string Encrypted;	//密保
};

struct Person {
	string p_Name;	//姓名
	int p_Age;		//年龄
	string p_Phone;	//电话
	string p_Sex;	//性别
	string p_Addr;	//地址
};

struct DataBase {
	struct User dataBases[MAX_SIZE];	//账号数据库数组
	int d_Size;
};

struct AddressList {
	struct Person personArray[MAX_PERSON];	//通讯录保存信息的数组
	int p_Size;		//记录通讯录中的人数
};

void RegisteUser(DataBase* data);
void LoginUser(DataBase* data);
void FindPasswd(DataBase* data);
void InManage(AddressList* abs);
void AddPerson(AddressList* abs);
void DeletePerson(AddressList* abs);
void PrintPerson(AddressList* abs);
void FindPerson(AddressList* abs);
void ClearPerson(AddressList* abs);
void Modify(AddressList* abs, int i, string name);
void ModifyPerson(AddressList* abs);
void ShowMenu1();
void ShowMenu2();

//注册用户
void RegisteUser(DataBase* data) {
	string account, passwd, encrypted;
	cout << "请输入创建的账户:";
	cin >> account;
	data->dataBases[data->d_Size].Account = account;
	cout << "请输入创建的密码:";
	cin >> passwd;
	data->dataBases[data->d_Size].Passwd = passwd;
	cout << "那请输入密保(找回密码):";
	cin >> encrypted;
	data->dataBases[data->d_Size].Encrypted = encrypted;
	data->d_Size++;

	cout << "创建账户成功" << endl;
	system("pause");
	system("cls");
}

//登录用户
void LoginUser(DataBase* data,AddressList* abs) {
	string account, passwd;
	int flag = 0,j = 1;
	while(true){
		if (j > LOGIN_NUM) {
			cout << "登录失败次数过3,请重新尝试" << endl;
			break;
		}
		else if (flag == 1) {
			break;
		}
		cout << "请输入登录账户:";
		cin >> account;
		cout << "请输入登录密码:";
		cin >> passwd;
		for (int i = 0; i < data->d_Size; i++) {
			if (data->dataBases[i].Account == account && data->dataBases[i].Passwd == passwd) {
				flag = 1;
				cout << "登陆成功" << endl;
				system("pause");
				system("cls");
				InManage(abs);
			}
		}
		if (flag == 0 && j <= LOGIN_NUM) {
			cout << "登录失败,请重新输入账户以及密码" << endl;
			system("pause");
			system("cls");
			j++;
		}
	}
}

//找回密码
void FindPasswd(DataBase* data){
	int flag = 0;
	if (data->d_Size == 0){
		cout << "没有任何账户,找回密码无效" << endl;
	}
	else {
		string account, encrypted;
		cout << "请输入该账户:";
		cin >> account;
		cout << "请输入该账户的密保:";
		cin >> encrypted;
		for (int i = 0; i < data->d_Size; i++) {
			if (data->dataBases[i].Account == account && data->dataBases[i].Encrypted == encrypted){
				cout << "该账户的密码 = " << data->dataBases[i].Passwd << endl;
				flag = 1;
				break;
			}
		}
		if (flag == 1){
			cout << "成功找回密码,请记住" << endl;
			system("pause");
			system("cls");
		}
		else {
			cout << "找回密码失败,账户错误或者密保错误,请重试" << endl;
			system("pause");
			system("cls");
		}

	}
}

//登录成功后进入管理系统函数
void InManage(AddressList* abs) {
	int select;
	while (true) {
		ShowMenu2();
		cout << "请输入管理操作序号:";
		cin >> select;
		if (select == 0) {
			cout << endl;
			cout << "======== 欢迎使用 ========" << endl;
			break;
		}
		switch (select) {
		case 1: AddPerson(abs);
			break;
		case 2: DeletePerson(abs);
			break;
		case 3: PrintPerson(abs);
			break;
		case 4: FindPerson(abs);
			break;
		case 5: ClearPerson(abs);
			break;
		case 6: ModifyPerson(abs);
			break;
		default:cout << "输入错误,请重新输入" << endl;
			break;
		}
	}
}

//添加联系人函数
void AddPerson(AddressList* abs) {
	if (abs->p_Size == MAX_PERSON) {
		cout << "抱歉,空间已满,无法添加,请选择其他操作" << endl;
	}
	string name, sex, phone, addr;
	int age;
	cout << "请输入添加联系人的姓名:";
	cin >> name;
	abs->personArray[abs->p_Size].p_Name = name;
	cout << "请输入添加联系人的年龄:";
	cin >> age;
	abs->personArray[abs->p_Size].p_Age = age;
	cout << "请输入添加联系人的电话:";
	cin >> phone;
	abs->personArray[abs->p_Size].p_Phone = phone;
	cout << "请输入添加联系人的性别:";
	cin >> sex;
	abs->personArray[abs->p_Size].p_Sex = sex;
	cout << "请输入添加联系人的住址:";
	cin >> addr;
	abs->personArray[abs->p_Size].p_Addr = addr;

	abs->p_Size++;	//人数++

	cout << "添加成功" << endl;
	system("pause");
	system("cls");
}

//删除联系人函数
void DeletePerson(AddressList* abs) {
	if (abs->p_Size == 0) {
		cout << "通讯录为空" << endl;
		system("pause");
		system("cls");
	}else {
		int flag = 0;
		string name;
		cout << "请输入删除的联系人姓名:";
		cin >> name;
		for (int i = 0; i < abs->p_Size; i++) {
			if (abs->personArray[i].p_Name == name) {
				for (int j = i; j <=abs->p_Size-1; j++) {	//删除联系人(实质信息前移覆盖)
					abs->personArray[j] = abs->personArray[j+1];
				}
				abs->p_Size--;
				flag = 1;
				cout << "删除完成" << endl;
				system("pause");
				system("cls");
			}
		}
		if (flag == 0){
			cout << "没有此联系人" << endl;
			system("pause");
			system("cls");
		}
	}
}

//显示联系人函数
void PrintPerson(AddressList* abs) {
	if (abs->p_Size == 0) {
		cout << "当前通讯录为空" << endl;
		system("pause");
		system("cls");
	}
	else {
		cout << "=============================================== 通讯录 ================================================" << endl;
		for (int i = 0; i < abs->p_Size; i++) {
			cout << "姓名:" << abs->personArray[i].p_Name << "\t";
			cout << "年龄:" << abs->personArray[i].p_Age << "\t";
			cout << "电话:" << abs->personArray[i].p_Phone << "\t";
			cout << "性别:" << abs->personArray[i].p_Sex << "\t";
			cout << "住址:" << abs->personArray[i].p_Addr << endl;
		}
		cout << "=======================================================================================================" << endl;
		cout << "显示成功" << endl;
		system("pause");
		system("cls");
	}
}

//查找联系人函数
void FindPerson(AddressList* abs) {
	int flag = 0;
	string name;
	cout << "请输入查找的联系人姓名:";
	cin >> name;
	for (int i = 0; i < abs->p_Size; i++) {
		if (abs->personArray[i].p_Name == name) {
			cout << "===================================== 查找的联系人如下 ===========================================" << endl;
			cout << "姓名:" << abs->personArray[i].p_Name << "\t";
			cout << "年龄:" << abs->personArray[i].p_Age << "\t";
			cout << "电话:" << abs->personArray[i].p_Phone << "\t";
			cout << "性别:" << abs->personArray[i].p_Sex << "\t";
			cout << "住址:" << abs->personArray[i].p_Addr << endl;
			cout << "========================================== 查找完成 ==============================================" << endl;
			system("pause");
			system("cls");
			flag = 1;
		}
	}
	if (flag == 0) {
		cout << "查无此人" << endl;
		system("pause");
		system("cls");
	}
}

//清空联系人函数
void ClearPerson(AddressList* abs) {
	string select;
	while (true) {
		cout << "请问是否执行清空?(y || Y || n || N):";
		cin >> select;
		if (select == "Y" || select == "y") {
			if (abs->p_Size == 0) {
				cout << "当前通讯录为空,清空无效" << endl;
				system("pause");
				system("cls");
			}
			else {
				abs->p_Size = 0;
				cout << "清空完成" << endl;
				system("pause");
				system("cls");
			}
			break;
		}
		else if (select == "N" || select == "n") {
			cout << "清空操作无效,退出操作" << endl;
			system("pause");
			system("cls");
			break;
		}
		else {
			cout << "输入有误,请重新输入" << endl;
		}
	}
}

//修改信息函数
void Modify(AddressList* abs,int i,string name) {
	string M_name,M_sex,M_phone,M_addr;
	int M_age;
	cout << "请输入" << name << "修改后的姓名:";
	cin >> M_name;
	abs->personArray[i].p_Name = M_name;
	cout << "请输入" << name << "修改后的年龄:";
	cin >> M_age;
	abs->personArray[i].p_Age = M_age;
	cout << "请输入" << name << "修改后的电话:";
	cin >> M_phone;
	abs->personArray[i].p_Phone = M_phone;
	cout << "请输入" << name << "修改后的性别:";
	cin >> M_sex;
	abs->personArray[i].p_Sex = M_sex;
	cout << "请输入" << name << "修改后的住址:";
	cin >> M_addr;
	abs->personArray[i].p_Addr = M_addr;
	cout << "修改成功" << endl;
}

//修改联系人函数
void ModifyPerson(AddressList* abs) {
	if (abs->p_Size == 0){
		cout << "当前通讯录为空" << endl;
		system("pause");
		system("cls");
	}
	else {
		string name;
		cout << "请输入修改的联系人姓名:";
		cin >> name;
		for (int i = 0; i < abs->p_Size; i++){
			if (abs->personArray[i].p_Name == name){
				Modify(abs,i,name);	//调用修改信息函数
				system("pause");
				system("cls");
			}
		}
	}
}

//登录界面1函数
void ShowMenu1() {
	cout << "==========================" << endl;
	cout << "======= 1.注册用户 =======" << endl;
	cout << "======= 2.登录用户 =======" << endl;
	cout << "======= 3.找回密码 =======" << endl;
	cout << "======= 0.退出系统 =======" << endl;
	cout << "==========================" << endl;
}


//菜单界面2函数
void ShowMenu2() {
	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() {
	AddressList abs;
	DataBase data;
	data.d_Size = 0;	//初始数据库账号数为0
	//用户选择序号变量
	int select = 0;
	abs.p_Size = 0;		//初始化通讯录人数为0

	while (true) {
		ShowMenu1();
		cout << "请输入登录操作序号:";
		cin >> select;
		if (select == 0) {
			cout << endl;
			cout << "========= 欢迎使用通讯录系统 ==========" << endl;
			break;
		}
		switch (select) {
		case 1: RegisteUser(&data);
			break;
		case 2: LoginUser(&data,&abs);
			break;
		case 3: FindPasswd(&data);
			break;
		default:cout << "输入错误,请重新输入" << endl;
			break;
		}
	}
	system("pause");
	return 0;
}

感谢浏览,下篇见

你可能感兴趣的:(C++语言学习笔记,实验与实战笔记,c++,学习,开发语言)