C++ structure project----address book management system

Table of contents

Address book management system

Each sub-function of the address book system

1. Header file, structure

2. Main function

3. Menu interface

4. Add contact function

5. Show contacts

 6. Delete contacts

7. Find contacts

8. Modify contact information

 9. Clear Contacts

10. Exit the address book management system 

 source code

The results of using the address book are displayed:


Address book management system

illustrate:

The functions to be implemented in the system are as follows:

  • Add contacts: Add new people to the address book, the information includes (name, gender, age, contact number, home address) and record up to 1000 people
  • Show Contacts: Display all contact information in the address book
  • Delete contact: delete the specified contact by name
  • Find contacts: view the specified contact information by name
  • Modify contact: re-modify the specified contact according to the name
  • Clear contacts: Clear all information in the address book
  • Exit address book: exit the current address book

Each sub-function of the address book system

1. Header file, structure

#include<iostream>
#include<string>
#include<ctype.h>
#define M 1000    // 定义联系人的最大个数
using namespace std;

struct person {        // 联系人结构体
	string name;       // 姓名
	string sex;        // 性别
	int age;           // 年龄
	string phone;      // 电话
	string addr;       // 地址
};
struct addr_book {
	struct person p_arr[M];
	int size; // 通讯录中人的个数
};

// 菜单界面
void showmenu();
// 1.添加联系人
void add_person(addr_book* abs);
// 2.显示联系人
void show_person(addr_book* abs);
// 3.删除联系人
// 首先判断联系人是否存在
int isexist(addr_book* abs, string name);
// 具体的删除
void del_person(addr_book* abs);
// 4.查找联系人
void find_person(addr_book* abs);
// 5.修改联系人信息
void change_person(addr_book* abs);
// 6.清空联系人
void clear_person(addr_book* abs);
// 0.退出函数
bool quite();

2. Main function

int main()
{
	struct addr_book abs;
	abs.size = 0;
	int select = 0;// 用户输入
	bool flag = true;
	while (flag) {
		// 菜单调用
		showmenu();
		cin >> select;
		switch (select) {
		case 1:
			// 1.添加联系人
			add_person(&abs);  // 利用地址传递,可以修饰实参
			break;
		case 2:
			// 2.显示联系人
			show_person(&abs);
			break;
		case 3:
			// 3.删除联系人
			del_person(&abs);
			break;
		case 4:
			// 4.查找联系人
			find_person(&abs);
			break;
		case 5:
			// 5.修改联系人
			change_person(&abs);
			break;
		case 6:
			// 6.清空联系人
			clear_person(&abs);
			break;
		case 0:
			flag = quite();
			//cout << "欢迎下次使用!" << endl;
			//flag = false;
			system("pause");
			system("cls");
			break;
		}
	}	
	return 0;
}

3. Menu interface

// 菜单界面
void showmenu()
{
	cout << "--------通讯录管理系统--------" << endl;
	cout << "\t1.添加联系人" << endl;
	cout << "\t2.显示联系人" << endl;
	cout << "\t3.删除联系人" << endl;
	cout << "\t4.查找联系人" << endl;
	cout << "\t5.修改联系人" << endl;
	cout << "\t6.清空联系人" << endl;
	cout << "\t0.推出通讯录" << endl;
	cout << "------------------------------" << endl;
}

4. Add contact function

Press 1 to enter the add contact interface

First, determine whether the address book system has already stored the upper limit of contacts,

Enter the name and gender respectively (judging whether the input is "male" or "female", and outputting and re-entering in other cases),

Age (judging whether the age is within the range of 10 to 100, if it exceeds the range, the user is required to re-enter),

Phone (judging whether the phone number has three digits, if the digits are wrong, the user is required to re-enter) (other digits are also available, 3 digits are more convenient),

Complete the entry after the address.

After the entry is complete, update the number of contacts in the address book and return to the menu interface.

// 1.添加联系人
void add_person(addr_book* abs)
{
	// 判断通讯录是否已满
	if (abs->size == M) {
		cout << "通讯录已满,无法添加!" << endl;
		return;
	}else{
		
		string name;
		// 添加姓名
		cout << "请输入姓名:  ";
		cin >> name;
		abs->p_arr[abs->size].name=name;
		// 添加性别
		string sex;
		cout << "请输入性别: ";
		while (true) {
			cin >> sex;
			if (sex == "男" || sex == "女") {
				abs->p_arr[abs->size].sex = sex;
				break;
			}
			else {
				cout << "请重新输入: ";
			}
		}
		// 添加年龄
		int age;
		cout << "请输入年龄: ";
		while (true) {
			cin >> age;
			if (age >= 10 && age <= 100) {
				abs->p_arr[abs->size].age = age;
				break;
			}
			else {
				cout << "请重新输入: ";
			}
		}
		// 添加电话
		cout << "请输入电话: ";
		string phone;
		while (true) {
			cin >> phone;
			if (phone.length() == 3) {
				abs->p_arr[abs->size].phone = phone;
				break;
			}
			else {
				cout << "你输入的电话位数不对,请重新输入: ";
			}
		}
		// 添加地址
		cout << "请输入家庭住址: ";
		cin >> abs->p_arr[abs->size].addr;

		abs->size++;
		cout << "--------添加成功--------" << endl;

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

5. Show contacts

Print out all the contacts in the address book. Similarly, first determine whether the address book is empty.

// 2.显示联系人
void show_person(addr_book* abs)
{
	// 判断通讯录中人数是否为0
	if (abs->size == 0) {
		cout << "当前的记录为空" << endl;
	}
	else {
		for (int i = 0; i < abs->size; i++) {
			cout << "姓名\t性别\t年龄\t电话\t家庭地址" << endl;
			cout << abs->p_arr[i].name << "\t" << abs->p_arr[i].sex << "\t" << abs->p_arr[i].age
				<< "\t" << abs->p_arr[i].phone << "\t" << abs->p_arr[i].addr << endl;
		}
	}
	system("pause");
	system("cls");
}

 6. Delete contacts

By judging whether the contact exists in the sub-function of the address book, find out whether the contact to be deleted exists in the address book, if found, return the subscript position of the current contact in the address book array, and return -1 if not found.

Find the contact to be deleted in the address book by the name of the contact, if not found, give the user feedback that the user does not exist,

If the contact is found, overwrite the current location with the information of the next location in the array, and so on. After updating the location of each contact after the contact to be deleted, the size of the address book can be -1.

// 3.删除联系人
// 首先判断联系人是否存在
int isexist(addr_book* abs, string name)
{
	for (int i = 0; i < abs->size; i++) {
		if (abs->p_arr[i].name == name) {
			return i;  // 找到返回在数组中的编号
		}
	}
	return -1;
}
// 具体的删除
void del_person(addr_book* abs) 
{
	string name;
	cout << "请输入您要删除的联系人: ";
	cin >> name;
	int ret = isexist(abs, name);
	if (ret == -1) {
		cout << "查无此人" << endl;
	}
	else {
		// 得到的是要删除的人的数组下标
		// 要删除可以直接将后一个数据覆盖到要删除的数据上
		for (int i = ret; i < abs->size; i++) {
			abs->p_arr[i] = abs->p_arr[i + 1];
		}
		abs->size--;
		cout << "--------删除成功--------" << endl;
	}
	system("pause");
	system("cls");
}

7. Find contacts

Same as deleting a contact, first determine whether the contact is in the address book system,

If it is not found, the user will be prompted that the contact does not exist, and if it is found, all the information of the contact will be printed.

// 4.查找联系人
void find_person(addr_book* abs)
{
	cout << "请输入您要查找的联系人: ";
	string name;
	cin >> name;
	int ret = isexist(abs, name);
	if (ret == -1) {
		cout << "您要查找的联系人不存在。" << endl;
	}
	else {
		cout << "姓名\t性别\t年龄\t电话\t家庭地址" << endl;
		cout << abs->p_arr[ret].name << "\t" << abs->p_arr[ret].sex << "\t" << abs->p_arr[ret].age
			<< "\t" << abs->p_arr[ret].phone << "\t" << abs->p_arr[ret].addr << endl;
		cout << "--------查找完毕--------" << endl;
	}
	system("pause");
	system("cls");
}

8. Modify contact information

Use the name of the contact to find out whether the desired contact exists in the address book system. If not found, give the user feedback that there is no such person.

After finding the location of the contact to be modified, just overwrite the newly entered information to the original location.

// 5.修改联系人信息
void change_person(addr_book* abs)
{
	cout << "请输入您要修改的联系人: ";
	string name;
	cin >> name;
	int ret = isexist(abs, name);
	if (ret == -1) {
		cout << "查无此人。" << endl;
		system("pause");// 请按任意键继续
		system("cls");
		
	}
	else {
		string name;
		// 修改姓名
		cout << "请输入姓名:  ";
		cin >> name;
		abs->p_arr[ret].name = name;
		// 修改性别
		string sex;
		cout << "请输入性别: ";
		while (true) {
			cin >> sex;
			if (sex == "男" || sex == "女") {
				abs->p_arr[ret].sex = sex;
				break;
			}
			else {
				cout << "请重新输入: ";
			}
		}
		// 添加年龄
		int age;
		cout << "请输入年龄: ";
		while (true) {
			cin >> age;
			if (age >= 10 && age <= 100) {
				abs->p_arr[ret].age = age;
				break;
			}
			else {
				cout << "请重新输入: ";
			}
		}
		// 添加电话
		cout << "请输入电话: ";
		string phone;
		while (true) {
			cin >> phone;
			if (phone.length() == 3) {
				abs->p_arr[ret].phone = phone;
				break;
			}
			else {
				cout << "你输入的电话位数不对,请重新输入: ";
			}
		}
		// 添加地址
		cout << "请输入家庭住址: ";
		cin >> abs->p_arr[ret].addr;

		cout << "--------修改成功--------" << endl;

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

 9. Clear Contacts

Just set the size to zero directly.

// 6.清空联系人
void clear_person(addr_book* abs)
{
	// 直接将size的数量置为0就ok
	abs->size = 0;
	cout << "--------所有联系人已经全部清空--------" << endl;
	system("pause");
	system("cls");
}

10. Exit the address book management system 

Determine whether to exit the system through user input, and convert the y or n input by the user to uppercase for comparison

y to exit the system, n to return to the menu interface, enter other content, and prompt to re-enter

// 0.退出函数
bool quite()
{
	char n;
	cout << "是否确认退出(Y/N): ";
	while (true)
	{		
		cin >> n;
		if (toupper(n) == 'Y') {
			cout << "欢迎下次使用!" << endl;
			return false;
			break;
		}
		else if (toupper(n) == 'N') {
			return true;
			break;			
		}
		else {
			cout << "输入错误,请重新输入: ";
		}		
	}
}

 source code

#include<iostream>
#include<string>
#include<ctype.h>
#define M 1000    // 定义联系人的最大个数
using namespace std;

struct person {        // 联系人结构体
	string name;       // 姓名
	string sex;        // 性别
	int age;           // 年龄
	string phone;      // 电话
	string addr;       // 地址
};
struct addr_book {
	struct person p_arr[M];
	int size; // 通讯录中人的个数
};

// 菜单界面
void showmenu();
// 1.添加联系人
void add_person(addr_book* abs);
// 2.显示联系人
void show_person(addr_book* abs);
// 3.删除联系人
// 首先判断联系人是否存在
int isexist(addr_book* abs, string name);
// 具体的删除
void del_person(addr_book* abs);
// 4.查找联系人
void find_person(addr_book* abs);
// 5.修改联系人信息
void change_person(addr_book* abs);
// 6.清空联系人
void clear_person(addr_book* abs);
// 0.退出函数
bool quite();

int main()
{
	struct addr_book abs;
	abs.size = 0;
	int select = 0;// 用户输入
	bool flag = true;
	while (flag) {
		// 菜单调用
		showmenu();
		cin >> select;
		switch (select) {
		case 1:
			// 1.添加联系人
			add_person(&abs);  // 利用地址传递,可以修饰实参
			break;
		case 2:
			// 2.显示联系人
			show_person(&abs);
			break;
		case 3:
			// 3.删除联系人
			del_person(&abs);
			break;
		case 4:
			// 4.查找联系人
			find_person(&abs);
			break;
		case 5:
			// 5.修改联系人
			change_person(&abs);
			break;
		case 6:
			// 6.清空联系人
			clear_person(&abs);
			break;
		case 0:
			flag = quite();
			//cout << "欢迎下次使用!" << endl;
			//flag = false;
			system("pause");
			system("cls");
			break;
		}
	}	
	return 0;
}
// 菜单界面
void showmenu()
{
	cout << "--------通讯录管理系统--------" << endl;
	cout << "\t1.添加联系人" << endl;
	cout << "\t2.显示联系人" << endl;
	cout << "\t3.删除联系人" << endl;
	cout << "\t4.查找联系人" << endl;
	cout << "\t5.修改联系人" << endl;
	cout << "\t6.清空联系人" << endl;
	cout << "\t0.推出通讯录" << endl;
	cout << "------------------------------" << endl;
}
// 1.添加联系人
void add_person(addr_book* abs)
{
	// 判断通讯录是否已满
	if (abs->size == M) {
		cout << "通讯录已满,无法添加!" << endl;
		return;
	}else{
		
		string name;
		// 添加姓名
		cout << "请输入姓名:  ";
		cin >> name;
		abs->p_arr[abs->size].name=name;
		// 添加性别
		string sex;
		cout << "请输入性别: ";
		while (true) {
			cin >> sex;
			if (sex == "男" || sex == "女") {
				abs->p_arr[abs->size].sex = sex;
				break;
			}
			else {
				cout << "请重新输入: ";
			}
		}
		// 添加年龄
		int age;
		cout << "请输入年龄: ";
		while (true) {
			cin >> age;
			if (age >= 10 && age <= 100) {
				abs->p_arr[abs->size].age = age;
				break;
			}
			else {
				cout << "请重新输入: ";
			}
		}
		// 添加电话
		cout << "请输入电话: ";
		string phone;
		while (true) {
			cin >> phone;
			if (phone.length() == 3) {
				abs->p_arr[abs->size].phone = phone;
				break;
			}
			else {
				cout << "你输入的电话位数不对,请重新输入: ";
			}
		}
		// 添加地址
		cout << "请输入家庭住址: ";
		cin >> abs->p_arr[abs->size].addr;

		abs->size++;
		cout << "--------添加成功--------" << endl;

		// 清屏的操作
		system("pause");// 请按任意键继续
		system("cls");
	}
}
// 2.显示联系人
void show_person(addr_book* abs)
{
	// 判断通讯录中人数是否为0
	if (abs->size == 0) {
		cout << "当前的记录为空" << endl;
	}
	else {
		for (int i = 0; i < abs->size; i++) {
			cout << "姓名\t性别\t年龄\t电话\t家庭地址" << endl;
			cout << abs->p_arr[i].name << "\t" << abs->p_arr[i].sex << "\t" << abs->p_arr[i].age
				<< "\t" << abs->p_arr[i].phone << "\t" << abs->p_arr[i].addr << endl;
		}
	}
	system("pause");
	system("cls");
}
// 3.删除联系人
// 首先判断联系人是否存在
int isexist(addr_book* abs, string name)
{
	for (int i = 0; i < abs->size; i++) {
		if (abs->p_arr[i].name == name) {
			return i;  // 找到返回在数组中的编号
		}
	}
	return -1;
}
// 具体的删除
void del_person(addr_book* abs) 
{
	string name;
	cout << "请输入您要删除的联系人: ";
	cin >> name;
	int ret = isexist(abs, name);
	if (ret == -1) {
		cout << "查无此人" << endl;
	}
	else {
		// 得到的是要删除的人的数组下标
		// 要删除可以直接将后一个数据覆盖到要删除的数据上
		for (int i = ret; i < abs->size; i++) {
			abs->p_arr[i] = abs->p_arr[i + 1];
		}
		abs->size--;
		cout << "--------删除成功--------" << endl;
	}
	system("pause");
	system("cls");
}
// 4.查找联系人
void find_person(addr_book* abs)
{
	cout << "请输入您要查找的联系人: ";
	string name;
	cin >> name;
	int ret = isexist(abs, name);
	if (ret == -1) {
		cout << "您要查找的联系人不存在。" << endl;
	}
	else {
		cout << "姓名\t性别\t年龄\t电话\t家庭地址" << endl;
		cout << abs->p_arr[ret].name << "\t" << abs->p_arr[ret].sex << "\t" << abs->p_arr[ret].age
			<< "\t" << abs->p_arr[ret].phone << "\t" << abs->p_arr[ret].addr << endl;
		cout << "--------查找完毕--------" << endl;
	}
	system("pause");
	system("cls");
}
// 5.修改联系人信息
void change_person(addr_book* abs)
{
	cout << "请输入您要修改的联系人: ";
	string name;
	cin >> name;
	int ret = isexist(abs, name);
	if (ret == -1) {
		cout << "查无此人。" << endl;
		system("pause");// 请按任意键继续
		system("cls");
		
	}
	else {
		string name;
		// 修改姓名
		cout << "请输入姓名:  ";
		cin >> name;
		abs->p_arr[ret].name = name;
		// 修改性别
		string sex;
		cout << "请输入性别: ";
		while (true) {
			cin >> sex;
			if (sex == "男" || sex == "女") {
				abs->p_arr[ret].sex = sex;
				break;
			}
			else {
				cout << "请重新输入: ";
			}
		}
		// 添加年龄
		int age;
		cout << "请输入年龄: ";
		while (true) {
			cin >> age;
			if (age >= 10 && age <= 100) {
				abs->p_arr[ret].age = age;
				break;
			}
			else {
				cout << "请重新输入: ";
			}
		}
		// 添加电话
		cout << "请输入电话: ";
		string phone;
		while (true) {
			cin >> phone;
			if (phone.length() == 3) {
				abs->p_arr[ret].phone = phone;
				break;
			}
			else {
				cout << "你输入的电话位数不对,请重新输入: ";
			}
		}
		// 添加地址
		cout << "请输入家庭住址: ";
		cin >> abs->p_arr[ret].addr;

		cout << "--------修改成功--------" << endl;

		// 清屏的操作
		system("pause");// 请按任意键继续
		system("cls");
	}
}
// 6.清空联系人
void clear_person(addr_book* abs)
{
	// 直接将size的数量置为0就ok
	abs->size = 0;
	cout << "--------所有联系人已经全部清空--------" << endl;
	system("pause");
	system("cls");
}
// 0.退出函数
bool quite()
{
	char n;
	cout << "是否确认退出(Y/N): ";
	while (true)
	{		
		cin >> n;
		if (toupper(n) == 'Y') {
			cout << "欢迎下次使用!" << endl;
			return false;
			break;
		}
		else if (toupper(n) == 'N') {
			return true;
			break;			
		}
		else {
			cout << "输入错误,请重新输入: ";
		}		
	}
}

The results of using the address book are displayed:

1. Menu interface

2. Add contacts

add normally

 

Add if input is wrong

  

 3. Show contacts

4. Delete contacts

The contact to delete exists

 

The contact to delete does not exist

  

5. Find contacts

Find contacts that exist in the address book management system

 

 find contact does not exist

 

6. Modify contacts

7. Clear contacts

8. Exit Contacts

Guess you like

Origin blog.csdn.net/hjl011006/article/details/131222594