[黑马程序员课程记录]C++基础部分4

第十四节课 通讯录管理系统

[黑马程序员课程记录]C++基础部分4_第1张图片

  

#include 
using namespace std;
#include
#define max 1000
​
struct message
{
    string name;
    string sex;
    int age;
    string phone;
    string address;
};
​
struct addressbook //创建通讯录函数
{
    message man[max];
    int number;
};
void add(addressbook *adb)
{
    if(adb->number==max)
    { 
        cout << "已经添加满了" << endl;
        return;
    }
    else
    {
        cout << "请添加联系人" << endl;
​
        cout << "请输入联系人姓名:  ";
        cin >> adb->man[adb->number].name;
​
        cout << "请输入联系人性别: ";
        cin >> adb->man[adb->number].sex;
​
        cout << "请输入联系人年龄: ";
        cin >> adb->man[adb->number].age;
​
        cout << "请输入联系人电话号码;  ";
        cin >> adb->man[adb->number].phone;
​
        cout << "请输入联系人地址:  ";
        cin >> adb->man[adb->number].address;
        
        adb->number++;
​
        cout << "添加完成" << endl;
        
        system("pause");//按任意键继续
        system("cls");//清屏
        
    }
}
void show(addressbook *adb)
{
    if (adb->number == 0)
    {
        cout << "暂无联系人" << endl;
    }
    else
    {
        for (int i = 0; i < adb->number; i++)
        {
            cout << "姓名: "<< adb->man[i].name << "   " 
                <<"年龄: "<< adb->man[i].age << "   " 
                <<"性别:" << adb->man[i].sex << "   " 
                <<"电话号码: "<< adb->man[i].phone << "   "
                <<"家庭地址: "<<  adb->man[i].address << endl;
        }
    }
    system("pause");//按任意键继续
    system("cls");//清屏
}
​
void reduce(addressbook *adb)//所谓的删除就是覆盖,依次让后一个数据覆盖前一个数据即可
{
    string r_name;
    cout << "请输入要查找联系人的名字" << endl;
    cin >> r_name;
    int a = 0;
    for (int i = 0; i < adb->number; i++)
    {
        if (adb->man[i].name == r_name)
        {
            for (int j = i; j < adb->number; j++)
            {
                adb->man[j] = adb->man[j + 1];
            }
            cout << "删除成功" << endl;
            adb->number--;
            a = 1;
        }
    }
    if (a == 0)
    {
        cout << "查无此人" << endl;
    }
​
    system("pause");//按任意键继续
    system("cls");//清屏
}
​
void find(addressbook *adb)
{
    string f_name;
    cout << "请输入要查找联系人的名字" << endl;
    cin >> f_name;
    int a = 0;
    for (int i = 0; i < adb->number; i++)
    {
        if (adb->man[i].name == f_name)
        {
            cout << adb->man[i].name << "   " 
                << adb->man[i].age << "   " 
                << adb->man[i].sex << "   " 
                << adb->man[i].phone << "   " 
                << adb->man[i].address << endl;
            a = 1;
        }
    }
    if (a == 0)
    {
        cout << "查无此人" << endl;
    }
​
    system("pause");//按任意键继续
    system("cls");//清屏
}
​
void change(addressbook *adb)
{
    string c_name;
    cout << "请输入你想修改的联系人" << endl;
    cin >> c_name;
    int a = 0;
    for (int i = 0; i < adb->number; i++)
    {
        if (adb->man[i].name == c_name)
        {
            cout << "请输入联系人姓名:  ";
            cin >> adb->man[i].name;
​
            cout << "请输入联系人性别: ";
            cin >> adb->man[i].sex;
​
            cout << "请输入联系人年龄: ";
            cin >> adb->man[i].age;
​
            cout << "请输入联系人电话号码;  ";
            cin >> adb->man[i].phone;
​
            cout << "请输入联系人地址:  ";
            cin >> adb->man[i].address;
​
            cout << "修改完成" << endl;
            a = 1;
        }
    }
    if (a == 0)
    {
        cout << "查无此人" << endl;
    }
​
    system("pause");//按任意键继续
    system("cls");//清屏
​
}
​
void clean(addressbook *adb)//直接令数组为零即可
{
    
    adb->number = 0;
​
    cout << "清除完成" << endl;
​
    system("pause");//按任意键继续
    system("cls");//清屏
}
​
​
int main ()
{
    int num1 = 0;
    addressbook adb;
    adb.number = 0;
​
    while(1)
    {
        cout << "1.添加联系人" << endl;
        cout << "2.显示联系人" << endl;
        cout << "3.删除联系人" << endl;
        cout << "4.查找联系人" << endl;
        cout << "5.修改联系人" << endl;
        cout << "6.清空联系人" << endl;
        cout << "0.退出通讯录" << endl;
        cout << "************" << endl;
​
        cin >> num1;
        switch (num1)//case语句中如果是一大段代码在第二个表达式部分,可以用{}来封装一下
        {
        case 1:
            add(&adb);
            break;
        case 2:
            show(&adb);
            break;
        case 3:
            reduce(&adb);
            break;
        case 4:
            find(&adb);
            break;
        case 5:
            change(&adb);
            break;
        case 6:
            clean(&adb);
            break;
        case 0:
            cout << "欢迎下次使用" << endl;
            system("pause");
            return 0;
            break;
        default:
            return 0;
                break;
        }
​
    }
    system("pause");//按任意键继续
    return 0;
    
}

你可能感兴趣的:(C++之基础,c++,开发语言,visual,studio)