Introduction to C++ C++ Realization of Address Book Management System

1. Task description

  Write an address book management system, so that the system has the functions of adding, deleting, modifying, and checking.

2. C++ code implementation

#include<iostream>
#include<string>
#define MAX 10

using namespace std;

struct Person
{
    string m_name;
    int m_age;
    string m_phone;
    string m_address;
};


struct PersonInfo
{
    Person personArray[MAX];
    int size = 0;
};

void show_info()
{
    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_person(PersonInfo * abs)
{
    int index = abs->size;
    cout << "请输入姓名" << endl;
    string name;
    cin >> name;
    abs->personArray[index].m_name = name;

    cout << "请输入年龄" << endl;
    int age;
    cin >> age;
    abs->personArray[index].m_age = age;

    cout << "请输入电话" << endl;
    string phone;
    cin >> phone;
    abs->personArray[index].m_phone = phone;

    cout << "请输入地址" << endl;
    string address;
    cin >> address;
    abs->personArray[index].m_address = address;

    abs->size++;

    cout << "成功添加联系人" << endl;

}


void show_person(PersonInfo * abs)
{
    int size = abs->size;
    for(int i=0; i < size; i++)
    {
        cout << abs->personArray[i].m_name + "\t"
             << abs->personArray[i].m_age << "\t"
             << abs->personArray[i].m_phone + "\t"
             << abs->personArray[i].m_address + "\t"
             << endl;
    }
}


int is_exists(PersonInfo * abs, string name)
{
    int size = abs->size;
    for(int i=0; i<size; i++)
    {
        if(abs->personArray[i].m_name == name)
        {
            return i;
        }
    }
    return -1;
}


void del_person(PersonInfo * abs, int index)
{
    int size = abs->size;
    for(int i=index; i<size-1; i++)
    {
        abs->personArray[i] = abs->personArray[i+1];
    }
    abs->size--;
}


void show_person(PersonInfo * abs, int index)
{
    cout << abs->personArray[index].m_name + "\t"
         << abs->personArray[index].m_age << "\t"
         << abs->personArray[index].m_phone + "\t"
         << abs->personArray[index].m_address + "\t"
         << endl;
}


void modify_person(PersonInfo * abs, int index)
{
    cout << "请输入年龄" << endl;
    int age;
    cin >> age;
    abs->personArray[index].m_age = age;

    cout << "请输入电话" << endl;
    string phone;
    cin >> phone;
    abs->personArray[index].m_phone = phone;

    cout << "请输入地址" << endl;
    string address;
    cin >> address;
    abs->personArray[index].m_address = address;
}


void clear_person(PersonInfo * abs)
{
    abs->size = 0;
}

int main()
{
    PersonInfo abs;

    while(true)
    {
        show_info();
        cout << "请输入选项!";
        int num;
        cin >> num;
        if(num==1)
        {
            cout << "添加联系人" << endl;
            add_person(&abs);
        }
        else if(num==2)
        {
            cout << "显示联系人" << endl;
            show_person(&abs);
        }
        else if(num==3)
        {
            cout << "删除联系人" << endl;
            string name;
            cout << "请输入删除人的名字" << endl;
            cin >> name;
            int det = is_exists(&abs, name);
            if(det != -1)
            {
                del_person(&abs, det);
            }
            else
            {
                cout << "通讯录中没有找到此人" << endl;
            }
        }
        else if(num==4)
        {
            cout << "查找联系人" << endl;
            string name;
            cout << "请输入查找人的名字" << endl;
            cin >> name;
            int det = is_exists(&abs, name);
            if(det != -1)
            {
                show_person(&abs, det);
            }
            else
            {
                cout << "通讯录中没有找到此人" << endl;
            }

        }
        else if(num==5)
        {
            cout << "修改联系人" << endl;
            string name;
            cout << "请输入要修改人的名字" << endl;
            cin >> name;
            int det = is_exists(&abs, name);
            if(det == -1)
            {
                cout << "通讯录中没有找到此人" << endl;
            }
            else
            {
                modify_person(&abs, det);
            }
        }
        else if(num==6)
        {
            cout << "清空联系人" << endl;
            clear_person(&abs);
        }
        else if(num==0)
        {
            cout << "退出通讯录" << endl;
            break;
        }
        else
        {
            cout << "输入错误,请重新输入" << endl;
        }

    }
    return 0;
}

 

Record: for beginners to learn C++.

Guess you like

Origin blog.csdn.net/Guo_Python/article/details/111290037