当前位置:   article > 正文

数据结构与算法导论---通讯录的实现_数据结构与算法实训通讯录代码总结

数据结构与算法实训通讯录代码总结

1、实验简介

   【实验内容的简要说明,具体说明实验完成的功能和性能要求】

      设计的通讯录可以实现1.添加联系人   2.显示联系人  删除联系人  4.查找联系人

5.清空联系人  6.修改联系人 7 .退出联系人的七大功能。性能比较快捷;

2、程序框架

3、关键代码实现

3.1 函数名1。Menu

void Menu()

{

    cout << "~~~~~~~~~~~~~~~~~~~~~~~~" << endl;

    cout << "

$1.添加联系人
$ "
<< endl;

    cout << "

$2.显示联系人
$ "
<< endl;

    cout << "

$3.删除联系人
$ "
<< endl;

    cout << "

$4.查找联系人
$ "
<< endl;

    cout << "

$5.清空联系人
$ "
<< endl;

    cout << "

$6.修改联系人
$ "
<< endl;

    cout << "

$0.退出联系人
$ "
<< endl;

    cout << "                        " << endl;

    cout << "~~~~~~~~~~~~~~~~~~~~~~~~" << endl;

}

用了一些字符使其更加美观

3.2 函数名2:addPerson

void addPerson(Addressbooks *abs)    //添加联系人

{

    //判断通讯录是否已满

    if (abs->size >= MAX) {

         cout << "通讯录已满,请删除一些联系人" << endl;

         return;

    }

    else {

         //添加具体联系人

         //姓名

         string Name;

         cout << "请输入姓名" << endl;

         cin >> Name;

         abs->personArray[abs->size].m_name = Name;

         //性别

         cout << "请输入性别" << endl;

         cout << "1--男" << endl;

         cout << "2--女" << endl;

         int a = 0;

         do

         {

             int  Sex;

             cin >> Sex;

             if (Sex == 1 || Sex == 2)

             {

                  if (Sex == 1)

                  {

                 

                      abs->personArray[abs->size].m_age = Sex;

                      break;

                  }

                  else

                  {

                     

                      abs->personArray[abs->size].m_age = Sex;

                      break;

                  }

                 

             }

             else {

                  cout << "输入错误,请重新输入" << endl;

                  a = 1;

             }

         } while (a = 1);

         //年龄

         cout << "请输入年龄" << endl;

         int Age = 0;

         cin >> Age;

         abs->personArray[abs->size].m_age = Age;

         //电话

         cout << "请输入电话号码" << endl;

         string Phone;

         cin >> Phone;

         abs->personArray[abs->size].m_phone = Phone;

         //住址

         cout << "请输入住址" << endl;

         string Address ;

         cin >> Address;

         abs->personArray[abs->size].m_address = Address;

         cout << "添加联系人成功!!!!" << endl;

         abs->size++;

         system("pause");

         system("cls");//实现清屏的操作

    }

}

3.3 函数名3:showPerson

void showPerson(Addressbooks *abs)

{

    //判断通讯录人数是否为零

    if (abs->size >= MAX)

       {

            cout << "通讯录人数已满,请进行删除操作" << endl;

       }

    else

        {

             for (int i = 0; i <= abs->size; i++)

             {

             cout << abs->personArray[i].m_name << "\t";

             if (abs->personArray[i].m_sex = 1)

             {

                  cout << "男" << "\t";

             }

             else {

                  cout << "女" << "\t";

             }

             cout << abs->personArray[i].m_sex << "\t";

             cout << abs->personArray[i].m_age << "\t";

             cout << abs->personArray[i].m_phone << "\t";

             cout << abs->personArray[i].m_address << endl;

            

             }

         }

}

3.4 函数名4:textbooks

void textbooks(Addressbooks *abs,string name)//判断有没有联系人在里面

{

   

    for (int i = 0; i <= abs->size; i++)

    {

         if (abs->personArray[i].m_name !=name)

         {

             cout << "没有找到该联系人" << endl;

         }

    }

}

3.5 函数名5:deletePerson

void deletePerson(Addressbooks *abs)

{

   

    if (abs->size == 0)//判断通讯录人数是否为零

    {

         cout << "通讯录人数为0,请进行添加操作" << endl;

    }

    else

    {

         cout << "请再次输入要删除的人名" << endl;

         string name;

         cin >> name;

         for (int i = 0; i <= abs->size; i++)

         {

             if (abs->personArray[i].m_name == name)

             {

                  abs->personArray[i] = abs->personArray[i + 1];

             }

             abs->size--;

            

         }

    }

    system("pause");

    system("cls");//实现清屏的操作

}

3.6 函数名6:Findperson

void Findperson(Addressbooks *abs, string rname)

{

    cout << "请输入要查找的人的姓名" << endl;

    for (int i = 0; i <= abs->size; i++)

    {

        

         cin >> rname;

         if (abs->personArray[i].m_name == rname)

         {

             cout << "已找到该联系人为第" << i << "位" << endl;

             cout << "是否进行操作?" << endl;

             cout << "是---1" << endl;

             cout << "否---2" << endl;

             int a = 0;

             cin>> a;

             if (a = 1)

             {

                  cout << abs->personArray[i].m_name << "\t";

                  if (abs->personArray[i].m_sex = 1)

                  {

                      cout << "男" << "\t";

                  }

                  else {

                      cout << "女" << "\t";

                  }

                  cout << abs->personArray[i].m_sex << "\t";

                  cout << abs->personArray[i].m_age << "\t";

                  cout << abs->personArray[i].m_phone << "\t";

                  cout << abs->personArray[i].m_address << endl;

                  system("pause");

                  system("cls");//实现清屏的操作

             }

            

         }

         else

         {

         cout<< "查无此人" << endl;

         system("pause");

         system("cls");//实现清屏的操作

         }

    }

}

3.7 函数名7: DeleteALLperson

void DeleteALLperson(Addressbooks *abs)

{

    abs->size = 0;

    cout << "通讯录已清空" << endl;

    system("pause");

    system("cls");

}

3.8 函数名8:fixPerson

oid fixPerson(Addressbooks *abs, string name)

{

    cin >> name;

    int x = 0;

    for (x = 0; x <= abs->size; x++)

    {

         if (abs->personArray[x].m_name == name)

         {

             int b;

             cout << "已找到该联系人为第" << x << "位" << endl;

             cout << "是否进行操作?" << endl;

             cout << "是---1" << endl;

             cout << "否---2" << endl;

             cout << "请选择修改的属性------" << endl;

             cout << "姓名---1" << endl;

             cout << "性别---2" << endl;

             cout << "年龄---3" << endl;

             cout << "电话号码---4" << endl;

             cout << "地址---5" << endl;

             cin >> b;

             if (b <= 5)

             {

                  switch (b)

                  {

                  case 1:

                  {

                      cout << "请输入修改后的姓名" << endl;

                      string Fixedname;

                      cin >> Fixedname;

                      abs->personArray[abs->size].m_name = Fixedname;

                      break;

                  }

                  case 2:

                  {

                      cout << "请输入修改后的性别" << endl;

                      char Fixedsex;

                      cin >> Fixedsex;

                      abs->personArray[abs->size].m_sex = Fixedsex;

                      break;

                  }

                  case 3:

                  {

                      cout << "请输入修改后的年龄" << endl;

                      int Fixedage;

                      cin >> Fixedage;

                      abs->personArray[abs->size].m_age = Fixedage;

                      break;

                  }

                  case 4:

                  {

                      cout << "请输入修改后的电话号码" << endl;

                      string Fixedphone;

                      cin >> Fixedphone;

                      abs->personArray[abs->size].m_phone = Fixedphone;

                      break;

                  }

                  case 5:

                  {

                      cout << "请输入修改后的地址" << endl;

                      string Fixedaddress;

                      cin >> Fixedaddress;

                      abs->personArray[abs->size].m_address = Fixedaddress;

                      break;

                  }

                  }

             }

             else

             {

                  cout << "未找到该联系人!!!" << endl;

             }

         }

    }

}

4、不足

【实验程序哪些函数功能还有缺陷或不足,或者程序架构有不足,或者性能还有待提高、或者代码不和规范等等,所有你自己对程序不满意的地方】

打开的速度比较慢,时间复杂度比较高。

5、心得体会

【所有你在实验中的感受和想和老师说的话都可以放在这里,篇幅不限】

通过学习通讯录,我又更加体会到了语法知识的重要性对运行效率的影响,我对数据结构和算法有了更多的认识和充分的了解。

.对项目进行性能测试,再做代码的时间复杂度、空间复杂度分析,会让我们通过不停的反思,去思考另一种算法的方式,在写代码的时候,要注意批注和注释的重要性,有的时候我就会因为读不懂自己的代码而花很多的时间又重新去看一遍。

     

附录  完整代码:

  1. #include <iostream>
  2. #include <string>
  3. #define MAX 100
  4. using namespace std;
  5. struct person//联系人的结构体
  6. {
  7. string m_name;
  8. char m_sex;
  9. int m_age;
  10. string m_phone;
  11. string m_address;
  12. int m_size;
  13. };
  14. void Menu()
  15. {
  16. cout << "~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
  17. cout << " $$$$$1.添加联系人$$$$$ " << endl;
  18. cout << " $$$$$2.显示联系人$$$$$ " << endl;
  19. cout << " $$$$$3.删除联系人$$$$$ " << endl;
  20. cout << " $$$$$4.查找联系人$$$$$ " << endl;
  21. cout << " $$$$$5.清空联系人$$$$$ " << endl;
  22. cout << " $$$$$6.修改联系人$$$$$ " << endl;
  23. cout << " $$$$$0.退出联系人$$$$$ " << endl;
  24. cout << " " << endl;
  25. cout << "~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
  26. }
  27. struct Addressbooks
  28. {
  29. //通讯录中保存的联系人数组
  30. struct person personArray[MAX];
  31. //通讯录中保存的记录的联系人个数
  32. int size=0;
  33. };
  34. void addPerson(Addressbooks *abs) //添加联系人
  35. {
  36. //判断通讯录是否已满
  37. if (abs->size >= MAX) {
  38. cout << "通讯录已满,请删除一些联系人" << endl;
  39. return;
  40. }
  41. else {
  42. //添加具体联系人
  43. //姓名
  44. string Name;
  45. cout << "请输入姓名" << endl;
  46. cin >> Name;
  47. abs->personArray[abs->size].m_name = Name;
  48. //性别
  49. cout << "请输入性别" << endl;
  50. cout << "1--男" << endl;
  51. cout << "2--女" << endl;
  52. int a = 0;
  53. do
  54. {
  55. int Sex;
  56. cin >> Sex;
  57. if (Sex == 1 || Sex == 2)
  58. {
  59. if (Sex == 1)
  60. {
  61. abs->personArray[abs->size].m_age = Sex;
  62. break;
  63. }
  64. else
  65. {
  66. abs->personArray[abs->size].m_age = Sex;
  67. break;
  68. }
  69. }
  70. else {
  71. cout << "输入错误,请重新输入" << endl;
  72. a = 1;
  73. }
  74. } while (a = 1);
  75. //年龄
  76. cout << "请输入年龄" << endl;
  77. int Age = 0;
  78. cin >> Age;
  79. abs->personArray[abs->size].m_age = Age;
  80. //电话
  81. cout << "请输入电话号码" << endl;
  82. string Phone;
  83. cin >> Phone;
  84. abs->personArray[abs->size].m_phone = Phone;
  85. //住址
  86. cout << "请输入住址" << endl;
  87. string Address ;
  88. cin >> Address;
  89. abs->personArray[abs->size].m_address = Address;
  90. cout << "添加联系人成功!!!!" << endl;
  91. abs->size++;
  92. system("pause");
  93. system("cls");//实现清屏的操作
  94. }
  95. }
  96. void showPerson(Addressbooks *abs)
  97. {
  98. //判断通讯录人数是否为零
  99. if (abs->size >= MAX)
  100. {
  101. cout << "通讯录人数已满,请进行删除操作" << endl;
  102. }
  103. else
  104. {
  105. for (int i = 0; i <= abs->size; i++)
  106. {
  107. cout << abs->personArray[i].m_name << "\t";
  108. if (abs->personArray[i].m_sex = 1)
  109. {
  110. cout << "男" << "\t";
  111. }
  112. else {
  113. cout << "女" << "\t";
  114. }
  115. cout << abs->personArray[i].m_sex << "\t";
  116. cout << abs->personArray[i].m_age << "\t";
  117. cout << abs->personArray[i].m_phone << "\t";
  118. cout << abs->personArray[i].m_address << endl;
  119. }
  120. }
  121. }
  122. void textbooks(Addressbooks *abs,string name)//判断有没有联系人在里面
  123. {
  124. for (int i = 0; i <= abs->size; i++)
  125. {
  126. if (abs->personArray[i].m_name !=name)
  127. {
  128. cout << "没有找到该联系人" << endl;
  129. }
  130. }
  131. }
  132. void deletePerson(Addressbooks *abs)
  133. {
  134. if (abs->size == 0)//判断通讯录人数是否为零
  135. {
  136. cout << "通讯录人数为0,请进行添加操作" << endl;
  137. }
  138. else
  139. {
  140. cout << "请再次输入要删除的人名" << endl;
  141. string name;
  142. cin >> name;
  143. for (int i = 0; i <= abs->size; i++)
  144. {
  145. if (abs->personArray[i].m_name == name)
  146. {
  147. abs->personArray[i] = abs->personArray[i + 1];
  148. }
  149. abs->size--;
  150. }
  151. }
  152. system("pause");
  153. system("cls");//实现清屏的操作
  154. }
  155. void Findperson(Addressbooks *abs, string rname)
  156. {
  157. cout << "请输入要查找的人的姓名" << endl;
  158. for (int i = 0; i <= abs->size; i++)
  159. {
  160. cin >> rname;
  161. if (abs->personArray[i].m_name == rname)
  162. {
  163. cout << "已找到该联系人为第" << i << "位" << endl;
  164. cout << "是否进行操作?" << endl;
  165. cout << "是---1" << endl;
  166. cout << "否---2" << endl;
  167. int a = 0;
  168. cin>> a;
  169. if (a = 1)
  170. {
  171. cout << abs->personArray[i].m_name << "\t";
  172. if (abs->personArray[i].m_sex = 1)
  173. {
  174. cout << "男" << "\t";
  175. }
  176. else {
  177. cout << "女" << "\t";
  178. }
  179. cout << abs->personArray[i].m_sex << "\t";
  180. cout << abs->personArray[i].m_age << "\t";
  181. cout << abs->personArray[i].m_phone << "\t";
  182. cout << abs->personArray[i].m_address << endl;
  183. system("pause");
  184. system("cls");//实现清屏的操作
  185. }
  186. }
  187. else
  188. {
  189. cout<< "查无此人" << endl;
  190. system("pause");
  191. system("cls");//实现清屏的操作
  192. }
  193. }
  194. }
  195. void DeleteALLperson(Addressbooks *abs)
  196. {
  197. abs->size = 0;
  198. cout << "通讯录已清空" << endl;
  199. system("pause");
  200. system("cls");
  201. }
  202. void fixPerson(Addressbooks *abs, string name)
  203. {
  204. cin >> name;
  205. int x = 0;
  206. for (x = 0; x <= abs->size; x++)
  207. {
  208. if (abs->personArray[x].m_name == name)
  209. {
  210. int b;
  211. cout << "已找到该联系人为第" << x << "位" << endl;
  212. cout << "是否进行操作?" << endl;
  213. cout << "是---1" << endl;
  214. cout << "否---2" << endl;
  215. cout << "请选择修改的属性------" << endl;
  216. cout << "姓名---1" << endl;
  217. cout << "性别---2" << endl;
  218. cout << "年龄---3" << endl;
  219. cout << "电话号码---4" << endl;
  220. cout << "地址---5" << endl;
  221. cin >> b;
  222. if (b <= 5)
  223. {
  224. switch (b)
  225. {
  226. case 1:
  227. {
  228. cout << "请输入修改后的姓名" << endl;
  229. string Fixedname;
  230. cin >> Fixedname;
  231. abs->personArray[abs->size].m_name = Fixedname;
  232. break;
  233. }
  234. case 2:
  235. {
  236. cout << "请输入修改后的性别" << endl;
  237. char Fixedsex;
  238. cin >> Fixedsex;
  239. abs->personArray[abs->size].m_sex = Fixedsex;
  240. break;
  241. }
  242. case 3:
  243. {
  244. cout << "请输入修改后的年龄" << endl;
  245. int Fixedage;
  246. cin >> Fixedage;
  247. abs->personArray[abs->size].m_age = Fixedage;
  248. break;
  249. }
  250. case 4:
  251. {
  252. cout << "请输入修改后的电话号码" << endl;
  253. string Fixedphone;
  254. cin >> Fixedphone;
  255. abs->personArray[abs->size].m_phone = Fixedphone;
  256. break;
  257. }
  258. case 5:
  259. {
  260. cout << "请输入修改后的地址" << endl;
  261. string Fixedaddress;
  262. cin >> Fixedaddress;
  263. abs->personArray[abs->size].m_address = Fixedaddress;
  264. break;
  265. }
  266. }
  267. }
  268. else
  269. {
  270. cout << "未找到该联系人!!!" << endl;
  271. }
  272. }
  273. }
  274. }
  275. int main()
  276. {
  277. Addressbooks abs;
  278. while (1)
  279. {
  280. Menu();
  281. int select = 0;
  282. cin >> select;
  283. switch (select)
  284. {
  285. case 1:
  286. {
  287. addPerson(&abs);
  288. break;
  289. }
  290. case 2:
  291. {
  292. showPerson(&abs);
  293. system("pause");
  294. system("cls");
  295. break;
  296. }
  297. case 3:
  298. {
  299. cout << "请输入要删除的人名" << endl;
  300. string FINDname;
  301. cin >> FINDname;
  302. textbooks(&abs, FINDname);
  303. deletePerson(&abs);
  304. break;
  305. }
  306. case 4:
  307. {
  308. string Findname;
  309. Findperson(&abs, Findname);
  310. break;
  311. }
  312. case 5:
  313. {
  314. DeleteALLperson(&abs);
  315. break;
  316. }
  317. case 6:
  318. {
  319. string Fixname;
  320. cin >> Fixname;
  321. fixPerson(&abs, Fixname);
  322. break;
  323. }
  324. case 0:
  325. {
  326. cout << "欢迎下次使用" << endl;
  327. system("pause");
  328. return 0;
  329. break;
  330. system("pause");
  331. }
  332. }
  333. }
  334. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/559399
推荐阅读
相关标签
  

闽ICP备14008679号