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;
}
相关推荐
网易独家音乐人Mike Zhou23 分钟前
【卡尔曼滤波】数据预测Prediction观测器的理论推导及应用 C语言、Python实现(Kalman Filter)
c语言·python·单片机·物联网·算法·嵌入式·iot
‘’林花谢了春红‘’2 小时前
C++ list (链表)容器
c++·链表·list
----云烟----2 小时前
QT中QString类的各种使用
开发语言·qt
lsx2024062 小时前
SQL SELECT 语句:基础与进阶应用
开发语言
开心工作室_kaic2 小时前
ssm161基于web的资源共享平台的共享与开发+jsp(论文+源码)_kaic
java·开发语言·前端
向宇it2 小时前
【unity小技巧】unity 什么是反射?反射的作用?反射的使用场景?反射的缺点?常用的反射操作?反射常见示例
开发语言·游戏·unity·c#·游戏引擎
武子康3 小时前
Java-06 深入浅出 MyBatis - 一对一模型 SqlMapConfig 与 Mapper 详细讲解测试
java·开发语言·数据仓库·sql·mybatis·springboot·springcloud
转世成为计算机大神3 小时前
易考八股文之Java中的设计模式?
java·开发语言·设计模式
机器视觉知识推荐、就业指导3 小时前
C++设计模式:建造者模式(Builder) 房屋建造案例
c++
宅小海3 小时前
scala String
大数据·开发语言·scala