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

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

思路:

1.显示菜单栏

  1. void menu() {
  2. ?
  3. ?? ?cout << "——————————————————" << endl;
  4. ?? ?cout << "*********** 1.添加联系人 ***********" << endl;
  5. ?? ?cout << "*********** 2.删除联系人 ***********" << endl;
  6. ?? ?cout << "*********** 3.修改联系人 ***********" << endl;
  7. ?? ?cout << "*********** 4.查找联系人 ***********" << endl;
  8. ?? ?cout << "*********** 5.显示通讯录 ***********" << endl;
  9. ?? ?cout << "*********** 6.清空联系人 ***********" << endl;
  10. ?? ?cout << "*********** 0.退出通讯录 ***********" << endl;
  11. ?? ?cout << "——————————————————" << endl;
  12. }

2.退出

  1. int main() {
  2. ?
  3. ?? ?int select = 0;
  4. ?? ?cin >> select;
  5. ?? ?switch (select) {
  6. ?? ??? ?
  7. ?? ??? ?case 0://退出通讯录
  8. ?? ??? ? ? ?cout << "欢迎下次使用" << endl;
  9. ?? ??? ??? ?system("pause");
  10. ?? ??? ??? ?return 0;
  11. ?? ??? ??? ?break;
  12. ?? ??? ?
  13. ?? ?}
  14. ?? ?
  15. ?? ?system("pause");
  16. ?? ?return 0;
  17. }

3.创建结构体

3.1创建联系人结构体
3.2创建通讯录结构体

  1. //定义联系人结构体
  2. //姓名、电话号码、邮箱、地址
  3. struct person {
  4. ?? ?string name;
  5. ?? ?string number;
  6. ?? ?string Email;
  7. ?? ?string address;
  8. };
  9. // 定义通讯录结构体
  10. struct contacts {
  11. ?? ?int people_num;
  12. ?? ?struct person personarr[MAX];//子结构体:联系人信息
  13. };

4.添加联系人

  1. void addperson(struct contacts* p) {
  2. ?? ?if (p->people_num == MAX ) {
  3. ?? ??? ?cout << "通讯录已满" << endl;
  4. ?? ?}
  5. ?? ?else {//添加联系人信息
  6. ?? ??? ?string name, number, Email, address;
  7. ?? ??? ?cout << "请输入姓名:" << endl;
  8. ?? ??? ?cin >> name;
  9. ?? ??? ?cout << "请输入电话:" << endl;
  10. ?? ??? ?cin >> number;
  11. ?? ??? ?cout << "请输入邮箱:" << endl;
  12. ?? ??? ?cin >> Email;
  13. ?? ??? ?cout << "请输入地址:" << endl;
  14. ?? ??? ?cin >> address;
  15. ?? ??? ?p->personarr[p->people_num].name = name;
  16. ?? ??? ?p->personarr[p->people_num].number = number;
  17. ?? ??? ?p->personarr[p->people_num].Email = Email;
  18. ?? ??? ?p->personarr[p->people_num].address = address;
  19. ?? ??? ?
  20. ?? ??? ?p->people_num++;
  21. ?? ??? ?cout << "添加成功!" << endl;
  22. ?? ?}
  23. }

5.删除联系人

判断联系人是否存在

  1. int existperson(struct contacts* p,string name) {
  2. ?? ?
  3. ?? ?for (int i = 0; i < p->people_num; i++) {
  4. ?? ??? ?if ( p->personarr[i].name == name ) {
  5. ?? ??? ??? ?return i;
  6. ?? ??? ?}
  7. ?? ?}
  8. ?? ?return -1;
  9. }

若存在,获取联系人在通讯录位置,将position后面的都往前移动一个位置,覆盖之前的值

  1. //删除联系人
  2. void delperson(struct contacts* p,int position) {
  3. ?? ?while (position < (p->people_num)) {
  4. ?? ??? ?p->personarr[position] = p->personarr[position + 1];
  5. ?? ??? ?position++;
  6. ?? ?}
  7. ?? ?p->people_num--;
  8. ?? ?cout << "删除成功!" << endl;
  9. }

6.修改

检查要修改联系人是否存在,并获取当前位置

  1. void modifyperson(struct contacts* p, int position) {
  2. ?? ?string ?number, Email, address;
  3. ?? ?
  4. ?? ?cout << "请输入修改电话:" << endl;
  5. ?? ?cin >> number;
  6. ?? ?cout << "请输入修改邮箱:" << endl;
  7. ?? ?cin >> Email;
  8. ?? ?cout << "请输入修改地址:" << endl;
  9. ?? ?cin >> address;
  10. ?
  11. ?? ?p->personarr[position].number = number;
  12. ?? ?p->personarr[position].Email = Email;
  13. ?? ?p->personarr[position].address = address;
  14. ?? ?cout << "修改成功!" << endl;
  15. }

7.查找

  1. void searchperson(struct contacts* p, int position) {
  2. ?? ?cout << "姓名:" << p->personarr[position].name << " ? ?"
  3. ?? ??? ?<< "电话:" << p->personarr[position].number << " ? ?"
  4. ?? ??? ?<< "邮箱:" << p->personarr[position].Email << " ? ?"
  5. ?? ??? ?<< "地址:" << p->personarr[position].address << " ? ?";
  6. }

8.显示通讯录

  1. void showcontact(struct contacts* p) {
  2. ?? ?for (int i = 0; i < (p->people_num); i++) {
  3. ?? ??? ?cout <<"姓名:" << p->personarr[i].name << " ? ?"
  4. ?? ??? ??? ?<<"电话:" << p->personarr[i].number << " ? ?"
  5. ?? ??? ??? ?<<"邮箱:" << p->personarr[i].Email << " ? ?"
  6. ?? ??? ??? ?<<"地址:" << p->personarr[i].address << " ? ?"<< endl;
  7. ?
  8. ?? ?}?? ??? ?
  9. }

9.清空通讯录

  1. void cleancontact(struct contacts* p) {
  2. ?? ?p->people_num = 0;
  3. ?? ?cout << "已清空!" << endl;
  4. }

全部代码

  1. #include<iostream>
  2. using namespace std;
  3. #define MAX 200?
  4. ?
  5. //1.菜单栏显示
  6. void menu() {
  7. ?
  8. ?? ?cout << "——————————————————" << endl;
  9. ?? ?cout << "*********** 1.添加联系人 ***********" << endl;
  10. ?? ?cout << "*********** 2.删除联系人 ***********" << endl;
  11. ?? ?cout << "*********** 3.修改联系人 ***********" << endl;
  12. ?? ?cout << "*********** 4.查找联系人 ***********" << endl;
  13. ?? ?cout << "*********** 5.显示通讯录 ***********" << endl;
  14. ?? ?cout << "*********** 6.清空联系人 ***********" << endl;
  15. ?? ?cout << "*********** 0.退出通讯录 ***********" << endl;
  16. ?? ?cout << "——————————————————" << endl;
  17. }
  18. ?
  19. //2.定义结构体
  20. //2.1定义联系人结构体
  21. //姓名、电话号码、邮箱、地址
  22. struct person {
  23. ?? ?string name;
  24. ?? ?string number;
  25. ?? ?string Email;
  26. ?? ?string address;
  27. };
  28. //2.2 定义通讯录结构体
  29. struct contacts {
  30. ?? ?int people_num;
  31. ?? ?struct person personarr[MAX];//子结构体:联系人信息
  32. };
  33. ?
  34. //3.添加联系人
  35. void addperson(struct contacts* p) {
  36. ?? ?if (p->people_num == MAX ) {
  37. ?? ??? ?cout << "通讯录已满" << endl;
  38. ?? ?}
  39. ?? ?else {//添加联系人信息
  40. ?? ??? ?string name, number, Email, address;
  41. ?? ??? ?cout << "请输入姓名:" << endl;
  42. ?? ??? ?cin >> name;
  43. ?? ??? ?cout << "请输入电话:" << endl;
  44. ?? ??? ?cin >> number;
  45. ?? ??? ?cout << "请输入邮箱:" << endl;
  46. ?? ??? ?cin >> Email;
  47. ?? ??? ?cout << "请输入地址:" << endl;
  48. ?? ??? ?cin >> address;
  49. ?? ??? ?p->personarr[p->people_num].name = name;
  50. ?? ??? ?p->personarr[p->people_num].number = number;
  51. ?? ??? ?p->personarr[p->people_num].Email = Email;
  52. ?? ??? ?p->personarr[p->people_num].address = address;
  53. ?? ??? ?
  54. ?? ??? ?p->people_num++;
  55. ?? ??? ?cout << "添加成功!" << endl;
  56. ?? ?}
  57. }
  58. //4.删除联系人
  59. //4.1检测联系人是否存在
  60. int existperson(struct contacts* p,string name) {
  61. ?? ?
  62. ?? ?for (int i = 0; i < p->people_num; i++) {
  63. ?? ??? ?if ( p->personarr[i].name == name ) {
  64. ?? ??? ??? ?return i;
  65. ?? ??? ?}
  66. ?? ?}
  67. ?? ?return -1;
  68. }
  69. //删除联系人
  70. void delperson(struct contacts* p,int position) {
  71. ?? ?while (position < (p->people_num)) {
  72. ?? ??? ?p->personarr[position] = p->personarr[position + 1];
  73. ?? ??? ?position++;
  74. ?? ?}
  75. ?? ?p->people_num--;
  76. ?? ?cout << "删除成功!" << endl;
  77. }
  78. //5.修改联系人
  79. void modifyperson(struct contacts* p, int position) {
  80. ?? ?string ?number, Email, address;
  81. ?? ?
  82. ?? ?cout << "请输入修改电话:" << endl;
  83. ?? ?cin >> number;
  84. ?? ?cout << "请输入修改邮箱:" << endl;
  85. ?? ?cin >> Email;
  86. ?? ?cout << "请输入修改地址:" << endl;
  87. ?? ?cin >> address;
  88. ?
  89. ?? ?p->personarr[position].number = number;
  90. ?? ?p->personarr[position].Email = Email;
  91. ?? ?p->personarr[position].address = address;
  92. ?? ?cout << "修改成功!" << endl;
  93. }
  94. ?
  95. //6.查找联系人
  96. void searchperson(struct contacts* p, int position) {
  97. ?? ?cout << "姓名:" << p->personarr[position].name << " ? ?"
  98. ?? ??? ?<< "电话:" << p->personarr[position].number << " ? ?"
  99. ?? ??? ?<< "邮箱:" << p->personarr[position].Email << " ? ?"
  100. ?? ??? ?<< "地址:" << p->personarr[position].address << " ? ?";
  101. }
  102. ?
  103. //7.显示通讯录
  104. void showcontact(struct contacts* p) {
  105. ?? ?for (int i = 0; i < (p->people_num); i++) {
  106. ?? ??? ?cout <<"姓名:" << p->personarr[i].name << " ? ?"
  107. ?? ??? ??? ?<<"电话:" << p->personarr[i].number << " ? ?"
  108. ?? ??? ??? ?<<"邮箱:" << p->personarr[i].Email << " ? ?"
  109. ?? ??? ??? ?<<"地址:" << p->personarr[i].address << " ? ?"<< endl;
  110. ?
  111. ?? ?}?? ??? ?
  112. }
  113. //8.清空联系人
  114. void cleancontact(struct contacts* p) {
  115. ?? ?p->people_num = 0;
  116. ?? ?cout << "已清空!" << endl;
  117. }
  118. ?
  119. int main() {
  120. ?? ?//创建通讯录结构体变量
  121. ?? ?struct contacts c;
  122. ?? ?//初始化通讯录当前联系人个数
  123. ?? ?c.people_num = 0;
  124. ?? ?int select = 0;
  125. ?? ?string name;
  126. ?? ?while (1) {
  127. ?? ??? ?menu();
  128. ?? ??? ?cin >> select;
  129. ?? ??? ?switch (select) {
  130. ?? ??? ??? ?case 1:// 添加联系人
  131. ?? ??? ??? ??? ?addperson(&c);
  132. ?? ??? ??? ??? ?system("pause");
  133. ?? ??? ??? ??? ?break;
  134. ?
  135. ?? ??? ??? ?case 2:// 删除联系人
  136. ?? ?
  137. ?? ??? ??? ??? ?cout << "请输入删除联系人的姓名:";
  138. ?? ??? ??? ??? ?cin >> name;
  139. ?? ??? ??? ??? ?if (existperson(&c,name)==-1) {
  140. ?? ??? ??? ??? ??? ?cout << "该联系人不存在!";
  141. ?? ??? ??? ??? ?}
  142. ?? ??? ??? ??? ?else{
  143. ?? ??? ??? ??? ??? ?delperson(&c, existperson(&c, name));
  144. ?? ??? ??? ??? ?}
  145. ?? ??? ??? ??? ?
  146. ?? ??? ??? ??? ?system("pause");
  147. ?? ??? ??? ??? ?break;
  148. ?
  149. ?? ??? ??? ?case 3:// 修改联系人
  150. ?? ??? ??? ?
  151. ?? ??? ??? ??? ?cout << "请输入要修改联系人的姓名:";
  152. ?? ??? ??? ??? ?cin >> name;
  153. ?? ??? ??? ??? ?if (existperson(&c,name) == -1) {
  154. ?? ??? ??? ??? ??? ?cout << "该联系人不存在!";
  155. ?? ??? ??? ??? ?}
  156. ?? ??? ??? ??? ?else {
  157. ?? ??? ??? ??? ??? ?modifyperson(&c, existperson(&c, name));
  158. ?? ??? ??? ??? ?}
  159. ?? ??? ??? ??? ?
  160. ?? ??? ??? ??? ?system("pause");
  161. ?? ??? ??? ??? ?break;
  162. ?
  163. ?? ??? ??? ?case 4:// 查找联系人
  164. ?? ??? ??? ??? ?
  165. ?? ??? ??? ??? ?cout << "请输入查找联系人的姓名:";
  166. ?? ??? ??? ??? ?cin >> name;
  167. ?? ??? ??? ??? ?if (existperson(&c,name) == -1) {
  168. ?? ??? ??? ??? ??? ?cout << "该联系人不存在!";
  169. ?? ??? ??? ??? ?}
  170. ?? ??? ??? ??? ?else {
  171. ?? ??? ??? ??? ??? ?searchperson(&c, existperson(&c, name));
  172. ?? ??? ??? ??? ?}
  173. ?? ??? ??? ??? ?
  174. ?? ??? ??? ??? ?system("pause");
  175. ?? ??? ??? ??? ?break;
  176. ?
  177. ?? ??? ??? ?case 5:// 显示通讯录?
  178. ?? ??? ??? ??? ?showcontact(&c);
  179. ?? ??? ??? ??? ?system("pause");?
  180. ?? ??? ??? ??? ?break;
  181. ?? ??? ??? ?case 6:// 清空联系人
  182. ?? ??? ??? ??? ?cleancontact(&c);
  183. ?? ??? ??? ??? ?system("pause");
  184. ?? ??? ??? ??? ?break;
  185. ?? ??? ??? ?case 0://退出通讯录
  186. ?? ??? ??? ??? ?cout << "欢迎下次使用" << endl;
  187. ?? ??? ??? ??? ?system("pause");//暂停批文件的处理
  188. ?? ??? ??? ??? ?return 0;
  189. ?? ??? ??? ??? ?break;
  190. ?? ??? ?}
  191. ?? ??? ?system("cls");//清屏
  192. ?? ?}
  193. ?? ?
  194. ?? ?system("pause");
  195. ?? ?return 0;
  196. }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持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号