这篇文章主要介绍“C++如何实现简易通讯录管理系统”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“C++如何实现简易通讯录管理系统”文章能帮助大家解决问题。
建议收藏,亲手写一遍代码,感受指针神奇的魅力;
可以帮助你更好的巩固知识体系,熟悉指针,结构体与函数一起使用时的妙处
完成通讯录管理系统所需知识体系
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"); }
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 100 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(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"); }
由于代码量过大,建议大家可以试着分文件编写代码,也可以方便查看
关于“C++如何实现简易通讯录管理系统”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。