C++学习日记 通讯录管理

通讯录管理

#include<iostream>
using namespace std;
#include<string.h>
constexpr auto MAX = 1000;

struct person
{
    string name;//名字
    int age{};//年龄
    int sex{};//性别 1男 2女
    string phone;//电话号
    string addr;//地址
};

struct adderessbooks
{
    struct person person_array[MAX];
    int m_size{};
};

void add_person(adderessbooks* abs)
{
    if (abs->m_size == MAX)
    {
        cout << "通讯录已满" << endl;
        return;
    }
    else
    {
        //姓名
        string name;
        cout << "请输入姓名" << endl;
        cin >> name;
        abs->person_array[abs->m_size].name = name;
        //性别
        int sex=0;
        cout << "请输入性别,1为男,2为女" << endl;
        while (true)
        {
            cin >> sex;
            if (sex == 1 || sex == 2)
            {
                abs->person_array[abs->m_size].sex = sex;
                break;
            }
            else
            {
                cout << "输入格式错误,请重新输入" << endl;
            }
        }
        //年龄
        int age=0;
        cout << "请输入年龄" << endl;
        cin >> age;
        abs->person_array[abs->m_size].age = age;
        //电话
        string phone;
        cout << "请输入电话号" << endl;
        cin >> phone;
        abs->person_array[abs->m_size].phone = phone;
        //地址
        string addr;
        cout << "请输入地址" << endl;
        cin >> addr;
        abs->person_array[abs->m_size].addr = addr;
        //更新通讯录人数
        abs->m_size++;

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

void show_person(adderessbooks* abs)
{
    if (abs->m_size == 0)
    {
        cout << "无联系人" << endl;
        system("pause");
        system("cls");
    }
    else
    {
        for (int i = 0; i < abs->m_size; i++)
        {
            cout << "姓名  " << abs->person_array[i].name << "\t";
            cout << "性别 " << (abs->person_array[i].sex == 1 ? "男":"女" )<< "\t";
            cout << "年龄  " << abs->person_array[i].age << "\t";
            cout << "电话号  " << abs->person_array[i].phone << "\t";
            cout << "住址  " << abs->person_array[i].addr << endl;
        }
        system("pause");
        system("cls");
    }
}

int is_exist(adderessbooks* abs, string name)
{
    for (int i = 0; i < abs->m_size; i++)
    {
        if (abs->person_array[i].name == name)
        {
            return i;
        }
    }
    return -1;
}

void delete_person(adderessbooks* abs)
{
    cout << "请输入要删除人姓名" << endl;
    string name;
    cin >> name;
    int num=is_exist(abs, name);
    if (num == -1)
    {
        cout << "查无此人" << endl;
        system("pause");
        system("cls");
    }
    else
    {
        for (int i = num; i < abs->m_size; i++)
        {
            abs->person_array[i] = abs->person_array[i + 1];
        }
        abs->m_size--;
        cout << "删除成功" << endl;
        system("pause");
        system("cls");
    }
}

void find_person(adderessbooks* abs)
{
    cout << "请输入要查找人姓名" << endl;
    string name;
    cin >> name;
    int num = is_exist(abs, name);
    if (num == -1)
    {
        cout << "查无此人" << endl;
        system("pause");
        system("cls");
    }
    else
    {
        cout << "姓名  " << abs->person_array[num].name << "\t";
        cout << " 性别 " << (abs->person_array[num].sex == 1 ? "男" : "女") << "\t";
        cout << " 年龄 " << abs->person_array[num].age << "\t";
        cout << "电话号  " << abs->person_array[num].phone << "\t";
        cout << "住址  " << abs->person_array[num].addr << endl;
        system("pause");
        system("cls");
    }
}

void change_person(adderessbooks* abs)
{
    cout << "请输入要修改人姓名" << endl;
    string name;
    cin >> name;
    int num = is_exist(abs, name);
    if (num == -1)
    {
        cout << "查无此人" << endl;
        system("pause");
        system("cls");
    }
    else
    {
        //姓名
        string name;
        cout << "请输入姓名" << endl;
        cin >> name;
        abs->person_array[num].name = name;
        //性别
        int sex;
        cout << "请输入性别,1为男,2为女" << endl;
        while (true)
        {
            cin >> sex;
            if (sex == 1 || sex == 2)
            {
                abs->person_array[num].sex = sex;
                break;
            }
            else
            {
                cout << "输入格式错误,请重新输入" << endl;
            }
        }
        //年龄
        int age;
        cout << "请输入年龄" << endl;
        cin >> age;
        abs->person_array[num].age = age;
        //电话
        string phone;
        cout << "请输入电话号" << endl;
        cin >> phone;
        abs->person_array[num].phone = phone;
        //地址
        string addr;
        cout << "请输入地址" << endl;
        cin >> addr;
        abs->person_array[num].addr = addr;
        cout << "成功修改" << endl;
        system("pause");
        system("cls");
    }
}

void empty_person(adderessbooks* abs)
{
    abs->m_size = 0;
    cout << "已清空" << endl;
    system("pause");
    system("cls");
}

void show_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()
{
    adderessbooks abs;
    abs.m_size = 0;
    int select_num = 0;
    while (true)
    {
        show_menu();
        cin >> select_num;
        switch (select_num)
        {
        case 1:
            add_person(&abs);
            break;
        case 2:
            show_person(&abs);
            break;
        case 3:
            delete_person(&abs);
            break;
        case 4:
            find_person(&abs);
            break;
        case 5:
            change_person(&abs);
            break;
        case 6:
            empty_person(&abs);
            break;
        case 0: 
            cout << "欢迎下次使用" << endl;
            system("pause");
            return 0;
            break;
        default:
            break;
        }
    }
    system("pause");
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值