#include <iostream>
#include <string>
using namespace std;
#define max 1000
//菜单选项
void caidan() {
    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 lianxiren {
    string mingzi;
    int nannv;//1男2女
    int nianling;
    string dianhua;
    string zhuzhi;

};
//通讯录结构体
struct tongxunlu {
    //通讯录粗出的总人数
    struct lianxiren lianxirens[max];
    //通讯录存储的当前人数
    int dangqian;
};
//添加联系人功能
void tianjia(tongxunlu* tongxunlus) {
    //判断通讯录人数 dangqian<1000;
    if (tongxunlus->dangqian == max) {
        cout << "当前添加人数已满" << endl;
        return;
    }
    //当通讯录没有满时
    else {
        //添加联系人
        //姓名
        string xingming;
        cout << "请输入姓名:" << endl;
        cin >> xingming;
        tongxunlus->lianxirens[tongxunlus->dangqian].mingzi = xingming;//把姓名添加到联系人结构体中
        //性别
        cout << "请输入性别:" << endl;
        cout << "1-----男" << endl;
        cout << "2-----女" << endl;
        int xingbie;
        while (true) {
            //输入不正确继续输入,正确则推出
            cin >> xingbie;
            if (xingbie == 1 || xingbie == 2) {
                        tongxunlus->lianxirens[tongxunlus->dangqian].nannv = xingbie;//把性别加到联系人结构体中
                        break;
                    }
            cout << "输入有误,请重新输入" << endl;
        }
        //年龄
        cout << "请输入年龄(请输入数字):" << endl;
        int nianlings;
        cin >> nianlings;
        tongxunlus->lianxirens[tongxunlus->dangqian].nianling = nianlings;
        
        //电话
        cout << "请输入练习电话:" << endl;
        string dianhuas;
        cin >> dianhuas;
        tongxunlus->lianxirens[tongxunlus->dangqian].dianhua = dianhuas;
    }
    //住址
    cout << "请输入家庭住址:" << endl;
    string zhuzhis;
    cin >> zhuzhis;
    tongxunlus->lianxirens[tongxunlus->dangqian].zhuzhi = zhuzhis;
    tongxunlus->dangqian++;//记录人数

    cout << "添加成功!" << endl;
    system("pause");
    system("cls");//清屏
}

//显示联系人
void xianshi(tongxunlu* tongxunlus) {
    //判断是否有联系人
    if (tongxunlus->dangqian == 0) {
        cout << "当前无联系人" << endl;
    }
    else {
        for (int i = 0; i < tongxunlus->dangqian; i++) {
            cout << "姓名:" << tongxunlus[i].lianxirens->mingzi << "\t";
            cout << "年龄:" << tongxunlus[i].lianxirens->nianling << "\t";
            cout << "性别:" <<(tongxunlus[i].lianxirens->nannv == 1?"男":"女")<< "\t";
            cout << "电话:" << tongxunlus[i].lianxirens->dianhua << "\t";
            cout << "住址:" << tongxunlus[i].lianxirens->zhuzhi << endl;
        }
    }
    system("pause");
    system("cls");//清屏
}

//检测联系人
int panduan(tongxunlu* tongxunlus , string xingming) {
    for (int i = 0; i < tongxunlus->dangqian;i++) {
        if (tongxunlus->lianxirens[i].mingzi == xingming) {
            return i;//找到返回编号
        }
    }
    return -1;//找不到返回-1
}

//删除联系人
void shanchu(tongxunlu* tongxunlus) {
    cout << "请输入你要删除的联系人的姓名:" << endl;

    string name;
    cin >> name;
    panduan(tongxunlus, name);
    int jie = panduan(tongxunlus, name);
    if(jie!=-1){
        for (int i = jie; i < tongxunlus->dangqian; i++) {
            tongxunlus->lianxirens[i] = tongxunlus->lianxirens[i + 1];
        }
        tongxunlus->dangqian--;
        cout << "删除成功" << endl;
    }
    
    else {
        cout << "查无此人" << endl;
    }
    system("pause");
    system("cls");
}
//查找联系人
void chazhao(tongxunlu* tongxunlus) {
    cout << "输入需要查找的联系人:" << endl;
    string name;
    cin >> name;
    //判断指定联系人是否存在
    int shou = panduan(tongxunlus, name);
    if (shou != -1) {
        cout << "姓名" << tongxunlus->lianxirens[shou].mingzi << "\t";
        cout << "年龄" << tongxunlus->lianxirens[shou].nianling << "\t";
        cout << "性别" << tongxunlus->lianxirens[shou].nannv << "\t";
        cout << "电话" << tongxunlus->lianxirens[shou].dianhua << "\t";
        cout << "住址" << tongxunlus->lianxirens[shou].zhuzhi << endl;
    }
    else {
        cout << "未找到此人" << endl;
    }
    system("pause");
    system("cls");
}
//修改联系人
void xiugai(tongxunlu* tongxunlus) {
    cout << "请输入你要修改的联系人的姓名:" << endl;
    string name;
    cin >> name;
    int jie = panduan(tongxunlus, name);
    if (jie != -1) {
        //姓名
        string xingming;
        cout << "请输入姓名:" << endl;
        cin >> xingming;
        tongxunlus->lianxirens[jie].mingzi = xingming;

        //性别
        cout << "请输入性别:" << endl;
        cout << "1----男" << endl;
        cout << "2----女" << endl;
        int xingbies;
        while (true) {
            cin >> xingbies;
            if (xingbies == 1 || xingbies == 2) {
                tongxunlus->lianxirens[jie].nannv = xingbies;
                break;
                }
            cout << "输入有误,请重新输入" << endl;
        }
        
        //年龄
        cout << "请输入年龄:" << endl;
        int nianlings;
        cin >> nianlings;
        tongxunlus->lianxirens[jie].nianling = nianlings;

        //电话
        cout << "请输入电话号码:" << endl;
        string dianhuas;
        cin >> dianhuas;
        tongxunlus->lianxirens[jie].dianhua = dianhuas;
        //住址
        cout << "请输入家庭住址:" << endl;
        string zhuzhis;
        cin >> zhuzhis;
        tongxunlus->lianxirens[jie].zhuzhi = zhuzhis;
    }
    else {
        cout << "未找到联系人" << endl;
    }
    system("pause");
    system("cls");


}
//清空联系人
void qingkong(tongxunlu* tongxunlus) {
    tongxunlus->dangqian == 0;
    cout << "通讯录已被清空" << endl;
    system("pause");
    system("cls");
}
int main(){
    //创建通讯里结构体变量
    struct tongxunlu tongxunlus;
    //通讯录当前人员个数
    tongxunlus.dangqian = 0;

    int xuanze;//用户选择变量
    
    //循环进行选择,知道选择零推出
    while (true) {
        caidan();//调用菜单函数
        cin >> xuanze;
        switch (xuanze)
            {
            case 1:   //1添加联系人
                tianjia(&tongxunlus);
                break;
            case 2:  //2显示联系人
                xianshi(&tongxunlus);
                break;
            case 3:  //3删除联系人
                shanchu(&tongxunlus);
                break;
            case 4:  //4查找联系人
                chazhao(&tongxunlus);
                break;
            case 5:  //5修改联系人 
                xiugai(&tongxunlus);
                break;
            case 6:  //6清空联系人 
                qingkong(&tongxunlus);
                break;
            case 0: //0推出通讯录
                cout << "欢迎下次使用" << endl;
                return 0;//结束程序
                break;
            default:
                break;
            }
    }
    
}

本源码实现的是一个通讯录的功能,主要练习的是结构体,指针与自定义函数等知识。