通讯录管理系统
1.前言
通讯录是一个可以记录联系人信息的工具。
目的:利用C++实现一个通讯录管理系统
实现功能:
1.菜单功能
2.退出功能
3.添加、显示、删除、查找、修改、清空联系人
基础知识:C++入门语法
2.各个功能的设计与实现
1.菜单功能
创建showMenu函数创建菜单:
1 2 3 4 5 6 7 8 9 10 11
| void showMenu() { cout << "请输入命令前对应的数字来完成命令" << endl; cout << "\t1.添加联系人" << endl; cout << "\t2.显示联系人" << endl; cout << "\t3.删除联系人" << endl; cout << "\t4.查找联系人" << endl; cout << "\t5.修改联系人" << endl; cout << "\t6.清空联系人" << endl; cout << "\t0.退出通讯录" << endl; }
|
2.退出功能
思路:根据用户不同的选择,进入不同的功能,选择switch分支结构将整个架构进行搭建。
1 2 3
| case 0: cout << "您已退出通讯录,欢迎您的使用" << endl; system("pause");
|
3.添加联系人
添加信息:姓名、性别、年龄、联系电话、家庭住址
设计联系人和通讯录结构体
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| struct Person { string m_Name; int m_Sex; int m_Age; string m_Phone; string m_Addr; };
struct Addressbook { struct Person personArray[Max]; int m_Size; };
|
main函数中创建通讯录(实例化)
1 2
| Addressbook abs; abs.m_Size = 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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| void addPerson(Addressbook *abs) { if (abs->m_Size == Max) { cout << "通讯录已满员,无法添加联系人" << endl; return; }
else { string name; cout << "请输入添加人姓名: " << endl; cin >> name; abs->personArray[abs->m_Size].m_Name = name;
int sex; while (1) { cout << "请输入添加人性别(1表示男,2表示女): " << endl; cin >> sex; if (sex ==1|| sex ==2) { abs->personArray[abs->m_Size].m_Sex = sex; break; } else { cout << "输入的信息有误,请重新输入!" << endl; } }
int age; cout << "请输入添加人年龄: " << endl; cin >> age; abs->personArray[abs->m_Size].m_Age = age;
string phone; cout << "请输入添加人电话号码: " << endl; cin >> phone; abs->personArray[abs->m_Size].m_Phone = phone;
string address; cout << "请输入添加人地址: " << endl; cin >> address; abs->personArray[abs->m_Size].m_Addr = address;
abs->m_Size++;
cout << "添加成功" << endl; system("pause"); system("cls"); } }
|
4.显示联系人
操作简单,遍历结构体数组即可。
5.删除联系人
实现功能:按照姓名进行删除指定联系人
实验步骤:
1.封装检测联系人是否存在
1 2 3 4 5 6 7 8 9 10 11 12
| int isExist(Addressbook *abs,string name) { for (int i = 0; i < abs->m_Size; i++) { if (abs->personArray[i].m_Name == name) { return i; }
} return -1; }
|
2.封装删测联系人函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| void deletePerson(Addressbook* abs) { cout << "请输入你要删除的联系人的姓名: " << endl; string name; cin >> name; int res = isExist(abs, name); if (res == -1) { cout << "查无此人!" << endl; } else { for (int j = res; j < abs->m_Size; j++) { abs->personArray[j] = abs->personArray[j + 1]; } abs->m_Size--; cout << "删除成功" << endl; } system("pause"); system("cls"); }
|
6.查找联系人
原理同显示联系人和检测联系人是否存在,操作简单。
7.修改联系人
原理同添加联系人和检测联系人是否存在,操作简单。
8.清空联系人
只需令abs_m_Size=0即可清空联系人。
3.完整代码
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
| #include<iostream> using namespace std; #include<string> #define Max 1000
struct Person { string m_Name; int m_Sex; int m_Age; string m_Phone; string m_Addr; };
struct Addressbook { struct Person personArray[Max]; int m_Size; };
void showMenu() { cout << "请输入命令前对应的数字来完成命令" << endl; cout << "\t1.添加联系人" << endl; cout << "\t2.显示联系人" << endl; cout << "\t3.删除联系人" << endl; cout << "\t4.查找联系人" << endl; cout << "\t5.修改联系人" << endl; cout << "\t6.清空联系人" << endl; cout << "\t0.退出通讯录" << endl; }
void addPerson(Addressbook *abs) { if (abs->m_Size == Max) { cout << "通讯录已满员,无法添加联系人" << endl; return; }
else { string name; cout << "请输入添加人姓名: " << endl; cin >> name; abs->personArray[abs->m_Size].m_Name = name;
int sex; while (1) { cout << "请输入添加人性别(1表示男,2表示女): " << endl; cin >> sex; if (sex ==1|| sex ==2) { abs->personArray[abs->m_Size].m_Sex = sex; break; } else { cout << "输入的信息有误,请重新输入!" << endl; } }
int age; cout << "请输入添加人年龄: " << endl; cin >> age; abs->personArray[abs->m_Size].m_Age = age;
string phone; cout << "请输入添加人电话号码: " << endl; cin >> phone; abs->personArray[abs->m_Size].m_Phone = phone;
string address; cout << "请输入添加人地址: " << endl; cin >> address; abs->personArray[abs->m_Size].m_Addr = address;
abs->m_Size++;
cout << "添加成功" << endl; system("pause"); system("cls"); } }
void showPerson(Addressbook* abs) { if (abs->m_Size == 0) { cout << "通讯录暂无联系人" << endl; return; }
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");
}
int isExist(Addressbook *abs,string name) { for (int i = 0; i < abs->m_Size; i++) { if (abs->personArray[i].m_Name == name) { return i; }
} return -1; }
void deletePerson(Addressbook* abs) { cout << "请输入你要删除的联系人的姓名: " << endl; string name; cin >> name; int res = isExist(abs, name); if (res == -1) { cout << "查无此人!" << endl; } else { for (int j = res; j < abs->m_Size; j++) { abs->personArray[j] = abs->personArray[j + 1]; } abs->m_Size--; cout << "删除成功" << endl; } system("pause"); system("cls"); }
void findPerson(Addressbook *abs) { cout << "请输入您要查找的联系人: " << endl; string name; cin >> name; int res= isExist(abs, name); if (res == -1) { cout << "查无此人!" << endl; } else { cout << "姓名: " << abs->personArray[res].m_Name << "\t"; cout << "性别: " << (abs->personArray[res].m_Sex==1?"男":"女") << "\t"; cout << "年龄: " << abs->personArray[res].m_Age << "\t"; cout << "电话号码: " << abs->personArray[res].m_Phone << "\t"; cout << "住址: " << abs->personArray[res].m_Addr << "\t"; } system("pause"); system("cls"); }
void modifyPerson(Addressbook* abs) { cout << "请输入您要修改的联系人: " << endl; string name; cin >> name; int res = isExist(abs, name); if (res != -1) { string name; cout << "请修改指定人姓名: " << endl; cin >> name; abs->personArray[res].m_Name = name;
int sex; while (1) { cout << "请修改指定人性别(1表示男,2表示女): " << endl; cin >> sex; if (sex == 1 || sex == 2) { abs->personArray[res].m_Sex = sex; break; } else { cout << "输入的信息有误,请重新输入!" << endl; } }
int age; cout << "请修改指定人年龄: " << endl; cin >> age; abs->personArray[res].m_Age = age;
string phone; cout << "请修改指定人电话号码: " << endl; cin >> phone; abs->personArray[res].m_Phone = phone;
string address; cout << "请修改指定人地址: " << endl; cin >> address; abs->personArray[res].m_Addr = address; cout << "修改成功" << endl; } else { cout << "查无此人!" << endl; } system("pause"); system("cls"); }
void cleanPerson(Addressbook* abs) { abs->m_Size = 0; cout << "通讯录已清空" << endl; system("pause"); system("cls"); }
int main() { Addressbook abs; abs.m_Size = 0;
int s = 0; while (1) { showMenu(); cin >> s; switch (s) { case 1: addPerson(&abs); break; case 2: showPerson(&abs); break; case 3: deletePerson(&abs); break; case 4: findPerson(&abs); break; case 5: modifyPerson(&abs); break; case 6: cleanPerson(&abs); break; case 0: cout << "您已退出通讯录,欢迎您的使用" << endl; system("pause"); return 0; break; } } system("pause"); return 0; }
|