编辑代码

#include<iostream>
using namespace std;
#include<string>
#define MAX 1000
struct people
{
    string s_name;
    int s_sex;//1为男, 2为女
    int s_age;
    string s_number;
    string s_address;
};
struct Addressbook
{
    struct people peopleArray[MAX];//当前通讯录中保存的数组
    int m_size;//当前通讯录的人员个数
};

//添加联系人
void addperson(Addressbook* abs)
{
    //判断通讯录是否满了
    if (abs->m_size == MAX)
    {
        cout << "通讯录里面的人员已满" << endl;
    }
    //姓名
    string name;
    cout << "请输入需要添加的联系人" << endl;
    cin >> name;
    abs->peopleArray[abs->m_size].s_name = name;
    //性别
    int sex;
    cout << "请输入添加的人的性别 "<<endl<<"1为男 2为女" << endl;
    while (true)
    {
        cin >> sex;
       if (sex == 1 || sex == 2)
        {
           abs->peopleArray[abs->m_size].s_sex = sex;
           break;
        }
        
        cout << "输入有误,请重新输入" << endl;
    }

    
    //年龄
    int age;
    cout << "请输入添加的人的年龄 " << endl;
    cin >> age;
    abs->peopleArray[abs->m_size].s_age = age;

    //地址
    string address;
    cout << "请输入添加的人的地址" << endl;
    cin >> address;
    abs->peopleArray[abs->m_size].s_address = address;

    //电话  
    string number;
    cout << "请输入添加的人的电话号" << endl;
    cin >> number;
    abs->peopleArray[abs->m_size].s_number = number;

    abs->m_size++;
    cout << "添加成功" << endl;
    system("pause");
    system("cls");
}
//显示联系人
void showperson(Addressbook* abs)
{
    if (abs->m_size == 0)
    {
        cout << "里面为空" << endl;
    }
    else
    {
        for (int i = 0; i < abs->m_size; i++)
        {
            cout << "姓名:" << abs->peopleArray[i].s_name<<"\t";
            cout << "年龄:" << abs->peopleArray[i].s_age << "\t" ;
            cout << "性别:" << (abs->peopleArray[i].s_sex == 1?"男":"女") << "\t";
            cout << "地址:" << abs->peopleArray[i].s_address << "\t";
            cout << "电话:" << abs->peopleArray[i].s_number<<endl;
           

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

//判断一个人是否在通讯录里面
int isExist(Addressbook* abs, string name)
{
    for (int i = 0; i < abs->m_size; i++)
    {
        if (abs->peopleArray[i].s_name == name)
        {
            return i;
        }
        else
        {
            return -1;
        }

            
    }
}

//删除联系人
void deleteperson(Addressbook* abs)
{
    string  name;
    cout << "请输入需要删除的名字" << endl;
    cin >> name;
    int ret = isExist(abs, name);
    if (ret == -1)
    {
        cout << "查无此人" << endl;
    }
    else
    {
        for (int i = ret; i < abs->m_size; i++)
        {
            abs->peopleArray[i] = abs->peopleArray[i + 1];//把每一个i后面的一个数组往前移
        }
        abs->m_size--;
        cout << "删除成功" << endl;
    }
    system("pause");
    system("cls");
}
//查找联系人
void findpeople(Addressbook* abs)
{
    string name;
    cout << "请输入需要找的联系人" << endl;
    cin >> name;
    int ret = isExist(abs, name);
    //找到联系人
    if (ret!=-1)
    {
        cout << "姓名:" << abs->peopleArray[ret].s_name << "\t";
        cout << "年龄:" << abs->peopleArray[ret].s_age << "\t";
        cout << "性别:" << abs->peopleArray[ret].s_sex << "\t";
        cout << "电话:" << abs->peopleArray[ret].s_number << "\t";
        cout << "地址:" << abs->peopleArray[ret].s_address << "\t";
    }
    //找不到联系人
    else
    {
        cout << "查无此人";
    }
    system("pause");
    system("cls");
}
//修改联系人

void modifypeople(Addressbook* abs)
{
    string name;
    cout << "请输入需要修改的姓名" << endl;
    cin >> name;
    isExist(abs, name);
    int ret = isExist(abs,name);
    if (ret != -1)
    {
        //输入修改的姓名 
        string name;
        cout << "请输入需要修改成的姓名" << endl;
        cin >> name;
        abs->peopleArray[ret].s_name = name;
        //输入修改的年龄
        int age;
        cout << "请输入需要修改成的年龄" << endl;
        cin >> age;
        abs->peopleArray[ret].s_age = age;
        //输入修改的性别
        int sex;
        cout << "请输入添加的人的性别 " << endl << "1为男 2为女" << endl;
        cin >> sex;
        while (true)
        {
            cin >> sex;
            if (sex == 1 || sex == 2)
            {
                abs->peopleArray[abs->m_size].s_sex = sex;
                break;
            }
            else
            cout << "输入有误,请重新输入" << endl;
        }
        abs->peopleArray[ret].s_sex = sex;

        //输入修改的电话
        int number;
        cout << "请输入需要修改成的电话号码" << endl;
        cin >> number;
        abs->peopleArray[ret].s_number = number;
        //输入修改的地址
        int address;
        cout << "请输入需要修改成的地址" << endl;
        cin >> address;
        abs->peopleArray[ret].s_address = address;
    }
    else
    {
        cout << "查无此人" << endl;
    }
    system("pause");
    system("cls");
}

//清空通讯录
void cleanPenple(Addressbook* abs)
{
    abs->m_size == 0;
    cout << "通讯录已经清空"<<endl;
    system("pause");
    system("cls");
}
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()
{
    Addressbook abs;
    int selece = 0;
    abs.m_size = 0;
    while (true)
    {
        showMenu();
        cout << "请输入执行的编号" << endl;
        cin >> selece;
        cout << endl;

    switch(selece)
    { 
    case 1://添加联系人
        addperson(&abs);//利用地址传递可以修改实参里面的值
        break;
    case 2://显示联系人
        showperson(&abs);
        break;
    case 3://删除联系人
        deleteperson(&abs);
        break;
    case 4://查找联系人
        findpeople(&abs);
        break;
    case 5://修改联系人
        modifypeople(&abs);
        break;
    case 6://清空联系人
        cleanPenple(&abs);
        break;
    case 0://退出通讯录
        cout << "欢迎下次使用" << endl;
        system("pause");
        return 0;
        break;
    }
    
   
        
    }

 system("pause");
 return 0;
}