[c/c++] Classic programming assignment: address book management system

#1. Program introduction

***This program is C++. If you need the C version, you can check out my next article for the complete code. ***

Writing a small program that can add, modify, delete, search, and traverse contact identity information (including age, gender, mobile phone number, etc.) has become a final assignment for many schools. Here is my code idea, and Provide code source.

#2. Programming ideas

First of all, we must make it clear that the main body of the program is contacts, so we need to create an address book class. The address book includes all contacts. A structure nested structure is used here.

If so, consider the functions you want to implement, such as adding, viewing, modifying, searching, deleting, and clearing contacts. Write functions one by one to complete the corresponding functions. (Before this, the main function framework must be completed first)

Finally, modify the program details through debugging.

#3. Code source sharing + code analysis

const int my_max = 1000;
struct Contact_person
{
    string name;
    string gender;
    int age;
    string phone_number;
    string address;
};

struct Communicator
{
    struct Contact_person peopleArray[my_max];
    int people_number = 0;
}num;

·First create a person class, including attributes such as age, and then create an address book class to record the number of people stored (the array memory is limited and cannot be added infinitely).

void Main_menu()
{
    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;
}
int main()
{
    int choose = 0;
    num.people_number = 0; //初始化人数
    while (1)
    {
        Main_menu();
        cin >> choose;
        switch (choose)
        {
        case 1:
            addperson(&num);
            break;
        case 2:
            showpeople(&num);
            break;
        case 3:
            changepeople(&num);
            break;
        case 4:
            queryperson(&num);
            break;
        case 5:
            deleteperson(&num);
            break;
        case 6:
            clearpeople(&num);
            break;
        case 0:
            cout << "欢迎下次使用" << endl;
            
            return 0;
            break;
        default:
            cout << "输入有误" << endl;
            system("pause");
            system("cls");
            break;
        }
    }
    
    system("pause");
    return 0;
}

·The main framework is built here, remember to initialize the number of people outside the main function loop.

void addperson(Communicator *num)
{
    if (num->people_number >= 1000)
    {
        cout << "人数已满" << endl;
    }
    else
    {
        string name;
        string sex;
        int age = 0;
        string phone;
        string adds;

        //1、输入姓名
        cout << "请输入联系人姓名" << endl;
        cin >> name;
        num->peopleArray[num->people_number].name = name;
        //2、输入性别
        cout << "请输入联系人性别" << endl;
        cout << "1、男" << endl;
        cout << "2、女" << endl;
        while (1)
        {
            cin >> sex;
            if (sex == "1")
            {
                num->peopleArray[num->people_number].gender = "男";
                break;
            }
            if (sex == "2")
            {
                num->peopleArray[num->people_number].gender = "女";
                break;
            }
            else
            {
                cout << "输入有误" << endl;
                continue;
            }
        }
        //3、输入年龄
        cout << "请输入联系人年龄" << endl;
        cin >> age;
        num->peopleArray[num->people_number].age = age;
        //4、输入号码
        cout << "请输入联系人电话号码" << endl;
        cin >> phone;
        num->peopleArray[num->people_number].phone_number = phone;
        //5、输入地址
        cout << "请输入联系人住址" << endl;
        cin >> adds;
        num->peopleArray[num->people_number].address = adds;

        //记录人数
        num->people_number++;

        cout << "添加成功" << endl;
    }
    system("pause");
    system("cls");
}

·The first function is implemented here, adding contacts. Please pay attention to details: use address to pass parameters, record the number of people, and use system("cls") to clear the screen.

void showpeople(Communicator* num)
{
    if(!num->people_number)
    {
        cout << "暂无联系人" << endl;
    }
    else
    {
        for (int i = 0; i < num->people_number; i++)
        {
            cout << "姓名是: " << num->peopleArray[i].name << "\t";
            cout << "性别是: " << num->peopleArray[i].gender << "\t";
            cout << "年龄是: " << num->peopleArray[i].age << "\t";
            cout << "号码是: " << num->peopleArray[i].phone_number << "\t";
            cout << "住址是: " << num->peopleArray[i].address << endl;
        }
    }
    system("pause");
    system("cls");
}

·Here is the second function, iterating through all contacts. Note: Judge if no one outputs "No one can check yet"

int findpeople(Communicator * num , string name)
{
    for (int i = 0; i < num->people_number; i++)
    {
        if (num->peopleArray[i].name == name)
        {
            return i;
        }
        return -1;
    }
}

*This function is the central function of the following functions. You can check whether the name of the person you entered is in the address book. If it exists, the location will be returned. If it does not exist, a -1 prompt will be returned.

void deleteperson(Communicator * abs)
{
    string name;
    if(abs->people_number == 0)
    {
        cout << "暂无人可删" << endl;
    }
    else
    {
        cout << "请输入要删除的联系人姓名" << endl;
        cin >> name;
        int a = findpeople(abs, name);
        if (a == -1)
        {
            cout << "查无此人" << endl;
        }
        else
        {
            for (int i = a; i < abs->people_number; i++)
            {
                abs->peopleArray[i] = abs->peopleArray[i + 1];
            }
            cout << "删除成功" << endl;
            abs->people_number--;
        }
    }
    system("pause");
    system("cls");
}

·Here is the function of deleting contacts. The idea of ​​deletion: overwrite the information of the person to be deleted with the information of the subsequent person (overwriting in sequence), which can be regarded as deletion.

void clearpeople(Communicator* num)
{
    string choose;
    cout << "是否要清除?“是” 或 “否”" << endl;
    cin >> choose;
    while (true)
    {
      if(choose == "是")
      {
          num->people_number = 0;
          cout << "清除完成" << endl;
          break;
      }
      else if (choose == "否")
      {
          cout << "重新选择" << endl;
          break;
      }
      else
      {
          cout << "请按规定输入" << endl;
          continue;
      }
    }
    system("pause");
    system("cls");
}

·Since the codes for searching and modifying contacts are similar, I won’t repeat them here. The clearing function is shown here. In fact, you can directly set the number of contacts to 0 (adding will overwrite it, and searching will also block information outside the range).

#4. Conclusion

This is the entire introduction. If you need complete and directly applicable code, please contact me.

Guess you like

Origin blog.csdn.net/m0_73747975/article/details/128738026