C++ course design major homework, design a simple address book, with the function of adding, deleting, checking and modifying

This code is a simple address book program, which can add, display, delete, find, modify and clear contacts. Here is a line-by-line explanation and introduction to the code:

  1. PersonFirst, a structure named is defined , which represents the information of a contact, including name, gender, age, phone number and address.

  2. Next, a class called class is defined Addressbooksfor managing the address book. This class contains a container personArraynamed vector, which is used to store contact information.

  3. In Addressbooksthe class, a series of member functions are defined to achieve different functions:

    • showMenu()The function is used to display menu options, prompting the user for possible operations.
    • addPerson()Function is used to add contacts. It will in turn get the contact's name, gender, age, phone, and address from the user input and add the contact to the personArraycontainer.
    • showPerson()Function to display contacts. It iterates through personArrayeach contact in the container and outputs its name, gender, age, phone, and address.
    • deletePerson()Function is used to delete a contact. It will ask the user to enter the name of the contact to delete, personArraylook for that contact in the container, and delete it if found.
    • findPerson()function to look up contacts. It will ask the user to enter the name of the contact they are looking for, personArraylook for that contact in a container, and output their details if found.
    • modifyPerson()Function is used to modify contact information. It will ask the user to enter the name of the contact to modify, personArraylook for the contact in the container, and if found, allow the user to modify the name, gender, age, phone, and address.
    • cleanPerson()The function is used to clear the address book, that is, delete all contacts.
  4. In the function, an object main()is created and the menu options are presented for the user to select through an infinite loop. According to the user's choice, the member function of the corresponding class is called to perform the corresponding operation. Until the user chooses to exit the address book, the program ends.AddressbooksaddressbooksAddressbooks

Overall, this code implements a simple address book management system, which can easily add, display, delete, find, modify and clear contact information. Through the use of object-oriented design, the contact information is encapsulated in a class, and a set of operation functions are provided to manage the address book. The code is clearly structured and easy to understand and extend.

When optimizing your code, consider the following aspects:

  1. Use a more suitable data structure: The current code uses an array to store contact information, but when inserting and deleting contacts, the elements of the array need to be moved, which is inefficient. You can consider using a dynamic array (such as std::vector) or a linked list (LinkedList) to store contacts to improve the efficiency of insertion and deletion.

  2. Reduce repetitive code: In the current code, some functions (such as inputting name, gender, age, phone number, and address) have repeated code in multiple functions. These common codes can be extracted and reused as independent functions to reduce duplication of labor and code redundancy.

  3. Introduce an error handling mechanism: The current code does not perform error handling on the input data. For example, when an illegal gender value or age value is entered, the program does not give an error message. Appropriate error handling mechanisms can be added to remind the user to enter correct data and perform appropriate fault tolerance.

  4. Optimize the search function: The way to find contacts in the current code is to traverse the entire address book array, which is less efficient when there are many contacts in the address book. You can consider using data structures such as HashMap or Binary Search Tree to optimize the search function to improve search efficiency.

  5. Use function parameters to pass structure objects: In the current code, each function needs to pass the pointer of the Addressbooks object as a parameter. You can consider passing the Addressbooks object as a parameter to the function, avoiding the use of global variables, and increasing the readability and maintainability of the code.

The following is an example of improving the code according to the above optimization points:

#include<iostream>
#include<string>
#include<vector>

using namespace std;

// 联系人结构体
struct Person
{
	string m_Name; // 姓名
	int m_Sex; // 性别:1男 2女
	int m_Age; // 年龄
	string m_Phone; // 电话
	string m_Addr; // 住址
};

// 通讯录类
class Addressbooks
{
private:
	vector<Person> personArray; // 通讯录中保存的联系人数组

public:
	// 添加联系人
	void addPerson();

	// 显示联系人
	void showPerson();

	// 删除联系人
	void deletePerson();

	// 查找联系人
	void findPerson();

	// 修改联系人
	void modifyPerson();

	// 清空联系人
	void cleanPerson();
};

// 菜单界面
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;
}

// 添加联系人
void Addressbooks::addPerson()
{
	Person person;
	cout << "请输入姓名:" << endl;
	cin >> person.m_Name;
	cout << "请输入性别:(1男 2女)" << endl;
	cin >> person.m_Sex;
	cout << "请输入年龄:" << endl;
	cin >> person.m_Age;
	cout << "请输入电话:" << endl;
	cin >> person.m_Phone;
	cout << "请输入住址:" << endl;
	cin >> person.m_Addr;
	personArray.push_back(person);
	cout << "联系人添加成功!" << endl;
}

// 显示联系人
void Addressbooks::showPerson()
{
	if (personArray.empty())
	{
		cout << "通讯录为空!" << endl;
		return;
	}
	for (const auto& person : personArray)
	{
		cout << "姓名:" << person.m_Name << "\t";
		cout << "性别:" << (person.m_Sex == 1 ? "男" : "女") << "\t";
		cout << "年龄:" << person.m_Age << "\t";
		cout << "电话:" << person.m_Phone << "\t";
		cout << "住址:" << person.m_Addr << endl;
	}
}

// 删除联系人
void Addressbooks::deletePerson()
{
	if (personArray.empty())
	{
		cout << "通讯录为空!" << endl;
		return;
	}
	string name;
	cout << "请输入要删除的联系人姓名:" << endl;
	cin >> name;
	for (auto it = personArray.begin(); it != personArray.end(); ++it)
	{
		if (it->m_Name == name)
		{
			personArray.erase(it);
			cout << "联系人删除成功!" << endl;
			return;
		}
	}
	cout << "未找到该联系人!" << endl;
}

// 查找联系人
void Addressbooks::findPerson()
{
	if (personArray.empty())
	{
		cout << "通讯录为空!" << endl;
		return;
	}
	string name;
	cout << "请输入要查找的联系人姓名:" << endl;
	cin >> name;
	for (const auto& person : personArray)
	{
		if (person.m_Name == name)
		{
			cout << "姓名:" << person.m_Name << "\t";
			cout << "性别:" << (person.m_Sex == 1 ? "男" : "女") << "\t";
			cout << "年龄:" << person.m_Age << "\t";
			cout << "电话:" << person.m_Phone << "\t";
			cout << "住址:" << person.m_Addr << endl;
			return;
		}
	}
	cout << "未找到该联系人!" << endl;
}

// 修改联系人
void Addressbooks::modifyPerson()
{
	if (personArray.empty())
	{
		cout << "通讯录为空!" << endl;
		return;
	}
	string name;
	cout << "请输入要修改的联系人姓名:" << endl;
	cin >> name;
	for (auto& person : personArray)
	{
		if (person.m_Name == name)
		{
			cout << "请输入姓名:" << endl;
			cin >> person.m_Name;
			cout << "请输入性别:(1男 2女)" << endl;
			cin >> person.m_Sex;
			cout << "请输入年龄:" << endl;
			cin >> person.m_Age;
			cout << "请输入电话:" << endl;
			cin >> person.m_Phone;
			cout << "请输入住址:" << endl;
			cin >> person.m_Addr;
			cout << "联系人修改成功!" << endl;
			return;
		}
	}
	cout << "未找到该联系人!" << endl;
}

// 清空联系人
void Addressbooks::cleanPerson()
{
	personArray.clear();
	cout << "通讯录已清空!" << endl;
}

int main()
{
	Addressbooks addressbooks;
	int select = 0;
	while (true)
	{
		showMenu();
		cin >> select;
		switch (select)
		{
		case 1: // 添加联系人
			addressbooks.addPerson();
			break;
		case 2: // 显示联系人
			addressbooks.showPerson();
			break;
		case 3: // 删除联系人
			addressbooks.deletePerson();
			break;
		case 4: // 查找联系人
			addressbooks.findPerson();
			break;
		case 5: // 修改联系人
			addressbooks.modifyPerson();
			break;
		case 6: // 清空联系人
			addressbooks.cleanPerson();
			break;
		case 0: // 退出通讯录
			cout << "欢迎下次使用!" << endl;
			return 0;
		default:
			cout << "输入有误,请重新输入!" << endl;
			break;
		}
	}

	return 0;
}

Guess you like

Origin blog.csdn.net/dsafefvf/article/details/131488704