用C++实现简单的通讯录系统,主要功能有添加、修改、删除、清空、查询、显示、退出
首先定义结构体
#define MAX 1000 // 联系人结构体 struct phoneMan { string name; // 姓名 int sex; // 性别 : 1 . 男 2 . 女 int age; // 年龄 string phone; // 电话 string address; // 住址 }; // 通讯录结构体 struct phoneBooks { struct phoneMan personArray[MAX]; // 通讯录中保存的联系人数组 int size; // 通讯录中的人数 };
声明主要函数
// 声明函数 void showMenu(); // 显示菜单界面 void addPerson(struct phoneBooks* pbs); // 添加联系人 void showPerson(struct phoneBooks* pbs); // 显示联系人 int isExist(struct phoneBooks* pbs, string name); // 检测联系人是否存在,不存在则返回-1 void deletePerson(struct phoneBooks* pbs); // 删除联系人 void findPerson(struct phoneBooks* pbs); // 查找联系人 void modifyPerson(struct phoneBooks* pbs); // 修改联系人 void cleanPerson(struct phoneBooks* pbs); // 清空所有联系人
接下来一个一个实现主要函数
showMenu() 菜单界面
void showMenu() { 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; }
addPerson(struct phoneBooks* pbs) 添加联系人
void addPerson(struct phoneBooks* pbs) { // 判断通讯录是否已满 if (pbs ->size == MAX) { cout << "通讯录已满,无法添加!" << endl; return; } else { // 姓名 string name; cout << "请输入姓名:" ; cin >> name; pbs->personArray[pbs->size].name = name; cout << endl; // 换行 // 性别 int sex; while (true) { cout << "1 为男性, 2 为女性" << endl; cout << "请输入性别:" ; cin >> sex; if (sex == 1 || sex == 2) { pbs->personArray[pbs->size].sex = sex; cout << endl; break; } cout << "性别有误,请重新输入" << endl; } // 年龄 int age; cout << "请输入年龄:"; cin >> age; pbs->personArray[pbs->size].age = age; cout << endl; // 联系方式 string phone; cout << "请输入联系方式:"; cin >> phone; pbs->personArray[pbs->size].phone = phone; cout << endl; // 住址 string address; cout << "请输入住址:"; cin >> address; pbs->personArray[pbs->size].address = address; cout << endl; // 通讯录人数变化 pbs->size++; cout << "添加成功,即将返回主界面" << endl; system("pause"); // 按任意键继续 system("cls"); // 清屏 } }
showPerson(struct phoneBooks* pbs) 显示所有联系人
void showPerson(struct phoneBooks* pbs) { // 判断通讯录是否为空 if (pbs->size == 0) { cout << "通讯录为空,无具体联系人" << endl; return; } else { for (int i = 0; i < pbs->size; i++) { cout << "姓名: " << pbs->personArray[i].name << "\t性别: "<< (pbs->personArray[i].sex == 1 ? "男" : "女") << "\t年龄: " << pbs->personArray[i].age << "\t联系方式: " << pbs->personArray[i].phone << "\t\t住址: " << pbs->personArray[i].address << endl; } cout << "以上为通讯录所有联系人" << endl; // 清屏 system("pause"); system("cls"); } }
isExist(struct phoneBooks* pbs, string name) 查找联系人是否存在通讯录中,不存在返回-1
int isExist(struct phoneBooks* pbs, string name) { for (int i = 0; i < pbs->size; i++) { if (pbs->personArray[i].name == name) { return i; } } return -1; }
deletePerson(struct phoneBooks* pbs); 删除联系人
void deletePerson(struct phoneBooks* pbs) { // 先进行查找 string name; cout << "请输入要删除的联系人姓名:"; cin >> name; int index = isExist(pbs, name); cout << endl; if (index == -1) { cout << "通讯录中查无此人" << endl; return; } else { for (int i = index; i < pbs->size; i++) { // 数据前移 pbs->personArray[i] = pbs->personArray[i + 1]; } // 更新通讯录里的人员数 pbs->size--; cout << "删除成功" << endl; // 清屏 system("pause"); system("cls"); } }
findPerson(struct phoneBooks* pbs); 查找联系人
void findPerson(struct phoneBooks* pbs) { // 先判断是否存在 cout << "请输入您要查找的联系人姓名:"; string name; cin >> name; int index = isExist(pbs, name); cout << endl; if (index == -1) { cout << "该联系人不存在" << endl; return; } else { cout << "以下为联系人基本信息" << endl; cout << "姓名: " << pbs->personArray[index].name << "\t性别: " << (pbs->personArray[index].sex == 1 ? "男" : "女") << "\t年龄: " << pbs->personArray[index].age << "\t联系方式: " << pbs->personArray[index].phone << "\t\t住址: " << pbs->personArray[index].address << endl; } // 清屏 system("pause"); system("cls"); }
modifyPerson(struct phoneBooks* pbs); 修改联系人
这里的修改默认姓名和性别不会更改
void modifyPerson(struct phoneBooks* pbs) { // 先进行查找 cout << "请输入您想修改的联系人姓名:"; string name; cin >> name; int index = isExist(pbs, name); cout << endl; if (index == -1) { cout << "该联系人不存在" << endl; return; } else { // 年龄 int age; cout << "请输入修改后的年龄:"; cin >> age; pbs->personArray[index].age = age; cout << endl; // 联系方式 string phone; cout << "请输入修改后的联系方式:"; cin >> phone; pbs->personArray[index].phone = phone; cout << endl; // 住址 string address; cout << "请输入修改后的住址:"; cin >> address; pbs->personArray[index], address = address; cout << endl; cout << "修改成功" << endl; cout << "以下为修改后的信息" << endl; cout << "姓名: " << pbs->personArray[index].name << "\t性别: " << (pbs->personArray[index].sex == 1 ? "男" : "女") << "\t年龄: " << pbs->personArray[index].age << "\t联系方式: " << pbs->personArray[index].phone << "\t\t住址: " << pbs->personArray[index].address << endl; } // 清屏 system("pause"); system("cls"); }
void cleanPerson(struct phoneBooks* pbs); 清空所有联系人
void cleanPerson(struct phoneBooks* pbs) { pbs->size = 0; cout << "通讯录已清空" << endl; // 清屏 system("pause"); system("cls"); }
main函数里的主要代码
int main() { int select = 0; // 创建结构体变量 phoneBooks pbs; // 初始化通讯录当前人员 pbs.size = 0; while (true) { // 显示菜单界面 showMenu(); // 记录用户选择 cout << "请输入您的选择:"; cin >> select; switch (select) { case 1: // 1. 添加联系人 addPerson(&pbs); break; case 2: // 2. 显示联系人 showPerson(&pbs); break; case 3: // 3. 删除联系人 deletePerson(&pbs); break; case 4: // 4. 查找联系人 findPerson(&pbs); break; case 5: // 5. 修改联系人 modifyPerson(&pbs); break; case 6: // 6. 清空联系人 cleanPerson(&pbs); break; case 0: // 0. 退出通讯录 cout << "欢迎下次使用" << endl; system("pause"); return 0; break; default: cout << "输入的选择有误,请重新输入" << endl; } } system("pause"); return 0; }