经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C++ » 查看文章
C++实现通讯录管理系统项目
来源:jb51  时间:2022/6/20 19:07:47  对本文有异议

本文实例为大家分享了C++实现通讯录管理系统的具体代码,供大家参考,具体内容如下

1、通讯录设计要点

1:添加联系人:向通讯录中添加新人(包括:性别,年龄,联系电话,家庭住址),并且最多记录1000人
2:显示联系人:显示通讯录中所有联系人信息
3:删除联系人:按照姓名进行删除指定联系人
4:查找联系人:按照姓名查找联系人
5:修改联系人:按照姓名修改联系人
6:清空联系人:按照姓名清空联系人
7:退出通讯录:退出当前使用的通讯录

2、设计思路

  1. /**
  2. ? ? 本教程主要利用C++来实现一个通讯管理系统,系统中需要实现如下功能:
  3. ? ? 1:添加联系人:向通讯录中添加新人(包括:性别,年龄,联系电话,家庭住址),并且最多记录1000人
  4. ? ? 2:显示联系人:显示通讯录中所有联系人信息
  5. ? ? 3:删除联系人:按照姓名进行删除指定联系人
  6. ? ? 4:查找联系人:按照姓名查找联系人
  7. ? ? 5:修改联系人:按照姓名修改联系人
  8. ? ? 6:清空联系人:按照姓名清空联系人
  9. ? ? 7:退出通讯录:退出当前使用的通讯录
  10. */
  11. ?
  12. // 引入C++标准包
  13. #include <iostream>
  14. #include <string>
  15. ?
  16. // #define MAX_NUMBER 2
  17. ?
  18. using namespace std;
  19. ?
  20. // const int MAX_NUMBER2 = 3;
  21. ?
  22. // 定义常量通讯录最大值 (auto 让编译其自己推断变量的类型)
  23. constexpr auto MAX = 3;
  24. ?
  25. // 定义联系人结构体
  26. struct Person
  27. {
  28. ? ? string name;
  29. ? ? int sex;
  30. ? ? int age;
  31. ? ? string phoneNamer;
  32. ? ? string address;
  33. };
  34. ?
  35. struct addressbook
  36. {
  37. ? ? struct Person perArray[MAX];
  38. ? ? // struct Person personArr[MAX_NUMBER2];
  39. ? ? // struct Person personArr[MAX_NUMBER];
  40. ? ? int person_size;
  41. ?
  42. };
  43. ?
  44. ?
  45. // 展示通讯录系统
  46. void showMenu() {
  47. ? ? cout << "欢迎来到通讯录管理系统" << endl;
  48. ? ? cout << "功能1:添加联系人" << endl;
  49. ? ? cout << "功能2:显示联系人" << endl;
  50. ? ? cout << "功能3:删除联系人" << endl;
  51. ? ? cout << "功能4:查找联系人" << endl;
  52. ? ? cout << "功能5:修改联系人" << endl;
  53. ? ? cout << "功能6:清空联系人" << endl;
  54. ? ? cout << "功能0:退出通讯录系统" << endl;
  55. }
  56. ?
  57. int isExist(addressbook* personBook , string name) {
  58. ? ? for (int i = 0; i < personBook->person_size;i++)
  59. ? ? {
  60. ? ? ? ? if (personBook->perArray[i].name == name)
  61. ? ? ? ? {
  62. ? ? ? ? ? ? // 从通讯录中找到了某个人
  63. ? ? ? ? ? ? return i;
  64. ? ? ? ? }
  65. ? ? }
  66. ? ? return -1;
  67. }
  68. ?
  69. void addPerson(addressbook *addBook) {
  70. ? ? if (addBook->person_size < MAX)
  71. ? ? {
  72. ? ? ? ? cout << "请输入姓名:" << endl;
  73. ? ? ? ? string name;
  74. ? ? ? ? cin >> name;
  75. ?
  76. ? ? ? ? // addBook 操作指针指向的哪个对象
  77. ? ? ? ? addBook->perArray[addBook->person_size].name = name;
  78. ?
  79. ? ? ? ? cout << "请输入性别对应的序号:1--男 ?2---女" << endl;
  80. ? ? ? ? int sex;
  81. ? ? ? ? // 通过while死循环持续性的读取用户输入的性别
  82. ? ? ? ? while(true) {
  83. ? ? ? ? ? ? cin >> sex;
  84. ? ? ? ? ? ? if ((sex ==1) || (sex ==2))
  85. ? ? ? ? ? ? {
  86. ? ? ? ? ? ? ? ? addBook->perArray[addBook->person_size].sex = sex;
  87. ? ? ? ? ? ? ? ? break;
  88. ? ? ? ? ? ? }
  89. ? ? ? ? ? ? else
  90. ? ? ? ? ? ? {
  91. ? ? ? ? ? ? ? ? cout << "您输入的信息有误,请重新出入" << endl;
  92. ? ? ? ? ? ? }
  93. ? ? ? ? }
  94. ? ? ? ? int age = 0;
  95. ? ? ? ? cout << "请输入年龄" << endl;
  96. ? ? ? ? cin >> age;
  97. ? ? ? ? addBook->perArray[addBook->person_size].age = age;
  98. ?
  99. ? ? ? ? string namuber;
  100. ? ? ? ? cout << "请输入电话号码:" << endl;
  101. ? ? ? ? cin >> namuber;
  102. ? ? ? ? addBook->perArray[addBook->person_size].phoneNamer = namuber;
  103. ?
  104. ? ? ? ? string address;
  105. ? ? ? ? cout << "请输入地址" << endl;
  106. ? ? ? ? cin >> address;
  107. ? ? ? ? addBook->perArray[addBook->person_size].address = address;
  108. ?
  109. ? ? ? ? // 联系人添加成功
  110. ? ? ? ? addBook->person_size++;
  111. ? ? ? ? cout << "联系人已成功添加到通讯录" << endl;
  112. ? ? ? ??
  113. ? ? }
  114. ? ? else
  115. ? ? {
  116. ? ? ? ? cout << "通讯录联系人已满,请删除部分联系人再添加!" << endl;
  117. ? ? }
  118. ? ? system("pause");
  119. ? ? system("cls");
  120. }
  121. ?
  122. // 显示联系人
  123. void showPerson(addressbook person) {
  124. ? ? if (person.person_size == 0) {
  125. ? ? ? ? cout << "您的通讯录列表为空" << endl;
  126. ? ? }
  127. ? ? else
  128. ? ? {
  129. ? ? ? ? for (int i = 0; i < person.person_size; i++)
  130. ? ? ? ? {
  131. ? ? ? ? ? ? cout << "序号:" << i + 1 << ": "
  132. ? ? ? ? ? ? ? ? << "姓名: " << person.perArray[i].name << ": "
  133. ? ? ? ? ? ? ? ? << "性别: " << person.perArray[i].sex << ": "
  134. ? ? ? ? ? ? ? ? << "年龄: " << person.perArray[i].age << ": "
  135. ? ? ? ? ? ? ? ? << "电话: " << person.perArray[i].phoneNamer << ": "
  136. ? ? ? ? ? ? ? ? << "住址: " << person.perArray[i].address << " "
  137. ? ? ? ? ? ? ? ? << endl;
  138. ? ? ? ? }
  139. ? ? }
  140. ? ? system("pause");
  141. ? ? system("cls");
  142. }
  143. ?
  144. // 删除联系人
  145. void deletePerson(addressbook* person) {
  146. ? ? string name;
  147. ? ? cout << "请输入您要删除的联系人姓名:" << endl;
  148. ? ? cin >> name;
  149. ? ? int isExis = isExist(person, name);
  150. ? ? if (isExis != -1)
  151. ? ? {
  152. ? ? ? ? for (int i = isExis; i < person->person_size; i++)
  153. ? ? ? ? {
  154. ? ? ? ? ? ? person->perArray[i] = person->perArray[i + 1];
  155. ? ? ? ? }
  156. ? ? ? ? person->person_size--;
  157. ? ? ? ? cout << "删除成功" << endl;
  158. ? ? }
  159. ? ? else
  160. ? ? {
  161. ? ? ? ? cout << "对不起,通讯录没有此人" << endl;
  162. ? ? }
  163. ?
  164. ? ? system("pause");
  165. ? ? system("cls");
  166. ?
  167. }
  168. ?
  169. // 查找联系人
  170. void findPerson(addressbook* address) {
  171. ? ? string name;
  172. ? ? cout << "请输入您想要查找的联系人" << endl;
  173. ? ? cin >> name;
  174. ? ? int exist = isExist(address, name);
  175. ? ? if (exist != -1)
  176. ? ? {
  177. ? ? ? ? cout << "该联系人信息如下:" << endl;
  178. ? ? ? ? cout << "姓名: " << address->perArray[exist].name << " ?"
  179. ? ? ? ? ? ? << "性别: " << address->perArray[exist].sex << " ?"
  180. ? ? ? ? ? ? << "年龄: " << address->perArray[exist].age << " ?"
  181. ? ? ? ? ? ? << "电话: " << address->perArray[exist].phoneNamer << " ?"
  182. ? ? ? ? ? ? << "住址: " << address->perArray[exist].address << " ?" << endl;
  183. ? ? }
  184. ? ? else
  185. ? ? {
  186. ? ? ? ? cout << "查无此人喔!" << endl;
  187. ? ? }
  188. ? ? system("pause");
  189. ? ? system("cls");
  190. }
  191. ?
  192. void modifyPerson(addressbook *person) {
  193. ? ? string modifyName;
  194. ? ? cout << "请输入修改后的姓名: " << endl;
  195. ? ? cin >> modifyName;
  196. ? ? int exist = isExist(person, modifyName);
  197. ? ? if (exist != -1)
  198. ? ? {
  199. ? ? ? ? person->perArray[exist].name = modifyName;
  200. ? ? ? ? while (true)
  201. ? ? ? ? {
  202. ? ? ? ? ? ? int modifySex;
  203. ? ? ? ? ? ? cout << "请输入修改后的性别: (1:男 ?2:女) " << endl;
  204. ? ? ? ? ? ? cin >> modifySex;
  205. ? ? ? ? ? ? if (modifySex == 1 || modifySex ==2)?
  206. ? ? ? ? ? ? {
  207. ? ? ? ? ? ? ? ? person->perArray[exist].sex = modifySex;
  208. ? ? ? ? ? ? ? ? break;
  209. ? ? ? ? ? ? }
  210. ? ? ? ? ? ? else
  211. ? ? ? ? ? ? {
  212. ? ? ? ? ? ? ? ? cout << "您应当输入1或者2,请重新输入" << endl;
  213. ? ? ? ? ? ? }
  214. ? ? ? ? }
  215. ? ? ? ? int modifyAge;
  216. ? ? ? ? cout << "请输入修改后的年龄: ";
  217. ? ? ? ? cin >> modifyAge;
  218. ? ? ? ? person->perArray[exist].age = modifyAge;
  219. ?
  220. ? ? ? ? string modifyPhoneNum;
  221. ? ? ? ? cout << "请输入修改后的电话: ";
  222. ? ? ? ? cin >> modifyPhoneNum;
  223. ? ? ? ? person->perArray[exist].phoneNamer = modifyPhoneNum;
  224. ?
  225. ? ? ? ? string modifyAddress;
  226. ? ? ? ? cout << "请输入修改后的地址: ";
  227. ? ? ? ? cin >> modifyAddress;
  228. ? ? ? ? person->perArray[exist].address = modifyAddress;
  229. ?
  230. ? ? ? ? cout << "修改成功!" << endl;
  231. ? ? }
  232. ? ? else
  233. ? ? {
  234. ? ? ? ? cout << "查无此人,故无法修改" << endl;
  235. ? ? }
  236. ? ? system("pause");
  237. ? ? system("cls");
  238. }
  239. ?
  240. // 清空通讯录
  241. void clearPersonAddress(addressbook *personBook) {
  242. ? ? string ensure;
  243. ? ? cout << "您确定要是清空所有联系人信息吗?注意此操作不可逆,请谨慎操作,请输入\"我同意\" " << endl;
  244. ? ? cin >> ensure;
  245. ? ? if (ensure == "我同意")
  246. ? ? {
  247. ? ? ? ? personBook->person_size = 0;
  248. ? ? ? ? for (int i = 0; i < personBook->person_size; i++)
  249. ? ? ? ? {
  250. ? ? ? ? ? ? personBook->perArray[i].address = "";
  251. ? ? ? ? ? ? personBook->perArray[i].name = "";
  252. ? ? ? ? ? ? personBook->perArray[i].phoneNamer = "";
  253. ? ? ? ? ? ? personBook->perArray[i].age =0;
  254. ? ? ? ? ? ? personBook->perArray[i].sex =0;
  255. ? ? ? ? }
  256. ? ? ? ? cout << "已成功清空通讯录列表" << endl;
  257. ? ? }
  258. ? ? else
  259. ? ? {
  260. ? ? ? ? cout << "撤销清空联系人列表" << endl;
  261. ? ? }
  262. ?
  263. ? ? system("pause");
  264. ? ? system("cls");
  265. }
  266. ?
  267. int main()
  268. {
  269. ? ? std::cout << "通许录管理系统 \n";
  270. ? ? struct addressbook address;
  271. ? ? address.person_size = 0;
  272. ? ? int userSelect = -1;
  273. ? ?
  274. ? ?
  275. ? ? while (true)
  276. ? ? {
  277. ? ? ? ? showMenu();
  278. ? ? ? ? cout << "请在下方输入您向选择的功能(输入下面数字即可)" << endl;
  279. ? ? ? ? cin >> userSelect;
  280. ? ? ? ? switch (userSelect)
  281. ? ? ? ? {
  282. ? ? ? ? case 1:
  283. ? ? ? ? ? ? addPerson(&address);
  284. ? ? ? ? ? ? break;
  285. ? ? ? ? case 2:
  286. ? ? ? ? ? ? showPerson(address);
  287. ? ? ? ? ? ? break;
  288. ? ? ? ? case 3:
  289. ? ? ? ? ? ? deletePerson(&address);
  290. ? ? ? ? ? ? break;
  291. ? ? ? ? case 4:
  292. ? ? ? ? ? ? findPerson(&address);
  293. ? ? ? ? ? ? break;
  294. ? ? ? ? case 5:
  295. ? ? ? ? ? ? modifyPerson(&address);
  296. ? ? ? ? ? ? break;
  297. ? ? ? ? case 6:
  298. ? ? ? ? ? ? clearPersonAddress(&address);
  299. ? ? ? ? ? ? break;
  300. ? ? ? ? case 0:
  301. ? ? ? ? ? ? cout << "退出系统成功,欢迎您下次使用!" << endl;
  302. ? ? ? ? ? ? return 0;
  303. ? ? ? ? default:
  304. ? ? ? ? ? ? system("pause");
  305. ? ? ? ? ? ? break;
  306. ? ? ? ? }
  307. ? ? }
  308. ?
  309. }

3、运行结果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号