通讯录管理系统
通讯录管理系统
添加联系人功能:联系人上限1000人,信息(姓名、性别、年龄、联系电话、住址)
实现步骤:
1.设计联系人结构体
2.设计通讯录结构体
3.main函数中创建通讯录
4.封装添加联系人函数
5.测试添加联系人功能
关于清屏操作:
system("pause"); //按任意键继续
system("cls"); //清屏
删除联系人:
1.检测是否存在联系人
2.封装删除联系人
3.测试删除联系人功能
源码如下:
1 #include<iostream> 2 #include<string> 3 #include<iomanip> 4 using namespace std; 5 #define MAX 1000 //最大人数 6 7 //联系人结构体 8 struct Person 9 { 10 string m_Name; //姓名 11 int m_Sex; //性别 1 - 男 2 - 女 12 int m_Age; //年龄 13 string m_Phone; //电话 14 string m_Address; //地址 15 }; 16 17 //通讯录的结构体 18 struct Addressbooks 19 { 20 struct Person personArray[MAX]; //通讯录中保存的联系人数组 21 int m_Size; //通讯录中人员个数 22 }; 23 24 void showMenu(); //菜单函数 25 void addPerson(Addressbooks* abs); //添加联系人 26 void showPerson(Addressbooks* abs); //显示所有联系人 27 int isExist(Addressbooks* abs, string name); //检测联系人是否存在,参数一 通讯录 参数二 对比姓名 28 void deletePerson(Addressbooks* abs); //删除指定联系人 29 void findPerson(Addressbooks* abs); //查找指定联系人信息 30 void modifyPerson(Addressbooks* abs); //修改指定联系人的信息 31 void cleanPerson(Addressbooks* abs); //清除所有联系人 32 33 int main() 34 { 35 // 创建通讯录结构体变量 36 Addressbooks abs; 37 //初始化通讯录中当前人数 38 abs.m_Size = 0; 39 40 int select = 0; 41 while (true) 42 { 43 showMenu(); //调用菜单 44 cout << '\t'; 45 cin >> select; 46 switch (select) 47 { 48 case 1: //1、添加联系人 49 addPerson(&abs); //利用地址传递,可以修改实参 50 break; 51 case 2: //2、显示联系人 52 showPerson(&abs); 53 break; 54 case 3: //3、删除联系人 55 deletePerson(&abs); 56 break; 57 case 4: //4、查找联系人 58 findPerson(&abs); 59 break; 60 case 5: //5、修改联系人 61 modifyPerson(&abs); 62 break; 63 case 6: //6、清空联系人 64 cleanPerson(&abs); 65 break; 66 case 0: //0、退出通讯录 67 cout << "\t欢迎下次使用!" << endl; 68 system("pause"); 69 return 0; 70 break; 71 default: 72 break; 73 } 74 } 75 return 0; 76 } 77 78 void showMenu() 79 { 80 cout << "\t****** 通讯录管理系统 ******" << endl; 81 cout << "\t**** 1、添加联系人 ****" << endl; 82 cout << "\t**** 2、显示联系人 ****" << endl; 83 cout << "\t**** 3、删除联系人 ****" << endl; 84 cout << "\t**** 4、查找联系人 ****" << endl; 85 cout << "\t**** 5、修改联系人 ****" << endl; 86 cout << "\t**** 6、清空联系人 ****" << endl; 87 cout << "\t**** 0、退出通讯录 ****" << endl; 88 cout << "\t***************************" << endl; 89 } 90 91 void addPerson(Addressbooks* abs) 92 { 93 //判断通讯录是否已满 94 if (abs->m_Size == MAX) 95 { 96 cout << "\t通讯录已满,无法添加!" << endl; 97 return; 98 } 99 else 100 { 101 //添加联系人 102 //姓名 103 string name; 104 cout << "\t请输入姓名:"; 105 cin >> name; 106 abs->personArray[abs->m_Size].m_Name = name; 107 //性别 108 cout << "\t请输入性别:(1-男,2-女) "; 109 int sex = 0; 110 while (true) 111 { 112 // 如果输入1,2则退出,否则重新输入 113 cin >> sex; 114 if (sex == 1 || sex == 2) 115 { 116 abs->personArray[abs->m_Size].m_Sex = sex; 117 break; 118 } 119 cout << "\t输入有误,请重新输入!" << endl; 120 } 121 //年龄 122 int age = 0; 123 cout << "\t请输入年龄:"; 124 cin >> age; 125 abs->personArray[abs->m_Size].m_Age = age; 126 //电话 127 cout << "\t请输入联系电话:"; 128 string phone; 129 cin >> phone; 130 abs->personArray[abs->m_Size].m_Phone = phone; 131 //住址 132 cout << "\t请输入住址:"; 133 string address; 134 cin >> address; 135 abs->personArray[abs->m_Size].m_Address = address; 136 //更新通讯录人数 137 abs->m_Size++; 138 cout << "\t添加成功!" << endl; 139 system("pause"); //按任意键继续 140 system("cls"); //清屏 141 } 142 } 143 144 void showPerson(Addressbooks* abs) 145 { 146 //判断通讯录中人数是否为0 147 if (abs->m_Size == 0) 148 { 149 cout << "\t当前通讯录的记录为空!" << endl; 150 } 151 else 152 { 153 cout << "\t当前通讯录信息:" << endl; 154 cout << "\t" << setw(5) << "序号" << setw(10) << "姓名" << setw(5) << "性别" << setw(5) << "年龄" << setw(15) << "联系电话" << setw(15) << "联系地址" << endl; 155 for (int i = 0; i < abs->m_Size; i++) 156 { 157 string ch; 158 if (abs->personArray[i].m_Sex == 1) 159 ch = "男"; 160 else 161 ch = "女"; 162 cout << "\t" << setw(5) << i + 1 << setw(10) << abs->personArray[i].m_Name << setw(5) << ch << setw(5) << abs->personArray[i].m_Age << setw(15) << abs->personArray[i].m_Phone << setw(15) << abs->personArray[i].m_Address << endl; 163 } 164 cout << "\t当前通讯录中的人数为:" << abs->m_Size << "人。" << endl; 165 } 166 system("pause"); //按任意键继续 167 system("cls"); //清屏 168 } 169 170 int isExist(Addressbooks* abs, string name) 171 { 172 for (int i = 0; i < abs->m_Size; i++) 173 { 174 //找到用户输入的姓名 175 if (abs->personArray[i].m_Name == name) 176 { 177 return i; //找到了返回数组下标 178 } 179 } 180 return -1; //如果遍历结束没找到返回-1 181 } 182 183 void deletePerson(Addressbooks* abs) 184 { 185 cout << "\t请输入要输入的联系人:"; 186 string name; 187 cin >> name; 188 int ret = isExist(abs, name); 189 //ret == -1 没查到 190 //ret != -1 查到 191 if (ret != -1) 192 { 193 //查找到,进行删除操作 194 for (int i = ret; i < abs->m_Size; i++) 195 { 196 //数据前移 197 abs->personArray[i] = abs->personArray[i + 1]; 198 } 199 abs->m_Size--; //更新总人数 200 cout << "\t删除成功!" << endl; 201 } 202 else 203 { 204 cout << "\t查无此人!" << endl; 205 } 206 system("pause"); //按任意键继续 207 system("cls"); //清屏 208 } 209 210 void findPerson(Addressbooks* abs) 211 { 212 cout << "\t请输入要查找的联系人:"; 213 string name; 214 cin >> name; 215 //判断输入的联系人姓名是否存在 216 int ret = isExist(abs, name); 217 if (ret != -1) 218 { 219 //找到了 220 cout << "\t姓名:" << abs->personArray[ret].m_Name << endl; 221 string ch; 222 if (abs->personArray[ret].m_Sex == 1) 223 ch = "男"; 224 else 225 ch = "女"; 226 cout << "\t性别:" << ch << endl; 227 cout << "\t年龄:" << abs->personArray[ret].m_Age << endl; 228 cout << "\t联系电话:" << abs->personArray[ret].m_Phone << endl; 229 cout << "\t住址:" << abs->personArray[ret].m_Address << endl; 230 } 231 else 232 { 233 //未找到联系人 234 cout << "\t查无此人!" << endl; 235 } 236 system("pause"); //按任意键继续 237 system("cls"); //清屏 238 } 239 240 void modifyPerson(Addressbooks* abs) 241 { 242 cout << "\t请输入要修改的联系人姓名:"; 243 string name; 244 cin >> name; 245 int ret = isExist(abs, name); 246 if (ret != -1) 247 { 248 //找到指定联系人 249 cout << "\t请输入要修改的内容:(1.姓名 2.性别 3.年龄 4.电话 5.住址 0.修改结束)" << endl; 250 int choice; 251 while (true) 252 { 253 cout << "\tchoice = "; 254 cin >> choice; 255 switch (choice) 256 { 257 case 1: 258 {string nam; 259 cout << "\t请输入姓名:"; 260 cin >> nam; 261 abs->personArray[ret].m_Name = nam; } 262 break; 263 case 2: 264 { 265 while(true) { 266 cout << "\t请输入性别:(1 - 男,2 - 女)"; 267 int s = 0; 268 cin >> s; 269 if (s == 1 || s == 2) 270 { 271 abs->personArray[ret].m_Sex = s; 272 break; 273 } 274 else 275 { 276 cout << "\t输入有误,请重新输入!" << endl; 277 } 278 } 279 } 280 break; 281 case 3: 282 { 283 cout << "\t请输入年龄:"; 284 int a; 285 cin >> a; 286 abs->personArray[ret].m_Age = a; 287 } 288 break; 289 case 4: 290 { 291 cout << "\t请输入联系电话:"; 292 string p; 293 cin >> p; 294 abs->personArray[ret].m_Phone = p; 295 } 296 break; 297 case 5: 298 { 299 cout << "\t请输入住址:"; 300 string add; 301 cin >> add; 302 abs->personArray[ret].m_Address = add; 303 } 304 break; 305 case 0: 306 break; 307 default: 308 cout << "\t输入内容有误!请重新输入!" << endl; 309 } 310 if (choice == 0) 311 break; 312 } 313 } 314 else //未找到联系人 315 { 316 cout << "\t查无此人!" << endl; 317 } 318 system("pause"); 319 system("cls"); 320 } 321 322 void cleanPerson(Addressbooks* abs) 323 { 324 325 char falg; 326 cout << "\t是否清空" << abs->m_Size << "条联系人信息!" << endl; 327 cout << "\tY. 确定 N. 放弃" << endl; 328 cout << "\t请输入你的选择:"; 329 while (true) { 330 cin >> falg; 331 if (falg == 'Y') 332 { 333 abs->m_Size = 0; //将当前人数置零,逻辑上的清空操作 334 cout << "\t通讯录已清空!" << endl; 335 break; 336 } 337 else if (falg == 'N') 338 { 339 cout << "\t您已放弃清空操作!" << endl; 340 break; 341 } 342 else 343 { 344 cout << "\t输入有误!请重新输入!" << endl; 345 } 346 } 347 system("pause"); 348 system("cls"); 349 }
You want something.Go get it!