7重学C++之【综合案例:通讯录管理系统】

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


struct Person{
    string name;
    int sex; // 1男2女
    int age;
    string phone;
    string addr;
};


struct Addressbooks{
    struct Person person_arr[MAX]; // 通讯录存储上限1000
    int size; // 当前通讯录存储数量
};


void show_menu(){
    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;
}


void add_person(Addressbooks * abs){
    if (abs->size == MAX){
        cout << "通讯录已满,无法添加!" << endl;
        return;
    }else{
        cout << "请输入姓名:" << endl;
        string name;
        cin >> name;
        abs->person_arr[abs->size].name = name;

        cout << "请输入性别(1--男,2--女):" << endl;
        int sex = 0;
        while (true){
            cin >> sex;
            if (sex==1 || sex==2){
                abs->person_arr[abs->size].sex = sex;
                break;
            }
            cout << "输入错误,请重新输入:" << endl;
        }

        cout << "请输入年龄:" << endl;
        int age = 0;
        while (true){
            cin >> age;
            if (age>=1 && age<=150){
                abs->person_arr[abs->size].age = age;
                break;
            }
            cout << "输入错误,请重新输入:" << endl;
        }

        cout << "请输入电话:" << endl;
        string phone;
        cin >> phone;
        abs->person_arr[abs->size].phone = phone;

        cout << "请输入地址:" << endl;
        string addr;
        cin >> addr;
        abs->person_arr[abs->size].addr = addr;

        abs->size++;
        cout << "添加成功" << endl;

        system("pause"); // 请按任意键继续
        system("cls"); // 清屏
    }
}


void show_person(Addressbooks * abs){
    if (abs->size == 0){
        cout << "当前记录为空" << endl;
    }else{
        for (int i=0; i<abs->size; i++){
            cout << "姓名:" << abs->person_arr[i].name << "\t";
            cout << "性别:" << (abs->person_arr[i].sex == 1 ? "男" : "女") << "\t";
            cout << "年龄:" << abs->person_arr[i].age << "\t";
            cout << "电话:" << abs->person_arr[i].phone << "\t";
            cout << "地址:" << abs->person_arr[i].addr << endl;
        }
    }
    system("pause");
    system("cls");
}


int is_exit(Addressbooks * abs, string name){ // 检测联系人是否存在,存在返回在数组中的位置下标,不存在返回-1
    for (int i=0; i<abs->size; i++){
        if (abs->person_arr[i].name == name){
            return i;
        }
    }
    return -1;
}


void delete_person(Addressbooks * abs){
    cout << "请输入要删除的联系人姓名:" << endl;
    string name;
    cin >> name;
    int result = is_exit(abs, name);
    if (result == -1){
        cout << "查无此人" << endl;
    }else{
        for (int i=result; i<abs->size; i++){
            // 数据前移实现逻辑删除
            abs->person_arr[i] = abs->person_arr[i+1];
        }
        abs->size--;
        cout << "删除成功" << endl;
    }
    system("pause");
    system("cls");
}


void find_person(Addressbooks * abs){
    cout << "请输入要查找的联系人姓名:" << endl;
    string name;
    cin >> name;
    int result = is_exit(abs, name);
    if (result == -1){
        cout << "查无此人" << endl;
    }else{
        cout << "姓名:" << abs->person_arr[result].name << "\t";
        cout << "性别:" << (abs->person_arr[result].sex == 1 ? "男" : "女") << "\t";
        cout << "年龄:" << abs->person_arr[result].age << "\t";
        cout << "电话:" << abs->person_arr[result].phone << "\t";
        cout << "地址:" << abs->person_arr[result].addr << endl;
    }
    system("pause");
    system("cls");
}


void modify_person(Addressbooks * abs){
    cout << "请输入要修改的联系人姓名:" << endl;
    string name;
    cin >> name;
    int result = is_exit(abs, name);
    if (result == -1){
        cout << "查无此人" << endl;
    }else{
        cout << "请输入姓名:" << endl;
        string name;
        cin >> name;
        abs->person_arr[result].name = name;

        cout << "请输入性别(1--男,2--女):" << endl;
        int sex = 0;
        while (true){
            cin >> sex;
            if (sex==1 || sex==2){
                abs->person_arr[result].sex = sex;
                break;
            }
            cout << "输入错误,请重新输入:" << endl;
        }

        cout << "请输入年龄:" << endl;
        int age = 0;
        while (true){
            cin >> age;
            if (age>=1 && age<=150){
                abs->person_arr[result].age = age;
                break;
            }
            cout << "输入错误,请重新输入:" << endl;
        }

        cout << "请输入电话:" << endl;
        string phone;
        cin >> phone;
        abs->person_arr[result].phone = phone;

        cout << "请输入地址:" << endl;
        string addr;
        cin >> addr;
        abs->person_arr[result].addr = addr;

        cout << "修改成功" << endl;
    }
    system("pause");
    system("cls");
}


void clear_person(Addressbooks * abs){
    abs->size = 0; // 逻辑清空
    cout << "通讯录已清空" << endl;
    system("pause");
    system("cls");
}


int main(){
    Addressbooks abs;
    abs.size = 0;

    while (true){
        show_menu();
        cout << "请输入指令:" << endl;
        int select = 0; // 用户输入
        cin >> select;

        switch (select){
        case 1:  // 添加联系人
            add_person(&abs); // 地址传递才能根据形参修改实参
            break;
        case 2:  // 显示联系人
            show_person(&abs);
            break;
        case 3:  // 删除联系人
            /*{   cout << "请输入要删除的联系人姓名:" << endl;
                string name;
                cin >> name;
                if (is_exit(&abs, name) == -1){
                    cout << "查无此人" << endl;
                }else{
                    cout << "确定删除吗?" << endl;
                }
                break; // 注意C++中case中break上面代码过多容易报错,可以加上{}形成代码段
            }*/ // 测试代码
            delete_person(&abs);
            break;
        case 4:  // 查找联系人
            find_person(&abs);
            break;
        case 5:  // 修改联系人
            modify_person(&abs);
            break;
        case 6:  // 清空联系人
            clear_person(&abs);
            break;
        case 0:  // 退出通讯录
            cout << "欢迎下次使用,再见" << endl;
            system("pause");
            return 0;
            break;
        default:
            cout << "输入错误,请重新输入:" << endl;
            break;
        }
    }

    system("pause");  // 提示按任意键继续,system需要导入<cstdlib>
    return 0;
}

 

上一篇:Java Math类的基本用法


下一篇:【object-c基础】Object-c基础之三:面对对象开发@interface,@implementation