10.3随笔

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

//界面展示函数
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;
}

//定义联系人结构体
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* adr)
{
    if (adr->m_Size == MAX) 
    {
        cout << "通讯录已满!" << endl;
        //此处本有一个return;
        system("pause");
        system("cls");
    }
    else
    {
        string name;
        cout << "请输入姓名:" << endl;
        cin >> name;
        adr->personArray[adr->m_Size].m_Name = name;

        int sex;
        cout << "请输入性别:" << endl;
        cout << "1-男" << endl;
        cout << "2-女" << endl;
        while (true) {
            cin >> sex;
            if (sex == 1 || sex == 2) {
                adr->personArray[adr->m_Size].m_Sex = sex;
                break;
            }
            else
            {
                cout << "错误输入" << endl;
            }
        }

        int age;
        cout << "请输入年龄:" << endl;
        cin >> age;
        adr->personArray[adr->m_Size].m_Age = age;

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

        string addr;
        cout << "请输入家庭住址:" << endl;
        cin >> addr;
        adr->personArray[adr->m_Size].m_Address = addr;

        adr->m_Size++;
        cout << "添加成功!" << endl;
        system("pause");
        system("cls");
    }
}

//显示联系人函数
void showPerson(Addressbooks* adr) {
    if (adr->m_Size == 0) {
        cout << "通讯录为空" << endl;
    }
    else
    {
        for (int i = 0; i < adr->m_Size; i++) {
            cout << "姓名:" << adr->personArray[i].m_Name << "\t";
            cout << "性别:" << (adr->personArray[i].m_Sex == 1 ? "" : "") << "\t";
            cout << "年龄:" << adr->personArray[i].m_Age << "\t";
            cout << "联系电话:" << adr->personArray[i].m_Phone << "\t";
            cout << "家庭住址:" << adr->personArray[i].m_Address << endl;
        }
    }
    system("pause");
    system("cls");
}

//检测是否有此人的函数
int isExist(Addressbooks *adr, string name)
{
    for (int i = 0; i < adr->m_Size; i++) {
        if (adr->personArray[i].m_Name == name) {
            return i;
        }
    }
    return -1;
}
//删除联系人的函数
void deletePerson(Addressbooks *adr) {
    cout << "请输入想要删除的联系人:" << endl;
    string name;
    cin >> name;
    isExist(adr, name);
    int result = isExist(adr, name);
    if (result == -1)
    {
        cout << "查无此人" << endl;
    }
    else
    {
        for (int i = result; i < adr->m_Size; i++) {
            adr->personArray[i].m_Name = adr->personArray[i + 1].m_Name;
        }
        adr->m_Size--;
        cout << "删除成功" << endl;
    }
    system("pause");
    system("cls");
}

//查找联系人的函数
void findPerson(Addressbooks* adr) {
    cout << "请输入要查找的姓名:" << endl;
    string name;
    cin >> name;
    isExist(adr, name);
    int result = isExist(adr, name);
    if (result == -1)
    {
        cout << "查无此人" << endl;
    }
    else
    {
        cout << "姓名:" << adr->personArray[result].m_Name << "\t";
        cout << "性别:" << (adr->personArray[result].m_Sex == 1 ? "" : "") << "\t";
        cout << "年龄:" << adr->personArray[result].m_Age << "\t";
        cout << "联系电话:" << adr->personArray[result].m_Phone << "\t";
        cout << "家庭住址:" << adr->personArray[result].m_Address << endl;
    }
    system("pause");
    system("cls");
}

//修改联系人的函数
void modifyPerson(Addressbooks* adr) {
    cout << "请输入要修改的联系人:" << endl;
    string name;
    cin >> name;
    isExist(adr, name);
    int result = isExist(adr, name);
    if (result == -1)
    {
        cout << "查无此人" << endl;
    }
    else
    {
        string name;
        cout << "请输入修改的姓名:" << endl;
        cin >> name;
        adr->personArray[result].m_Name = name;

        int sex;
        cout << "请输入修改的性别:" << endl;
        cout << "1-男" << endl;
        cout << "2-女" << endl;
        while (true) {
            cin >> sex;
            if (sex == 1 || sex == 2) {
                adr->personArray[result].m_Sex = sex;
                break;
            }
            else
            {
                cout << "错误输入" << endl;
            }
        }

        int age;
        cout << "请输入修改的年龄:" << endl;
        cin >> age;
        adr->personArray[result].m_Age = age;

        string phone;
        cout << "请输入修改的电话号码:" << endl;
        cin >> phone;
        adr->personArray[result].m_Phone = phone;

        string addr;
        cout << "请输入修改的家庭住址:" << endl;
        cin >> addr;
        adr->personArray[result].m_Address = addr;
    }
    system("pause");
    system("cls");
}

//通讯录清空功能
void cleanPerson(Addressbooks* adr) {
    adr->m_Size = 0;
    cout << "通讯录已经清空" << endl;
    system("pause");
    system("cls");
}

//主函数部分
int main() {
    Addressbooks adr;
    adr.m_Size = 0;
    int select = 0;
    while (true) {
        showMenu();
        cout << "选择您的功能:" << endl;
        cin >> select;
        switch (select) {
        case(1): {
            addPerson(&adr);
            break;
            //添加联系人
        }
        case(2): {
            showPerson(&adr);
            break;
            //显示联系人
        }
        case(3): {
            deletePerson(&adr);
            break;
            //删除联系人
        }
        case(4): {
            findPerson(&adr);
            break;
            //查找联系人
        }
        case(5): {
            modifyPerson(&adr);
            break;
            //修改联系人
        }
        case(6): {
            cleanPerson(&adr);
            break;
            //清空联系人
        }
        case(0): {
            cout << "欢迎下次使用" << endl;
            system("pause");
            return 0;
            break;
            //退出通讯录
        }
        default:
               break;
        }
    }
    system("pause");
    return 0;
}

进行简单的c++复习,编写程序管理系统

posted @ 2023-10-07 00:08  beiqu  阅读(2)  评论(0编辑  收藏  举报