当前位置:   article > 正文

C语言——通讯录实现

C语言——通讯录实现

一、介绍

本文只是对于结构体类型的练习。

只是简单的静态通讯录实现,没有具体的UI界面,只有一些简单的功能,同时也比较粗糙,有很多地方没有经过足够的打磨。

二、源码

本项目包含三个文件:

test.c/cpp

  1. #include "contacts_list.h"
  2. int main()
  3. {
  4. int status = 0;
  5. Contacts_list contacts_list;//通讯录结构体变量
  6. InitContact(&contacts_list);
  7. int res = 0;
  8. int num = 0;
  9. do
  10. {
  11. menu();//打印菜单
  12. scanf("%d", &status);
  13. system("cls");
  14. switch (status)
  15. {
  16. case 1:
  17. AddContact(&contacts_list);
  18. break;
  19. case 2:
  20. DeleteContact(&contacts_list);
  21. break;
  22. case 3:
  23. char name1[21];
  24. printf("please type in the name>:");
  25. scanf("%s", name1);
  26. res = SearchContact(&contacts_list, name1);
  27. if (res != -1)
  28. {
  29. printf("%d\n", res);
  30. }
  31. else
  32. {
  33. printf("cannot find\n");
  34. }
  35. break;
  36. case 4:
  37. char name2[21];
  38. printf("please type in the name>:");
  39. scanf("%s", name2);
  40. ModifyContact(&contacts_list,name2);
  41. break;
  42. case 5:
  43. DisplayContact(&contacts_list);
  44. break;
  45. case 6:
  46. printf("1.sort by name\n");
  47. printf("2.sort by age\n");
  48. scanf("%d", &num);
  49. SortContact(&contacts_list,num);
  50. break;
  51. case 0:
  52. printf("exit\n");
  53. break;
  54. default :
  55. printf("error\n");
  56. break;
  57. }
  58. } while (status);
  59. return 0;
  60. }

contact.c/cpp

  1. #include "contacts_list.h"
  2. void menu()
  3. {
  4. printf("*************************************\n");
  5. printf("***** 1.add 2.del ****\n");
  6. printf("***** 3.search 4.modify ****\n");
  7. printf("***** 5.display 6.sort ****\n");
  8. printf("***** 0.exit ****\n");
  9. printf("*************************************\n");
  10. printf("please choose a option>:");
  11. }
  12. void InitContact(Contacts_list* list)
  13. {
  14. int i = 0;
  15. for (i = 0; i < 100; i++)
  16. {
  17. list->num = 0;//将联系人个数初始化为零
  18. memset(list->data, 0, sizeof(list->data));//将数据部分初始化为0
  19. }
  20. }
  21. void AddContact(Contacts_list* list)
  22. {
  23. if (list->num == MAXCONTACTS)
  24. {
  25. printf("there is no more space\n");//没有空间
  26. return;
  27. }
  28. printf("please type in name(maximum 20)>:");
  29. scanf("%s", list->data[list->num].name);
  30. printf("please type in gender(maximum 8)>:");
  31. scanf("%s", list->data[list->num].gender);
  32. printf("please type in age>:");
  33. scanf("%d", &(list->data[list->num].age));
  34. list->num++;
  35. printf("operating successfully\n");
  36. }
  37. void DisplayContact(const Contacts_list* list)
  38. {
  39. int i = 0;
  40. for (i = 0; i < list->num; i++)
  41. {
  42. printf("name: %s\n", list->data[i].name);
  43. printf("gender: %s\n", list->data[i].gender);
  44. printf("age: %d\n", list->data[i].age);
  45. printf("-------------------------\n");
  46. }
  47. printf("display successfully\n");
  48. }
  49. void DeleteContact(Contacts_list* list)
  50. {
  51. char name[21];
  52. if (list->num == 0)
  53. {
  54. printf("there is no contact can be deleted\n");//联系人为零,无需删除
  55. return;
  56. }
  57. printf("please type in the name who you want to delete>:");
  58. scanf("%s", name);
  59. int res = SearchContact(list, name);
  60. if (res != -1)
  61. {
  62. int i = 0;
  63. for (i = res; i < list->num - 1; i++)
  64. {
  65. list->data[i] = list->data[i + 1];
  66. }
  67. list->num--;
  68. printf("delete successfully\n");
  69. }
  70. else
  71. {
  72. printf("cannot find\n");//没找到要删除的联系人
  73. return;
  74. }
  75. }
  76. int SearchContact(Contacts_list* list, const char* string)
  77. {
  78. int i = 0;
  79. for (i = 0; i < list->num; i++)
  80. {
  81. if (strcmp(list->data[i].name, string) == 0)
  82. {
  83. return i;//查找到返回下标
  84. }
  85. }
  86. return -1;//未查找到返回-1
  87. }
  88. void ModifyContact(Contacts_list* list, const char* string)
  89. {
  90. int res = SearchContact(list, string);
  91. if (res == -1)
  92. {
  93. printf("cannot find\n");
  94. return;
  95. }
  96. printf("please type in name(maximum 20)>:");
  97. scanf("%s", list->data[res].name);
  98. printf("please type in gender(maximum 8)>:");
  99. scanf("%s", list->data[res].gender);
  100. printf("please type in age>:");
  101. scanf("%d", &(list->data[res].age));
  102. printf("modification successfully\n");
  103. }
  104. int cmp_by_name(const void* element1, const void* element2)
  105. {
  106. PeopleInfo* ele1 = (PeopleInfo*)element1;//强制转换为联系人结构体类型,方便访问name变量
  107. PeopleInfo* ele2 = (PeopleInfo*)element2;
  108. return strcmp(ele1->name, ele2->name);
  109. }
  110. int cmp_by_age(const void* element1, const void* element2)
  111. {
  112. int res = ((PeopleInfo*)element1)->age - ((PeopleInfo*)element2)->age;
  113. return ((-res < 0) - (res < 0));
  114. }
  115. void SortContact(Contacts_list* list, int num)
  116. {
  117. switch (num)
  118. {
  119. case 1:
  120. qsort(list->data, list->num, sizeof(list->data[0]), cmp_by_name);
  121. printf("sort successfully\n");
  122. break;
  123. case 2:
  124. qsort(list->data, list->num, sizeof(list->data[0]), cmp_by_age);
  125. printf("sort successfully\n");
  126. break;
  127. default :
  128. printf("error\n");
  129. break;
  130. }
  131. }

contact.h

  1. #pragma once
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #define MAXCONTACTS 100
  6. //打印菜单函数
  7. void menu();
  8. //声明联系人信息结构体变量
  9. typedef struct PeopleInfo
  10. {
  11. char name[21];
  12. char gender[9];
  13. int age;
  14. }PeopleInfo;
  15. //声明通讯录结构体变量
  16. typedef struct Contacts_list
  17. {
  18. PeopleInfo data[MAXCONTACTS];//存放联系人数据
  19. int num;//记录通讯录联系人个数
  20. }Contacts_list;
  21. //初始化函数
  22. void InitContact(Contacts_list* list);
  23. //添加联系人
  24. void AddContact(Contacts_list* list);
  25. //显示通讯录
  26. void DisplayContact(const Contacts_list* list);
  27. //删除联系人
  28. void DeleteContact(Contacts_list* list);
  29. //查找联系人
  30. int SearchContact(Contacts_list* list, const char* string);
  31. //修改联系人
  32. void ModifyContact(Contacts_list* list,const char* string);
  33. //排序联系人
  34. void SortContact(Contacts_list* list, int num);
  35. //按名字排序
  36. int cmp_by_name(const void* element1, const void* element2);
  37. //按年龄排序
  38. int cmp_by_age(const void* element1, const void* element2);

如有错误,欢迎指正。

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

闽ICP备14008679号