C++ implements address book management system

Design menu interface

1. The address book implements multiple functions, and the user can enter the corresponding function by entering the function serial number.--Switch statement

2. After using one function, you can continue to use other functions and keep the system from exiting --while loop

Menu function

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

	while (true)
	{
		showMenu();//调用菜单函数
		cin >> select;

		switch (select)
		{
		case 1://1.添加联系人
			addperson(&abooks);
			break;
		case 2://2.显示联系人
			showPerson(&abooks);
			break;
		case 3://3.删除联系人
			deletePerson(&abooks);
			break;
		case 4://4.查找联系人
			findPerson(&abooks);
			break;
		case 5://5.修改联系人
			modifyPerson(&abooks);
			break;
		case 6://6.清空联系人
			cleanPerson(&abooks);
			break;
		case 0://0.退出通讯录  
			cout << "欢迎下次使用" << endl;
			return 0;
			break;
		default:
			break;

		}

 menu page function

void showMenu()
{
	cout << "\t**************************" << endl;
	cout << "\t*****  1.添加联系人  *****" << endl
		 << "\t*****  2.显示联系人  *****" << endl
		 << "\t*****  3.删除联系人  *****" << endl
		 << "\t*****  4.查找联系人  *****" << endl
		 << "\t*****  5.修改联系人  *****" << endl
		 << "\t*****  6.清空联系人  *****" << endl
		 << "\t*****  0.退出通讯录  *****" << endl;
	cout << "\t**************************" << endl;
}

Add contacts

1. Contact is a custom data type -- use a structure

2. Contacts exist in the address book -- also need to define the address book structure

3. Define the add contact function and pass the structure as a parameter to the function

4. Set the upper limit of contacts in the address book, whether to add contacts or not, to determine whether the current number of contacts exceeds the upper limit

5. Save the entered information in the address book

  Take the name as an example

        abooks->p[abooks->size].name = name;

 explain:

        Assign name to the address book structure variable to access the address of the contact attribute (abooks->p[].name = name;);

        The size attribute in the address book structure records the number of contacts in the address book, so abooks->size can know which contact is currently assigned;

6. After adding a contact, clear the screen before proceeding to the next step

//定义联系人结构体
struct person
{
	string name;
	int sex;
	int age;
	string phone;
	string addr;
};

//定义通讯录结构体
struct addressbooks
{
	struct person p[MAX];
	int size;

};

//1.添加联系人
void addperson(addressbooks *abooks )
{
	if (abooks->size == MAX)
	{
		cout << "通讯录已满,无法添加!" << endl;
		return;
	}
	else {
		string name;
		cout << "请输入姓名:  " << endl;
		cin >> name;
		abooks->p[abooks->size].name = name;//将输入的信息放回通讯录里

		cout << "请输入性别" << endl;
		cout << "1-男" << endl;
		cout << "2-女" << endl;
		int sex = 0;
		while (true)
		{
			cin >> sex;
			if (sex == 1 || sex == 2)
			{
				abooks->p[abooks->size].sex = sex;
				break;
			}
			cout << "输入有误,请重新输入" << endl;
		}
		cout << "请输入年龄:   " << endl;
		int age = 0;
		cin >> age;
		abooks->p[abooks->size].age = age;
		cout << "请输入电话:  " << endl;
		string phone;
		cin >> phone;
		abooks->p[abooks->size].phone = phone;
		cout << "请输入家庭住址:  " << endl;
		string address;
		cin >> address;
		abooks->p[abooks->size].addr = address;
		abooks->size++;
		cout << "已添加成功" << endl;
		system("pause");
		system("cls");//清屏
	}
}

show contacts

1. Define the display contact function and pass the structure as a parameter to the function

2. When outputting gender, use the ternary operator

void showPerson(addressbooks* abooks)
{
	if (abooks->size == 0)
	{
		cout << "当前记录为空" << endl;
	}
	else {
		for (int i = 0; i < abooks->size; i++)
		{
			cout << "姓名:  " << abooks->p[i].name << "\t";
			cout << "性别:  " << (abooks->p[i].sex == 1 ? "男" : "女") << "\t";
			cout << "年龄:  " << abooks->p[i].age << "\t";
			cout << "电话:  " << abooks->p[i].phone << "\t";
			cout << "住址:  " << abooks->p[i].addr << "\t" << endl;
		}
	}
	system("pause");
	system("cls");
}

delete contact

find contact

1. Define the contact search function, pass the address book structure and contact name as parameters to the function

2. Traverse the address book structure, if the contact is found, return the serial number of the contact

int isExist(addressbooks* abooks, string name)
{
	for (int i = 0; i < abooks->size; i++)
	{
		if (abooks->p[i].name == name)
		{
			return i;
		}
	}
	return -1;
}

delete contact

1. First, determine whether the contact exists by looking up the return value in the contact function

2. Traverse all the contacts after this contact, cover the contact information of the next contact to the position of this contact information, and so on, move the data forward.

3. After deleting a contact, size--.

void deletePerson(addressbooks* abooks)
{
	cout << "请输入您要删除的联系人" << endl;
	string name;
	cin >> name;
	int ret = isExist(abooks, name);
	if (ret != -1)
	{
		for (int i = ret; i < abooks->size; i++)
		{
			abooks->p[i] = abooks->p[i + 1];//数据前移
		}
		abooks->size--;
		cout << "删除成功" << endl;
	}
	else
	{
		cout << "查无此人" << endl;
	}
	system("pause");
	system("cls");
}

find a contact

1. Call the previously defined contact lookup function isExist to find the contact

2. Output this contact information

3.ret is the return value of the function isExist, that is, the serial number of the contact to be found

void findPerson(addressbooks* abooks)
{
	cout << "请输入您要查找的联系人" << endl;
	string name;
	cin >> name;
	int ret = isExist(abooks, name);
	if (ret != -1)
	{
		cout << "姓名:  " << abooks->p[ret].name << "\t";
		cout << "性别:  " << abooks->p[ret].sex << "\t";
		cout << "年龄:  " << abooks->p[ret].age << "\t";
		cout << "电话:  " << abooks->p[ret].phone << "\t";
		cout << "住址:  " << abooks->p[ret].addr << "\t" << endl;
	}
	else 
	{
		cout << "查无此人" << endl;
	}
	system("pause");
	system("cls");
}

Edit contacts

1. Combine the search function with the operation of adding a contact

2. Note: When assigning a value, the subscript of the array p is the return value ret obtained when looking for a contact

void modifyPerson(addressbooks* abooks)
{
	cout << "请输入您要修改的联系人" << endl;
	string name;
	cin >> name;
	int ret = isExist(abooks, name);
	if (ret != -1)
	{
		string name;
		cout << "请输入姓名:  " << endl;
		cin >> name;
		abooks->p[ret].name = name;
		cout << "请输入性别" << endl;
		cout << "1-男" << endl;
		cout << "2-女" << endl;
		int sex = 0;
		while (true)
		{
			cin >> sex;
			if (sex == 1 || sex == 2)
			{
				abooks->p[ret].sex = sex;
				break;
			}
			cout << "输入有误,请重新输入" << endl;
		}
		cout << "请输入年龄:   " << endl;
		int age = 0;
		cin >> age;
		abooks->p[ret].age = age;
		cout << "请输入电话:  " << endl;
		string phone;
		cin >> phone;
		abooks->p[ret].phone = phone;
		cout << "请输入家庭住址:  " << endl;
		string address;
		cin >> address;
		abooks->p[ret].addr = address;
		cout << "已修改成功" << endl;
	}
	else
	{
		cout << "查无此人" << endl;
	}
	system("pause");
	system("cls");
}

clear contacts

1. Logically clear, that is, assign the number of contacts to 0.

void cleanPerson(addressbooks* abooks)
{
	abooks->size = 0;
	cout << "通讯录已清空" << endl;
	system("pause");
	system("cls");
}

main function

1. Set the maximum capacity of the address book to 1000 people

2. Call the address book structure in the main function

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

int main()
{
	struct addressbooks abooks;//调用结构体
	abooks.size = 0;//初始化联系人个数

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

	while (true)
	{
		showMenu();//调用菜单函数
		cin >> select;

		switch (select)
		{
		case 1://1.添加联系人
			addperson(&abooks);
			break;
		case 2://2.显示联系人
			showPerson(&abooks);
			break;
		case 3://3.删除联系人
			deletePerson(&abooks);
			break;
		case 4://4.查找联系人
			findPerson(&abooks);
			break;
		case 5://5.修改联系人
			modifyPerson(&abooks);
			break;
		case 6://6.清空联系人
			cleanPerson(&abooks);
			break;
		case 0://0.退出通讯录  
			cout << "欢迎下次使用" << endl;
			return 0;
			break;
		default:
			break;

		}
	}
	return 0;
}

Related Posts