C++基础实现通讯录管理系统

本文详细介绍了使用C++编写的通讯录管理系统,包括添加、显示、删除、查找和修改联系人功能,以及清空联系人操作的代码实现。
摘要由CSDN通过智能技术生成

目录

1.系统描述

2.功能

3.编程技术

4.实现代码


1.系统描述

        通讯录管理系统是一个高效的信息管理工具,旨在记录和管理用户的亲友信息。该系统能够保存各种联系信息,包括但不限于姓名、性别、年龄、联系电话和家庭住址等。
2.系统主要功能

       添加联系人,显示联系人,删除联系人,修改联系人,清空联系人, 退出通讯录。
3.使用技术

      C++基础知识进行编程。

4.代码实现

#define  _CRT_SECURE_NO_WARNINGS 1
#include <iostream> //标准的输入输出流
#include <string>
//通讯录的最大容量 
#define MAX 1000
using namespace std;//标准的命名空间
//联系人的结构体
struct Person
{
	//姓名
	string m_Name;
	//性别:设置为int型 防止输入其他不合格的数据
	// 1 男 2 女
	int m_Sex; 
	//年龄
	int m_Age;
	//电话
	string m_Phone;
	//地址
	string m_Addr;
};
//通讯录的结构体
struct Addressbooks
{
	//通讯录中保存的联系人数组
	struct Person personArray[MAX];
	//通讯录中人员的个数
	int m_size;
};
//1.添加联系人
void addPerson(Addressbooks * abs)
{
	
	if (abs->m_size == MAX)
	{
		cout << "通讯录已满,无法添加!" << endl;
		return;
	}
	else
	{
		//添加具体联系人
		//姓名
		string name;
		cout << "请输入姓名:" << endl;
		cin >> name;
		abs->personArray[abs->m_size].m_Name = name;
		//性别
		int sex=0;
		
		while (1)
		{
			cout << "请输入性别:" << endl;
			cout << "1----男" << endl;
			cout << "2---女" << endl;
			cin >> sex;
			if (sex == 1 || sex == 2)
			{
				abs->personArray[abs->m_size].m_Sex = sex;
				break;
			}
			else
			{
				cout << "输入错误,请重新输入" << endl;
			}
		}
		//年龄
		int age = 0;
		while (1)
		{
			cout << "请输入年龄:" << endl;
			cin >> age;
			if (age>100||age<0)
			{
				cout << "输入错误,请重新输入"<<endl;
				continue;
			}
			else
			{
				abs->personArray[abs->m_size].m_Age = age;
				break;
			}
		}
		//电话
		string phone;
		cout << "请输入电话号码:" << endl;
		cin >> phone;
		abs->personArray[abs->m_size].m_Phone = phone;
		//住址
		string address;
		cout << "请输入地址:" << endl;
		cin >> address;
		abs->personArray[abs->m_size].m_Addr = address;
		//更新通讯录人数
		abs->m_size++;
		cout << "添加成功" << endl;
		system("pause");//请按任意键继续
		system("cls");//清屏
	}
}
//2.显示联系人
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].m_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_Addr << endl;
		}
	}
	system("pause");
	system("cls");
}
//检查联系人是否存在,如果存在,返回联系人在数组中的位置,不存在返回-1
int isExit(Addressbooks* abs, string name)
{
	if (abs->m_size==0)
	{
		return -1;
	}
	for (size_t i = 0; i < abs->m_size; i++)
	{
		if (abs->personArray[i].m_Name == name)
			return i;
	}
	return -1;
}

//3.删除联系人
void deletePerson(Addressbooks * abs)
{
	string delete_person;
	cout << "请输入要删除的联系人" << endl;
	cin >> delete_person;
	if (isExit(abs,delete_person)==-1)
	{
		cout << "没有你要删除的联系人" << endl;
	}
	if (isExit(abs, delete_person)!=-1)
	{
		for (int i = isExit(abs, delete_person); i < abs->m_size; i++)
		{
			abs->personArray[i] = abs->personArray[i + 1];
		}
		abs->m_size--;
		cout << "删除成功" << endl;
	}
	system("pause");
	system("cls");
}
//4.查找联系人
void searchPerson(Addressbooks * abs)
{
	string search_person;
	cout << "请输入你要查找的联系人姓名:" << endl;
	cin >> search_person;
	if (isExit(abs,search_person) == -1)
	{
		cout << "查无此人" << endl;
	}
	else
	{
		for (int i = 0; i < abs->m_size; i++)
		{
			cout << "姓名:" << abs->personArray[i].m_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_Addr << endl;
		}
	}
	system("pause");
	system("cls");
}
void AlterPerson(Addressbooks* abs)
{
	//姓名
	string altername;
	cout << "请输入你要修改的联系人姓名:" << endl;
	cin >> altername;
	int ret = isExit(abs, altername);
	if (ret== -1)
	{
		cout << "查无此人" << endl;
	}
	else
	{
		//修改联系人
		string name;
		cout << "请输入要修改的姓名:" << endl;
		cin >> name;
		abs->personArray[ret].m_Name = name;
		//性别
		int sex;
		cout << "请输入要修改的性别:" << endl;
		cout << "男---1" << endl;
		cout << "女---2" << endl;
		cin >> sex;
		abs->personArray[ret].m_Sex = sex;
		//年龄
		int age;
		cout << "请输入要修改的年龄:" << endl;
		cin >> age;
		abs->personArray[ret].m_Age = age;
		//电话
		string phone;
		cout << "请输入要修改的电话:" << endl;
		cin >> phone;
		abs->personArray[ret].m_Phone = phone;
		//住址
		string address;
		cout << "请输入要修改的地址:" << endl;
		cin >> address;
		abs->personArray[ret].m_Addr = address;
		cout << "修改完成!"<<endl;
		system("pause");
		system("cls");
	}
}
void clearPerson(Addressbooks* abs)
{
	string clear;
	cout << "你确定要情况通讯录吗?(yes/no)" << endl;
	cin >> clear;
	if (clear=="yes")
	{
		abs->m_size = 0;
		cout << "清空成功!" << endl;
	}
	else
	{
		cout << "清空失败" << endl;
	}
	system("pause");
	system("cls");
}
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;
}

int main()
{
	int select = 0;
	//创建通讯录结构体变量
	Addressbooks abs;
	//初始化通讯录中当前人员的个数
	abs.m_size = 0;
	while (true)
	{
		showMenu();
		cout << "请选择:>" << endl;
		cin >> select;
		switch (select)
		{
		case 1:
			//添加联系人
			addPerson(&abs);//利用地址传递,可以修饰修改实参
			break;
		case 2:
			//显示联系人
			showPerson(&abs);
			break;
		case 3:
			//删除联系人
			deletePerson(&abs);
			break;
		case 4:
			//查找联系人
			searchPerson(&abs);
			break;
		case 5:
			AlterPerson(&abs);
			//修改联系人
			break;
		case 6:
			clearPerson(&abs);
			//清空联系人
			break;
		case 0:
			//退出通讯录
			cout << "欢迎下次使用" << endl;
			system("pause");
			return 0;
			break;
		default:
			cout << "输入错误,请重新输入" << endl;
			break;
		}

	}
			
	system("pause");
	return 0;
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
通讯录管理系统可以使用 C++ 的面向对象编程思想进行开发,以下是一个简单的示例: ```c++ #include <iostream> #include <string> #include <vector> using namespace std; class Contact { public: string name; string phone; string email; }; class AddressBook { public: vector<Contact> contacts; void addContact(Contact contact) { contacts.push_back(contact); } void listContacts() { for (int i = 0; i < contacts.size(); i++) { cout << "Name: " << contacts[i].name << endl; cout << "Phone: " << contacts[i].phone << endl; cout << "Email: " << contacts[i].email << endl; } } }; int main() { AddressBook addressBook; Contact contact1 = {"John Doe", "555-1234", "johndoe@example.com"}; addressBook.addContact(contact1); Contact contact2 = {"Jane Smith", "555-5678", "janesmith@example.com"}; addressBook.addContact(contact2); addressBook.listContacts(); return 0; } ``` 在这个示例中,我们定义了两个类,一个是 Contact 类用来存储联系人的信息,另一个是 AddressBook 类用来管理联系人。我们将联系人存储在一个 vector 容器中,并提供了添加联系人和列出联系人的方法。在 main 函数中,我们创建了两个联系人并将它们添加到通讯录中,然后列出了所有联系人的信息。 当然,这只是一个简单的示例,实际的通讯录管理系统可能需要更多的功能,比如搜索联系人、编辑联系人、删除联系人等等。但是这个示例可以作为一个起点,让你了解如何使用 C++ 进行面向对象编程来开发通讯录管理系统

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值