美文网首页
C++实战案例 -- 通讯录管理系统

C++实战案例 -- 通讯录管理系统

作者: 生信摆渡 | 来源:发表于2021-07-16 22:45 被阅读0次

    来源:黑马程序员匠心之作|C++教程从0到1入门编程,学习编程不再难

    很简单的一个小项目,新手练习还是非常不错滴~

    #include <iostream>
    using namespace std;
    
    /*
    
    系统需求:
    
    通讯录是一个可以记录亲人、好友信息的工具。
    
    系统中需要实现的功能如下:
    
    - 添加联系人:向通讯录中添加新人,信息包括(姓名、性别、年龄、联系电话、家庭住址)最多记录1000人
    - 显示联系人:显示通讯录中所有联系人信息
    - 删除联系人:按照姓名进行删除指定联系人
    - 查找联系人:按照姓名查看指定联系人信息
    - 修改联系人:按照姓名重新修改指定联系人
    - 清空联系人:清空通讯录中所有信息
    - 退出通讯录:退出当前使用的通讯录
    
    */
    
    // 功能菜单
    void showMenu(){
        cout << "******* 通讯录管理系统 *******\n" << endl;
        cout << "\t1. 添加联系人" << endl;
        cout << "\t2. 显示联系人" << endl;
        cout << "\t3. 删除联系人" << endl;
        cout << "\t4. 查找联系人" << endl;
        cout << "\t5. 修改联系人" << endl;
        cout << "\t6. 清空联系人" << endl;
        cout << "\t0. 退出通讯录" << endl;
        cout << "\n你想要: ";
    }
    
    // 定义单个联系人
    struct person
    {
        string name;
        string gender;
        int age;
        string phone;
        string address;
    };
    
    // 定义通讯录
    #define MAX 1000
    struct Addressbooks
    {
        person addressArr[MAX];
        int size;
    };
    
    // 检查名字
    int checkName(Addressbooks * abs, string name)
    {
        for (int i = 0; i < abs->size; i++)
        {
            if (abs->addressArr[i].name == name)
            {
                return(1);
            }
        }
        return(0);
    }
    
    // 功能 1 - 添加联系人
    void addPerson(Addressbooks * abs)
    {
        // 检查通讯录是否已满
        if (abs->size == MAX)
        {
            cout << "抱歉,通讯录已满,无法继续添加联系人!" << endl;
            return;
        }
    
        // 检查待添加联系人是否已存在
        string name;
        cout << "姓名:";
        cin >> name;
        int check = checkName(abs, name);
        if (check)
        {
            cout << endl;
            cout << "抱歉,该联系人已存在,无法添加!\n\n";
    
            system("pause");
            system("cls");
    
            return;
        }
    
        // 姓名
        abs->addressArr[abs->size].name = name;
        cout << endl;
        
        // 性别
        cout << "性别:";
        string gender;
        cin >> gender;
        cout << endl;
        abs->addressArr[abs->size].gender = gender;
    
        // 年龄
        cout << "年龄:";
        int age;
        cin >> age;
        cout << endl;
        abs->addressArr[abs->size].age = age;
        
        // 联系电话
        cout << "联系电话:";
        string phone;
        cin >> phone;
        cout << endl;
        abs->addressArr[abs->size].phone = phone;
    
        // 家庭住址
        cout << "家庭住址:";
        string address;
        cin >> address;
        cout << endl;
        abs->addressArr[abs->size].address = address;
    
        // 信息确认/创建成功提示
        cout << "联系人 " << name << " 创建成功:\n" << endl;
        cout << "\t姓名:" << name << "\t性别:" << gender << "\t年龄:" << age;
        cout << "\t电话:" << phone << "\t地址:" << address << "\n" << endl;
    
        // 通讯录人数加一
        abs->size++;
    
        system("pause");
        system("cls");
    }
    
    // 功能 2 - 打印通讯录
    void printAddressBooks(Addressbooks *abs)
    {
        // 检查通讯录是否为空
        if (!abs->size)
        {
            cout << "通讯录为空!\n" << endl;
    
            system("pause");
            system("cls");
    
            return;
        }
    
        cout << "\n当前通讯录:\n" << endl;
        for (int i = 0; i < abs->size; i++)
        {
            cout << "\t姓名:" << abs->addressArr[i].name;
            cout << "\t性别:" << abs->addressArr[i].gender;
            cout << "\t年龄:" << abs->addressArr[i].age;
            cout << "\t电话:" << abs->addressArr[i].phone;
            cout << "\t地址:" << abs->addressArr[i].address << "\n" << endl;
        }
    
        system("pause");
        system("cls");
    }
    
    // 寻找联系人在通讯录里的位置
    int findIdx(Addressbooks *abs, string name)
    {
        for (int i = 0; i < abs->size; i++)
        {
            if (abs->addressArr[i].name == name)
            {
                return(i);
            }
        }
    }
    
    // 功能 3 - 删除联系人
    void deletePerson(Addressbooks *abs)
    {
        cout << "请输入想要删除的联系人姓名:";
        string name;
        cin >> name;
        cout << "" << endl;
    
        // 检查待删除联系人是否存在
        int check = checkName(abs, name);
        if (!check)
        {
            cout << "通讯录不存在该联系人,无法删除!\n" << endl;
    
            system("pause");
            system("cls");
    
            return;
        }
    
         // 删除指定联系人
        //int idx = findIdx(abs, name);
        //for (int i = idx; i < abs->size - 1; i++)
        //{
        //  abs->addressArr[i] = abs->addressArr[i + 1];
        //}
        //  cout << "联系人 " << abs->addressArr[idx].name << " 已删除!\n" <<endl; 
    
        for (int i = 0; i < abs->size; i++)
        {
            if (abs->addressArr[i].name  == name)
            {
                abs->addressArr[i] = abs->addressArr[i + 1];
                cout << "联系人 " << name << " 已删除!\n" << endl;
            }
    
        }
    
        // 通讯录人数减一
        abs->size--;
    
        system("pause");
        system("cls");
    }
    
    // 功能 4 - 查找联系人
    void findPerson(Addressbooks* abs)
    {
        cout << "请输入想要查找的联系人姓名:";
        string name;
        cin >> name;
        cout << "" << endl;
    
        // 检查待查找联系人是否存在
        int check = checkName(abs, name);
        if (!check)
        {
            cout << "通讯录不存在该联系人!\n" << endl;
    
            system("pause");
            system("cls");
    
            return;
        }
    
        for (int i = 0; i < abs->size; i++)
        {
            if (abs->addressArr[i].name == name)
            {
                person tmp = abs->addressArr[i];
                cout << "\t姓名:" << tmp.name << "\t性别:" << tmp.gender << "\t年龄:" << tmp.age;
                cout << "\t电话:" << tmp.phone << "\t地址:" << tmp.address << "\n" << endl;
            }
        }
    
        system("pause");
        system("cls");
    }
    
    // 功能 5 - 修改联系人
    void modifyPerson(Addressbooks* abs)
    {
        cout << "请输入想要修改的联系人姓名:";
        string name;
        cin >> name;
        cout << "" << endl;
    
        // 检查待修改联系人是否存在
        int check = checkName(abs, name);
        if (!check)
        {
            cout << "通讯录不存在该联系人,无法修改!\n" << endl;
    
            system("pause");
            system("cls");
    
            return;
        }
    
        // 修改联系人
        for (int i = 0; i < abs->size; i++)
        {
            if (abs->addressArr[i].name == name)
            {
                // 姓名
                cout << "姓名:";
                string name;
                cin >> name;
                cout << endl;
                abs->addressArr[i].name = name;
    
                // 性别
                cout << "性别:";
                string gender;
                cin >> gender;
                cout << endl;
                abs->addressArr[i].gender = gender;
    
                // 年龄
                cout << "年龄:";
                int age;
                cin >> age;
                cout << endl;
                abs->addressArr[i].age = age;
    
                // 联系电话
                cout << "联系电话:";
                string phone;
                cin >> phone;
                cout << endl;
                abs->addressArr[i].phone = phone;
    
                // 家庭住址
                cout << "家庭住址:";
                string address;
                cin >> address;
                cout << endl;
                abs->addressArr[i].address = address;
    
                // 信息确认/创建成功提示
                cout << "联系人 " << name << " 修改成功:\n" << endl;
                person tmp = abs->addressArr[i];
                cout << "\t姓名:" << tmp.name << "\t性别:" << tmp.gender << "\t年龄:" << tmp.age;
                cout << "\t电话:" << tmp.phone << "\t地址:" << tmp.address << "\n" << endl;
    
    
            }
        }
    
        system("pause");
        system("cls");
    }
    
    // 功能 6 - 清空联系人
    void cleanPerson(Addressbooks* abs)
    {
        abs->size = 0;
        cout << "通讯录已清空!\n" << endl;
        system("pause");
        system("cls");
    }
    
    // 主程序
    int main()
    {
        //创建通讯录
        Addressbooks abs;
    
        //初始化通讯录中人数
        abs.size = 0;
    
        // 循环工作,使用while循环
        while (1)
        {
            // 功能提示
            showMenu();
            int select;
            cin >> select;
            cout << endl;
    
            // 七种可选功能,使用switch-case结构
            switch (select)
            {
            case 1:
                // cout << "添加联系人: " << endl;
                addPerson(&abs);
                break;
            case 2:
                // cout << "显示联系人: " << endl;
                printAddressBooks(&abs);
                break;
            case 3:
                // cout << "删除联系人: " << endl;
                deletePerson(&abs);
    
                break;
            case 4:
                // cout << "查找联系人: " << endl;
                findPerson(&abs);
                break;
            case 5:
                // cout << "修改联系人: " << endl;
                modifyPerson(&abs);
                break;
            case 6:
                // cout << "清空联系人: " << endl;
                cleanPerson(&abs);
                break;
            case 0:
                cout << "欢迎下次使用! " << endl;
                system("pause");
                return 0;
                break;
            default:
                cout << "请按提示输入!\n" << endl;
                system("pause");
                system("cls");
            }
        }
    
        system("pause");
    
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:C++实战案例 -- 通讯录管理系统

          本文链接:https://www.haomeiwen.com/subject/ivlipltx.html