一、运用所学的结构体、地址指针等基础知识,完成通讯录管理系统
二、系统主要有以下6个功能:
1.添加联系人
2.显示联系人
3.删除联系人
4.查找联系人
5.修改联系人
6.清空联系人
1 #include<iostream> 2 using namespace std; 3 #include<string> 4 #define MAX 1000 5 6 7 //菜单界面 8 void showMenu() 9 { 10 cout << "**************************" << endl; 11 cout << "***** 1.添加联系人 *****" << endl; 12 cout << "***** 2.显示联系人 *****" << endl; 13 cout << "***** 3.删除联系人 *****" << endl; 14 cout << "***** 4.查找联系人 *****" << endl; 15 cout << "***** 5.修改联系人 *****" << endl; 16 cout << "***** 6.清空联系人 *****" << endl; 17 cout << "***** 0.退出通讯录 *****" << endl; 18 cout << "**************************" << endl; 19 } 20 21 //联系人结构体 22 struct Person 23 { 24 string m_Name;//姓名 25 int m_sex;//性别1男,2女 26 int m_age;//年龄 27 string m_phone;//电话 28 string m_Addr; //住址 29 }; 30 //通讯录结构体 31 struct Addressbooks 32 { 33 //联系人数组 34 struct Person personarray[MAX]; 35 //联系人个数 36 int m_size; 37 38 }; 39 //1添加联系人 40 void addPerson(Addressbooks* abs) 41 { 42 //判断通讯录是否满了 43 if (abs->m_size==MAX) 44 { 45 cout << "通讯录已满,无法添加!" << endl; 46 return; 47 } 48 else 49 { 50 //添加联系人 51 52 //姓名 53 string name=""; 54 cout << "请输入姓名: " << endl; 55 cin >> name; 56 abs->personarray[abs->m_size].m_Name = name; 57 58 //性别 59 int sex=0; 60 cout << "请输入性别: " << endl; 61 cout << "1 --- 男" << endl; 62 cout << "2 --- 女" << endl; 63 while (true) 64 { 65 cin >> sex; 66 if (sex == 1 || sex == 2) 67 { 68 abs->personarray[abs->m_size].m_sex = sex; 69 break; 70 } 71 cout << "输入有误,请重新输入!" << endl; 72 } 73 74 //年龄 75 cout << "请输入年龄: " << endl; 76 int age = 0; 77 while (true) 78 { 79 cin >> age; 80 if (age<101) 81 { 82 abs->personarray[abs->m_size].m_age = age; 83 break; 84 } 85 cout << "输入有误,请重新输入!" << endl; 86 87 } 88 89 //电话 90 cout << "请输入电话: " << endl; 91 string phone=""; 92 cin >> phone; 93 abs->personarray[abs->m_size].m_phone = phone; 94 95 //住址 96 cout << "请输入家庭住址: " << endl; 97 string place=""; 98 cin >> place; 99 abs->personarray[abs->m_size].m_Addr = place; 100 101 //更新通讯录人数 102 abs->m_size++; 103 cout << "添加成功" << endl; 104 105 system("pause");//请按任意键继续 106 system("cls");//清屏操作 107 } 108 } 109 110 //2.显示联系人 111 void showperson(Addressbooks* abs) 112 { 113 if (abs->m_size == 0) 114 { 115 cout << "当前记录为空" << endl; 116 } 117 else 118 { 119 for (int i = 0; i < abs->m_size; i++) 120 { 121 cout << "姓名: " << abs->personarray[i].m_Name << "\t"; 122 cout << "性别: " << (abs->personarray[i].m_sex == 1 ? "男":"女") << "\t"; 123 cout << "年龄: " << abs->personarray[i].m_age << "\t"; 124 cout << "电话: " << abs->personarray[i].m_phone << "\t"; 125 cout << "住址: " << abs->personarray[i].m_Addr << endl; 126 } 127 } 128 system("pause"); 129 system("cls"); 130 } 131 132 //3.1检测联系人是否存在 133 int Exist(Addressbooks* abs,string name)//通讯录,对比姓名 134 { 135 for (int i = 0; i < abs->m_size; i++) 136 { 137 if (abs->personarray[i].m_Name == name) 138 { 139 return i; 140 } 141 } 142 return -1; 143 } 144 //3.2删除联系人 145 void Delteperson(Addressbooks* abs) 146 { 147 cout << "请输入您要删除的联系人" << endl; 148 149 string name; 150 cin >> name; 151 152 int ret = Exist(abs, name); 153 154 if (ret!=-1) 155 { 156 for (int i = ret; i < abs->m_size; i++) 157 { 158 //数据前移 159 abs->personarray[i] = abs->personarray[i + 1]; 160 } 161 abs->m_size--;//更新人员数量 162 cout << "删除成功" << endl; 163 } 164 else 165 { 166 cout << "查无此人!" << endl; 167 } 168 system("pause"); 169 system("cls"); 170 } 171 172 //4.查找联系人 173 void findperson(Addressbooks* abs) 174 { 175 cout << "请输入需要查找的联系人" << endl; 176 string name; 177 cin >> name; 178 179 //判断是否存在 180 int ret = Exist(abs, name); 181 182 if (ret!=-1) 183 { 184 int i = ret; 185 cout << "姓名: " << abs->personarray[i].m_Name << "\t"; 186 cout << "性别: " << (abs->personarray[i].m_sex == 1 ? "男" : "女") << "\t"; 187 cout << "年龄: " << abs->personarray[i].m_age << "\t"; 188 cout << "电话: " << abs->personarray[i].m_phone << "\t"; 189 cout << "住址: " << abs->personarray[i].m_Addr << endl; 190 } 191 else 192 { 193 cout << "查无此人!" << endl; 194 } 195 system("pause"); 196 system("cls"); 197 198 } 199 200 //5.修改联系人 201 void modifyperson(Addressbooks* abs) 202 { 203 cout << "请输入需要修改的联系人" << endl; 204 string name; 205 cin >> name; 206 207 //判断是否存在 208 int ret = Exist(abs, name); 209 210 if (ret != -1) 211 { 212 int i= ret; 213 //显示要修改人信息 214 cout << "---------------------------------------------" << endl; 215 cout << "姓名: " << abs->personarray[i].m_Name << "\t"; 216 cout << "性别: " << (abs->personarray[i].m_sex == 1 ? "男" : "女") << "\t"; 217 cout << "年龄: " << abs->personarray[i].m_age << "\t"; 218 cout << "电话: " << abs->personarray[i].m_phone << "\t"; 219 cout << "住址: " << abs->personarray[i].m_Addr << endl; 220 221 //根据不同需求修改联系人信息 222 int n = 0; 223 cout << "---------------------------------------------" << endl; 224 cout << " 请选择需要修改的内容选项: " << endl; 225 cout << "---------------------------------------------" << endl; 226 cout << "1--姓名;2--性别;3--年龄;4--电话;5--住址;6--全部修改;7--退出" << endl; 227 228 while (n!=7) 229 { 230 cin >> n; 231 string me = ""; 232 int sex=0; 233 int age = 0; 234 string phone = ""; 235 string place = ""; 236 237 switch (n) 238 { 239 case 1: 240 //姓名 241 cout << "请输入姓名: " << endl; 242 cin >> me; 243 abs->personarray[i].m_Name = me; 244 cout << "修改成功" << endl; 245 cout << "如要修改请继续选择其他选项,退出请按7" << endl; 246 break; 247 case 2: 248 //性别 249 cout << "请输入性别: " << endl; 250 cout << "1 --- 男" << endl; 251 cout << "2 --- 女" << endl; 252 253 while (true) 254 { 255 cin >> sex; 256 if (sex == 1 || sex == 2) 257 { 258 abs->personarray[i].m_sex = sex; 259 break; 260 } 261 cout << "输入有误,请重新输入!" << endl; 262 } 263 cout << "修改成功" << endl; 264 cout << "如要修改请继续选择其他选项,退出请按7" << endl; 265 break; 266 267 case 3: 268 //年龄 269 cout << "请输入年龄: " << endl; 270 271 cin >> age; 272 abs->personarray[i].m_age = age; 273 cout << "修改成功" << endl; 274 cout << "如要修改请继续选择其他选项,退出请按7" << endl; 275 break; 276 277 case 4: 278 //电话 279 cout << "请输入电话: " << endl; 280 281 cin >> phone; 282 abs->personarray[i].m_phone = phone; 283 cout << "修改成功" << endl; 284 cout << "如要修改请继续选择其他选项,退出请按7" << endl; 285 break; 286 287 case 5: 288 //住址 289 cout << "请输入家庭住址: " << endl; 290 291 cin >> place; 292 abs->personarray[i].m_Addr = place; 293 cout << "修改成功" << endl; 294 cout << "如要修改请继续选择其他选项,退出请按7" << endl; 295 break; 296 297 298 case 6: 299 //姓名 300 cout << "请输入姓名: " << endl; 301 cin >> name; 302 abs->personarray[i].m_Name=name; 303 304 //性别 305 cout << "请输入性别: " << endl; 306 cout << "1 --- 男" << endl; 307 cout << "2 --- 女" << endl; 308 309 while (true) 310 { 311 cin >> sex; 312 if (sex == 1 || sex == 2) 313 { 314 abs->personarray[i].m_sex = sex; 315 break; 316 } 317 cout << "输入有误,请重新输入!" << endl; 318 } 319 320 //年龄 321 cout << "请输入年龄: " << endl; 322 cin >> age; 323 abs->personarray[i].m_age = age; 324 325 //电话 326 cout << "请输入电话: " << endl; 327 cin >> phone; 328 abs->personarray[i].m_phone = phone; 329 330 //住址 331 cout << "请输入家庭住址: " << endl; 332 cin >> place; 333 abs->personarray[i].m_Addr = place; 334 cout << "修改成功" << endl; 335 n = 7; 336 break; 337 338 default: 339 break; 340 } 341 } 342 343 } 344 else 345 { 346 cout << "查无此人!" << endl; 347 } 348 system("pause"); 349 system("cls"); 350 351 } 352 //6、清空所有联系人 353 void cleanperson(Addressbooks* abs) 354 { 355 cout << "请问是否真的要清空通讯录? " << endl; 356 cout << "1--确定" << endl; 357 cout << "0--不清空" << endl; 358 int b; 359 cin >> b; 360 while (b==1) 361 { 362 abs->m_size = 0; 363 cout << "通讯录已清空" << endl; 364 break; 365 } 366 system("pause"); 367 system("cls"); 368 } 369 370 int main() 371 { 372 //创建通讯录结构体变量 373 Addressbooks abs; 374 //当前人员个数 375 abs.m_size = 0; 376 377 int select = 0;//创建用户选择输入变量 378 379 while (true) //除了选0,能一直使用 380 { 381 //菜单调用 382 showMenu(); 383 384 cin >> select; 385 switch (select) 386 { 387 case 1: //1.添加联系人 388 addPerson(&abs); //地址传递 389 break; 390 case 2: //2.显示联系人 391 showperson(&abs); 392 break; 393 case 3: //3.删除联系人 394 Delteperson(&abs); 395 break; 396 case 4: //4.查找联系人 397 findperson(&abs); 398 break; 399 case 5: //5.修改联系人 400 modifyperson(&abs); 401 break; 402 case 6: //6.清空联系人 403 cleanperson(&abs); 404 break; 405 case 0: //0.退出通讯录 406 cout << "欢迎下次使用" << endl; 407 system("pause"); 408 return 0; 409 break; 410 default: 411 break; 412 } 413 } 414 415 416 417 system("pause"); 418 return 0; 419 }
0.退出通讯录