• 通讯录管理系统C++版本


    前言:小刘之前用C++写了一个通讯录管理系统具体描述如下;

    系统功能说明

    在这里插入图片描述

    系统功能说明:
    初始化功能后,根据命令指令提示操作相应的功能。
    1.导入初始数据
    根据模板文件,将模板文件的数据导入到系统中
    2.显示信息
    展示系统中通讯录列表数据
    3.输入记录
    根据提示将输入的姓名,手机号,邮件,年龄,性别,地址等信息添加到系统中
    4.删除记录
    根据通讯录列表记录的编号将系统中该记录删除
    5.查询记录
    根据提示的搜索条件进行按照姓名或者手机号的维度进行搜索查询
    6.编辑记录
    根据提示按照通讯录的编号查询此记录并且按照提示进行编辑操作
    7.去重
    根据提示的去重条件进行按照姓名或者手机号的维度进行去重
    8.批量导出数据
    将系统中的数据批量全部导出到模板导出文件中
    9.退出
    退出系统程序

    开发环境

    1.操作系统:Windows 10
    2.运行环境:mingw64
    3.开发工具:CLion 2020.1 x64
    4.DEBUG模式运行项目

    三、系统运行效果截图

    1.导入模板文件
    在这里插入图片描述

    2.导入初始数据并显示数据
    在这里插入图片描述

    3.输入记录并显示数据
    在这里插入图片描述

    4.删除记录并显示信息
    添加记录一共7个,删除编号为7的记录
    在这里插入图片描述

    5.查询数据并显示数据
    按姓名查询
    在这里插入图片描述

    按手机查询
    在这里插入图片描述

    编辑记录
    选择编号为4的记录进行编辑
    在这里插入图片描述

    6.去重数据并显示数据
    按姓名去重
    在这里插入图片描述

    按手机号去重
    在这里插入图片描述

    7.导出数据到模板文件
    在这里插入图片描述

    项目源码如下

    1.#include <iostream>  
    2.#include <string>  
    3.#include <fstream>  
    4.  
    5.using namespace std;  
    6.#define MAX 1000//定义通讯录最大容量为1000  
    7.  
    8./** 
    9. * 联系人结构体 
    10. */  
    11.struct person {  
    12.    int id;         //编号  
    13.    string name;    //姓名  
    14.    string phone;   //手机号  
    15.    string email;   //邮箱  
    16.    string sex;     //性别  
    17.    int age;        //年龄  
    18.    string address; //住址  
    19.    int state;      //去重状态100 表示该记录已重复  
    20.};  
    21.  
    22./** 
    23. * 通讯录结构体 
    24. */  
    25.struct contact {  
    26.    struct person p[MAX];  
    27.    int size; //容量  
    28.    int index; //自增  
    29.};  
    30.  
    31./** 
    32. * 操作菜单 
    33. */  
    34.void showMenu() {  
    35.    cout << "\n*********通讯录管理系统**********" << endl;  
    36.    cout << "********* 1.导入初始数据  **********" << endl;  
    37.    cout << "********* 2.显示信息     **********" << endl;  
    38.    cout << "********* 3.输入记录     **********" << endl;  
    39.    cout << "********* 4.删除记录     **********" << endl;  
    40.    cout << "********* 5.查询记录     **********" << endl;  
    41.    cout << "********* 6.编辑记录     **********" << endl;  
    42.    cout << "********* 7.去重        **********" << endl;  
    43.    cout << "********* 8.批量导出数据 **********" << endl;  
    44.    cout << "********* 0.退出通讯录   **********" << endl;  
    45.    cout << "请输入你的选择:" << endl;  
    46.}  
    47.  
    48./** 
    49. * 显示联系人函数 
    50. * @param con 
    51. */  
    52.void showTable(contact *con) {  
    53.    cout << "================显示联系人=================" << endl;  
    54.    if (con->size == 0) {  
    55.        cout << "暂无联系人" << endl;  
    56.    } else {  
    57.        for (int i = 0; i < con->size; i++) {  
    58.            cout  
    59.                    << "  编号: " << con->p[i].id  
    60.                    << "  姓名: " << con->p[i].name  
    61.                    << "  手机: " << con->p[i].phone  
    62.                    << "  邮箱: " << con->p[i].email  
    63.                    << "  年龄: " << con->p[i].age  
    64.                    << "  性别: " << con->p[i].sex  
    65.                    << "  地址: " << con->p[i].address  
    66.                    << endl;  
    67.        }  
    68.    }  
    69.}  
    70.  
    71./** 
    72. * 添加联系人函数 
    73. * @param con 
    74. */  
    75.void addPerson(contact *con) {//传入数组的指针  
    76.    if (con->size == MAX) {  
    77.        cout << con->size;  
    78.        cout << "通讯录已满" << endl;  
    79.    } else {  
    80.        cout << "输入姓名:";  
    81.        string name;  
    82.        cin >> name;  
    83.  
    84.        cout << "输入手机号:";  
    85.        string phone;  
    86.        cin >> phone;  
    87.  
    88.        cout << "输入邮箱:";  
    89.        string email;  
    90.        cin >> email;  
    91.  
    92.  
    93.        cout << "输入年龄:";  
    94.        int age;  
    95.        cin >> age;  
    96.  
    97.        cout << "输入性别【男/女】:";  
    98.        string sex;  
    99.        cin >> sex;  
    100.        cout << "输入地址:";  
    101.        string address;  
    102.        cin >> address;  
    103.        con->p[con->size].name = name;  
    104.        con->p[con->size].phone = phone;  
    105.        con->p[con->size].email = email;  
    106.        con->p[con->size].age = age;  
    107.        con->p[con->size].sex = sex;  
    108.        con->p[con->size].address = address;  
    109.  
    110.        con->p[con->size].id = con->index;  
    111.        con->size++;  
    112.        con->index++;  
    113.    }  
    114.    cout << "添加成功" << endl;  
    115.}  
    116.  
    117./** 
    118. * 封装查询联系人是否存在 
    119. * @param con 
    120. * @param name 
    121. * @return 
    122. */  
    123.int isExist(contact *con, string name) {  
    124.    for (int i = 0; i < con->size; i++) {  
    125.        if (con->p[i].name == name) {  
    126.            return i;  
    127.        }  
    128.    }  
    129.    return -1;  
    130.}  
    131.  
    132./** 
    133. * id是否存在 
    134. * @param con 
    135. * @param id 
    136. * @return 
    137. */  
    138.int isIdExist(contact *con, int id) {  
    139.    for (int i = 0; i < con->size; i++) {  
    140.        if (con->p[i].id == id) {  
    141.            return i;  
    142.        }  
    143.    }  
    144.    return -1;  
    145.}  
    146.  
    147.  
    148./** 
    149. * 按照编号id 删除 
    150. * @param con 
    151. */  
    152.void deleteById(contact *con) {  
    153.    int id;  
    154.    cout << "输入要删除的记录编号";  
    155.    cin >> id;  
    156.    int res = isIdExist(con, id);  
    157.    if (res != -1) {  
    158.        for (int i = res; i < con->size; i++) {  
    159.            con->p[res] = con->p[res + 1];  
    160.        }  
    161.        cout << "删除成功" << endl;  
    162.        con->size--;  
    163.    } else {  
    164.        cout << "查无此记录" << endl;  
    165.    }  
    166.}  
    167.  
    168./** 
    169. * 修改联系人函数 
    170. * @param con 
    171. */  
    172.void updateById(contact *con) {  
    173.    int id;  
    174.    cout << "输入要修改的联系人编号" << endl;  
    175.    cin >> id;  
    176.    int res = isIdExist(con, id);  
    177.    if (res != -1) {  
    178.        cout  
    179.                << "编号:" << con->p[res].id  
    180.                << "姓名:" << con->p[res].name  
    181.                << "手机:" << con->p[res].phone  
    182.                << "邮箱:" << con->p[res].email  
    183.                << "年龄:" << con->p[res].age  
    184.                << "性别:" << con->p[res].sex  
    185.                << "地址:" << con->p[res].address  
    186.                << endl;  
    187.  
    188.        cout << "输入姓名:";  
    189.        string name;  
    190.        cin >> name;  
    191.        con->p[res].name = name;  
    192.  
    193.        cout << "输入手机号:";  
    194.        string phone;  
    195.        cin >> phone;  
    196.        con->p[res].phone = phone;  
    197.  
    198.        cout << "输入邮箱:";  
    199.        string email;  
    200.        cin >> email;  
    201.        con->p[res].email = email;  
    202.  
    203.        cout << "输入年龄:";  
    204.        int age;  
    205.        cin >> age;  
    206.        con->p[res].age = age;  
    207.  
    208.        cout << "输入性别【男/女】:";  
    209.        string sex;  
    210.        cin >> sex;  
    211.        con->p[res].sex = sex;  
    212.  
    213.        cout << "输入地址:";  
    214.        string address;  
    215.        cin >> address;  
    216.        con->p[res].address = address;  
    217.  
    218.        cout << "修改成功" << endl;  
    219.    } else {  
    220.        cout << "查无此人" << endl;  
    221.    }  
    222.}  
    223.  
    224.  
    225./** 
    226. * 退出系统函数 
    227. * @return 
    228. */  
    229.int exitSystem() {  
    230.    exit(0);  
    231.}  
    232.  
    233./** 
    234. * 导入数据  按照模板导入数据,一行为一个属性字段的值 
    235. */  
    236.void importInfo(contact *con) {  
    237.  
    238.    // 以读模式打开文件  
    239.    ifstream infile;  
    240.    infile.open("import.txt");  
    241.  
    242.    if (!infile.is_open()) {  
    243.        cout << "无法找到这个文件!" << endl;  
    244.  
    245.    }  
    246.    string buff;  
    247.    string arr[6];  
    248.    int index = 0;  
    249.    cout << "读取模板文件如下" << endl;  
    250.    while (getline(infile, buff)) {  
    251.        cout << buff << endl;  
    252.        arr[index] = buff;  
    253.        index++;  
    254.    }  
    255.    std::string str = arr[3];  
    256.    int n = atoi(str.c_str());  
    257.    con->p[con->size].name = arr[0];  
    258.    con->p[con->size].phone = arr[1];  
    259.    con->p[con->size].email = arr[2];  
    260.    con->p[con->size].age = n;  
    261.    con->p[con->size].sex = arr[4];  
    262.    con->p[con->size].address = arr[5];  
    263.  
    264.    con->p[con->size].id = con->index;  
    265.    con->size++;  
    266.    con->index++;  
    267.  
    268.    infile.close();  
    269.    // 关闭打开的文件  
    270.    infile.close();  
    271.  
    272.}  
    273.  
    274./** 
    275. * 导出数据 
    276. */  
    277.void exportInfo(contact *con) {  
    278.    if (con->size == 0) {  
    279.        cout << "暂无联系人" << endl;  
    280.    } else {  
    281.        ofstream ofile;                     //定义输出文件  
    282.        ofile.open("exportInfo.txt");     //作为输出文件打开  
    283.        for (int i = 0; i < con->size; i++) {  
    284.            ofile  
    285.                    << "  编号:" << con->p[i].id  
    286.                    << "  姓名:" << con->p[i].name  
    287.                    << "  姓名:" << con->p[i].phone  
    288.                    << "  姓名:" << con->p[i].email  
    289.                    << "  年龄:" << con->p[i].age  
    290.                    << "  性别:" << con->p[i].sex  
    291.                    << "  地址:" << con->p[i].address  
    292.                    << endl;  
    293.        }  
    294.        ofile.close();      //关闭文件  
    295.  
    296.        cout << "批量导出数据完成" << endl;  
    297.    }  
    298.  
    299.  
    300.}  
    301.  
    302.  
    303./** 
    304. * 去重检索 
    305. * @param con 
    306. */  
    307.void deWeight(contact *con) {  
    308.    cout << "请输入要去重检索的维度:a:姓名;b:手机号 :\n";  
    309.    string i;  
    310.    cin >> i;  
    311.    string msg;  
    312.    if (i == "a") {  
    313.        string name = con->p[0].name;  
    314.        for (int i = 1; i < con->size; i++) {  
    315.            if (con->p[i].name == name) {  
    316.                con->p[i].state = 100;  //重复的标识  
    317.            }  
    318.        }  
    319.        for (int i = 0; i < con->size; i++) {  
    320.            if (con->p[i].state != 100) {  
    321.                cout  
    322.                        << "  编号:" << con->p[i].id  
    323.                        << "  姓名:" << con->p[i].name  
    324.                        << "  手机:" << con->p[i].phone  
    325.                        << "  邮箱:" << con->p[i].email  
    326.                        << "  年龄:" << con->p[i].age  
    327.                        << "  性别:" << con->p[i].sex  
    328.                        << "  地址:" << con->p[i].address  
    329.                        << endl;  
    330.            }  
    331.  
    332.            con->p[i].state = 0;//恢复  
    333.        }  
    334.  
    335.  
    336.    } else if (i == "b") {  
    337.        string phone = con->p[0].phone;  
    338.  
    339.        for (int i = 1; i < con->size; i++) {  
    340.            if (con->p[i].phone == phone) {  
    341.                con->p[i].state = 100;  
    342.            }  
    343.        }  
    344.        for (int i = 0; i < con->size; i++) {  
    345.            if (con->p[i].state != 100) {  
    346.                cout  
    347.                        << "  编号:" << con->p[i].id  
    348.                        << "  姓名:" << con->p[i].name  
    349.                        << "  手机:" << con->p[i].phone  
    350.                        << "  邮箱:" << con->p[i].email  
    351.                        << "  年龄:" << con->p[i].age  
    352.                        << "  性别:" << con->p[i].sex  
    353.                        << "  地址:" << con->p[i].address  
    354.                        << endl;  
    355.            }  
    356.        }  
    357.  
    358.    } else {  
    359.        cout << "请输入合法的命令" << endl;  
    360.    }  
    361.}  
    362.  
    363.  
    364./** 
    365. * 按照名称搜索 
    366. * @param con 
    367. * @param name 
    368. */  
    369.void selectByName(contact *con, string name) {  
    370.    for (int i = 0; i < con->size; i++) {  
    371.        if (con->p[i].name == name) {  
    372.            cout  
    373.                    << "  编号:" << con->p[i].id  
    374.                    << "  姓名:" << con->p[i].name  
    375.                    << "  手机:" << con->p[i].phone  
    376.                    << "  邮箱:" << con->p[i].email  
    377.                    << "  年龄:" << con->p[i].age  
    378.                    << "  性别:" << con->p[i].sex  
    379.                    << "  地址:" << con->p[i].address  
    380.                    << endl;  
    381.        }  
    382.    }  
    383.}  
    384.  
    385./** 
    386. * 按照手机号搜索 
    387. * @param con 
    388. * @param phone 
    389. */  
    390.void selectByPhone(contact *con, string phone) {  
    391.    for (int i = 0; i < con->size; i++) {  
    392.        if (con->p[i].phone == phone) {  
    393.            cout  
    394.                    << "  编号:" << con->p[i].id  
    395.                    << "  姓名:" << con->p[i].name  
    396.                    << "  姓名:" << con->p[i].phone  
    397.                    << "  姓名:" << con->p[i].email  
    398.                    << "  年龄:" << con->p[i].age  
    399.                    << "  性别:" << con->p[i].sex  
    400.                    << "  地址:" << con->p[i].address  
    401.                    << endl;  
    402.        }  
    403.    }  
    404.}  
    405.  
    406./** 
    407. * 查询 
    408. * @param con 
    409. */  
    410.void select(contact *con) {  
    411.    cout << "请输入要查看搜索的维度:a:姓名;b:手机号";  
    412.    string i;  
    413.    cin >> i;  
    414.    string msg;  
    415.    if (i == "a") {  
    416.        cout << "请输入姓名:";  
    417.        cin >> msg;  
    418.        selectByName(con, msg);  
    419.    } else if (i == "b") {  
    420.        cout << "请输入手机号:";  
    421.        cin >> msg;  
    422.        selectByPhone(con, msg);  
    423.    } else {  
    424.        cout << "请输入合法的命令" << endl;  
    425.    }  
    426.}  
    427.  
    428.  
    429./** 
    430.通讯录管理系统主程序 
    431.*/  
    432.int main() {  
    433.  
    434.    contact con;                  //结构体  
    435.    con.size = 0;                 //长度  
    436.    con.index = 1;                //索引  
    437.    while (true) {  
    438.        showMenu();               //循环显示菜单  
    439.        int i;  
    440.        cin >> i;  
    441.        switch (i) {  
    442.            case 1:  
    443.                importInfo(&con); //导入数据  
    444.                break;  
    445.            case 2:  
    446.                showTable(&con);  //显示信息  
    447.                break;  
    448.            case 3:  
    449.                addPerson(&con);  //输入记录  
    450.                break;  
    451.            case 4:  
    452.                deleteById(&con); //删除记录  
    453.                break;  
    454.            case 5:  
    455.                select(&con);     //查询记录  
    456.                break;  
    457.            case 6:  
    458.                updateById(&con); //编辑记录  
    459.                break;  
    460.            case 7:  
    461.                deWeight(&con);   //去重检索  
    462.                break;  
    463.            case 8:  
    464.                exportInfo(&con); //批量导出数据  
    465.                break;  
    466.            case 0:  
    467.                exitSystem();     //退出通讯录  
    468.                break;  
    469.        }  
    470.    }  
    471.}  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358
    • 359
    • 360
    • 361
    • 362
    • 363
    • 364
    • 365
    • 366
    • 367
    • 368
    • 369
    • 370
    • 371
    • 372
    • 373
    • 374
    • 375
    • 376
    • 377
    • 378
    • 379
    • 380
    • 381
    • 382
    • 383
    • 384
    • 385
    • 386
    • 387
    • 388
    • 389
    • 390
    • 391
    • 392
    • 393
    • 394
    • 395
    • 396
    • 397
    • 398
    • 399
    • 400
    • 401
    • 402
    • 403
    • 404
    • 405
    • 406
    • 407
    • 408
    • 409
    • 410
    • 411
    • 412
    • 413
    • 414
    • 415
    • 416
    • 417
    • 418
    • 419
    • 420
    • 421
    • 422
    • 423
    • 424
    • 425
    • 426
    • 427
    • 428
    • 429
    • 430
    • 431
    • 432
    • 433
    • 434
    • 435
    • 436
    • 437
    • 438
    • 439
    • 440
    • 441
    • 442
    • 443
    • 444
    • 445
    • 446
    • 447
    • 448
    • 449
    • 450
    • 451
    • 452
    • 453
    • 454
    • 455
    • 456
    • 457
    • 458
    • 459
    • 460
    • 461
    • 462
    • 463
    • 464
    • 465
    • 466
    • 467
    • 468
    • 469
    • 470
    • 471

    有啥不懂得小伙伴们加群交流啦:852665736;

    无偿有偿免费分享源码以及技术和面试文档,更多优秀精致的源码技术栈分享请关注微信公众号:gh_817962068649

  • 相关阅读:
    一、什么是JAVA
    制作网线——双绞线
    菜鸟、顺丰、京东物流:无“智”难行?
    Mysql各种锁
    Unity Rain Ai 插件的使用入门
    Java进阶3 - 易错知识点整理(待更新)
    HarmonyOS 学习方法
    Android设计模式--策略模式
    点云深度学习系列博客(三): 多尺度特征分析
    分享一个基于Python的电子产品销售系统可视化销量统计java版本相同(源码+调试+开题+lw)
  • 原文地址:https://blog.csdn.net/qq_38310446/article/details/125457777