怎么用C++实现通讯录管理系统

这篇“怎么用C++实现通讯录管理系统”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“怎么用C++实现通讯录管理系统”文章吧。

系统中需要实现的功能如下:

  • 添加联系人:向通讯录中添加新人,信息包括(姓名、性别、年龄、联系电话、家庭住址)最多记录1000人

  • 显示联系人:显示通讯录中所有联系人信息

  • 删除联系人:按照姓名进行删除指定联系人

  • 查找联系人:按照姓名查看指定联系人信息

  • 修改联系人:按照姓名重新修改指定联系人

  • 清空联系人:清空通讯录中所有信息

  • 退出通讯录:退出当前使用的通讯录

实现代码:

#include <iostream>
#include <string>
using namespace std;

#define MAX 1000

struct person
{
	string name;
	int sex {};
	int age {};
	string phonenumber;
	string address;
};

struct addressbook
{
	struct person personArr[MAX];
	int person_size {};
};

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;
}

void addPerson(addressbook* aaa) //添加联系人
{
	if (aaa->person_size < MAX)
	{
			string name;
			cout << "请输入姓名:" << endl;
			cin >> name;
			aaa->personArr[aaa->person_size].name = name;

			int sex;
			cout << "请输入性别对应序号:(1--男    2--女)" << endl;
			while (true)
			{
				cin >> sex;
				if ((sex == 1) || (sex == 2))
				{
					aaa->personArr[aaa->person_size].sex = sex;
					break;
				}
				else
				{
					cout << "您输入的有误,请检查后重新输入!" << endl;
				}
			}

			int age = 0;
			cout << "请输入年龄:" << endl;
			cin >> age;
			aaa->personArr[aaa->person_size].age = age;

			string phonenumber;
			cout << "请输入电话:" << endl;
			cin >> phonenumber;
			aaa->personArr[aaa->person_size].phonenumber = phonenumber;

			string address;
			cout << "请输入地址:" << endl;
			cin >> address;
			aaa->personArr[aaa->person_size].address = address;

			aaa->person_size++;
			cout << "添加联系人成功!" << endl;
	}
	else
	{
		cout << "联系人已满,请删除部分联系人再添加!" << endl;
	
	}
	system("pause");
	system("cls");
}

void showPerson(addressbook person)
{
	if (person.person_size == 0)
	{
		cout << "联系人列表为空!" << endl;
	}
	for (int i = 0; i < person.person_size; i++)
	{
		cout << i + 1 << ". " << "姓名:" << person.personArr[i].name << " "
			<< "性别:" << (person.personArr[i].sex == 1 ? "男" : "女") << " "
			<< "年龄:" << person.personArr[i].age << " "
			<< "电话:" << person.personArr[i].phonenumber << " "
			<< "住址:" << person.personArr[i].address << " " << endl;
	}
	system("pause");
	system("cls");
}

int isExist(addressbook* person,string name)//根据姓名判断是否存在
{
	for (int i = 0; i < person->person_size; i++)
	{
		if (person->personArr[i].name == name)
		{
			return i;
		}
	}
	return -1;
}

void deletePerson( addressbook* person)//删除联系人
{
	string name;
	cout << "请输入您要删除的联系人姓名!" << endl;
	cin >> name;
	int exist = isExist(person, name);
	if(exist != -1)
	{
		for (int i = exist; i < person->person_size; i++)
		{

			{
				person->personArr[i] = person->personArr[i + 1];
			}
		}
		(person->person_size)--;
		cout << "删除成功!" << endl;
	}
	else
	{
		cout << "没有这个人!" << endl;
	}

	system("pause");
	system("cls");
}

void findPerson(addressbook* person)//查找联系人
{
	string name;
	cout << "请输入您要查找的联系人姓名:" << endl;
	cin >> name;
	int exist = isExist(person, name);
	if (exist != -1)
	{
		cout << "该联系人信息如下:" << endl;
		cout << "姓名:" << person->personArr[exist].name << " "
			<< "性别:" << (person->personArr[exist].sex == 1 ? "男" : "女") << " "
			<< "年龄:" << person->personArr[exist].age << " "
			<< "电话:" << person->personArr[exist].phonenumber << " "
			<< "住址:" << person->personArr[exist].address << " " << endl;
	}
	else
	{
		cout << "没有查到这个人哦!" << endl;
	}

	system("pause");
	system("cls");
}

void modifyPerson(addressbook* person)
{
	string name;
	cout << "请输入要修改联系人的姓名 :" << endl;
	cin >> name;
	int exist = isExist(person,name);
	if (exist != -1)
	{
		string modifyName;
		cout << "请输入修改后的名字:";
		cin >> modifyName;
		person->personArr[exist].name = modifyName;

		while (true)
		{
			int modifySex;
			cout << "请输入修改后的性别(1、男    2、女):";
			cin >> modifySex;
			if (modifySex == 1 || modifySex == 2)
			{
				person->personArr[exist].sex = modifySex;
				break;
			}
			else
			{
				cout << "您应当输入1或2,请重新输入!" << endl;
			}
		}

		int modifyAge;
		cout << "请输入修改后的年龄:";
		cin >> modifyAge;
		person->personArr[exist].age = modifyAge;

		string modifyPhone;
		cout << "请输入修改后的电话:";
		cin >> modifyPhone;
		person->personArr[exist].phonenumber = modifyPhone;

		string modifyAddress;
		cout << "请输入修改后的住址:";
		cin >> modifyAddress;
		person->personArr[exist].address = modifyAddress;

		cout << "修改成功" << endl;
	}
	else
	{
		cout << "没有查到这个名字的人,故无法修改" << endl;
	}
	system("pause");
	system("cls");
}

void emptyPerson(addressbook* person)
{
	string ensure;
	cout << "您确定要清空所有联系人吗,此操作不可逆,如需清空,请输入\"我同意\"这三个字: " << endl;
	cin >> ensure;
	if (ensure == "我同意")
	{
		person->person_size = 0;
		cout << "清空联系人成功" << endl;
	}
	else
	{
		cout << "撤销了清空联系人操作!" << endl;
	}
	system("pause");
	system("cls");
}

int main()
{
	int userselect = 0;
	struct addressbook aaa;
	aaa.person_size = 0;

	while (true)
	{
		showMenu();
		cout << "请在下方输入您想选择的功能(输入前面的数字即可): " << endl;
		cin >> userselect;
		switch (userselect)
		{
		case 1:
			addPerson(&aaa);
			break;
		case 2:
			showPerson(aaa);
			break;
		case 3:
			deletePerson(&aaa);
			break;
		case 4:
			findPerson(&aaa);
			break;
		case 5:
			modifyPerson(&aaa);
			break;
		case 6:
			emptyPerson(&aaa);
			break;
		case 0:
			cout << "退出系统成功,欢迎您下次使用!" << endl;
			system("pause");
			return 0;
		default:
			cout << "输入有误,请重新输入!" << endl;
			break;
		}
	}
}

运行结果:

怎么用C++实现通讯录管理系统  c++ 第1张

这个系统里用到了system(“cls”),这个是清屏的意思。

“system("cls")”是在C语言程序中,调用系统命令cls完成清屏操作。

system函数是C语言提供的与操作系统衔接的函数,函数原型如下:

#include <stdlib.h> //所在头文件
int system(const char *command);  //参数为操作系统命令

函数功能:execute a shell command 执行一个操作系统命令

如:

system("time /t") ;显示时间
system("dir"); //列目录

示例:

#include<stdlib.h>
main()
{system("cls");/*清屏*/
system("dirc://");/*列C盘根目录*/
}

以上就是关于“怎么用C++实现通讯录管理系统”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注蜗牛博客行业资讯频道。

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:niceseo99@gmail.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

评论

有免费节点资源,我们会通知你!加入纸飞机订阅群

×
天气预报查看日历分享网页手机扫码留言评论电报频道链接