AI智能
改变未来

C++实现通讯录管理系统

目录

  • 前言
  • 结构体
  • 联系人结构体
  • 通讯录结构体
  • 函数模块
    • 菜单
    • 添加联系人
    • 显示联系人
    • 判断联系人
    • 删除联系人
    • 查找联系人
    • 清空所有联系人
  • main函数
  • 源代码
  • 运行结果
  • 前言

    **

    建议收藏,亲手写一遍代码,感受指针神奇的魅力;
    可以帮助你更好的巩固知识体系,熟悉指针,结构体与函数一起使用时的妙处

    **

    完成通讯录管理系统所需知识体系
    1.结构体
    2.指针
    3.函数的封装
    4.指针与函数的结合使用
    5.指针与结构体的结合使用

    结构体

    联系人结构体

    struct person{string name;//姓名string sex; //性别int age;  //年龄string phone;//手机号string home;//地址};

    通讯录结构体

    struct addressbook{struct person personArray[MAX]; //通讯录扩展到100;int size=0;  //当前联系人个数(后面就相当于i++)};

    函数模块

    void menu();//菜单void putit(addressbook* add);//添加联系人void showperson(addressbook* add);// 显示联系人int if_include(addressbook* add, string name);//判断联系人void deleteperson(addressbook* add, int i);//删除联系人void findPerson( addressbook* add);//查找联系人void cleanperson(struct addressbook* add);//清空所有联系人

    菜单

    void menu(){cout << endl;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 putit(addressbook* add)//添加联系人功能{if (add->size == MAX)cout << "通讯录已满" << endl;else{string name;string sex;int age;string phone;string home;cout << "请输入姓名" << endl;cin >> name;add->personArray[add->size].name = name;cout << "请输入姓别" << endl;cin >> sex;add->personArray[add->size].sex = sex;cout << "请输入年龄" << endl;cin >> age;add->personArray[add->size].age = age;cout << "请输入电话号码" << endl;cin >> phone;add->personArray[add->size].phone = phone;cout << "请输入家庭住址" << endl;cin >> home;add->personArray[add->size].home = home;add->size++;cout << "添加联系人成功" << endl;}system("pause");system("cls");menu();}

    有人问添加联系人函数中为什么要使用指针?
    因为值传递中形参无法改变实参;
    而利用指针的地址传递可以通过函数中的形参改变实参;
    具体原理可以参考博主之前的指针基础和指针进阶内容]

    显示联系人

    void showperson(addressbook* add){for (int i = 0; i <add->size; i++){cout << "姓名:  " << add->personArray[i].name;cout << "\\t姓别:  " << add->personArray[i].sex;cout << "\\t年龄:  " << add->personArray[i].age;cout << "\\t电话号码:  " << add->personArray[i].phone;cout << "\\t家庭住址:  " << add->personArray[i].home << endl;}system("pause");system("cls");menu();}

    判断联系人

    int if_include(addressbook* add, string name){for (int i = 0; i < add->size; i++){if (name == add->personArray[i].name){return i;}else{return -1;}}}

    删除联系人

    void deleteperson(addressbook* add, int i){for (; i < add->size; i++){add->personArray[i] = add->personArray[i + 1];}system("pause");system("cls");}

    查找联系人

    void findPerson( addressbook* add){cout << "请输入您想要查找的联系人:" << endl;string name;cin >> name;int ret = if_include(add, name);if (ret == -1){cout << "查无此人" << endl;}else{   //查到此人,进行显示操作int i = ret;cout << "姓名:" << add->personArray[i].name << "\\t";cout << "性别:" << add->personArray[i].sex << "\\t";cout << "年龄:" << add->personArray[i].age << "\\t";cout << "联系方式:" << add->personArray[i].phone << "\\t";cout << "地址:" << add->personArray[i].home << endl;}//按任意键清屏system("pause");system("cls");}

    清空所有联系人

    void cleanperson(struct addressbook* add)//清空所有联系人{cout << "是否确认清空?" << endl;cout << "1 --- 是" << endl;cout << "2 --- 否" << endl;int a;cin >> a;if (a == 1){add->size = 0;//将当前记录联系人数量置为0,做逻辑上的清空操作cout << "通讯录已清空" << endl;}system("pause");system("cls");}

    main函数

    int main(){menu();addressbook add;//定义一个通讯录int choice=1;while (choice != 0){cin >> choice;switch (choice)//选择{case 1: putit(&add);break;case 2: showperson(&add);break;case 3: {cout << "请输入你要删除的人的名字" << endl;string aname;cin >> aname;if (if_include(&add, aname) == -1){cout << "查无此人" << endl;break;}if (if_include(&add, aname)){deleteperson(&add, if_include(&add, aname));}}break;case 4:break;case 5:break;case 6:break;case 0: cout << "欢迎下次使用" << endl;return 0;break;default: {cout << "输入不合法,请重新输入" << endl;break; }}}}

    源代码

    #include<iostream>using namespace std;#include<string>#define MAX 100struct person{string name;//姓名string sex; //性别int age;  //年龄string phone;//手机号string home;//地址};struct addressbook{struct person personArray[MAX]; //通讯录扩展到100;int size=0;  //当前联系人个数(后面就相当于i++)};void menu();//菜单void putit(addressbook* add);void showperson(addressbook* add);int if_include(addressbook* add, string name);void deleteperson(addressbook* add, int i);void findPerson(struct addressbooks* add);//查找联系人void cleanperson(struct addressbook* add);//清空所有联系人int main(){menu();addressbook add;int choice=1;while (choice != 0){cin >> choice;switch (choice){case 1: putit(&add);break;case 2: showperson(&add);break;case 3: {cout << "请输入你要删除的人的名字" << endl;string aname;cin >> aname;if (if_include(&add, aname) == -1){cout << "查无此人" << endl;break;}if (if_include(&add, aname)){deleteperson(&add, if_include(&add, aname));}}break;case 4:break;case 5:break;case 6:break;case 0: cout << "欢迎下次使用" << endl;return 0;break;default: {cout << "输入不合法,请重新输入" << endl;break; }}}}void menu(){cout << endl;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 putit(addressbook* add)//添加联系人功能{if (add->size == MAX)cout << "通讯录已满" << endl;else{string name;string sex;int age;string phone;string home;cout << "请输入姓名" << endl;cin >> name;add->personArray[add->size].name = name;cout << "请输入姓别" << endl;cin >> sex;add->personArray[add->size].sex = sex;cout << "请输入年龄" << endl;cin >> age;add->personArray[add->size].age = age;cout << "请输入电话号码" << endl;cin >> phone;add->personArray[add->size].phone = phone;cout << "请输入家庭住址" << endl;cin >> home;add->personArray[add->size].home = home;add->size++;cout << "添加联系人成功" << endl;}system("pause");system("cls");menu();}void showperson(addressbook* add){for (int i = 0; i <add->size; i++){cout << "姓名:  " << add->personArray[i].name;cout << "\\t姓别:  " << add->personArray[i].sex;cout << "\\t年龄:  " << add->personArray[i].age;cout << "\\t电话号码:  " << add->personArray[i].phone;cout << "\\t家庭住址:  " << add->personArray[i].home << endl;}system("pause");system("cls");menu();}int if_include(addressbook* add, string name){for (int i = 0; i < add->size; i++){if (name == add->personArray[i].name){return i;}else{return -1;}}}void deleteperson(addressbook* add, int i){for (; i < add->size; i++){add->personArray[i] = add->personArray[i + 1];}system("pause");system("cls");}void findPerson( addressbook* add){cout << "请输入您想要查找的联系人:" << endl;string name;cin >> name;int ret = if_include(add, name);if (ret == -1){cout << "查无此人" << endl;}else{   //查到此人,进行显示操作int i = ret;cout << "姓名:" << add->personArray[i].name << "\\t";cout << "性别:" << add->personArray[i].sex << "\\t";cout << "年龄:" << add->personArray[i].age << "\\t";cout << "联系方式:" << add->personArray[i].phone << "\\t";cout << "地址:" << add->personArray[i].home << endl;}//按任意键清屏system("pause");system("cls");}void cleanperson(struct addressbook* add)//清空所有联系人{cout << "是否确认清空?" << endl;cout << "1 --- 是" << endl;cout << "2 --- 否" << endl;int a;cin >> a;if (a == 1){add->size = 0;//将当前记录联系人数量置为0,做逻辑上的清空操作cout << "通讯录已清空" << endl;}system("pause");system("cls");}

    由于代码量过大,建议大家可以试着分文件编写代码,也可以方便查看

    运行结果



    点赞收藏,反复练习

    赞(0) 打赏
    未经允许不得转载:爱站程序员基地 » C++实现通讯录管理系统