c++开始篇

键盘侠 2021年09月02日 1,012次浏览

抄点代码,练习练习
教程原址:c++练习

#include "iostream"
#include "string"

#define MAX 1000

using namespace std;

struct Persion
{
    string m_Name;  //姓名
    int m_Sex;      //性别 : 1男 2女
    int m_Age;      //年龄
    string m_Phone; //电话
    string m_Addr;  //住址
};

struct AddressBooks
{
    struct Persion persionArray[MAX]; //通讯录中保存的联系人数组
    int m_Size;                       //通讯录中人员个数
};

void addPersion(AddressBooks *abs)
{
    //判断电话本是否满了
    if (abs->m_Size == MAX)
    {
        cout << "通讯录已满,无法添加" << endl;
        return;
    }
    else
    {
        //姓名
        string name;
        cout << "请输入姓名 : " << endl;
        cin >> name;
        abs->persionArray[abs->m_Size].m_Name = name;

        cout << "请输入性别 : " << endl;
        cout << "1 -- 男" << endl;
        cout << "2 -- 女" << endl;

        //性别
        int sex = 0;
        while (true)
        {
            cin >> sex;
            if (sex == 1 || sex == 2)
            {
                abs->persionArray[abs->m_Size].m_Sex = sex;
                break;
            }
            cout << "输入有误,请重新输入";
        }

        //年龄
        cout << "请输入年龄 : " << endl;
        int age = 0;
        cin >> age;
        abs->persionArray[abs->m_Size].m_Age = age;

        //联系电话
        cout << "请输入联系电话 : " << endl;
        string phone = "";
        cin >> phone;
        abs->persionArray[abs->m_Size].m_Phone = phone;

        //家庭住址
        cout << "请输入家庭住址 : " << endl;
        string address;
        cin >> address;
        abs->persionArray[abs->m_Size].m_Addr = address;

        //更新通讯录人数
        abs->m_Size++;
        cout << "添加成功" << endl;
    }
}

void showPersion(AddressBooks *abs)
{
    if (abs->m_Size == 0)
    {
        cout << "当前记录为空" << endl;
    }
    else
    {
        for (int i = 0; i < abs->m_Size; i++)
        {
            cout << "姓名:" << abs->persionArray[i].m_Name << "\t";
            cout << "性别:" << (abs->persionArray[i].m_Sex == 1 ? "男" : "女") << "\t";
            cout << "年龄:" << abs->persionArray[i].m_Age << "\t";
            cout << "电话:" << abs->persionArray[i].m_Phone << "\t";
            cout << "住址:" << abs->persionArray[i].m_Addr << endl;
        }
    }
}

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

//删除联系人信息
void deletePersion(AddressBooks *abs)
{
    cout << "请输入您要删除的联系人" << endl;
    string name;
    cin >> name;

    int ret = isExist(abs, name);
    if (ret != -1)
    {
        for (int i = ret; i < abs->m_Size; i++)
        {
            abs->persionArray[i] = abs->persionArray[i + 1];
        }
        abs->m_Size--;
        cout << "删除成功" << endl;
    }
    else
    {
        cout << "查无此人" << endl;
    }
}

//查找指定联系人信息
void findPersion(AddressBooks *abs)
{
    cout << "请输入您要查找的联系人" << endl;
    string name;
    cin >> name;
    int ret = isExist(abs, name);
    if (ret != -1)
    {
        cout << "姓名:" << abs->persionArray[ret].m_Name << "\t";
        cout << "性别:" << abs->persionArray[ret].m_Sex << "\t";
        cout << "年龄:" << abs->persionArray[ret].m_Age << "\t";
        cout << "电话:" << abs->persionArray[ret].m_Phone << "\t";
        cout << "住址:" << abs->persionArray[ret].m_Addr << endl;
    }
    else
    {
        cout << "查无此人" << endl;
    }
}

//修改指定联系人信息
void modifyPersion(AddressBooks *abs)
{
    cout << "请输入您要修改的联系人" << endl;
    string name;
    cin >> name;

    int ret = isExist(abs, name);
    if (ret != -1)
    {
        string name;
        cout << "请输入姓名:" << endl;
        cin >> name;
        abs->persionArray[ret].m_Name = name;

        cout << "请输入性别:" << endl;
        cout << "1 -- 男" << endl;
        cout << "1 -- 女" << endl;

        //性别
        int sex = 0;
        while (true)
        {
            cin >> sex;
            if (sex == 1 || sex == 2)
            {
                abs->persionArray[ret].m_Sex = sex;
                break;
            }
            cout << "输入有误,请重新输入";
        }

        //年龄
        cout << "请输入年龄:" << endl;
        int age = 0;
        cin >> age;
        abs->persionArray[ret].m_Age = age;

        //联系电话
        cout << "请输入联系电话:" << endl;
        string phone = "";
        cin >> phone;
        abs->persionArray[ret].m_Phone = phone;

        //家庭住址
        cout << "请输入家庭住址:" << endl;
        string address;
        cin >> address;
        abs->persionArray[ret].m_Addr = address;

        cout << "修改成功" << endl;
    }
    else
    {
        cout << "查无此人" << endl;
    }
}

//清空所有联系人
void cleanPersion(AddressBooks *abs)
{
    abs->m_Size = 0;
    cout << "通讯录已清空 " << endl;
}

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()
{
    int select = 0;
    //创建通讯录
    AddressBooks abs;
    //初始化通讯录中人数
    abs.m_Size = 0;
    while (true)
    {
        showMenu();
        cin >> select;
        switch (select)
        {
        case 1: //添加联系人
            addPersion(&abs);
            break;
        case 2: //显示联系人
            showPersion(&abs);
            break;
        case 3: //删除联系人
            deletePersion(&abs);
            break;
        case 4: //查找联系人
            findPersion(&abs);
            break;
        case 5: //修改联系人
            modifyPersion(&abs);
            break;
        case 6: //修改联系人
            cleanPersion(&abs);
            break;
        case 0:
            cout << "欢迎下次使用" << endl;
            return 0;
            break;
        default:
            break;
        }
    }

    return 0;
}