通讯录管理系统的实现
1、系统需求
通讯录是一个可以记录亲人、好友信息的工具。
本教程主要利用C++来实现一个通讯录管理系统
系统中需要实现的功能如下:
- 添加联系人:向通讯录中添加新人,信息包括(姓名、性别、年龄、联系电话、家庭住址)最多记录1000人
- 显示联系人:显示通讯录中所有联系人信息
- 删除联系人:按照姓名进行删除指定联系人
- 查找联系人:按照姓名查看指定联系人信息
- 修改联系人:按照姓名重新修改指定联系人
- 清空联系人:清空通讯录中所有信息
- 退出通讯录:退出当前使用的通讯录
2、创建项目
创建项目步骤如下:
2.1 创建项目
打开vs2017后,点击创建新项目,创建新的C++项目
填写项目名称,选择项目路径
2.2添加文件
添加成功后,效果如图:
至此,项目已创建完毕
3、菜单功能
功能描述: 用户选择功能的界面
菜单界面效果如下图:
步骤:
- 封装函数显示该界面 如
void showMenu()
- 在main函数中调用封装好的函数
代码:
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
| #include<iostream> using namespace std;
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; }
int main() {
showMenu();
system("pause");
return 0; }
|
4、退出功能
功能描述:退出通讯录系统
思路:根据用户不同的选择,进入不同的功能,可以选择switch分支结构,将整个架构进行搭建
当用户选择0时候,执行退出,选择其他先不做操作,也不会退出程序
代码:
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
| int main() {
int select = 0;
while (true) { showMenu();
cin >> select; switch (select) { case 1: break; case 2: break; case 3: break; case 4: break; case 5: break; case 6: break; case 0: cout << "欢迎下次使用" << endl; system("pause"); return 0; break; default: break; } }
system("pause");
return 0; }
|
效果图:
5、添加联系人
功能描述:
实现添加联系人功能,联系人上限为1000人,联系人信息包括(姓名、性别、年龄、联系电话、家庭住址)
添加联系人实现步骤:
- 设计联系人结构体
- 设计通讯录结构体
- main函数中创建通讯录
- 封装添加联系人函数
- 测试添加联系人功能
5.1 设计联系人结构体
联系人信息包括:姓名、性别、年龄、联系电话、家庭住址
设计如下:
1 2 3 4 5 6 7 8 9 10
| #include <string>
struct Person { string m_Name; int m_Sex; int m_Age; string m_Phone; string m_Addr; };
|
5.2 设计通讯录结构体
设计时候可以在通讯录结构体中,维护一个容量为1000的存放联系人的数组,并记录当前通讯录中联系人数量
设计如下
1 2 3 4 5 6 7 8
| #define MAX 1000
struct Addressbooks { struct Person personArray[MAX]; int m_Size; };
|
5.3 main函数中创建通讯录
添加联系人函数封装好后,在main函数中创建一个通讯录变量,这个就是我们需要一直维护的通讯录
1 2 3 4 5 6
| mian函数起始位置添加:
Addressbooks abs; abs.m_Size = 0;
|
5.4 封装添加联系人函数
思路:添加联系人前先判断通讯录是否已满,如果满了就不再添加,未满情况将新联系人信息逐个加入到通讯录
添加联系人代码:
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
| void addPerson(Addressbooks *abs) { if (abs->m_Size == MAX) { cout << "通讯录已满,无法添加" << endl; return; } else { string name; cout << "请输入姓名:" << endl; cin >> name; abs->personArray[abs->m_Size].m_Name = name;
cout << "请输入性别:" << endl; cout << "1 -- 男" << endl; cout << "2 -- 女" << endl;
int sex = 0; while (true) { cin >> sex; if (sex == 1 || sex == 2) { abs->personArray[abs->m_Size].m_Sex = sex; break; } cout << "输入有误,请重新输入"; }
cout << "请输入年龄:" << endl; int age = 0; cin >> age; abs->personArray[abs->m_Size].m_Age = age;
cout << "请输入联系电话:" << endl; string phone = ""; cin >> phone; abs->personArray[abs->m_Size].m_Phone = phone;
cout << "请输入家庭住址:" << endl; string address; cin >> address; abs->personArray[abs->m_Size].m_Addr = address;
abs->m_Size++;
cout << "添加成功" << endl; system("pause"); system("cls"); } }
|
5.5 测试添加联系人功能
选择界面中,如果玩家选择了1,代表添加联系人,我们可以测试下该功能
在switch case 语句中,case1里添加:
1 2 3
| case 1: addPerson(&abs); break;
|
测试效果如图:
6、显示联系人
功能描述:显示通讯录中已有的联系人信息
显示联系人实现步骤:
6.1 封装显示联系人函数
思路:判断如果当前通讯录中没有人员,就提示记录为空,人数大于0,显示通讯录中信息
显示联系人代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| void showPerson(Addressbooks * abs) { if (abs->m_Size == 0) { cout << "当前记录为空" << endl; } else { for (int i = 0; i < abs->m_Size; i++) { cout << "姓名:" << abs->personArray[i].m_Name << "\t"; cout << "性别:" << (abs->personArray[i].m_Sex == 1 ? "男" : "女") << "\t"; cout << "年龄:" << abs->personArray[i].m_Age << "\t"; cout << "电话:" << abs->personArray[i].m_Phone << "\t"; cout << "住址:" << abs->personArray[i].m_Addr << endl; } } system("pause"); system("cls");
}
|
6.2 测试显示联系人功能
在switch case语句中,case 2 里添加
1 2 3
| case 2: showPerson(&abs); break;
|
测试效果如图:
7、删除联系人
功能描述:按照姓名进行删除指定联系人
删除联系人实现步骤:
- 封装检测联系人是否存在
- 封装删除联系人函数
- 测试删除联系人功能
7.1 封装检测联系人是否存在
设计思路:
删除联系人前,我们需要先判断用户输入的联系人是否存在,如果存在删除,不存在提示用户没有要删除的联系人
因此我们可以把检测联系人是否存在封装成一个函数中,如果存在,返回联系人在通讯录中的位置,不存在返回-1
检测联系人是否存在代码:
1 2 3 4 5 6 7 8 9 10 11 12 13
| int isExist(Addressbooks * abs, string name) { for (int i = 0; i < abs->m_Size; i++) { if (abs->personArray[i].m_Name == name) { return i; } } return -1; }
|
7.2 封装删除联系人函数
根据用户输入的联系人判断该通讯录中是否有此人
查找到进行删除,并提示删除成功
查不到提示查无此人。
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
| void deletePerson(Addressbooks * abs) { cout << "请输入您要删除的联系人" << endl; string name; cin >> name;
int ret = isExist(abs, name); if (ret != -1) { for (int i = ret; i < abs->m_Size; i++) { abs->personArray[i] = abs->personArray[i + 1]; } abs->m_Size--; cout << "删除成功" << endl; } else { cout << "查无此人" << endl; }
system("pause"); system("cls"); }
|
7.3 测试删除联系人功能
在switch case 语句中,case3里添加:
1 2 3
| case 3: deletePerson(&abs); break;
|
测试效果如图:
存在情况:
不存在情况:
8、查找联系人
功能描述:按照姓名查看指定联系人信息
查找联系人实现步骤
8.1 封装查找联系人函数
实现思路:判断用户指定的联系人是否存在,如果存在显示信息,不存在则提示查无此人。
查找联系人代码:
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
| void findPerson(Addressbooks * abs) { cout << "请输入您要查找的联系人" << endl; string name; cin >> name;
int ret = isExist(abs, name); if (ret != -1) { cout << "姓名:" << abs->personArray[ret].m_Name << "\t"; cout << "性别:" << abs->personArray[ret].m_Sex << "\t"; cout << "年龄:" << abs->personArray[ret].m_Age << "\t"; cout << "电话:" << abs->personArray[ret].m_Phone << "\t"; cout << "住址:" << abs->personArray[ret].m_Addr << endl; } else { cout << "查无此人" << endl; }
system("pause"); system("cls");
}
|
8.2 测试查找指定联系人
在switch case 语句中,case4里添加:
1 2 3
| case 4: findPerson(&abs); break;
|
测试效果如图
存在情况:
不存在情况:
9、修改联系人
功能描述:按照姓名重新修改指定联系人
修改联系人实现步骤
9.1 封装修改联系人函数
实现思路:查找用户输入的联系人,如果查找成功进行修改操作,查找失败提示查无此人
修改联系人代码:
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
| void modifyPerson(Addressbooks * abs) { cout << "请输入您要修改的联系人" << endl; string name; cin >> name;
int ret = isExist(abs, name); if (ret != -1) { string name; cout << "请输入姓名:" << endl; cin >> name; abs->personArray[ret].m_Name = name;
cout << "请输入性别:" << endl; cout << "1 -- 男" << endl; cout << "2 -- 女" << endl;
int sex = 0; while (true) { cin >> sex; if (sex == 1 || sex == 2) { abs->personArray[ret].m_Sex = sex; break; } cout << "输入有误,请重新输入"; }
cout << "请输入年龄:" << endl; int age = 0; cin >> age; abs->personArray[ret].m_Age = age;
cout << "请输入联系电话:" << endl; string phone = ""; cin >> phone; abs->personArray[ret].m_Phone = phone;
cout << "请输入家庭住址:" << endl; string address; cin >> address; abs->personArray[ret].m_Addr = address;
cout << "修改成功" << endl; } else { cout << "查无此人" << endl; }
system("pause"); system("cls");
}
|
9.2 测试修改联系人功能
在switch case 语句中,case 5里添加:
1 2 3
| case 5: modifyPerson(&abs); break;
|
测试效果如图:
查不到指定联系人情况:
查找到联系人,并修改成功:
再次查看通讯录,确认修改完毕
10、清空联系人
功能描述:清空通讯录中所有信息
清空联系人实现步骤
10.1 封装清空联系人函数
实现思路: 将通讯录所有联系人信息清除掉,只要将通讯录记录的联系人数量置为0,做逻辑清空即可。
清空联系人代码:
1 2 3 4 5 6 7 8
| void cleanPerson(Addressbooks * abs) { abs->m_Size = 0; cout << "通讯录已清空" << endl; system("pause"); system("cls"); }
|
10.2 测试清空联系人
在switch case 语句中,case 6 里添加:
1 2 3
| case 6: cleanPerson(&abs); break;
|
测试效果如图:
清空通讯录
再次查看信息,显示记录为空
完整代码实现
代码: mian.cpp
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
|
#include <iostream> #include "BusinessImplementation.h"
using namespace std;
int main() { Addressbooks abs; abs.m_Size = 0;
int select = 0; BusinessImplementation * pBusinessImplementation; pBusinessImplementation = BusinessImplementation::GetBusinessImplementation(); if (NULL == pBusinessImplementation) { abort(); }
while (true) { pBusinessImplementation->showMenu();
cin >> select;
switch (select) { case 1: pBusinessImplementation->addPerson(&abs); break; case 2: pBusinessImplementation->showPerson(&abs); break; case 3: pBusinessImplementation->deletePerson(&abs); break; case 4: pBusinessImplementation->findPerson(&abs); break; case 5: pBusinessImplementation->modifyPerson(&abs); break; case 6: pBusinessImplementation->cleanPerson(&abs); break; case 0: cout << "欢迎下次使用" << endl; return 0; break; default: break; } }
return 0; }
|
代码: BusinessImplementation.h
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
|
#ifndef ADDRESSBOOK_BUSINESSIMPLEMENTATION_H #define ADDRESSBOOK_BUSINESSIMPLEMENTATION_H #include <iostream> #include <string> using namespace std;
#define MAX 1000
struct Person { string m_Name; int m_Sex; int m_Age; string m_Phone; string m_Addr; };
struct Addressbooks { struct Person personArray[MAX]; int m_Size; };
class BusinessImplementation { protected: static BusinessImplementation *m_pBusinessImplementation; protected: BusinessImplementation(); virtual ~BusinessImplementation(); public: static BusinessImplementation * GetBusinessImplementation(); void showMenu(); void addPerson(Addressbooks *abs); void showPerson(Addressbooks * abs); void deletePerson(Addressbooks * abs); void findPerson(Addressbooks * abs); void modifyPerson(Addressbooks * abs); void cleanPerson(Addressbooks * abs); private: int isExist(Addressbooks * abs, string name);
};
#endif
|
代码: BusinessImplementation.cpp
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
|
#include "BusinessImplementation.h"
BusinessImplementation* BusinessImplementation::m_pBusinessImplementation = NULL;
BusinessImplementation::BusinessImplementation() {
}
BusinessImplementation::~BusinessImplementation() {
}
void BusinessImplementation::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; }
void BusinessImplementation::addPerson(Addressbooks *abs) { if (abs->m_Size == MAX) { cout << "通讯录已满,无法添加" << endl; return; } else { string name; cout << "请输入姓名:" << endl; cin >> name; abs->personArray[abs->m_Size].m_Name = name;
cout << "请输入性别:" << endl; cout << "1 -- 男" << endl; cout << "2 -- 女" << endl;
int sex = 0; while (true) { cin >> sex; if (sex == 1 || sex == 2) { abs->personArray[abs->m_Size].m_Sex = sex; break; } cout << "输入有误,请重新输入"; }
cout << "请输入年龄:" << endl; int age = 0; cin >> age; abs->personArray[abs->m_Size].m_Age = age;
cout << "请输入联系电话:" << endl; string phone = ""; cin >> phone; abs->personArray[abs->m_Size].m_Phone = phone;
cout << "请输入家庭住址:" << endl; string address; cin >> address; abs->personArray[abs->m_Size].m_Addr = address;
abs->m_Size++;
cout << "添加成功" << endl; } }
void BusinessImplementation::showPerson(Addressbooks * abs) { if (abs->m_Size == 0) { cout << "当前记录为空" << endl; } else { for (int i = 0; i < abs->m_Size; i++) { cout << "姓名:" << abs->personArray[i].m_Name << "\t"; cout << "性别:" << (abs->personArray[i].m_Sex == 1 ? "男" : "女") << "\t"; cout << "年龄:" << abs->personArray[i].m_Age << "\t"; cout << "电话:" << abs->personArray[i].m_Phone << "\t"; cout << "住址:" << abs->personArray[i].m_Addr << endl; } }
}
int BusinessImplementation::isExist(Addressbooks * abs, string name) { for (int i = 0; i < abs->m_Size; i++) { if (abs->personArray[i].m_Name == name) { return i; } } return -1; }
void BusinessImplementation::deletePerson(Addressbooks * abs) { cout << "请输入您要删除的联系人" << endl; string name; cin >> name;
int ret = isExist(abs, name); if (ret != -1) { for (int i = ret; i < abs->m_Size; i++) { abs->personArray[i] = abs->personArray[i + 1]; } abs->m_Size--; cout << "删除成功" << endl; } else { cout << "查无此人" << endl; }
}
void BusinessImplementation::findPerson(Addressbooks * abs) { cout << "请输入您要查找的联系人" << endl; string name; cin >> name;
int ret = isExist(abs, name); if (ret != -1) { cout << "姓名:" << abs->personArray[ret].m_Name << "\t"; cout << "性别:" << abs->personArray[ret].m_Sex << "\t"; cout << "年龄:" << abs->personArray[ret].m_Age << "\t"; cout << "电话:" << abs->personArray[ret].m_Phone << "\t"; cout << "住址:" << abs->personArray[ret].m_Addr << endl; } else { cout << "查无此人" << endl; }
}
void BusinessImplementation::modifyPerson(Addressbooks * abs) { cout << "请输入您要修改的联系人" << endl; string name; cin >> name;
int ret = isExist(abs, name); if (ret != -1) { string name; cout << "请输入姓名:" << endl; cin >> name; abs->personArray[ret].m_Name = name;
cout << "请输入性别:" << endl; cout << "1 -- 男" << endl; cout << "2 -- 女" << endl;
int sex = 0; while (true) { cin >> sex; if (sex == 1 || sex == 2) { abs->personArray[ret].m_Sex = sex; break; } cout << "输入有误,请重新输入"; }
cout << "请输入年龄:" << endl; int age = 0; cin >> age; abs->personArray[ret].m_Age = age;
cout << "请输入联系电话:" << endl; string phone = ""; cin >> phone; abs->personArray[ret].m_Phone = phone;
cout << "请输入家庭住址:" << endl; string address; cin >> address; abs->personArray[ret].m_Addr = address;
cout << "修改成功" << endl; } else { cout << "查无此人" << endl; }
}
void BusinessImplementation::cleanPerson(Addressbooks * abs) { abs->m_Size = 0; cout << "通讯录已清空" << endl; }
BusinessImplementation* BusinessImplementation::GetBusinessImplementation() { if(!m_pBusinessImplementation) { m_pBusinessImplementation = new BusinessImplementation(); } return m_pBusinessImplementation; }
|
至此,通讯录管理系统完成!