C++ 通讯录实现

头文件:

cpp 复制代码
#include<iostream>
#include<string>
#define MAX 1000
using namespace std;
//联系人结构体
struct Person {
	string name;
	int age;
	string address;
	string gender;
	string phone;
};
//通讯录结构体
struct Addressbooks {
	Person personAddress[MAX];
	int size;
};
int CheckHave(Addressbooks* ps, string name);//检查所给联系人在哪个位置
void SelectPerson(Addressbooks* ps);//显示指定联系人
void print(Addressbooks* ps);//显示目前所有的联系人
void menu();//菜单
void DeletePerson(struct Addressbooks* ps);//删除联系人
void addPerson(struct Addressbooks* ps);//添加联系人
void upDataPerson(struct Addressbooks* ps);//修改联系人
void cleanPerson(struct Addressbooks* ps);//清空通讯录

函数:

cpp 复制代码
#include"TelPhone.h"
int CheckHave(Addressbooks* ps, string name)
{

	for (int i = 0; i < ps->size; i++)
	{

		if (ps->personAddress[i].name == name)
		{
			return i;
		}
	}
	return -1;
}
void SelectPerson(Addressbooks* ps)
{
	cout << "输入需要查找的联系人的姓名" << endl;
	string name;
	cin >> name;
	int flag = CheckHave(ps, name);
	if (flag == -1) {
		cout << "通讯录中没有此联系人" << endl;
		system("pause");
		system("cls");
		return;
	}
	else {
		cout << "姓名:\t" << "年龄:\t" << "性别:\t" << "电话号码:\t" << "地址:\t" << endl;
		cout << ps->personAddress[flag].name << "\t" <<
			ps->personAddress[flag].age << "\t" <<
			ps->personAddress[flag].gender << "\t" <<
			ps->personAddress[flag].phone << "\t" <<
			ps->personAddress[flag].address << "\t" <<
			endl;
		system("pause");
		system("cls");
	}

}
void print(Addressbooks* ps)
{
	if (ps->size == 0)
	{
		cout << "当前通讯录中没有联系人" << endl;
		system("pause");
		system("cls");
		return;
	}
	cout << "姓名:\t" << "年龄:\t" << "性别:\t" << "电话号码:\t" << "地址:\t" << endl;
	for (int i = 0; i < ps->size; i++) {

		cout << ps->personAddress[i].name << "\t" <<
			ps->personAddress[i].age << "\t" <<
			ps->personAddress[i].gender << "\t" <<
			ps->personAddress[i].phone << "\t" <<
			ps->personAddress[i].address << "\t" <<
			endl;
	}
	system("pause");
	system("cls");
}
void menu()
{
	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;
}
void DeletePerson(struct Addressbooks* ps)
{
	//判断通讯录是不是存在数据
	if (ps->size == 0) {
		cout << "没有联系人数据,请添加后重试" << endl;
		system("pause");
		system("cls");
		return;
	}
	string name;
	cout << "输入要删除的联系人" << endl;
	cin >> name;
	int flag = CheckHave(ps, name);
	if (flag == -1)
	{
		cout << "当前通讯录没有此联系人" << endl;
		return;
	}
	else
	{
		int cur = flag;
		while (cur < ps->size)
		{
			ps->personAddress[cur] = ps->personAddress[cur + 1];
			cur++;
		}
		ps->size--;
		cout << "删除成功" << endl;
	}
}
void addPerson(struct Addressbooks* ps)
{
	// 判读通讯录是否已经满了
	if (ps->size == MAX)
	{
		cout << "通讯录已满" << endl;
		system("pause");
		system("cls");
		return;
	}
	cout << "输入一个姓名" << endl;
	string name;
	cin >> name;
	ps->personAddress[ps->size].name = name;
	while (1) {
		cout << "输入性别" << endl;
		string gender;
		cin >> gender;
		if (!(gender == "男" || gender == "女"))
		{		//0                  -1
			cout << "性别错误 输入 0 退出 1 重新输入" << endl;
			int flag;
			cin >> flag;
			if (!flag) {
				return;
			}
			else {
				continue;
			}
		}
		ps->personAddress[ps->size].gender = gender;
		break;
	}
	while (1) {
		cout << "输入年龄" << endl;
		int age;
		cin >> age;
		if (age < 0 || age>150)
		{
			cout << "年龄错误 输入 0 退出 1 重新输入" << endl;
			int flag;
			cin >> flag;
			if (!flag) {
				break;
			}
			else {
				continue;
			}
		}
		ps->personAddress[ps->size].age = age;
		break;
	}
	while (1) {
		cout << "输入电话号码" << endl;
		string phone;
		cin >> phone;
		if (phone.length() != 11)
		{
			cout << "电话号码的长度错误 输入 0 退出 1 重新输入" << endl;
			int flag;
			cin >> flag;
			if (!flag) {
				break;
			}
			else {
				continue;
			}
		}
		ps->personAddress[ps->size].phone = phone;
		break;
	}
	cout << "输入地址" << endl;
	string address;
	cin >> address;
	ps->personAddress[ps->size].address = address;
	ps->size++;
	cout << "添加成功" << endl;
	system("pause");
	system("cls");
}
void upDataPerson(struct Addressbooks* ps) {
	string name;
	cout << "输入要修改的联系人" << endl;
	cin >> name;
	int flag = CheckHave(ps, name);
	if (flag == -1)
	{
		cout << "当前通讯录没有此联系人" << endl;
		system("pause");
		system("cls");
		return;
	}
	else {
		int temp;
		cout << "输入 1 修改姓名" << endl;
		cout << "输入 2 修改地址" << endl;
		cout << "输入 3 修改年龄" << endl;
		cout << "输入 4 修改电话" << endl;
		cout << "输入 5 修改性别" << endl;
		cin >> temp;
		switch (temp) {
		case 1: {
			//修改姓名
			cout << "输入新的姓名" << endl;
			string name;
			cin >> name;
			ps->personAddress[flag].name = name;
			cout << "修改成功" << endl;
			break;
		}
		case 2: {
			//修改地址
			cout << "输入新的地址" << endl;
			string address;
			cin >> address;
			ps->personAddress[flag].address = address;
			cout << "修改成功" << endl;
			break;
		}
		case 3:
		{
			//修改年龄

			while (1) {
				cout << "输入新的年龄" << endl;
				int age;
				cin >> age;
				if (age < 0 || age>150)
				{
					cout << "年龄错误 输入 0 退出 1 重新输入" << endl;
					int t;
					cin >> t;
					if (!t) {
						break;
					}
					else {
						continue;
					}
				}
				ps->personAddress[flag].age = age;
				cout << "修改成功" << endl;
				break;
			}
			break;
		}
		//修改电话

		case 4: {
			while (1) {
				cout << "输入新的电话号码" << endl;
				string phone;
				cin >> phone;
				if (phone.length() != 11)
				{
					cout << "电话号码的长度错误 输入 0 退出 1 重新输入" << endl;
					int t;
					cin >> t;
					if (!t) {
						break;
					}
					else {
						continue;
					}
				}
				ps->personAddress[flag].phone = phone;
				cout << "修改成功" << endl;
				break;
			}
			break;
		}
			  //修改性别
		case 5: {
			while (1) {
				cout << "输入新的性别" << endl;
				string gender;
				cin >> gender;
				if (!(gender == "男" || gender == "女"))
				{		//0                  -1
					cout << "性别错误 输入 0 退出 1 重新输入" << endl;
					int t;
					cin >> t;
					if (!t) {
						return;
					}
					else {
						continue;
					}
				}
				ps->personAddress[flag].gender = gender;
				cout << "修改成功" << endl;
				break;
			}
			break;
		}
		default: {
			cout << "没有此选项" << endl;
			break;
		}
		}
	}
	system("pause");
	system("cls");
}
void cleanPerson(struct Addressbooks* ps)
{
	//删除所有
	ps->size = 0;
	cout << "通讯录已清空" << endl;
	system("pause");
	system("cls");
}

测试:

cpp 复制代码
#include"TelPhone.h"
int main()
{
	Addressbooks abs;
	abs.size = 0;
	int select = 0;
	while (true)
	{
		menu();

		cin >> select;

		switch (select)
		{
		case 1:  //添加联系人
			addPerson(&abs);
			break;
		case 2:  //显示联系人
			print(&abs);
			break;
		case 3:  //删除联系人
			DeletePerson(&abs);
			break;
		case 4:  //查找联系人
			SelectPerson(&abs);
			break;
		case 5:  //修改联系人
			upDataPerson(&abs);
			break;
		case 6:  //清空联系人
			cleanPerson(&abs);
			break;
		case 0:  //退出通讯录
			cout << "欢迎下次使用" << endl;
			return 0;
			break;
		default:
			break;
		}
	}
	return 0;
}
相关推荐
ZJ_.2 分钟前
WPSJS:让 WPS 办公与 JavaScript 完美联动
开发语言·前端·javascript·vscode·ecmascript·wps
Narutolxy7 分钟前
深入探讨 Go 中的高级表单验证与翻译:Gin 与 Validator 的实践之道20241223
开发语言·golang·gin
XiaoLeisj13 分钟前
【递归,搜索与回溯算法 & 综合练习】深入理解暴搜决策树:递归,搜索与回溯算法综合小专题(二)
数据结构·算法·leetcode·决策树·深度优先·剪枝
Hello.Reader14 分钟前
全面解析 Golang Gin 框架
开发语言·golang·gin
禁默25 分钟前
深入浅出:AWT的基本组件及其应用
java·开发语言·界面编程
yuyanjingtao28 分钟前
CCF-GESP 等级考试 2023年9月认证C++四级真题解析
c++·青少年编程·gesp·csp-j/s·编程等级考试
Jasmine_llq32 分钟前
《 火星人 》
算法·青少年编程·c#
Code哈哈笑34 分钟前
【Java 学习】深度剖析Java多态:从向上转型到向下转型,解锁动态绑定的奥秘,让代码更优雅灵活
java·开发语言·学习
程序猿进阶38 分钟前
深入解析 Spring WebFlux:原理与应用
java·开发语言·后端·spring·面试·架构·springboot