zoukankan      html  css  js  c++  java
  • c++实例之通讯录管理系统之删除联系人功能(四)

    #include<iostream>
    using namespace std;
    constexpr auto MAX = 1000;
    
    //联系人结构体
    struct Person
    {
        string m_name;
        int m_sex;
        int m_age;
        string m_phone;
        string m_address;
    
    };
    //通讯录结构体
    struct AddressBooks
    {
        //联系人数组
        struct Person personArray[MAX];
        //记录联系人个数
        int m_size;
    };
    
    //添加人员
    void addPerson(AddressBooks* abs) {
        //判断通讯录是否已满,已满就不再添加
        if (abs->m_size == MAX) {
            cout << "通讯录已满" << endl;
        }
        else{
            string name;
            int sex;
            int age;
            string phone;
            string address;
            cout << "请输入姓名:";
            cin >> name;
            while (true) {
                cout << "请输入性别(0代表男,1代表女):";
                cin >> sex;
                if (sex == 0 || sex == 1) {
                    break;
                }else{
                    cout << "您输入有误,请重新输入!" << endl;
                }
            }
            cout << "请输入年龄:";
            cin >> age;
            cout << "请输入电话:";
            cin >> phone;
            cout << "请输入地址:";
            cin >> address;
            abs->personArray[abs->m_size].m_name = name;
            abs->personArray[abs->m_size].m_sex = sex;
            abs->personArray[abs->m_size].m_age = age;
            abs->personArray[abs->m_size].m_phone = phone;
            abs->personArray[abs->m_size].m_address = address;
            abs->m_size++;
            cout << "添加成功!" << endl;
            //请按任意键继续
            system("pause");
            //清屏
            system("cls");
        }
    }
    //显示联系人
    void showPerson(AddressBooks* abs) {
        if (abs->m_size == 0) {
            cout << "当前记录为空" << endl;
        }else {
            for (int i = 0; i < abs->m_size; i++) {
                cout << "姓名:" << abs->personArray[i].m_name << "	"
                    << "性别:" << (abs->personArray[i].m_sex == 0 ? "" : "") << "	"
                    << "年龄:" << abs->personArray[i].m_age << "	"
                    << "电话:" << abs->personArray[i].m_phone << "	"
                    << "地址:" << abs->personArray[i].m_address << endl;
            }
        }
        system("pause");
        system("cls");
    }
    //判断联系人是否存在,若存在,则返回所在下标,否则返回-1
    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;
    }
    
    //真正的删除操作
    void del(AddressBooks* abs, int id) {
        for (int i = id; i < abs->m_size; i++) 
        {
            abs->personArray[i] = abs->personArray[i + 1];
        }
        abs->m_size--;
    }
    
    //查找并删除
    void deletePerson(AddressBooks* abs) {
        string name;
        cout << "请输入要删除的名字:";
        cin >> name;
        int tmp;
        tmp = isExist(abs, name);
        if (tmp != -1){
            del(abs, tmp);
            cout << "删除成功"<<endl;
        }else{
            cout << "查无此人"<<endl;
        }
        system("pause");
        system("cls");
    }
    
    //菜单界面
    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() {
        //创建通讯录结构体变量
        AddressBooks abs;
        abs.m_size = 0;
        int select = 0;
        while(true){
            showMenu();
            cout << "请输入相应的选项:" << endl;
            cin >> select;
            switch (select){
            case 1://添加
                addPerson(&abs);
                break;
            case 2://显示
                showPerson(&abs);
                break;
            case 3://删除
                deletePerson(&abs);
                break;
            case 4://修改
                break;
            case 5://查找
                break;
            case 6://清空
                break;
            case 0://退出
                cout << "欢迎下次使用" << endl;
                system("pause");
                return 0;
                break;
            }
        }
    }
  • 相关阅读:
    入栈的方式
    出栈的方式
    入栈的方式
    累加数据段中的前3个字型数据
    累加数据段中的前3个字型数据
    出栈的方式
    入栈的方式
    python中如何清空列表
    python中统计列表元素出现的次数
    python中删除列表元素
  • 原文地址:https://www.cnblogs.com/xiximayou/p/12083674.html
Copyright © 2011-2022 走看看