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;
}
相关推荐
混迹网络的权某8 分钟前
蓝桥杯真题——乐乐的序列和(C语言)
c语言·算法·蓝桥杯
wheeldown12 分钟前
【数据结构】快速排序
c语言·数据结构·算法·排序算法
神仙别闹12 分钟前
基于 Java 语言双代号网络图自动绘制系统
java·开发语言
猫爪笔记20 分钟前
JAVA基础:单元测试;注解;枚举;网络编程 (学习笔记)
java·开发语言·单元测试
passer__jw76720 分钟前
【LeetCode】【算法】739. 每日温度
算法·leetcode
螺蛳粉只吃炸蛋的走风21 分钟前
网络编程IO多路复用之poll模式
网络·c++·面试·poll·阻塞与非阻塞
会写代码的饭桶22 分钟前
【C++刷题】力扣-#566-重塑矩阵
c++·leetcode·矩阵
aqua353574235824 分钟前
杨辉三角——c语言
java·c语言·数据结构·算法·蓝桥杯
API快乐传递者24 分钟前
用 Python 爬取淘宝商品价格信息时需要注意什么?
java·开发语言·爬虫·python·json
Aurora_th28 分钟前
蓝桥杯 Python组-神奇闹钟(datetime库)
python·算法·职场和发展·蓝桥杯·datetime