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

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

简介:通讯录由一个拥有者以及通讯信息组成。

基本功能:增删改查

拥有者和通讯信息的基础结构相同,由struct构成

  1. struct Person {
  2. ?? ?int m_id;
  3. ?? ?string m_name;
  4. ?? ?string m_tele;//手机号码可以作为id,但是过于长(11位)
  5. ?? ?//string m_addr;
  6. ?
  7. ?? ?Person& operator = (const Person& r) {
  8. ?? ??? ?if (this == &r) return *this;
  9. ?? ??? ?m_id = r.m_id;
  10. ?? ??? ?m_name = r.m_name;
  11. ?? ??? ?m_tele = r.m_tele;
  12. ?? ??? ?return *this;
  13. ?? ?}
  14. };

Person:id+姓名+手机号。还可以添加需要的信息,例如:地址、性别
重载了一个=操作符
通讯录建立class AddressList

  1. class AddressList {
  2. private:
  3. ?? ?Person owner;//通讯录拥有者
  4. ?? ?vector<Person> information;//通讯录:好友的信息组成
  5. public:
  6. ?? ?AddressList();
  7. ?? ?AddressList(const Person&, const vector<Person>& info = {});
  8. ?? ?AddressList(const AddressList&);
  9. ?
  10. ?? ?void Add(const Person&);//添加一个好友信息至通讯录
  11. ?? ?void Delete();//通过姓名删除
  12. ?? ??? ? ? ? ?//通过电话号码删除
  13. ?? ?void Modify();//输入id 修改姓名and号码
  14. ?? ?void Search(int);//1:id搜索
  15. ?? ??? ??? ? //2:姓名搜索
  16. ?? ??? ??? ? //3:号码指定搜索
  17. ?? ?void Print()const;
  18. ?? ?//查看通讯录所有信息:仅显示id和姓名,详细信息输入id查看?? ?
  19. };

cpp:

  1. #include "AddressList.h"
  2. #include <iostream>
  3. using namespace std;
  4. ?
  5. AddressList::AddressList() {}
  6. ?
  7. AddressList::AddressList(const Person& r, const vector<Person>& info) {
  8. ?? ?owner = r;
  9. ?? ?for (const auto& i : info) {
  10. ?? ??? ?information.push_back(i);
  11. ?? ?}
  12. }
  13. ?
  14. AddressList::AddressList(const AddressList& r) {
  15. ?? ?owner = r.owner;
  16. ?? ?for (const auto& i : r.information) {
  17. ?? ??? ?information.push_back(i);
  18. ?? ?}
  19. }
  20. ?
  21. void AddressList::Add(const Person& p) {
  22. ?? ?//添加一个好友信息至通讯录
  23. ?? ?//首先确认不存在:id+tele
  24. ?? ?for (const auto& it : information) {
  25. ?? ??? ?if (p.m_id == it.m_id) {
  26. ?? ??? ??? ?cout << "Id已存在,添加失败!\n";
  27. ?? ??? ??? ?return;
  28. ?? ??? ?}
  29. ?? ??? ?else if (p.m_tele == it.m_tele) {
  30. ?? ??? ??? ?cout << "Telephone已存在,添加失败!\n";
  31. ?? ??? ??? ?return;
  32. ?? ??? ?}
  33. ?? ?}
  34. ?? ?information.push_back(p);
  35. }
  36. ?
  37. void AddressList::Delete() {
  38. ?? ?//通过姓名删除
  39. ?? ?//略:通过电话号码删除
  40. ?? ?string name;
  41. ?? ?cout << "姓名:"; cin >> name;
  42. ?? ?cout << "查找到信息如下:";
  43. ?? ?auto it = information.begin();
  44. ?? ?vector<int> info;//存储下标
  45. ?? ?for (int i(0); it != information.end(); ++it,++i) {
  46. ?? ??? ?if (it->m_name == name) info.push_back(i);
  47. ?? ?}
  48. ?? ?if (info.size() == 0) {
  49. ?? ??? ?cout << "No such name.\n";
  50. ?? ??? ?return;
  51. ?? ?}
  52. ?? ?for (const auto& i : info) {
  53. ?? ??? ?cout << i << ":\t" << information[i].m_id << '\t' << information[i].m_name
  54. ?? ??? ??? ?<< '\t' << information[i].m_tele << endl;
  55. ?? ?}
  56. ?
  57. ?? ?int ind;
  58. ?? ?cout << "输入下标(第一列)删除信息:";
  59. ?? ?cin>>ind;
  60. ?? ?for (const auto& i : info) {
  61. ?? ??? ?if (i == ind) {
  62. ?? ??? ??? ?information.erase(information.begin() + i);
  63. ?? ??? ??? ?return;
  64. ?? ??? ?}
  65. ?? ?}
  66. ?? ?cout << "输入信息错误,删除失败!\n";
  67. }
  68. ?
  69. void AddressList::Modify() {
  70. ?? ?//输入id:修改姓名and号码
  71. ?? ?long id;
  72. ?? ?cout << "Id:"; cin >> id;
  73. ?? ?cout << "查找到信息如下:\n";
  74. ?? ?auto it = information.begin();
  75. ?? ?for (; it != information.end(); ++it) {
  76. ?? ??? ?if (it->m_id == id) {
  77. ?? ??? ??? ?cout << it->m_id << '\t' << it->m_name << '\t' << it->m_tele << endl;
  78. ?? ??? ??? ?break;
  79. ?? ??? ?}
  80. ?? ?}
  81. ?? ?if (it == information.end()) {
  82. ?? ??? ?cout << "No such Id.\n";
  83. ?? ??? ?return;
  84. ?? ?}
  85. ?? ?cout << "修改信息:\n";
  86. ?? ??? ??? ?string name;
  87. ?? ??? ??? ?string tele;
  88. ?? ??? ??? ?cout << "新的姓名:"; cin >> name;
  89. ?? ??? ??? ?cout << "新的号码:"; cin >> tele;
  90. ?? ??? ??? ?char c;
  91. ?? ??? ??? ?cout << "确认?<y/n> ";
  92. ?? ??? ??? ?cin >> c;
  93. ?? ??? ??? ?if (c == 'y' || c == 'Y') {
  94. ?? ??? ??? ??? ?it->m_name = name;
  95. ?? ??? ??? ??? ?it->m_tele = tele;
  96. ?? ??? ??? ??? ?return;
  97. ?? ??? ??? ?}
  98. ?? ??? ??? ?cout << "取消修改!\n";
  99. ?? ??? ??? ?return;
  100. }
  101. void AddressList::Search(int cho) {
  102. ?? ?//1:id搜索
  103. ?? ?//2:姓名搜索
  104. ?? ?//3:号码指定搜索
  105. ?? ?int id;
  106. ?? ?string name;
  107. ?? ?string tele;
  108. ?? ?auto it = information.begin();
  109. ?? ?switch (cho) {
  110. ?? ??? ?case 1:
  111. ?? ??? ??? ?cout << "Id:";
  112. ?? ??? ??? ?cin >> id;
  113. ?? ??? ??? ?cout << "搜索到的信息如下:\n";
  114. ?? ??? ??? ?for (it = information.begin(); it != information.end(); ++it) {
  115. ?? ??? ??? ??? ?if (it->m_id == id) {
  116. ?? ??? ??? ??? ??? ?cout << it->m_id << '\t' << it->m_name << '\t' << it->m_tele << endl;
  117. ?? ??? ??? ??? ??? ?break;
  118. ?? ??? ??? ??? ?}
  119. ?? ??? ??? ?}
  120. ?? ??? ??? ?break;
  121. ?? ??? ?case 2:
  122. ?? ??? ??? ?cout << "Name:";
  123. ?? ??? ??? ?cin >> name;
  124. ?? ??? ??? ?cout << "搜索到的信息如下:\n";
  125. ?? ??? ??? ?for (it = information.begin(); it != information.end(); ++it) {
  126. ?? ??? ??? ??? ?if (it->m_name == name)
  127. ?? ??? ??? ??? ??? ?cout << it->m_id << '\t' << it->m_name << '\t' << it->m_tele << endl;
  128. ?? ??? ??? ?}
  129. ?? ??? ??? ?break;
  130. ?? ??? ?case 3:
  131. ?? ??? ??? ?cout << "Tele:";
  132. ?? ??? ??? ?cin >> tele;
  133. ?? ??? ??? ?cout << "搜索到的信息如下:\n";
  134. ?? ??? ??? ?for (it = information.begin(); it != information.end(); ++it) {
  135. ?? ??? ??? ??? ?if (it->m_tele == tele) {
  136. ?? ??? ??? ??? ??? ?cout << it->m_id << '\t' << it->m_name << '\t' << it->m_tele << endl;
  137. ?? ??? ??? ??? ??? ?break;
  138. ?? ??? ??? ??? ?}
  139. ?? ??? ??? ?}
  140. ?? ??? ??? ?break;
  141. ?? ??? ?default:break;
  142. ?? ?}
  143. }
  144. void AddressList::Print()const {
  145. ?? ?cout << "ID:" << owner.m_id << endl;
  146. ?? ?cout << "Owner:" << owner.m_name << endl
  147. ?? ??? ?<< "Tele:" << owner.m_tele << endl;
  148. ?? ?int n(information.size());
  149. ?? ?cout << "通讯录人数:" << n << endl;
  150. ?? ?for (int i(0); i < n; ++i) {
  151. ?? ??? ?cout << information[i].m_id << '\t' << information[i].m_name << endl;
  152. ?? ?}
  153. ?? ?while (1) {
  154. ?? ??? ?cout << endl
  155. ?? ??? ??? ?<< "详细信息,请输入id:-1终止查看\n";
  156. ?? ??? ?int id;
  157. ?? ??? ?cin >> id;
  158. ?? ??? ?if (id == -1) break;
  159. ?? ??? ?bool b(false);
  160. ?? ??? ?for (const auto& it : information) {
  161. ?? ??? ??? ?if (id == it.m_id) {
  162. ?? ??? ??? ??? ?b = true;
  163. ?? ??? ??? ??? ?cout << it.m_id << '\t' << it.m_name << '\t' << it.m_tele << endl;
  164. ?? ??? ??? ??? ?break;
  165. ?? ??? ??? ?}
  166. ?? ??? ?}
  167. ?? ??? ?if (!b) {
  168. ?? ??? ??? ?cout << "No such Id.!" << endl;
  169. ?? ??? ?}
  170. ?? ?}
  171. }

main.cpp:测试

  1. #include"AddressList.h"
  2. #include <iostream>
  3. using namespace std;
  4. ?
  5. int main() {
  6. ?? ?Person p;
  7. ?? ?{
  8. ?? ??? ?p.m_id = 0;
  9. ?? ??? ?p.m_name = "一号";
  10. ?? ??? ?p.m_tele = "11012011900";//任意
  11. ?? ?}
  12. ?? ?int I = 1;//m_id编号
  13. ?? ?AddressList addr(p);
  14. ?? ?{
  15. ?? ??? ?cout << "0.退出\n"
  16. ?? ??? ??? ?<< "1.添加\n"
  17. ?? ??? ??? ?<< "2.删除\n"
  18. ?? ??? ??? ?<< "3.修改\n"
  19. ?? ??? ??? ?<< "4.搜索\n"
  20. ?? ??? ??? ?<< "5.查看\n"
  21. ?? ??? ??? ?<< endl
  22. ?? ??? ??? ?<< endl;
  23. ?? ?}
  24. ?? ?int cho2;
  25. ?? ?bool b(true);
  26. ?? ?while (b) {
  27. ?? ??? ?int cho;
  28. ?? ??? ?int id;
  29. ?? ??? ?string name;
  30. ?? ??? ?string tele;
  31. ?
  32. ?? ??? ?cout << "Your choose:";
  33. ?? ??? ?cin >> cho;
  34. ?? ??? ?switch (cho) {
  35. ?? ??? ?case 0:
  36. ?? ??? ??? ?b = false;
  37. ?? ??? ??? ?break;
  38. ?? ??? ?case 1:
  39. ?? ??? ??? ?cout << "添加信息:\n"
  40. ?? ??? ??? ??? ?<< "姓名:";
  41. ?? ??? ??? ?cin >> name;
  42. ?? ??? ??? ?cout << "号码:";
  43. ?? ??? ??? ?cin >> tele;
  44. ?? ??? ??? ?id = I++;
  45. ?? ??? ??? ?{
  46. ?? ??? ??? ??? ?p.m_id = id;
  47. ?? ??? ??? ??? ?p.m_name = name;
  48. ?? ??? ??? ??? ?p.m_tele = tele;
  49. ?? ??? ??? ?}
  50. ?? ??? ??? ?addr.Add(p);
  51. ?? ??? ??? ?break;
  52. ?? ??? ?case 2:
  53. ?? ??? ??? ?cout << "删除信息:\n";
  54. ?? ??? ??? ?addr.Delete();
  55. ?? ??? ??? ?break;
  56. ?? ??? ?case 3:
  57. ?? ??? ??? ?cout << "修改信息:\n";
  58. ?? ??? ??? ?addr.Modify();
  59. ?? ??? ??? ?break;
  60. ?? ??? ?case 4:
  61. ?? ??? ??? ?cout << "搜索信息\n"
  62. ?? ??? ??? ??? ?<< "1.Id\n"
  63. ?? ??? ??? ??? ?<< "2.Name\n"
  64. ?? ??? ??? ??? ?<< "3.Telephone\n";
  65. ?? ??? ??? ?cout << "Chosse:";
  66. ?? ??? ??? ?cin >> cho2;
  67. ?? ??? ??? ?addr.Search(cho2);
  68. ?? ??? ??? ?break;
  69. ?? ??? ?case 5:
  70. ?? ??? ??? ?cout << "查看信息\n";
  71. ?? ??? ??? ?addr.Print();
  72. ?? ??? ??? ?break;
  73. ?? ??? ?default:break;
  74. ?? ??? ?}
  75. ?? ??? ?cout << endl;
  76. ?? ?}
  77. ?? ?return 0;
  78. }

截图:

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