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

Posted 2654501228

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++实现通讯录管理系统相关的知识,希望对你有一定的参考价值。

前言

**

建议收藏,亲手写一遍代码,感受指针神奇的魅力;
可以帮助你更好的巩固知识体系,熟悉指针,结构体与函数一起使用时的妙处

**

完成通讯录管理系统所需知识体系
1.结构体
2.指针
3.函数的封装
4.指针与函数的结合使用
5.指针与结构体的结合使用

结构体

联系人结构体

struct person
{
	string name;//姓名
	string sex; //性别
	int age;  //年龄
	string phone;//手机号
	string home;//地址

};

通讯录结构体

struct addressbook
{
	struct person personArray[MAX]; //通讯录扩展到100;
	int size=0;  //当前联系人个数(后面就相当于i++)
};

函数模块

void menu();//菜单
void putit(addressbook* add);//添加联系人
void showperson(addressbook* add);// 显示联系人
int if_include(addressbook* add, string name);//判断联系人
void deleteperson(addressbook* add, int i);//删除联系人
void findPerson( addressbook* add);//查找联系人
void cleanperson(struct addressbook* add);//清空所有联系人

菜单

void menu()
{
	cout << endl;
	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 putit(addressbook* add)//添加联系人功能
{
	if (add->size == MAX)
		cout << "通讯录已满" << endl;
	else
	{
		string name;
		string sex;
		int age;
		string phone;
		string home;
		cout << "请输入姓名" << endl;
		cin >> name;
		add->personArray[add->size].name = name;
		cout << "请输入姓别" << endl;
		cin >> sex;
		add->personArray[add->size].sex = sex;
		cout << "请输入年龄" << endl;
		cin >> age;
		add->personArray[add->size].age = age;
		cout << "请输入电话号码" << endl;
		cin >> phone;
		add->personArray[add->size].phone = phone;
		cout << "请输入家庭住址" << endl;
		cin >> home;
		add->personArray[add->size].home = home;
		add->size++;
		cout << "添加联系人成功" << endl;
		
	}
	system("pause");
	system("cls");
	menu();
}

有人问添加联系人函数中为什么要使用指针?
因为值传递中形参无法改变实参;
而利用指针的地址传递可以通过函数中的形参改变实参;
具体原理可以参考博主之前的指针基础和指针进阶内容]

显示联系人


void showperson(addressbook* add)
{
	for (int i = 0; i <add->size; i++)
	{
		cout << "姓名:  " << add->personArray[i].name;
		cout << "\\t姓别:  " << add->personArray[i].sex;
		cout << "\\t年龄:  " << add->personArray[i].age;
		cout << "\\t电话号码:  " << add->personArray[i].phone;
		cout << "\\t家庭住址:  " << add->personArray[i].home << endl;
	}
	system("pause");
	system("cls");
	menu();
}

判断联系人

int if_include(addressbook* add, string name)
{
	for (int i = 0; i < add->size; i++)
	{
		if (name == add->personArray[i].name)
		{
			return i;
		}
		else
		{
			return -1;
		}
	}

}

删除联系人

void deleteperson(addressbook* add, int i)
{
	for (; i < add->size; i++)
	{
		add->personArray[i] = add->personArray[i + 1];
	}
	system("pause");
	system("cls");
}


查找联系人

void findPerson( addressbook* add)
{
	cout << "请输入您想要查找的联系人:" << endl;
	string name;
	cin >> name;
	int ret = if_include(add, name);
	if (ret == -1)
	{
		cout << "查无此人" << endl;
	}
	else
	{   //查到此人,进行显示操作
		int i = ret;
		cout << "姓名:" << add->personArray[i].name << "\\t";
		cout << "性别:" << add->personArray[i].sex << "\\t";
		cout << "年龄:" << add->personArray[i].age << "\\t";
		cout << "联系方式:" << add->personArray[i].phone << "\\t";
		cout << "地址:" << add->personArray[i].home << endl;
	}
	//按任意键清屏
	system("pause");
	system("cls");
}

清空所有联系人

void cleanperson(struct addressbook* add)//清空所有联系人
{
	cout << "是否确认清空?" << endl;
	cout << "1 --- 是" << endl;
	cout << "2 --- 否" << endl;
	int a;
	cin >> a;
	if (a == 1)
	{
		add->size = 0;//将当前记录联系人数量置为0,做逻辑上的清空操作
		cout << "通讯录已清空" << endl;
	}
	system("pause");
	system("cls");
}

main函数

int main()
{
	menu();
	addressbook add;//定义一个通讯录
	int choice=1;
	while (choice != 0)
	{
		cin >> choice;
		switch (choice)//选择
		{
		case 1: putit(&add);
			break;
		case 2: showperson(&add);
			break;
		case 3: {cout << "请输入你要删除的人的名字" << endl;
			string aname;
			cin >> aname;
			if (if_include(&add, aname) == -1)
			{
				cout << "查无此人" << endl;
				break;
			}
			if (if_include(&add, aname))
			{
				deleteperson(&add, if_include(&add, aname));
			}
		}
			break; 
		case 4:
			break;
		case 5:
			break;
		case 6:
			break;
		case 0: cout << "欢迎下次使用" << endl;
			return 0;
			break;
		default: {cout << "输入不合法,请重新输入" << endl;
			break; }
		}
	}
}

源代码

#include<iostream>
using namespace std;
#include<string>
#define MAX 100

struct person
{
	string name;//姓名
	string sex; //性别
	int age;  //年龄
	string phone;//手机号
	string home;//地址

};
struct addressbook
{
	struct person personArray[MAX]; //通讯录扩展到100;
	int size=0;  //当前联系人个数(后面就相当于i++)
};
void menu();//菜单
void putit(addressbook* add);
void showperson(addressbook* add); 
int if_include(addressbook* add, string name);
void deleteperson(addressbook* add, int i);
void findPerson(struct addressbooks* add);//查找联系人
void cleanperson(struct addressbook* add);//清空所有联系人
int main()
{
	menu();
	addressbook add;
	int choice=1;
	while (choice != 0)
	{
		cin >> choice;
		switch (choice)
		{
		case 1: putit(&add);
			break;
		case 2: showperson(&add);
			break;
		case 3: {
			cout << "请输入你要删除的人的名字" << endl;
			string aname;
			cin >> aname;
			if (if_include(&add, aname) == -1)
			{
				cout << "查无此人" << endl;
				break;
			}
			if (if_include(&add, aname))
			{
				deleteperson(&add, if_include(&add, aname));
			}
		}
			break; 
		case 4:
			break;
		case 5:
			break;
		case 6:
			break;
		case 0: cout << "欢迎下次使用" << endl;
			return 0;
			break;
		default: {cout << "输入不合法,请重新输入" << endl;
			break; }
		}
	}
}
void menu()
{
	cout << endl;
	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 putit(addressbook* add)//添加联系人功能
{
	if (add->size == MAX)
		cout << "通讯录已满" << endl;
	else
	{
		string name;
		string sex;
		int age;
		string phone;
		string home;
		cout << "请输入姓名" << endl;
		cin >> name;
		add->personArray[add->size].name = name;
		cout << "请输入姓别" << endl;
		cin >> sex;
		add->personArray[add->size].sex = sex;
		cout << "请输入年龄" << endl;
		cin >> age;
		add->personArray[add->size].age = age;
		cout << "请输入电话号码" << endl;
		cin >> phone;
		add->personArray[add->size].phone = phone;
		cout << "请输入家庭住址" << endl;
		cin >> home;
		add->personArray[add->size].home = home;
		add->size++;
		cout << "添加联系人成功" << endl;
		
	}
	system("pause");
	system("cls");
	menu();
}
void showperson(addressbook* add)
{
	for (int i = 0; i <add->size; i++)
	{
		cout << "姓名:  " << add->personArray[i].name;
		cout << "\\t姓别:  " << add->personArray[i].sex;
		cout << "\\t年龄:  " << add->personArray[i].age;
		cout << "\\t电话号码:  " << add->personArray[i].phone;
		cout << "\\t家庭住址:  " << add->personArray[i].home << endl;
	}
	system("pause");
	system("cls");
	menu();
}
int if_include(addressbook* add, string name)
{
	for (int i = 0; i < add->size; i++)
	{
		if (name == add->personArray[i].name)
		{
			return i;
		}
		else
		{
			return -1;
		}
	}

}
void deleteperson(addressbook* add, int i)
{
	for (; i < add->size; i++)
	{
		add->personArray[i] = add->personArray[i + 1];
	}
	system("pause");
	system("cls");
}

void findPerson( addressbook* add)
{
	cout << "请输入您想要查找的联系人:" << endl;
	string name;
	cin >> name;
	int ret = if_include(add, name);
	if (ret == -1)
	{
		cout << "查无此人" << endl;
	}
	else
	{   //查到此人,进行显示操作
		int i = ret;
		cout << "姓名:" << add->personArray[i].name << "\\t";
		cout << "性别:" << add->personArray[i].sex << "\\t";
		cout << "年龄:" << add->personArray[i].age << "\\t";
		cout << "联系方式:" << add->personArray[i].phone << "\\t";
		cout << "地址:" << add->personArray[i].home << endl;
	}
	//按任意键清屏
	system("pause");
	system("cls");
}
void cleanperson(struct addressbook* add)//清空所有联系人
{
	cout << "是否确认清空?" << endl;
	cout << "1 --- 是" << endl;
	cout << "2 --- 否" << endl;
	int a;
	cin >> a;
	if (a == 1我的Android进阶之旅NDK开发之在C++代码中使用Android Log打印日志,打印出C++的函数耗时以及代码片段耗时详情

基于C++的通讯录管理系统|附源码

异常和TCP通讯

通讯录管理系统

c++—通讯录管理系统

C++ 通讯录管理系统