本站消息

  出租广告位,需要合作请联系站长

  今日名言-想象你自己对困难作出的反应,不是逃避或绕开它们,而是面对它们,同它们打交道,以一种进取的和明智的方式同它们奋斗 。——马克斯威尔·马尔兹

  今日名言-用谅解、宽恕的目光和心理看人、待人。人就会觉得葱笼的世界里,春意盎然,到处充满温暖。——蔡文甫


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

黑马程序员c++通讯录

发布于2021-07-24 20:49     阅读(1351)     评论(0)     点赞(28)     收藏(1)


以下代码为本人所作,因水平所限及时间仓促,疏忽之处在所难免。

  1. #include <iostream>
  2. #include <string>
  3. #include <windows.h>
  4. using namespace std;
  5. #define max 100
  6. struct person
  7. {
  8. string name;
  9. string sex;
  10. string address;
  11. int phonenumber;
  12. int age;
  13. };
  14. struct phonebook
  15. {
  16. person personarr[max];
  17. int personnum;
  18. };
  19. void showmenu()
  20. {
  21. cout << "请输入您要选择的功能" << endl;
  22. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE);
  23. cout << "1-添加联系人" << endl;
  24. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE);//设置蓝色
  25. cout << "2-显示所有联系人" << endl;
  26. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);//设置绿色
  27. cout << "3-删除联系人" << endl;
  28. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);//设置红色
  29. cout << "4-查找联系人" << endl;
  30. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE);//设置蓝色
  31. cout << "5-修改联系人" << endl;
  32. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);//设置绿色
  33. cout << "6-清空通讯录" << endl;
  34. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);//设置红色
  35. cout << "7-退出通讯录" << endl;
  36. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED|FOREGROUND_BLUE);//设置红色
  37. }
  38. void addperson(phonebook* p)
  39. {
  40. if (p->personnum == max)
  41. {
  42. cout << "联系人已满" << endl;
  43. return;
  44. }
  45. else
  46. {
  47. string name;
  48. cout << "请输入您要添加的联系人的名字" << endl;
  49. cin >> name;
  50. p->personarr[p->personnum].name = name;
  51. cout << "请输入您要添加的联系人的地址" << endl;
  52. string address;
  53. cin >> address;
  54. p->personarr[p->personnum].address = address;
  55. cout << "请输入您要添加的联系人的年龄" << endl;
  56. int age;
  57. cin >> age;
  58. p->personarr[p->personnum].age = age;
  59. cout << "请输入您要添加的联系人的电话号码" << endl;
  60. int phonenumber;
  61. cin >> phonenumber;
  62. p->personarr[p->personnum].phonenumber = phonenumber;
  63. cout << "请输入您要添加的联系人的性别" << endl;
  64. string sex;
  65. cin >> sex;
  66. p->personarr[p->personnum].sex = sex;
  67. p->personnum++;
  68. }
  69. cout << "输入完成" << endl;
  70. system("pause");
  71. system("cls");
  72. }
  73. void showphonebook(phonebook* p)
  74. {
  75. if (p->personnum == 0)
  76. {
  77. cout << "联系人为空" << endl;
  78. system("pause");
  79. system("cls");
  80. }
  81. else
  82. {
  83. for (int i = 0; i < p->personnum; i++)
  84. {
  85. cout << "姓名:" << p->personarr[i].name << "\t";
  86. cout << "年龄:" << p->personarr[i].age << "\t";
  87. cout << "住址:" << p->personarr[i].address << "\t";
  88. cout << "电话:" << p->personarr[i].phonenumber << "\t";
  89. cout << "性别:" << p->personarr[i].sex << "\t";
  90. cout << endl;
  91. }
  92. system("pause");
  93. system("cls");
  94. }
  95. }
  96. int isexit(phonebook*p)
  97. {
  98. if (p->personnum == 0)
  99. {
  100. cout << "通讯录为空" << endl;
  101. system("pause");
  102. system("cls");
  103. return -1;
  104. }
  105. else
  106. {
  107. string name;
  108. cout << "请输入要删除的名字" << endl;
  109. cin >> name;
  110. for (int i = 0; i < p->personnum; i++)
  111. {
  112. if (name == p->personarr[i].name)
  113. {
  114. return i;
  115. }
  116. }
  117. }
  118. }
  119. void deleteinperson(phonebook* p,int ssd)
  120. {
  121. if (ssd >= 0)
  122. {
  123. for(int i=ssd;i<p->personnum;i++)
  124. {
  125. p->personarr[i] = p->personarr[i + 1];
  126. }
  127. cout << "删除成功" << endl;
  128. system("pause");
  129. system("cls");
  130. }
  131. }
  132. void searchinperson(phonebook*p)
  133. {
  134. string name;
  135. cout << "请输入要查找的联系人名字:" << endl;
  136. cin >> name;
  137. for (int i = 0; i <= p->personnum; i++)
  138. {
  139. if (p->personarr[i].name == name)
  140. {
  141. cout << "以下为联系人信息" << endl;
  142. cout << p->personarr[i].address << '\t'; cout << p->personarr[i].age << '\t'; cout << p->personarr[i].name << '\t'; cout << p->personarr[i].phonenumber << '\t'; cout << p->personarr[i].address << '\t'; cout << p->personarr[i].sex << endl;
  143. }
  144. }
  145. cout << "若未显示联系人信息,则是您未将该联系人添加到通讯录" << endl;
  146. system("pause");
  147. system("cls");
  148. }
  149. void exchangeinperson(phonebook*p)
  150. {
  151. string name;
  152. cout << "请输入要修改的联系人的名字" << endl;
  153. cin >> name;
  154. for(int i=0;i<=p->personnum;i++)
  155. {
  156. if (name == p->personarr[i].name)
  157. {
  158. int num;
  159. cout << "请输入你要修改的手机号" << endl;
  160. cin >> num;
  161. p->personarr->phonenumber = num;
  162. cout << "修改完成" << endl;
  163. }
  164. }
  165. system("pause");
  166. system("cls");
  167. }
  168. void blankalldata(phonebook* p)
  169. {
  170. p->personnum = 0;
  171. cout << "清空完成" << endl;
  172. system("pause");
  173. system("cls");
  174. }
  175. int main()
  176. {
  177. phonebook inperson;
  178. inperson.personnum = 0;
  179. int a = 0;
  180. while (true)
  181. {
  182. showmenu();
  183. cin >> a;
  184. switch (a)
  185. {
  186. case 1:addperson(&inperson);
  187. break;
  188. case 2:
  189. showphonebook(&inperson);
  190. break;
  191. case 3:
  192. deleteinperson(&inperson, isexit(&inperson));
  193. break;
  194. case 4:
  195. searchinperson(&inperson);
  196. break;
  197. case 5:
  198. exchangeinperson(&inperson);
  199. break;
  200. case 6:
  201. blankalldata(&inperson);
  202. break;
  203. case 7:cout << "祝您生活愉快,欢迎下次使用。" << endl;
  204. system("pause");
  205. return 0;
  206. break;
  207. default:
  208. break;
  209. }
  210. }
  211. }



所属网站分类: 程序员的那点事

作者:小可爱不要爱

链接:http://www.pythonpdf.com/blog/article/284/1d9e2461538ee5ee7c73/

来源:编程知识网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

28 0
收藏该文
已收藏

评论内容:(最多支持255个字符)