C++通讯录管理系统

#include<iostream>
using namespace std;
#include<string>
#define MAX 1000
struct Person
{
    string m_Name;
    int m_Sex;
    int m_Age;
    string m_Phone;
    string m_Addr;
};
struct Addressbook
{
    struct Person personArray[MAX];
    int m_Size;
};
void showMenu()
{
	cout<< "***********************" << endl
        << "*****1、添加联系人*****" << endl
        << "*****2、显示联系人*****" << endl
        << "*****3、删除联系人*****" << endl
        << "*****4、查找联系人*****" << endl
        << "*****5、修改联系人*****" << endl
        << "*****6、*备份通讯录*****" << endl
        << "*****7、*恢复通讯录*****" << endl
        << "*****8、清空通讯录*****" << endl
        << "*****0、退出通讯录*****" << endl
        << "***********************" << endl;

}
void addPerson(struct Addressbook* absp)
{
    if (absp->m_Size == MAX)
    {
        cout << "通讯录已满,无法添加!" << endl;
        return;
    }
    //输入姓名
    string name;
    cout << "请输入姓名:" << endl;
    cin >> name;
    absp->personArray[absp->m_Size].m_Name = name;

    //输入性别
    int sex = 0;
    while (true)
    {
        cout << "请输入性别:" << endl
            << "1--女性" << endl << "2--男性" << endl;
        cin >> sex;
        if (sex == 1 || sex == 2)
        {
            absp->personArray[absp->m_Size].m_Sex = sex;
            break;
        }
        else
        {
            cout << "请输入正确的性别!" ;
        }
    }
    

    //输入年龄
    int age = 0;
    cout << "请输入年龄:" << endl;
    while (true)
    {
        cin >> age;
        if (age > 0 && age < 150)
        {
            absp->personArray[absp->m_Size].m_Age = age;
            break;
        }
        else
        {
            cout << " 年龄非法,请重新输入!";
        }
    }


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

    //输入住址
    cout << "请输入住址:" << endl;
    string address;
    cin >> address;
    absp->personArray[absp->m_Size].m_Addr = address;
    absp->m_Size++;
    
    cout << "添加成功!" << endl;
    system("pause");
    system("cls");
    
}
    
void showPerson(Addressbook* absp)
{
    if (absp->m_Size == 0)
    {
        cout << "通讯录为空!" << endl;
    }
    else
    {
        for (int i = 0; i < absp->m_Size; i++)
        {
            cout << "姓名:" << absp->personArray[i].m_Name << "\t"
                << "性别:" << (absp->personArray[i].m_Sex == 1 ? "女" : "男") << "\t"
                << "年龄:" << absp->personArray[i].m_Age << "\t"
                << "电话:" << absp->personArray[i].m_Phone << "\t"
                << "住址:" << absp->personArray[i].m_Addr << endl;
        }
    }
    system("pause");
    system("cls");
}

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

void findPerson(Addressbook* absp)
{
    cout << "请输入姓名:" << endl;
    string name;
    cin >> name;
    int temp = isExist(absp, name);
    if (temp == -1)
        cout << "查无此人!" << endl;
    else
        cout << "姓名:" << absp->personArray[temp].m_Name << "\t"
        << "性别:" << (absp->personArray[temp].m_Sex == 1 ? "女" : "男") << "\t"
        << "年龄:" << absp->personArray[temp].m_Age << "\t"
        << "电话:" << absp->personArray[temp].m_Phone << "\t"
        << "住址:" << absp->personArray[temp].m_Addr << endl;
}
void modifyPerson(Addressbook* absp)
{
    cout << "请输入姓名修改:" << endl;
    string name;
    cin >> name;
    int ret = isExist(absp, name);
    if (ret != -1)
    {
        //姓名
        cout << "请输入姓名:" << endl;
        string name;
        cin >> name;
        absp->personArray[ret].m_Name = name;
        //性别
        while (true)
        {
            cout << "请输入性别:" << endl;
            int sex;
            cin >> sex;
            if(sex == 1 || sex == 2)
            absp->personArray[ret].m_Sex = sex;
            break;
        }
        //年龄
        cout << "请输入年龄:" << endl;
        int age;
        cin >> age;
        absp->personArray[ret].m_Age = age;
        //电话
        cout << "请输入电话:" << endl;
        string phone;
        cin >> phone;
        absp->personArray[ret].m_Phone = phone;
        //住址
        cout << "请输入住址:" << endl;
        string addr;
        cin >> addr;
        absp->personArray[ret].m_Addr = addr;
        cout << "修改成功!" << endl;
        system("pause");
    }
    else
    {
        cout << "查无此人!" << endl;
    }
    system("pause");
    system("cls");
}
void cleanPerson(Addressbook* absp)
{
    absp->m_Size = 0;
    cout << "通讯录已清空" << endl;
    system("pause");
    system("cls");
}
int main()
{
    int select = 0;
    struct Addressbook abs;
    abs.m_Size = 0;
    while (true)  //循环输入
    {
        showMenu(); //始终显示菜单调用
        cin >> select;
        switch (select)
        {
        case 1:  //添加联系人
            addPerson(&abs);
            break;
        case 2:  //显示联系人
            showPerson(&abs);
            break;
        case 3:  //删除联系人
        {
            cout << "请输入删除的姓名:" << endl;
            string name;
            cin >> name;
            int temp = isExist(&abs, name);
            if (temp == -1)
                cout << " 查无此人!" << endl;
            else
            {
                for (int i = temp; i < abs.m_Size; i++)
                {
                    abs.personArray[i] = abs.personArray[i + 1];
                }
                abs.m_Size--;
                cout << "删除成功!" << endl;
            }
            system("pause");
            system("cls");
        }
            break;
        case 4:  //查找联系人
        {
            findPerson(&abs);
            break;
        }
            
        case 5:  //修改联系人
            modifyPerson(&abs);
            break;
        case 8:  //清空联系人
            cleanPerson(&abs);
            break;
        case 0:  //退出通讯录
            cout << "欢迎下次使用" << endl;
            system("pause");
            return 0;
            break;
        }
     }
	
    system("pause");
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值