小言_互联网的博客

c++简单小程序(通讯录)

292人阅读  评论(0)

c++语言简单小程序

#include <iostream>
#include <string>
#define MAX 1000
using namespace std;
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;
        cout << "请输入姓名: " << endl;
        cin >> name;
        abs->personArray[abs->m_size].m_name = name;
        //性别
        int sex = 0;
        while(true)
        {
        cout << "请输入性别:1--男,2--女" << endl;
        cin >> sex;
        if(sex == 1 || sex == 2)
        {
            abs->personArray[abs->m_size].m_sex = sex;
            break;
        }
        else
        {
            cout << "格式有误请重新输入" << endl;
        }
        }
        //年龄
        cout << "请输入年龄: " << endl;
        int age;
        cin >> age;
        abs->personArray[abs->m_size].m_age = age;
        //电话
        cout << "请输入电话:" << endl;
        string phone;
        cin >> phone;
        abs->personArray[abs->m_size].m_phone = phone;
        //地址
        cout << "请输入地址:" << endl;
        string address;
        cin >> address;
        abs->personArray[abs->m_size].m_address = address;
        cout << "添加成功" << endl;
        abs->m_size++;
    }
}
//显示联系人
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 << " ";
        cout << "性别:" << (abs->personArray[i].m_sex == 1?"男":"女") << " ";
        cout << "年龄:" << abs->personArray[i].m_age << " ";
        cout << "电话:" << abs->personArray[i].m_phone << " ";
        cout << "地址:" << abs->personArray[i].m_address << endl;
        }
    }
}
//查找联系人
int findPerson(Addressbooks *abs,string name)
{
    for(int i = 0;i < abs->m_size;i++)
    {
        if(abs->personArray[i].m_name == name)
        {
            return i;
        }
        else
        {
            return -1;
        }

    }
}
//删除联系人
void deletePerson(Addressbooks *abs)
{
    cout << "请输入你要删除的姓名" << endl;
    string name;
    cin >> name;
    int indenx = findPerson(abs,name);
    if(indenx != -1)
    {
        for(int i = indenx;i < abs->m_size;i++)
        {
            abs->personArray[i] = abs->personArray[i+1];
        }
        cout << "删除成功!" << endl;
        abs->m_size--;
    }
    else
    {
        cout << "查无此人" << endl;
    }
}
//查找联系人
void findshowPerson(Addressbooks *abs)
{
    cout << "请输入你要查找人的姓名: " << endl;
    string f_name;
    cin >> f_name;
    int f_indenx = findPerson(abs,f_name);
    if(f_indenx != -1)
    {
        cout << "姓名:" << abs->personArray[f_indenx].m_name << " ";
        cout << "性别:" << (abs->personArray[f_indenx].m_sex == 1?"男":"女") << " ";
        cout << "年龄:" << abs->personArray[f_indenx].m_age << " ";
        cout << "电话:" << abs->personArray[f_indenx].m_phone << " ";
        cout << "地址:" << abs->personArray[f_indenx].m_address << endl;
    }
    else
    {
        cout << "查无此人!" << endl;
    }
}
//修改联系人
void modifyPerson(Addressbooks *abs)
{
    cout << "请输入你要修改人的姓名: " << endl;
    string mf_name;
    cin >> mf_name;
    int mf_indenx = findPerson(abs,mf_name);
    if(mf_indenx != -1)
    {
        string name;
        cout << "请输入姓名: " << endl;
        cin >> name;
        abs->personArray[mf_indenx].m_name = name;
        //性别
        int sex = 0;
        while(true)
        {
        cout << "请输入性别:1--男,2--女" << endl;
        cin >> sex;
        if(sex == 1 || sex == 2)
        {
            abs->personArray[mf_indenx].m_sex = sex;
            break;
        }
        else
        {
            cout << "格式有误请重新输入" << endl;
        }
        }
        //年龄
        cout << "请输入年龄: " << endl;
        int age;
        cin >> age;
        abs->personArray[mf_indenx].m_age = age;
        //电话
        cout << "请输入电话:" << endl;
        string phone;
        cin >> phone;
        abs->personArray[mf_indenx].m_phone = phone;
        //地址
        cout << "请输入地址:" << endl;
        string address;
        cin >> address;
        abs->personArray[mf_indenx].m_address = address;
        cout << "修改成功!" << endl;
    }
    else
    {
        cout << "查无此人!" << endl;
    }
}
void showMenu()
{
    cout << "***************************" << endl;
    cout << "*****1、添加联系人 ********" << endl;
    cout << "*****2、显示联系人 ********" << endl;
    cout << "*****3、删除联系人 ********" << endl;
    cout << "*****4、查找联系人 ********" << endl;
    cout << "*****5、修改联系人 ********" << endl;
    cout << "*****0、退出通讯录 ********" << endl;
    cout << "***************************" << endl;
}
int main()
{
    showMenu();
    Addressbooks abs;
    abs.m_size = 0;
    int select = 0;
    while(true)
    {
    cout << "请输入对应的序号" << endl;
    cin >> select;
    switch(select)
    {
    case 1 :
        //添加联系人
        addPerson(&abs);
        break;
    case 2 :
        //显示联系人
        showPerson(&abs);
        break;
    case 3 :
        deletePerson(&abs);
        break;
    case 4 :
        //查找联系人
        findshowPerson(&abs);
        break;
    case 5 :
        //修改联系人
        modifyPerson(&abs);
        break;
    case 0 :
        return 0;
    }
    }
    return 0;

}


转载:https://blog.csdn.net/kaximoudu/article/details/104523044
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场