我是靠谱客的博主 怡然吐司,最近开发中收集的这篇文章主要介绍【C++】C++通讯录管理系统前言,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

前言

这是C++基础编程阶段性练习,主要以小项目为载体对于基础部分学过的知识点进行复习巩固,为以后学习核心编程打下基础。

01菜单功能实现

#include <iostream>
using namespace std;

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;
}

int main()
{
    showMenu();

    system("pause");
    return 0;
}

运行结果:

在这里插入图片描述

02退出功能实现

#include <iostream>
using namespace std;

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;
}

int main()
{
    int select;

    while(1){
        showMenu();
        cin >> select;

        switch(select){
            case 1: //添加联系人
                break;
            case 2: //显示联系人
                break;
            case 3: //删除联系人
                break;
            case 4: //查找联系人
                break;
            case 5: //修改联系人
                break;
            case 6: //清空联系人
                break;
            case 0: //退出系统
                cout << "欢迎下次使用!" << endl;
                system("pause");
                return 0;
                break;
    }

    }
    
    system("pause");
    return 0;
}

运行结果:

在这里插入图片描述

03添加联系人_结构体设计

#include <iostream>
#include <string>
using namespace std;
#define MAX 1000

struct  Person
{
    string m_Name;
    int m_Sex;
    int m_Age;
    string m_Phone;
    string m_Addr;
};

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;
}

int main()
{
    int select;

    while(1){
        showMenu();
        cin >> select;

        switch(select){
            case 1: //添加联系人
                break;
            case 2: //显示联系人
                break;
            case 3: //删除联系人
                break;
            case 4: //查找联系人
                break;
            case 5: //修改联系人
                break;
            case 6: //清空联系人
                break;
            case 0: //退出系统
                cout << "欢迎下次使用!" << endl;
                system("pause");
                return 0;
                break;
    }

    }
    
    system("pause");
    return 0;
}

04添加联系人_功能实现

#include <iostream>
#include <string>
using namespace std;
#define MAX 1000

struct  Person
{
    string m_Name;
    int m_Sex;
    int m_Age;
    string m_Phone;
    string m_Addr;
};

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 addPersons(Addressbooks * ads)
{
    if(ads->m_Size == MAX){
        cout << "通讯录已满" << endl;
        return;
    }else
    {
        string name;
        cout << "请输入添加人的姓名:" << endl;
        cin >> name;
        ads->personArray[ads->m_Size].m_Name = name;

        int Sex;
        cout << "请输入添加人的性别:" << endl;
        while(true){
            cout << "0-男" << endl;
            cout << "1-女" << endl;
            cin >> Sex;
            if(Sex == 1 || Sex == 0){
                ads->personArray[ads->m_Size].m_Sex = Sex;
                break;
            }
            cout << "年龄输入有误,请重新输入" << endl;
        }

        int age;
        cout << "请输入添加人的年龄:" << endl;
        cin >> age;
        ads->personArray[ads->m_Size].m_Age = age;

        string phone;
        cout << "请输入添加人的电话:" << endl;
        cin >> phone;
        ads->personArray[ads->m_Size].m_Phone = phone;

        string address;
        cout << "请输入添加人的地址:" << endl;
        cin >> address;
        ads->personArray[ads->m_Size].m_Addr = address;

        ads->m_Size++;

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

    }
}

int main()
{
    int select;

    Addressbooks ads;
    ads.m_Size = 0;

    while(1){
        showMenu();
        cin >> select;

        switch(select){
            case 1: //添加联系人
                addPersons(&ads);
                break;
            case 2: //显示联系人
                break;
            case 3: //删除联系人
                break;
            case 4: //查找联系人
                break;
            case 5: //修改联系人
                break;
            case 6: //清空联系人
                break;
            case 0: //退出系统
                cout << "欢迎下次使用!" << endl;
                system("pause");
                return 0;
                break;
    }

    }
    
    system("pause");
    return 0;
}

运行结果:

在这里插入图片描述

05显示联系人

#include <iostream>
#include <string>
using namespace std;
#define MAX 1000

struct  Person
{
    string m_Name;
    int m_Sex;
    int m_Age;
    string m_Phone;
    string m_Addr;
};

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 addPersons(Addressbooks * ads)
{
    if(ads->m_Size == MAX){
        cout << "通讯录已满" << endl;
        return;
    }else
    {
        string name;
        cout << "请输入添加人的姓名:" << endl;
        cin >> name;
        ads->personArray[ads->m_Size].m_Name = name;

        int Sex;
        cout << "请输入添加人的性别:" << endl;
        while(true){
            cout << "0-女" << endl;
            cout << "1-男" << endl;
            cin >> Sex;
            if(Sex == 1 || Sex == 0){
                ads->personArray[ads->m_Size].m_Sex = Sex;
                break;
            }
            cout << "年龄输入有误,请重新输入" << endl;
        }

        int age;
        cout << "请输入添加人的年龄:" << endl;
        cin >> age;
        ads->personArray[ads->m_Size].m_Age = age;

        string phone;
        cout << "请输入添加人的电话:" << endl;
        cin >> phone;
        ads->personArray[ads->m_Size].m_Phone = phone;

        string address;
        cout << "请输入添加人的地址:" << endl;
        cin >> address;
        ads->personArray[ads->m_Size].m_Addr = address;

        ads->m_Size++;

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

    }
}

void showPersons(Addressbooks * ads)
{
    if(ads->m_Size == 0){
        cout << "通讯录为空!" << endl;
        system("pause");
        system("cls");
        return;
    }

    for(int i = 0;i < ads->m_Size;i++){
        cout << "姓名:" << ads->personArray[i].m_Name << "t";
        cout << "性别:" << (ads->personArray[i].m_Sex == 1?"男":"女") << "t";
        cout << "年龄:" << ads->personArray[i].m_Age << "t";
        cout << "电话:" << ads->personArray[i].m_Phone << "t";
        cout << "地址:" << ads->personArray[i].m_Addr << endl;
    }

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

int main()
{
    int select;

    Addressbooks ads;
    ads.m_Size = 0;

    while(1){
        showMenu();
        cout << "请输入您要使用的功能:" << endl;
        cin >> select;
        switch(select){
            case 1: //添加联系人
                addPersons(&ads);
                break;
            case 2: //显示联系人
                showPersons(&ads);
                break;
            case 3: //删除联系人
                break;
            case 4: //查找联系人
                break;
            case 5: //修改联系人
                break;
            case 6: //清空联系人
                break;
            case 0: //退出系统
                cout << "欢迎下次使用!" << endl;
                system("pause");
                return 0;
                break;
        }
    }

    system("pause");
    return 0;
}

运行结果:

在这里插入图片描述

06删除联系人_查找功能的实现

查找代码:

#include <iostream>
#include <string>
using namespace std;
#define MAX 1000

struct  Person
{
    string m_Name;
    int m_Sex;
    int m_Age;
    string m_Phone;
    string m_Addr;
};

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 addPersons(Addressbooks * ads)
{
    if(ads->m_Size == MAX){
        cout << "通讯录已满" << endl;
        return;
    }else
    {
        string name;
        cout << "请输入添加人的姓名:" << endl;
        cin >> name;
        ads->personArray[ads->m_Size].m_Name = name;

        int Sex;
        cout << "请输入添加人的性别:" << endl;
        while(true){
            cout << "0-女" << endl;
            cout << "1-男" << endl;
            cin >> Sex;
            if(Sex == 1 || Sex == 0){
                ads->personArray[ads->m_Size].m_Sex = Sex;
                break;
            }
            cout << "年龄输入有误,请重新输入" << endl;
        }

        int age;
        cout << "请输入添加人的年龄:" << endl;
        cin >> age;
        ads->personArray[ads->m_Size].m_Age = age;

        string phone;
        cout << "请输入添加人的电话:" << endl;
        cin >> phone;
        ads->personArray[ads->m_Size].m_Phone = phone;

        string address;
        cout << "请输入添加人的地址:" << endl;
        cin >> address;
        ads->personArray[ads->m_Size].m_Addr = address;

        ads->m_Size++;

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

    }
}

void showPersons(Addressbooks * ads)
{
    if(ads->m_Size == 0){
        cout << "通讯录为空!" << endl;
        system("pause");
        system("cls");
        return;
    }

    for(int i = 0;i < ads->m_Size;i++){
        cout << "姓名:" << ads->personArray[i].m_Name << "t";
        cout << "性别:" << (ads->personArray[i].m_Sex == 1?"男":"女") << "t";
        cout << "年龄:" << ads->personArray[i].m_Age << "t";
        cout << "电话:" << ads->personArray[i].m_Phone << "t";
        cout << "地址:" << ads->personArray[i].m_Addr << endl;
    }

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

int isExisting(Addressbooks * ads,string name)
{
    for(int i = 0;i < ads->m_Size;i++){
        if(ads->personArray[i].m_Name == name){
            return i;
        }
    }
    return -1;
}

int main()
{
    int select;

    Addressbooks ads;
    ads.m_Size = 0;

    while(1){
        showMenu();
        cout << "请输入您要使用的功能:" << endl;
        cin >> select;
        switch(select){
            case 1: //添加联系人
                addPersons(&ads);
                break;
            case 2: //显示联系人
                showPersons(&ads);
                break;
            case 3: //删除联系人
                {
                    string name;
                    cout << "请输入要删除联系人的姓名:" << endl;
                    cin >>  name;
                    if(isExisting(&ads,name) == -1){
                        cout << "查无此人" << endl;
                    }else
                    {
                        cout << "找到了" << endl;
                    }
                }
                system("pause");
                system("cls");
                break;
            case 4: //查找联系人
                break;
            case 5: //修改联系人
                break;
            case 6: //清空联系人
                break;
            case 0: //退出系统
                cout << "欢迎下次使用!" << endl;
                system("pause");
                return 0;
                break;
        }
    }

    system("pause");
    return 0;
}

运行结果:

在这里插入图片描述

07删除联系人

删除实现代码:

#include <iostream>
#include <string>
using namespace std;
#define MAX 1000

struct  Person
{
    string m_Name;
    int m_Sex;
    int m_Age;
    string m_Phone;
    string m_Addr;
};

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 addPersons(Addressbooks * ads)
{
    if(ads->m_Size == MAX){
        cout << "通讯录已满" << endl;
        return;
    }else
    {
        string name;
        cout << "请输入添加人的姓名:" << endl;
        cin >> name;
        ads->personArray[ads->m_Size].m_Name = name;

        int Sex;
        cout << "请输入添加人的性别:" << endl;
        while(true){
            cout << "0-女" << endl;
            cout << "1-男" << endl;
            cin >> Sex;
            if(Sex == 1 || Sex == 0){
                ads->personArray[ads->m_Size].m_Sex = Sex;
                break;
            }
            cout << "年龄输入有误,请重新输入" << endl;
        }

        int age;
        cout << "请输入添加人的年龄:" << endl;
        cin >> age;
        ads->personArray[ads->m_Size].m_Age = age;

        string phone;
        cout << "请输入添加人的电话:" << endl;
        cin >> phone;
        ads->personArray[ads->m_Size].m_Phone = phone;

        string address;
        cout << "请输入添加人的地址:" << endl;
        cin >> address;
        ads->personArray[ads->m_Size].m_Addr = address;

        ads->m_Size++;

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

    }
}

void showPersons(Addressbooks * ads)
{
    if(ads->m_Size == 0){
        cout << "通讯录为空!" << endl;
        system("pause");
        system("cls");
        return;
    }

    for(int i = 0;i < ads->m_Size;i++){
        cout << "姓名:" << ads->personArray[i].m_Name << "t";
        cout << "性别:" << (ads->personArray[i].m_Sex == 1?"男":"女") << "t";
        cout << "年龄:" << ads->personArray[i].m_Age << "t";
        cout << "电话:" << ads->personArray[i].m_Phone << "t";
        cout << "地址:" << ads->personArray[i].m_Addr << endl;
    }

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

int isExisting(Addressbooks * ads,string name)
{
    for(int i = 0;i < ads->m_Size;i++){
        if(ads->personArray[i].m_Name == name){
            return i;
        }
    }
    return -1;
}

void isDelete(Addressbooks * ads)
{
    string name;
    cout << "请输入要删除联系人的姓名:" << endl;
    cin >>  name;
    int res = isExisting(ads,name);
    if(res != -1){
        for(int i = res;i < ads->m_Size;i++){
            ads->personArray[i] = ads->personArray[i+1];
            ads->m_Size--;
        }
        cout << "删除成功" << endl;
    }else
    {
        cout << "查无此人" << endl;
    }

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

int main()
{
    int select;

    Addressbooks ads;
    ads.m_Size = 0;

    while(1){
        showMenu();
        cout << "请输入您要使用的功能:" << endl;
        cin >> select;
        switch(select){
            case 1: //添加联系人
                addPersons(&ads);
                break;
            case 2: //显示联系人
                showPersons(&ads);
                break;
            case 3: //删除联系人
                isDelete(&ads);
                break;
            case 4: //查找联系人
                break;
            case 5: //修改联系人
                break;
            case 6: //清空联系人
                break;
            case 0: //退出系统
                cout << "欢迎下次使用!" << endl;
                system("pause");
                return 0;
                break;
        }
    }

    system("pause");
    return 0;
}

运行结果:

在这里插入图片描述

08查找联系人

#include <iostream>
#include <string>
using namespace std;
#define MAX 1000

struct  Person
{
    string m_Name;
    int m_Sex;
    int m_Age;
    string m_Phone;
    string m_Addr;
};

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 addPersons(Addressbooks * ads)
{
    if(ads->m_Size == MAX){
        cout << "通讯录已满" << endl;
        return;
    }else
    {
        string name;
        cout << "请输入添加人的姓名:" << endl;
        cin >> name;
        ads->personArray[ads->m_Size].m_Name = name;

        int Sex;
        cout << "请输入添加人的性别:" << endl;
        while(true){
            cout << "0-女" << endl;
            cout << "1-男" << endl;
            cin >> Sex;
            if(Sex == 1 || Sex == 0){
                ads->personArray[ads->m_Size].m_Sex = Sex;
                break;
            }
            cout << "年龄输入有误,请重新输入" << endl;
        }

        int age;
        cout << "请输入添加人的年龄:" << endl;
        cin >> age;
        ads->personArray[ads->m_Size].m_Age = age;

        string phone;
        cout << "请输入添加人的电话:" << endl;
        cin >> phone;
        ads->personArray[ads->m_Size].m_Phone = phone;

        string address;
        cout << "请输入添加人的地址:" << endl;
        cin >> address;
        ads->personArray[ads->m_Size].m_Addr = address;

        ads->m_Size++;

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

    }
}

void showPersons(Addressbooks * ads)
{
    if(ads->m_Size == 0){
        cout << "通讯录为空!" << endl;
        system("pause");
        system("cls");
        return;
    }

    for(int i = 0;i < ads->m_Size;i++){
        cout << "姓名:" << ads->personArray[i].m_Name << "t";
        cout << "性别:" << (ads->personArray[i].m_Sex == 1?"男":"女") << "t";
        cout << "年龄:" << ads->personArray[i].m_Age << "t";
        cout << "电话:" << ads->personArray[i].m_Phone << "t";
        cout << "地址:" << ads->personArray[i].m_Addr << endl;
    }

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

int isExisting(Addressbooks * ads,string name)
{
    for(int i = 0;i < ads->m_Size;i++){
        if(ads->personArray[i].m_Name == name){
            return i;
        }
    }
    return -1;
}

void isDelete(Addressbooks * ads)
{
    string name;
    cout << "请输入要删除联系人的姓名:" << endl;
    cin >>  name;
    int res = isExisting(ads,name);
    if(res != -1){
        for(int i = res;i < ads->m_Size;i++){
            ads->personArray[i] = ads->personArray[i+1];
            ads->m_Size--;
        }
        cout << "删除成功" << endl;
    }else
    {
        cout << "查无此人" << endl;
    }

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

void findPerson(Addressbooks * ads)
{
    string name;
    cout << "请输入要查找联系人的姓名:" << endl;
    cin >>  name;
    int res = isExisting(ads,name);
    if(res != -1){
        cout << "姓名:" << ads->personArray[res].m_Name << "t";
        cout << "性别:" << (ads->personArray[res].m_Sex == 1?"男":"女") << "t";
        cout << "年龄:" << ads->personArray[res].m_Age << "t";
        cout << "电话:" << ads->personArray[res].m_Phone << "t";
        cout << "地址:" << ads->personArray[res].m_Addr << endl;
    }else
    {
        cout << "查无此人" << endl;
    }

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

int main()
{
    int select;

    Addressbooks ads;
    ads.m_Size = 0;

    while(1){
        showMenu();
        cout << "请输入您要使用的功能:" << endl;
        cin >> select;
        switch(select){
            case 1: //添加联系人
                addPersons(&ads);
                break;
            case 2: //显示联系人
                showPersons(&ads);
                break;
            case 3: //删除联系人
                isDelete(&ads);
                break;
            case 4: //查找联系人
                findPerson(&ads);
                break;
            case 5: //修改联系人
                break;
            case 6: //清空联系人
                break;
            case 0: //退出系统
                cout << "欢迎下次使用!" << endl;
                system("pause");
                return 0;
                break;
        }
    }

    system("pause");
    return 0;
}

运行结果:

在这里插入图片描述

09修改联系人

#include <iostream>
#include <string>
using namespace std;
#define MAX 1000

struct  Person
{
    string m_Name;
    int m_Sex;
    int m_Age;
    string m_Phone;
    string m_Addr;
};

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 addPersons(Addressbooks * ads)
{
    if(ads->m_Size == MAX){
        cout << "通讯录已满" << endl;
        return;
    }else
    {
        string name;
        cout << "请输入添加人的姓名:" << endl;
        cin >> name;
        ads->personArray[ads->m_Size].m_Name = name;

        int Sex;
        cout << "请输入添加人的性别:" << endl;
        while(true){
            cout << "0-女" << endl;
            cout << "1-男" << endl;
            cin >> Sex;
            if(Sex == 1 || Sex == 0){
                ads->personArray[ads->m_Size].m_Sex = Sex;
                break;
            }
            cout << "年龄输入有误,请重新输入" << endl;
        }

        int age;
        cout << "请输入添加人的年龄:" << endl;
        cin >> age;
        ads->personArray[ads->m_Size].m_Age = age;

        string phone;
        cout << "请输入添加人的电话:" << endl;
        cin >> phone;
        ads->personArray[ads->m_Size].m_Phone = phone;

        string address;
        cout << "请输入添加人的地址:" << endl;
        cin >> address;
        ads->personArray[ads->m_Size].m_Addr = address;

        ads->m_Size++;

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

    }
}

void showPersons(Addressbooks * ads)
{
    if(ads->m_Size == 0){
        cout << "通讯录为空!" << endl;
        system("pause");
        system("cls");
        return;
    }

    for(int i = 0;i < ads->m_Size;i++){
        cout << "姓名:" << ads->personArray[i].m_Name << "t";
        cout << "性别:" << (ads->personArray[i].m_Sex == 1?"男":"女") << "t";
        cout << "年龄:" << ads->personArray[i].m_Age << "t";
        cout << "电话:" << ads->personArray[i].m_Phone << "t";
        cout << "地址:" << ads->personArray[i].m_Addr << endl;
    }

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

int isExisting(Addressbooks * ads,string name)
{
    for(int i = 0;i < ads->m_Size;i++){
        if(ads->personArray[i].m_Name == name){
            return i;
        }
    }
    return -1;
}

void isDelete(Addressbooks * ads)
{
    string name;
    cout << "请输入要删除联系人的姓名:" << endl;
    cin >>  name;
    int res = isExisting(ads,name);
    if(res != -1){
        for(int i = res;i < ads->m_Size;i++){
            ads->personArray[i] = ads->personArray[i+1];
            ads->m_Size--;
        }
        cout << "删除成功" << endl;
    }else
    {
        cout << "查无此人" << endl;
    }

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

void findPerson(Addressbooks * ads)
{
    string name;
    cout << "请输入要查找联系人的姓名:" << endl;
    cin >>  name;
    int res = isExisting(ads,name);
    if(res != -1){
        cout << "姓名:" << ads->personArray[res].m_Name << "t";
        cout << "性别:" << (ads->personArray[res].m_Sex == 1?"男":"女") << "t";
        cout << "年龄:" << ads->personArray[res].m_Age << "t";
        cout << "电话:" << ads->personArray[res].m_Phone << "t";
        cout << "地址:" << ads->personArray[res].m_Addr << endl;
    }else
    {
        cout << "查无此人" << endl;
    }

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

void modifyPerson(Addressbooks * ads)
{
    string name1;
    cout << "请输入要修改联系人的姓名:" << endl;
    cin >>  name1;
    int res = isExisting(ads,name1);
    if(res != -1){
        string name;
        cout << "请输入姓名:" << endl;
        cin >> name;
        ads->personArray[res].m_Name = name;

        int Sex;
        cout << "请输入性别:" << endl;
        while(true){
            cout << "0-女" << endl;
            cout << "1-男" << endl;
            cin >> Sex;
            if(Sex == 1 || Sex == 0){
                ads->personArray[res].m_Sex = Sex;
                break;
            }
            cout << "年龄输入有误,请重新输入" << endl;
        }

        int age;
        cout << "请输入年龄:" << endl;
        cin >> age;
        ads->personArray[res].m_Age = age;

        string phone;
        cout << "请输入电话:" << endl;
        cin >> phone;
        ads->personArray[res].m_Phone = phone;

        string address;
        cout << "请输入地址:" << endl;
        cin >> address;
        ads->personArray[res].m_Addr = address;

        cout << "修改成功" << endl;
    }else
    {
        cout << "查无此人" << endl;
    }

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

int main()
{
    int select;

    Addressbooks ads;
    ads.m_Size = 0;

    while(1){
        showMenu();
        cout << "请输入您要使用的功能:" << endl;
        cin >> select;
        switch(select){
            case 1: //添加联系人
                addPersons(&ads);
                break;
            case 2: //显示联系人
                showPersons(&ads);
                break;
            case 3: //删除联系人
                isDelete(&ads);
                break;
            case 4: //查找联系人
                findPerson(&ads);
                break;
            case 5: //修改联系人
                modifyPerson(&ads);
                break;
            case 6: //清空联系人
                break;
            case 0: //退出系统
                cout << "欢迎下次使用!" << endl;
                system("pause");
                return 0;
                break;
        }
    }

    system("pause");
    return 0;
}

运行结果:

在这里插入图片描述

10清空联系人(此为全部代码)

#include <iostream>
#include <string>
using namespace std;
#define MAX 1000

struct  Person
{
    string m_Name;
    int m_Sex;
    int m_Age;
    string m_Phone;
    string m_Addr;
};

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 addPersons(Addressbooks * ads)
{
    if(ads->m_Size == MAX){
        cout << "通讯录已满" << endl;
        return;
    }else
    {
        string name;
        cout << "请输入添加人的姓名:" << endl;
        cin >> name;
        ads->personArray[ads->m_Size].m_Name = name;

        int Sex;
        cout << "请输入添加人的性别:" << endl;
        while(true){
            cout << "0-女" << endl;
            cout << "1-男" << endl;
            cin >> Sex;
            if(Sex == 1 || Sex == 0){
                ads->personArray[ads->m_Size].m_Sex = Sex;
                break;
            }
            cout << "年龄输入有误,请重新输入" << endl;
        }

        int age;
        cout << "请输入添加人的年龄:" << endl;
        cin >> age;
        ads->personArray[ads->m_Size].m_Age = age;

        string phone;
        cout << "请输入添加人的电话:" << endl;
        cin >> phone;
        ads->personArray[ads->m_Size].m_Phone = phone;

        string address;
        cout << "请输入添加人的地址:" << endl;
        cin >> address;
        ads->personArray[ads->m_Size].m_Addr = address;

        ads->m_Size++;

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

    }
}

void showPersons(Addressbooks * ads)
{
    if(ads->m_Size == 0){
        cout << "通讯录为空!" << endl;
        system("pause");
        system("cls");
        return;
    }

    for(int i = 0;i < ads->m_Size;i++){
        cout << "姓名:" << ads->personArray[i].m_Name << "t";
        cout << "性别:" << (ads->personArray[i].m_Sex == 1?"男":"女") << "t";
        cout << "年龄:" << ads->personArray[i].m_Age << "t";
        cout << "电话:" << ads->personArray[i].m_Phone << "t";
        cout << "地址:" << ads->personArray[i].m_Addr << endl;
    }

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

int isExisting(Addressbooks * ads,string name)
{
    for(int i = 0;i < ads->m_Size;i++){
        if(ads->personArray[i].m_Name == name){
            return i;
        }
    }
    return -1;
}

void isDelete(Addressbooks * ads)
{
    string name;
    cout << "请输入要删除联系人的姓名:" << endl;
    cin >>  name;
    int res = isExisting(ads,name);
    if(res != -1){
        for(int i = res;i < ads->m_Size;i++){
            ads->personArray[i] = ads->personArray[i+1];
            ads->m_Size--;
        }
        cout << "删除成功" << endl;
    }else
    {
        cout << "查无此人" << endl;
    }

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

void findPerson(Addressbooks * ads)
{
    string name;
    cout << "请输入要查找联系人的姓名:" << endl;
    cin >>  name;
    int res = isExisting(ads,name);
    if(res != -1){
        cout << "姓名:" << ads->personArray[res].m_Name << "t";
        cout << "性别:" << (ads->personArray[res].m_Sex == 1?"男":"女") << "t";
        cout << "年龄:" << ads->personArray[res].m_Age << "t";
        cout << "电话:" << ads->personArray[res].m_Phone << "t";
        cout << "地址:" << ads->personArray[res].m_Addr << endl;
    }else
    {
        cout << "查无此人" << endl;
    }

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

void modifyPerson(Addressbooks * ads)
{
    string name1;
    cout << "请输入要修改联系人的姓名:" << endl;
    cin >>  name1;
    int res = isExisting(ads,name1);
    if(res != -1){
        string name;
        cout << "请输入姓名:" << endl;
        cin >> name;
        ads->personArray[res].m_Name = name;

        int Sex;
        cout << "请输入性别:" << endl;
        while(true){
            cout << "0-女" << endl;
            cout << "1-男" << endl;
            cin >> Sex;
            if(Sex == 1 || Sex == 0){
                ads->personArray[res].m_Sex = Sex;
                break;
            }
            cout << "年龄输入有误,请重新输入" << endl;
        }

        int age;
        cout << "请输入年龄:" << endl;
        cin >> age;
        ads->personArray[res].m_Age = age;

        string phone;
        cout << "请输入电话:" << endl;
        cin >> phone;
        ads->personArray[res].m_Phone = phone;

        string address;
        cout << "请输入地址:" << endl;
        cin >> address;
        ads->personArray[res].m_Addr = address;

        cout << "修改成功" << endl;
    }else
    {
        cout << "查无此人" << endl;
    }

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

void cleanPerson(Addressbooks * abs)
{
    abs->m_Size = 0;
    cout << "通讯录已清空" << endl;
    system("pause");
    system("cls");
}

int main()
{
    int select;

    Addressbooks ads;
    ads.m_Size = 0;

    while(1){
        showMenu();
        cout << "请输入您要使用的功能:" << endl;
        cin >> select;
        switch(select){
            case 1: //添加联系人
                addPersons(&ads);
                break;
            case 2: //显示联系人
                showPersons(&ads);
                break;
            case 3: //删除联系人
                isDelete(&ads);
                break;
            case 4: //查找联系人
                findPerson(&ads);
                break;
            case 5: //修改联系人
                modifyPerson(&ads);
                break;
            case 6: //清空联系人
                cleanPerson(&ads);
                break;
            case 0: //退出系统
                cout << "欢迎下次使用!" << endl;
                system("pause");
                return 0;
                break;
        }
    }

    system("pause");
    return 0;
}

运行结果:

在这里插入图片描述

最后

以上就是怡然吐司为你收集整理的【C++】C++通讯录管理系统前言的全部内容,希望文章能够帮你解决【C++】C++通讯录管理系统前言所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(47)

评论列表共有 0 条评论

立即
投稿
返回
顶部