Two hours to achieve a simple C++ address book management system

table of Contents

Prospect summary:

Overall function:

One, to achieve the overall architecture:

Second, add contacts

Three, display contacts

Fourth, delete contacts

Five, modify the contact

Six, clear contacts

Seven, exit the address book

All codes


Prospect summary:

First, you need to understand the overall structure and requirements of the project, and then disassemble it step by step for writing in blocks.

This small project is to "implement a simple address book management system". Pay attention to the simple two words, which is the simplest: add, delete, modify, and check.

So don't learn how to deal with the problem piece by piece, and realize the requirements piece by piece, but the gain will not be worth the loss.

Another prerequisite is the need to establish a good code format and other issues: the person's information here is the structure used, and then stored in an array, as follows:

That is, there is one person for each number in the array, and the maximum is 1000 people.

//Contact structure
struct Person
{     string m_name; //Name     int m_Sex; //Gender: 1, male; 2, female     int m_Age; //Age     string m_Phone; //Phone     string m_Addr; //Address }; # define MAX 1000 //Maximum number of people






//Addressbook structure
struct Addressbooks
{     struct Person personArray[MAX]; //Maximum number of people saved in the address book     int m_size; // Number of people in the address book };


The overall preparation is as follows:

Overall function:

  1. add contact
  2. Show contacts
  3. Delete contact
  4. Find a contact
  5. Edit contact
  6. Clear contacts
  7. Sign out of address book

One, to achieve the overall architecture:

In order to make the overall architecture clear, we must learn to write custom functions:

           That is to build the overall structure in the main function, and then introduce each part through the function call method, so that the overall structure is clear and simple.

Here is the method of while + switch :

  • Switch function, jump to the corresponding custom function by judging the input command.
  • The function of while can be used repeatedly through looping.

The code in the figure below shows that the menu table is displayed first, and then input 1 to jump to the function of addPerson to realize the function of adding contacts; input 2 to jump to the part of displaying contacts.

    while (true)
    {         //The menu calls         showMenu();

        cin >> select;

        switch (select)
        {         case 1: // 1, add a contact             addPerson(&abs);             break;


        case 2: // 2, display the contact
            showPerson(&abs);
            break;
        case 3: // 3, delete the contact
            deletePreson(&abs);
            break;
        case 4: // 4, find the contact
            findPerson(&abs);
            break ;
        case 5: // 5, modify the contact
            modifyPerson(&abs);
            break;
        case 6: // 6, clear the contact
            clearPerson(&abs);
            break;
        case 0: // 0, exit the address book
            cout << "Welcome Next time use "<< endl;
            system("pause");
            return 0;
            break;
        }
    }

Second, add contacts

It can be seen from the main function that the custom function of this part is addPerson.

The add function in this place was originally in addPerson, but in order to make it easier for other functions to use this code in the future, I wrote it as a separate function. Then when used in other places, just call add with value.

//Add information
void add(Addressbooks *abs, int i)
{     //Add specific contact

    //Name
    string name;
    cout << "Please enter your name:" << endl;
    cin >> name;
    abs->personArray[i].m_name = name;

    //Sex
    cout << "Please enter the gender:" << endl;
    cout << "1 --- male" << endl;
    cout << "2 --- female" << endl;
    int sex = 0;

    while (true)
    {         //If you enter 1 or 2, you can exit the loop.         //If you make a mistake, re-enter         cout << "Please enter gender:" << endl;         cin >> sex;         if (sex == 1 || sex == 2)         {             abs->personArray[i].m_Sex = sex;             break;         }         cout << "The input is wrong, please re-enter" << endl;     }










    //Age
    cout << "Please enter age:" << endl;
    int age = 0;
    cin >> age;
    abs->personArray[i].m_Age = age;

    //Phone
    cout << "Please enter the phone number:" << endl;
    string phone;
    cin >> phone;
    abs->personArray[i].m_Phone = phone;

    //Address
    cout << "Please enter your home address:" << endl;
    string address;
    cin >> address;
    abs->personArray[i].m_Addr = address;
}

//1, add a contact
void addPerson(Addressbooks *abs)
{     // Determine whether the address book is full, if it is full, do not add it again     if (abs->m_size == MAX)     {         cout << "The address book is full , Cannot add "<< endl;         return;     }     else     {         add(abs, abs->m_size);         //Update the number of address book         abs->m_size++;









        

        cout << "Added successfully" << endl;

        system("pause"); //Press any key to continue
        system("cls"); //Clear the screen
    }
}

Three, display contacts

To display contacts, just use a for loop, which is relatively simple.

The display in this place is similar to the above. I also wrote the displayed code separately to facilitate the calls below and avoid repeated code writing.

//Display the information of a person
void show(Addressbooks * abs,int i)
{     cout << "Name:" << abs->personArray[i].m_name << "\t";     cout << "Gender:" < <(abs->personArray[i].m_Sex == 1? "Male": "Female") <<'\t';     cout << "Age:" << abs->personArray[i].m_Age << '\t';     cout << "Phone:" << abs->personArray[i].m_Phone <<'\t';     cout << "Address:" << abs->personArray[i].m_Addr << endl; } //2, display contacts void showPerson(Addressbooks * abs) {     // Determine whether the number of people in the address book is 0, if it is 0, the prompt record is 0     //If it is not 0, display the recorded contact information     if (abs->m_size == 0)     {         cout << "The current record is empty" << endl;     }     else     {

















        for (int i = 0; i < abs->m_size; i++)
        {
            show(abs, i);
        }
    }

    system("pause"); //Press any key to continue
    system("cls"); //Clear the screen
}

Fourth, delete contacts

Two steps are required to delete:

  1. The first step is to find the position of the person to be deleted in the array
  2. Delete the position. The number of times is that the array cannot be deleted, so the method of overwriting is adopted, and all the positions behind the deleted position are moved forward one bit, so as to achieve the effect of deletion

//Detect the location of the contact
int isExist(Addressbooks *abs, string name)
{     for (int i = 0; i <abs->m_size; i++)     {         //Find the name entered by the user         if (abs->personArray[i ].m_name == name)         {             return i; //If found, return the subscript of this person         }     }     return -1; //If not found, return -1 }









//3, delete someone
void deletePreson(Addressbooks *abs)
{     cout << "Please enter the name of the deleted contact:" << endl;     string name;     cin >> name;     int returnset = isExist(abs, name);     if (returnset == -1)     {         cout << " Check for this person" << endl;     }     else     {         //If this person is found, data must be deleted         for (int i = returnset; i <abs->m_size; i++)         {             //Data move forward             abs->personArray[i] = abs->personArray[i + 1];         }         abs->m_size--;         cout << "Delete successfully" << endl;     }


















    system("pause");
    system("cls");
}
 

Five, modify the contact

To modify, two steps are required:

  1. The first step is to find the subscript position of the person you need to find in the array from all the information.
  2. Then call the display function I separated in the above step to output (this place, the benefits of writing separately are displayed)
  3. The search position and display of this place are the functions before the call, so it is necessary to write the function separately

//4, find the information of the specified contact
void findPerson(Addressbooks *abs)
{     cout << "Please enter the contact you want to find:" << endl;     string name;     cin >> name;


    //Determine whether the contact exists in the address book
    int ret = isExist(abs, name);

    if (ret != -1)
    {
        show(abs, ret);
    }
    else
    {
        cout << "查无此人" << endl;
    }

    system("pause");
    system("cls");
}

Six, clear contacts

This place only needs to clear the number of people in the list.

//6, clear contacts
void clearPerson(Addressbooks *abs)
{     abs->m_size = 0;     cout << "The address book has been emptied" << endl;     system("pause");     system("cls"); }




Seven, exit the address book

That is, when 0 is input, the main function exits the while loop, and then the entire program exits.

All codes

#include<iostream>
#include<string>
using namespace std;

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

#define MAX 1000  //最大人数
//通讯录结构体
struct Addressbooks
{
	struct Person personArray[MAX];   //通讯录中保存的最大人数
	int m_size;   //通讯录中人员个数
};

//菜单界面
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 add(Addressbooks *abs, int i)
{
	//添加具体联系人

	//姓名
	string name;
	cout << "请输入姓名:" << endl;
	cin >> name;
	abs->personArray[i].m_name = name;

	//性别
	cout << "请输入性别:" << endl;
	cout << "1 --- 男" << endl;
	cout << "2 --- 女" << endl;
	int sex = 0;

	while (true)
	{
		//如果输入的是1或者2可以退出循环,
		//如果输入有误,重新输入
		cout << "请输入性别:" << endl;
		cin >> sex;
		if (sex == 1 || sex == 2)
		{
			abs->personArray[i].m_Sex = sex;
			break;
		}
		cout << "输入有误,请重新输入" << endl;
	}

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

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

	//住址
	cout << "请输入家庭住址:" << endl;
	string address;
	cin >> address;
	abs->personArray[i].m_Addr = address;
}

//1,添加联系人
void addPerson(Addressbooks *abs)
{
	//判断通讯录是否已满,如果满了就不再添加
	if (abs->m_size == MAX)
	{
		cout << "通讯录已满,无法添加" << endl;
		return;
	}
	else
	{
		add(abs, abs->m_size);
		
		//更新通讯录人数
		abs->m_size++;

		cout << "添加成功" << endl;

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

//显示某个人信息
void show(Addressbooks * abs,int i)
{
	cout << "姓名:" << abs->personArray[i].m_name << "\t";
	cout << "性别:" << (abs->personArray[i].m_Sex == 1 ? "男" : "女") << '\t';
	cout << "年龄:" << abs->personArray[i].m_Age << '\t';
	cout << "电话:" << abs->personArray[i].m_Phone << '\t';
	cout << "住址:" << abs->personArray[i].m_Addr << endl;
}
//2,显示联系人
void showPerson(Addressbooks * abs)
{
	//判断通讯录中人数是否为0,如果为0,提示记录为0
	//如果不为0,显示记录的联系人信息
	if (abs->m_size == 0)
	{
		cout << "当前记录为空" << endl;
	}
	else
	{
		for (int i = 0; i < abs->m_size; i++)
		{
			show(abs, i);
		}
	}

	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;   //没有找到,则返回-1
}

//3,删除某个人
void deletePreson(Addressbooks *abs)
{
	cout << "请输入删除联系人的姓名:" << endl;
	string name;
	cin >> name;
	int returnset = isExist(abs, name);
	if (returnset == -1)
	{
		cout << "查无此人" << endl;
	}
	else
	{
		//查到此人,要进行数据删除
		for (int i = returnset; i < abs->m_size; i++)
		{
			//数据前移
			abs->personArray[i] = abs->personArray[i + 1];
		}
		abs->m_size--;
		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)
	{
		show(abs, ret);
	}
	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)  //找到指定联系人
	{
		add(abs, ret);
	}
	else
	{
		cout << "查无此人" << endl;
	}
	system("pause");
	system("cls");
}

//6,清空联系人
void clearPerson(Addressbooks *abs)
{
	abs->m_size = 0;
	cout << "通讯录已清空" << endl;
	system("pause");
	system("cls");
}
int main()
{
	Addressbooks abs;  //创建通讯录结构体变量
	abs.m_size = 0;   //通讯录中人数初始化

	int select = 0;   //创建用户选择输入的变量
	
	while (true)
	{
		//菜单调用
		showMenu();

		cin >> select;

		switch (select)
		{
		case 1:  //  1,添加联系人
			addPerson(&abs);
			break;

		case 2:  //  2,显示联系人
			showPerson(&abs);
			break;
		case 3:  //  3,删除联系人
			deletePreson(&abs);
			break;
		case 4:  //  4,查找联系人
			findPerson(&abs);
			break;
		case 5:  //  5,修改联系人
			modifyPerson(&abs);
			break;
		case 6:  //  6,清空联系人
			clearPerson(&abs);
			break;
		case 0:  //  0,退出通讯录
			cout << "欢迎下次使用" << endl;
			system("pause");
			return 0;
			break;
		}
	}

	system("pause");
	return 0;
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_46423166/article/details/112124812