【C++学习系列】2.通讯录管理

ref:https://github.com/Blitzer207/C-Resource/blob/master/%E7%AC%AC2%E9%98%B6%E6%AE%B5%E5%AE%9E%E6%88%98-%E9%80%9A%E8%AE%AF%E5%BD%95%E7%AE%A1%E7%90%86/%E9%80%9A%E8%AE%AF%E5%BD%95%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F%E8%AE%B2%E4%B9%89/%E9%80%9A%E8%AE%AF%E5%BD%95%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F.md
知识点:
1.数组删除一个元素,需要后面的往前挪。
2.删除数组,只需要update size;

#include
#include
#include
#include
using namespace std;
// 联系人结构体
struct Person{
    string name; 
    int sex; // 性别:1男 2女
    int age; // 年龄
    string phone; // 电话
    string addr; // 地址
};
#define MAX 1000
// 通讯录结构体
struct AddressBooks{
    struct Person personArray[MAX]; // 通讯录中保存的联系人数组
    int size; // 通讯录中人员个数
};

//菜单页面
void showMenu(){
    cout << "***********************" << endl;
    cout << "1.添加联系人"<size == MAX){
        cout << "通讯录已满,无法添加" << endl;
        return;
    }else{
        //姓名
        string name;
        cout << "请输入姓名:" << endl;
        cin >> name;
        abs->personArray[abs->size].name = name;
        // 性别
        cout << "请输入性别:" << endl;
        cout << "1 -- 男" << endl;
		cout << "2 -- 女" << endl;
        int sex =0;
        while(true){
            cin >> sex;
            if (sex==1 || sex ==2){
                abs->personArray[abs->size].sex = sex;
                break;
            }
            cout << "输入有误,请重新输入";
        }
        // 年龄
        cout << "请输入年龄:" << endl;    
        int age = 0;
        cin >> age;
        abs->personArray[abs->size].age = age;

        // 电话
        cout << "请输入手机号:" << endl;    
        string phone = "";
        cin >> phone;
        abs->personArray[abs->size].age = age;

        // 家庭住址
        cout << "请输入家庭住址:" << endl;    
        string address = "";
        cin >> address;
        abs->personArray[abs->size].addr = address;
        // 更新通讯录人数
        abs->size++;
        cout << "添加成功,返回主菜单!"<< endl;
    }

}
void printPerson(Person p){
    cout << "姓名:" << p.name << "\t";
    cout << "性别:" << (p.sex == 1 ?"男":"女") << "\t";
    cout << "年龄:" << p.age << "\t";
    cout << "电话:" << p.phone << "\t";
    cout << "住址:" << p.addr << "\n";
}
void showPerson(AddressBooks * abs){
    if (abs->size == 0){
        cout << "当前记录为空!" << endl;
    }else{
        for (size_t i = 0; i < abs->size; i++)
        {
            printPerson(abs->personArray[i]);
        }
        cout << "共:" << abs->size << "人" << endl;
    }
}
int isExist(AddressBooks * abs, string name){
    for (size_t i = 0; i < abs->size; i++)
    {
        if(abs->personArray[i].name == name){
            return i;
        }
    }
    return -1;
}
string getInputName(){
    string name;
    cin>>name;
    return name;
}

void deletePerson(AddressBooks * abs){
    cout << "请输入您要删除的联系人" << endl;
    string name = getInputName();
    int ret = isExist(abs,name);
    if (ret!=-1){
        for (int i = ret; i < abs->size; i++)
        {
            abs->personArray[i] = abs->personArray[i+1];
        }
        abs->size--;
    }else{
        cout << "查无此人" << endl;
    }
}

void findPerson(AddressBooks *abs){
    cout << "请输入您要查找的联系人的名字:"<personArray[ret]);
    }else{
        cout << "查无此人:"<> new_name;
        abs->personArray[ret].name = new_name;
        // 性别
        cout << "请输入性别:" << endl;
        cout << "1 -- 男" << endl;
		cout << "2 -- 女" << endl;
        int sex =0;
        while(true){
            cin >> sex;
            if (sex==1 || sex ==2){
                abs->personArray[ret].sex = sex;
                break;
            }
            cout << "输入有误,请重新输入";
        }
        // 年龄
        cout << "请输入年龄:" << endl;    
        int age = 0;
        cin >> age;
        abs->personArray[ret].age = age;

        // 电话
        cout << "请输入手机号:" << endl;    
        string phone = "";
        cin >> phone;
        abs->personArray[ret].age = age;

        // 家庭住址
        cout << "请输入家庭住址:" << endl;    
        string address = "";
        cin >> address;
        abs->personArray[ret].addr = address;
        // 更新通讯录人数
        cout << "修改成功,返回主菜单!"<< endl;
       
    }else{
        cout << "查无此人" << endl;
    }
}

// 6. 清空联系人
void cleanPerson(AddressBooks * abs){
    abs->size = 0;
    cout << "通讯录已清空" << endl;
}

int main(){

    // 创建通讯录
    AddressBooks abs;
    // 初始化通讯录中的人数
    abs.size = 0;
    int select = 0;
    while (true)
    {
        showMenu();      
        cin>>select;
        switch (select)
        {
            case 1: //1.添加联系人
                addPerson(&abs);
                break;
            case 2: //2.显示联系人
                showPerson(&abs);
                break;
            case 3: //3.删除联系人
                deletePerson(&abs);
                break;
            case 4: //4.查找联系人
                findPerson(&abs);
                break;
            case 5: //5.修改联系人
                modifyPerson(&abs);
                break;
            case 6: //6.清空联系人
                break;
            case 0:  //退出通讯录
                cout << "欢迎下次使用" << endl;
                // while True 里面套用 return 也是可以跳出的。
                return 0;
                break;
            default:
                break;
        }
    }
    
    
    string str;
    getline(cin,str);
    return 1;
}

你可能感兴趣的:(C++,c++,学习,开发语言)