【学习笔记】C++实现通讯录管理系统

目标:

1.最大容量1000人

2.允许用户添加,修改,查找,浏览,删除,清空,退出通讯录

3.每条通讯录记录用户姓名,电话,性别

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

struct Person
{

    string name;

    string phone;

    int sex;
};

struct filesystem
{
    struct Person userArray[MAX];
    int num;
};

void adduser(struct filesystem *f1)//添加用户
{

    cout << "\tplease enter name" << endl;

    cin >> f1->user[f1->num].name;

    cout << "\tplease enter phone" << endl;

    cin >> f1->user[f1->num].phone;

    while (true)
    {
        cout << "\tplease enter sex 0/1" << endl;

        int sex;

        cin >> sex;
        if (sex == 1 || sex == 0)
        {
            f1->user[f1->num].sex = sex;

            f1->num++;

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

            return;
        }
        cout << "\t输入错误" << endl;

        system("cls");
    }
}

void printInfo(struct filesystem *f1)//打印信息
{
    if (f1->num > 0)
    {
        for (int i = 0; i < f1->num; i++)
        {
            cout << "姓名 " << f1->user[i].name << "\t性别 " << (f1->user[i].sex == 1 ? "男" : "女") << "\t电话:" << f1->user[i].phone << endl;
        }

        system("pause");

        system("cls");

        return;
    }
    else
    {
        cout << "\t当前通讯录为空" << endl;

        system("pause");

        system("cls");

        return;
    }
}

void showmenu()//菜单打印
{
    cout << "*****\t**************\t*****" << endl;
    cout << "*****\t1.添加联系人\t*****" << endl;
    cout << "*****\t2.显示联系人\t*****" << endl;
    cout << "*****\t3.查找联系人\t*****" << endl;
    cout << "*****\t4.修改联系人\t*****" << endl;
    cout << "*****\t5.删除联系人\t*****" << endl;
    cout << "*****\t6.删除通讯录\t*****" << endl;
    cout << "*****\t0.退出通讯录\t*****" << endl;
    cout << "*****\t**************\t*****" << endl;
}

int detectUser(struct filesystem *f1, string name)
{

    for (int i = 0; i < f1->num; i++)
    {
        if (f1->user[i].name == name && f1->num != 0)
        {
            return i;
        }
    }

    return -1;
}

void deleteUser(int id, struct filesystem *f1)//查找用户
{

    if (id != -1)
    {
        if (id == f1->num)
        {
            f1->user[id] = {"0", "0", -1};

            cout << "\t删除成功" << endl;

            f1->num--;

            system("pause");

            system("cls");

            return;
        }
        for (; id < f1->num; id++)
        {

            f1->user[id] = f1->user[id + 1];
        }
        f1->num--;

        cout << "\t删除成功" << endl;

        system("pause");

        system("cls");

        return;
    }
    else
    {

        cout << "\t 查无此人" << endl;

        system("pause");

        system("cls");

        return;
    }
}

void modifyUser(int id, struct filesystem *f1)//修改信息
{
    if (id != -1)
    {
        cout << "\tplease enter name" << endl;

        cin >> f1->user[id].name;

        cout << "\tplease enter phone" << endl;

        cin >> f1->user[id].phone;

        while (true)
        {
            cout << "\tplease enter sex 0/1" << endl;

            int sex;

            cin >> sex;
            if (sex == 1 || sex == 0)
            {
                f1->user[id].sex = sex;

                system("pause");

                system("cls");

                return;
            }
            cout << "\t输入错误" << endl;

            system("cls");
        }
    }
    else
    {
        cout << "\t 查无此人" << endl;

        system("pause");

        system("cls");

        return;
    }
}
void searchUser(int id, struct filesystem *f1)//查找用户
{
    if (id == -1)
    {
        cout << "\t查无此人" << endl;

        system("pause");

        system("cls");

        return;
    }
    else
    {
        cout << "姓名 " << f1->user[id].name << "\t性别 " << (f1->user[id].sex == 1 ? "男" : "女") << "\t电话:" << f1->user[id].phone << endl;

        system("pause");

        system("cls");

        return;
    }
}

int main()//主函数
{
    filesystem f1;

    f1.num = 0;

    while (true)
    {
        showmenu();

        int input = 0;

        string name;

        int id = -1;

        cin >> input;

        switch (input)
        {
        case 1: //添加联系人
        {
            adduser(&f1);

            break;
        }
        case 2: //显示联系人
        {
            printInfo(&f1);
            break;
        }
        case 3: //查找联系人
        {       /* code */
            cout << "\t请输入姓名" << endl;

            cin >> name;

            id = detectUser(&f1, name);

            searchUser(id, &f1);

            break;
        }
        case 4: //修改联系人
        {       /* code */
            cout << "\t请输入姓名" << endl;

            cin >> name;

            id = detectUser(&f1, name);

            modifyUser(id, &f1);

            break;
        }
        case 5: //删除联系人
        {       /* code */
            cout << "\t请输入姓名" << endl;

            cin >> name;

            id = detectUser(&f1, name);

            deleteUser(id, &f1);

            break;
        }
        case 6: //清空
        {       /* code */
            f1.num = 0;
            cout << "\t清空成功" << endl;
            system("pause");
            system("cls");
            break;
        }
        case 0:
        {
            cout << "\t感谢使用,再见" << endl;
            system("pause");
            return 0;
        }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值