利用C++实现通讯录管理系统的完整代码

2022-06-13 15:46:04
目录
学习目标:案例描述:实现代码:总结

通讯录管理系统

学习目标:

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

    运行结果:

    这个系统里用到了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++实现通讯录管理系统的文章就介绍到这了,更多相关C++通讯录管理系统内容请搜索易采站长站以前的文章或继续浏览下面的相关文章希望大家以后多多支持易采站长站!