• ​7.1 项目1 学生通讯录管理:文本文件增删改查(C++版本)(自顶向下设计+断点调试) (A)​


    C++自学精简教程 目录(必读)

    作业目标:

    这个作业中,你需要综合运用之前文章中的知识,来解决一个相对完整的应用程序。

    作业描述:

    1 在这个作业中你需要在文本文件中存储学生通讯录的信息,并在程序启动的时候加载这些数据到内存中。

    2 在程序运行过程中允许用户用键盘输入信息来完成对通讯录数的增删改查。

    交互示例:

    开始代码

    开始代码不是完整的代码,需要你填写一部分代码,使之完整。

    答案在本文最后

    当你填写完整之后,运行程序和示例的交互输出一致,就算完成了这个作业

    开始代码:

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. using namespace std;
    8. class Person
    9. {
    10. public:
    11. friend ostream& operator<<(ostream& os, const Person& _person);
    12. friend istream& operator>>(istream& is, Person& _person);
    13. public:
    14. string m_id;
    15. string m_name;
    16. string m_tel;
    17. };
    18. ostream& operator<<(ostream& os, const Person& person)
    19. {
    20. os << left << setw(5) << person.m_id << setw(15) << person.m_name << setw(20) << person.m_tel;
    21. return os;
    22. }
    23. istream& operator>>(istream& is, Person& person)
    24. {
    25. //(1) your code
    26. // 使用输入操作符重载,将流中的数据,提取赋值给person对象的成员变量中
    27. //see https://zhuanlan.zhihu.com/p/412724745
    28. return is;
    29. }
    30. class PersonManager
    31. {
    32. public:
    33. void InputOnePerson(void);
    34. bool LoadAllPersonFromFile(const string& fileName);
    35. bool DeletePerson(void);
    36. bool QueryPersonByName() const;
    37. bool QueryPersonByTel() const;
    38. void ShowAllPerson(void) const;
    39. bool SaveAllPersonToFile(void) const;
    40. private:
    41. vector m_allPerson;
    42. };
    43. bool PersonManager::DeletePerson(void)
    44. {
    45. cout << "Please input person id for delete:";
    46. string id;
    47. cin >> id;
    48. for (auto itr = m_allPerson.begin(); itr != m_allPerson.cend(); ++itr)
    49. {
    50. if (itr->m_id == id)
    51. {
    52. //(2) your code
    53. // 容器的erase方法支持删除容器的元素时,传入指向元素的迭代器
    54. //see https://zhuanlan.zhihu.com/p/441293600
    55. }
    56. }
    57. return false;
    58. }
    59. bool PersonManager::QueryPersonByName() const
    60. {
    61. //注意该函数需要返回bool值
    62. cout << "Please input name for query:";
    63. string name;
    64. cin >> name;
    65. for (auto itr = m_allPerson.cbegin(); itr != m_allPerson.cend(); ++itr)
    66. {
    67. if (itr->m_name == name)
    68. {
    69. cout << "Find:" << endl;
    70. //(3) your code
    71. //see https://zhuanlan.zhihu.com/p/376440190
    72. //see https://zhuanlan.zhihu.com/p/376446724
    73. }
    74. }
    75. cout << "not found " << name << endl;
    76. return false;
    77. }
    78. bool PersonManager::QueryPersonByTel() const
    79. {
    80. cout << "Please input tel for query:";
    81. string tel;
    82. cin >> tel;
    83. for (auto itr = m_allPerson.cbegin(); itr != m_allPerson.cend(); ++itr)
    84. {
    85. if (itr->m_tel == tel)
    86. {
    87. cout << "Find:" << endl;
    88. //(4) your code
    89. //see https://zhuanlan.zhihu.com/p/376440190
    90. //see https://zhuanlan.zhihu.com/p/376446724
    91. }
    92. }
    93. cout << "not found " << tel << endl;
    94. return false;
    95. }
    96. void PersonManager::ShowAllPerson(void) const
    97. {
    98. cout << "All Person:" << endl;
    99. cout << left << setw(5) << "id" << setw(15) << "name" << setw(20) << "tel" << endl;
    100. for (auto& item : m_allPerson)
    101. {
    102. cout << item << endl;
    103. }
    104. }
    105. bool PersonManager::SaveAllPersonToFile(void) const
    106. {
    107. ofstream fout("data_saved.txt");
    108. //下面的常量迭代器 cbegin cend 中的 c 指的是 const的意思,表示不可以修改容器的元素
    109. for (auto itr = m_allPerson.cbegin(); itr != m_allPerson.cend(); ++itr)
    110. {
    111. //(5) your code
    112. //see https://zhuanlan.zhihu.com/p/262508774
    113. }
    114. return true;
    115. }
    116. bool PersonManager::LoadAllPersonFromFile(const string& fileName)
    117. {
    118. ifstream fin(fileName);
    119. if (!fin)
    120. {
    121. cout << "load data failed . file " << fileName << " not exits." << endl;
    122. return false;
    123. }
    124. Person person;
    125. while (fin >> person)
    126. {
    127. m_allPerson.push_back(person);
    128. }
    129. cout << "load data from file success." << endl;
    130. return true;
    131. }
    132. void PersonManager::InputOnePerson(void)
    133. {
    134. cout << "Please input one person:" << endl;
    135. cout << "Please input id:";
    136. string id;
    137. cin >> id;
    138. Person person;
    139. person.m_id = id;
    140. for (auto itr = m_allPerson.cbegin(); itr != m_allPerson.cend(); ++itr)
    141. {
    142. if (itr->m_id == id)
    143. {
    144. cout << id << " already existed! Save failed." << endl;
    145. return;
    146. }
    147. }
    148. cout << "Please input name:";
    149. string name;
    150. cin >> name;
    151. person.m_name = name;
    152. cout << "Please input tel:";
    153. string tel;
    154. cin >> tel;
    155. person.m_tel = tel;
    156. cout << "Input finished, save successed." << endl;
    157. m_allPerson.push_back(person);
    158. }
    159. int main(int argv, char* argc[])
    160. {
    161. PersonManager personMgr;
    162. personMgr.LoadAllPersonFromFile("input_data.txt");
    163. personMgr.ShowAllPerson();
    164. while(true)
    165. {
    166. cout<<"input a commond : "<
    167. cout<<"1 [AddPerson]"<
    168. cout<<"2 [ShowAllPerson]"<
    169. cout<<"3 [QueryPerson by name]"<
    170. cout<<"4 [QueryPerson by tel]"<
    171. cout<<"5 [SaveAllPersonToFile]"<
    172. cout<<"6 [DeletePerson]"<
    173. cout<<"0 [ExitAndSaveChange]"<
    174. int commond;
    175. cin>>commond;
    176. switch(commond)
    177. {
    178. case 1: { personMgr.InputOnePerson(); break;}
    179. case 2: { personMgr.ShowAllPerson(); break;}
    180. case 3: { personMgr.QueryPersonByName(); break;}
    181. case 4: { personMgr.QueryPersonByTel(); break;}
    182. case 5: { personMgr.SaveAllPersonToFile(); break;}
    183. case 6: { personMgr.DeletePerson(); break;}
    184. case 0: { personMgr.SaveAllPersonToFile(); return 0;}
    185. default:{ cout<<"System Exit."<return 0;}
    186. }
    187. }
    188. return 0;
    189. }

    输入文件

    input_data.txt

    文件内容:

    1. 2 zhangsan2 13788889992
    2. 3 zhangsan3 13788889993
    3. 4 zhangsan4 13788889994
    4. 5 wanger 13333333333

    运行与输出

    1. load data from file success.
    2. All Person:
    3. id name tel
    4. 2 zhangsan2 13788889992
    5. 3 zhangsan3 13788889993
    6. 4 zhangsan4 13788889994
    7. 5 wanger 13333333333
    8. input a commond :
    9. 1 [AddPerson]
    10. 2 [ShowAllPerson]
    11. 3 [QueryPerson by name]
    12. 4 [QueryPerson by tel]
    13. 5 [SaveAllPersonToFile]
    14. 6 [DeletePerson]
    15. 0 [ExitAndSaveChange]
    16. 2
    17. All Person:
    18. id name tel
    19. 2 zhangsan2 13788889992
    20. 3 zhangsan3 13788889993
    21. 4 zhangsan4 13788889994
    22. 5 wanger 13333333333
    23. input a commond :
    24. 1 [AddPerson]
    25. 2 [ShowAllPerson]
    26. 3 [QueryPerson by name]
    27. 4 [QueryPerson by tel]
    28. 5 [SaveAllPersonToFile]
    29. 6 [DeletePerson]
    30. 0 [ExitAndSaveChange]
    31. 1
    32. Please input one person:
    33. Please input id:1
    34. Please input name:zhangsan
    35. Please input tel:13344445555
    36. Input finished, save successed.
    37. input a commond :
    38. 1 [AddPerson]
    39. 2 [ShowAllPerson]
    40. 3 [QueryPerson by name]
    41. 4 [QueryPerson by tel]
    42. 5 [SaveAllPersonToFile]
    43. 6 [DeletePerson]
    44. 0 [ExitAndSaveChange]
    45. 2
    46. All Person:
    47. id name tel
    48. 2 zhangsan2 13788889992
    49. 3 zhangsan3 13788889993
    50. 4 zhangsan4 13788889994
    51. 5 wanger 13333333333
    52. 1 zhangsan 13344445555
    53. input a commond :
    54. 1 [AddPerson]
    55. 2 [ShowAllPerson]
    56. 3 [QueryPerson by name]
    57. 4 [QueryPerson by tel]
    58. 5 [SaveAllPersonToFile]
    59. 6 [DeletePerson]
    60. 0 [ExitAndSaveChange]
    61. 3
    62. Please input name for query:zhangsan
    63. Find:
    64. 1 zhangsan 13344445555
    65. input a commond :
    66. 1 [AddPerson]
    67. 2 [ShowAllPerson]
    68. 3 [QueryPerson by name]
    69. 4 [QueryPerson by tel]
    70. 5 [SaveAllPersonToFile]
    71. 6 [DeletePerson]
    72. 0 [ExitAndSaveChange]
    73. 3
    74. Please input name for query:zhang
    75. not found zhang
    76. input a commond :
    77. 1 [AddPerson]
    78. 2 [ShowAllPerson]
    79. 3 [QueryPerson by name]
    80. 4 [QueryPerson by tel]
    81. 5 [SaveAllPersonToFile]
    82. 6 [DeletePerson]
    83. 0 [ExitAndSaveChange]
    84. 4
    85. Please input tel for query:13344445555
    86. Find:
    87. 1 zhangsan 13344445555
    88. input a commond :
    89. 1 [AddPerson]
    90. 2 [ShowAllPerson]
    91. 3 [QueryPerson by name]
    92. 4 [QueryPerson by tel]
    93. 5 [SaveAllPersonToFile]
    94. 6 [DeletePerson]
    95. 0 [ExitAndSaveChange]
    96. 4
    97. Please input tel for query:1334444
    98. not found 1334444
    99. input a commond :
    100. 1 [AddPerson]
    101. 2 [ShowAllPerson]
    102. 3 [QueryPerson by name]
    103. 4 [QueryPerson by tel]
    104. 5 [SaveAllPersonToFile]
    105. 6 [DeletePerson]
    106. 0 [ExitAndSaveChange]
    107. 6
    108. Please input person id for delete:4
    109. input a commond :
    110. 1 [AddPerson]
    111. 2 [ShowAllPerson]
    112. 3 [QueryPerson by name]
    113. 4 [QueryPerson by tel]
    114. 5 [SaveAllPersonToFile]
    115. 6 [DeletePerson]
    116. 0 [ExitAndSaveChange]
    117. 2
    118. All Person:
    119. id name tel
    120. 2 zhangsan2 13788889992
    121. 3 zhangsan3 13788889993
    122. 5 wanger 13333333333
    123. 1 zhangsan 13344445555
    124. input a commond :
    125. 1 [AddPerson]
    126. 2 [ShowAllPerson]
    127. 3 [QueryPerson by name]
    128. 4 [QueryPerson by tel]
    129. 5 [SaveAllPersonToFile]
    130. 6 [DeletePerson]
    131. 0 [ExitAndSaveChange]
    132. 5
    133. input a commond :
    134. 1 [AddPerson]
    135. 2 [ShowAllPerson]
    136. 3 [QueryPerson by name]
    137. 4 [QueryPerson by tel]
    138. 5 [SaveAllPersonToFile]
    139. 6 [DeletePerson]
    140. 0 [ExitAndSaveChange]
    141. 0

    最终保存数据到文件 data_saved.txt

    文件 data_saved.txt 的内容为:

    1. 2 zhangsan2 13788889992
    2. 3 zhangsan3 13788889993
    3. 5 wanger 13333333333
    4. 1 zhangsan 13344445555

    你的结果也是这样吗?

    答案在此

    C++自学精简教程 全部答案

    学生完成该作业展示

    另一个学生实现的效果

    如需答案和答疑:请私信。

  • 相关阅读:
    中国500米分辨率年最大LAI数据集(2000-2020)
    数据库基本概念
    likely和unlikely的用法
    飞天使-mysql-安装以及环境搭建
    数据结构系列学习(七) - 链栈(Chain_Stack)
    【通信】Matlab实现改进的多同步压缩变换
    数字藏品NFT“无聊猿”BAYC的内忧与外患
    LeetCode-热题100-笔记-day31
    解决在pycharm中使用matplotlib画图问题
    (附源码)spring boot建材市场销售管理系统 毕业设计 191544
  • 原文地址:https://blog.csdn.net/ClamReason/article/details/132636814