编程语言
首页 > 编程语言> > c++小项目:通讯录管理系统

c++小项目:通讯录管理系统

作者:互联网

该项目非常适合新手c++的同学来做(当然也包括我),先看一个通讯录管理系统需要写什么东西。

学习于:https://www.bilibili.com/video/BV1et411b73Z?p=75(自我感觉讲的超级棒)

其中值得注意的是调用函数需要传值时,如果不需要改变实参 直接传值就好了,如果需要改变实参,必须得传指针(也就是传址)

然后还有个注意的点就是结构体和结构体指针变量的使用,剩下的就敲代码了!

 得达到上图这些功能,成功之后菜单栏显示如下所示

 

下面就是各个小块的代码:

主函数,头文件和结构体的构造函数的代码会放在最后面

菜单栏的代码

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;
	cout << "*************************" << endl;
}

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;
		

		//性别
		cout << "请输入性别:" << endl;
		cout << "1---男" << endl;
		cout << "2---女" << endl;
		int sex = 0;
		while (true)
		{
			cin >> sex;
			if (sex == 1 || sex == 2)
			{
				abs->personArray[abs->m_size].m_Sex = sex;
				break;
			}
			cout << "输入有误  请重新输入" << endl;
		}

		//年龄
		cout << "请输入年龄 :" << endl;
		int age = 0;
		cin >> age;
		abs->personArray[abs->m_size].m_Age = age;


		//电话
		cout << "请输入电话:" << endl;
		string phone;
		cin >> phone;
		abs->personArray[abs->m_size].m_Phone = phone;


		//地址
		cout << "请输入地址:" << endl;
		string address;
		cin >> address;
		abs->personArray[abs->m_size].m_Adds = address;
	}

	//更新通讯录人数
	abs->m_size++;
	cout << "添加成功!!!" << endl;

	system("pause");     //请按任意键继续
	system("cls");       //清屏操作
}

2.显示联系人

void showPerson(Addressbooks* abs)
{
	assert(abs != NULL);
	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 << "\t";
			cout << "年龄:" << abs->personArray[i].m_Age << "\t";
			cout << "电话:" << abs->personArray[i].m_Phone << "\t";
			cout << "住址:" << abs->personArray[i].m_Adds << endl;
		}
	}
	system("pause");     //请按任意键继续
	system("cls");       //清屏操作
}

下面有个判断输入名字是否在数组中的一个函数,在后面的函数中都会用到

int isExist(Addressbooks* abs, string name)
{
	for (int i = 0; i < abs->m_size; i++)
	{
		if (abs->personArray[i].m_Name == name)
		{
			return i;
		}
	}
	return -1;
}            //查找到的话  返回的是该名字在数组中的下标

3.删除指定联系人

void deletePerson(Addressbooks* abs)
{
	cout << "请输入要删除的联系人姓名:" << endl;
	string name;
	cin >> name;

	int ret = isExist(abs, name);

	if (ret != -1)
	{
		for (int i = ret; i < abs->m_size; i++)
		{
			abs->personArray[i] = abs->personArray[i + 1];
		}
		abs->m_size--;
		cout << "删除成功" << endl;
	}
	else
	{
		cout << "查无此人" << endl;
	}
	system("pause");     //请按任意键继续
	system("cls");       //清屏操作
}

4.查找指定联系人信息

void findPerson(Addressbooks* abs)
{
	cout << "请输入您要查找的联系人:" << endl;
	string name;
	cin >> name;

	int ret = isExist(abs, name);
	if (ret != -1)
	{
		cout << "姓名:" << abs->personArray[ret].m_Name << "\t";
		cout << "性别:" << abs->personArray[ret].m_Sex << "\t";
		cout << "年龄:" << abs->personArray[ret].m_Age << "\t";
		cout << "电话:" << abs->personArray[ret].m_Phone << "\t";
		cout << "住址:" << abs->personArray[ret].m_Adds << endl;
	}
	else
	{
		cout << "查无此人" << endl;
	}
	system("pause");     //请按任意键继续
	system("cls");       //清屏操作
}

5.修改联系人

void modifyPerson(Addressbooks* abs)
{
	cout << "请输入要修改的联系人:" << endl;
	string name;
	cin >> name;

	int ret = isExist(abs, name);

	if (ret != -1)
	{
		//姓名
		string name;
		cout << "请输入姓名:" << endl;
		cin >> name;
		abs->personArray[ret].m_Name = name;

		//性别
		cout << "请输入性别:" << endl;
		cout << "1---男" << endl;
		cout << "2---女" << endl;
		int sex = 0;
		while (true)
		{
			cin >> sex;
			if (sex == 1 || sex == 2)
			{
				abs->personArray[ret].m_Sex = sex;
				break;
			}
			cout << "输入有误,请重新输入" << endl;
		}

		//年龄
		cout << "请输入年龄 :" << endl;
		int age = 0;
		cin >> age;
		abs->personArray[ret].m_Age = age;


		//电话
		cout << "请输入电话:" << endl;
		string phone;
		cin >> phone;
		abs->personArray[ret].m_Phone = phone;

		//住址
		cout << "请输入地址:" << endl;
		string address;
		cin >> address;
		abs->personArray[ret].m_Adds = address;

		cout << "修改成功" << endl;
	}
	else
	{
		cout << "查无此人" << endl;
	}

	system("pause");     //请按任意键继续
	system("cls");       //清屏操作
}

6.清空通讯录列表

void clearPerson(Addressbooks* abs)
{
	abs->m_size = 0;
	cout << "清除成功" << endl;
	system("pause");     //请按任意键继续
	system("cls");       //清屏操作
}

  

主函数:

int main()
{

	int select = 0;   //创建用户输入的选择

	Addressbooks abs;
	abs.m_size = 0;//初始化联系人个数

	while (true)
	{
		//菜单调用
		ShowMenu();

		cin >> select;
		switch (select)
		{
		case 1:
			//showPerson(&abs);
			addPerson(&abs);  //利用地址传递,达到修改实参
			break;
		case 2:
			showPerson(&abs);
			break;
		case 3:
			deletePerson(&abs);
			break;
		case 4:
			findPerson(&abs);
			break;
		case 5:
			modifyPerson(&abs);
			break;
		case 6:
			break;
		case 0:
			cout << "欢迎下次使用" << endl;
			system("pause");
			return 0;
			break;
		default:
			break;
		}
	}
	system("pause");
	return 0;
}		

头文件包含和结构体设计:

#include<iostream>
using namespace std;
#include<string>
#include<assert.h>
#define MAX 1000

//设计联系人结构体
struct Person
{
	string m_Name;
	int m_Sex;//1.男,2.女
	int m_Age;
	string m_Phone;
	string m_Adds;
};

//设计通讯录结构体
struct Addressbooks
{
	struct Person personArray[MAX];  //联系人数组

	int m_size;  //记录通讯录中联系人个数
};

不要一天天的把自己菜当作理由,菜是你懒的结果,而不是自己逃避的理由!!!加油!!!

标签:cout,管理系统,c++,name,abs,通讯录,personArray,size,ret
来源: https://blog.csdn.net/qq_45829112/article/details/120090698