Research portable Memory game | Исследовать портируемую игру Память
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
3.5KB

  1. #include <stdio.h> /* gets */
  2. #include <stdlib.h> /* atoi, malloc */
  3. #include <string.h> /* strcpy */
  4. #include "uthash.h"
  5. struct my_struct {
  6. int id; /* key */
  7. char name[10];
  8. UT_hash_handle hh; /* makes this structure hashable */
  9. };
  10. struct my_struct *users = NULL;
  11. void add_user(int user_id, char *name)
  12. {
  13. struct my_struct *s;
  14. HASH_FIND_INT(users, &user_id, s); /* id already in the hash? */
  15. if (s == NULL) {
  16. s = (struct my_struct*)malloc(sizeof(struct my_struct));
  17. s->id = user_id;
  18. HASH_ADD_INT(users, id, s); /* id: name of key field */
  19. }
  20. strcpy(s->name, name);
  21. }
  22. struct my_struct *find_user(int user_id)
  23. {
  24. struct my_struct *s;
  25. HASH_FIND_INT(users, &user_id, s); /* s: output pointer */
  26. return s;
  27. }
  28. void delete_user(struct my_struct *user)
  29. {
  30. HASH_DEL(users, user); /* user: pointer to deletee */
  31. free(user);
  32. }
  33. void delete_all()
  34. {
  35. struct my_struct *current_user, *tmp;
  36. HASH_ITER(hh, users, current_user, tmp) {
  37. HASH_DEL(users, current_user); /* delete it (users advances to next) */
  38. free(current_user); /* free it */
  39. }
  40. }
  41. void print_users()
  42. {
  43. struct my_struct *s;
  44. for (s = users; s != NULL; s = (struct my_struct*)(s->hh.next)) {
  45. printf("user id %d: name %s\n", s->id, s->name);
  46. }
  47. }
  48. int name_sort(struct my_struct *a, struct my_struct *b)
  49. {
  50. return strcmp(a->name, b->name);
  51. }
  52. int id_sort(struct my_struct *a, struct my_struct *b)
  53. {
  54. return (a->id - b->id);
  55. }
  56. void sort_by_name()
  57. {
  58. HASH_SORT(users, name_sort);
  59. }
  60. void sort_by_id()
  61. {
  62. HASH_SORT(users, id_sort);
  63. }
  64. int main()
  65. {
  66. char in[10];
  67. int id = 1, running = 1;
  68. struct my_struct *s;
  69. unsigned num_users;
  70. while (running) {
  71. printf(" 1. add user\n");
  72. printf(" 2. add/rename user by id\n");
  73. printf(" 3. find user\n");
  74. printf(" 4. delete user\n");
  75. printf(" 5. delete all users\n");
  76. printf(" 6. sort items by name\n");
  77. printf(" 7. sort items by id\n");
  78. printf(" 8. print users\n");
  79. printf(" 9. count users\n");
  80. printf("10. quit\n");
  81. gets(in);
  82. switch(atoi(in)) {
  83. case 1:
  84. printf("name?\n");
  85. add_user(id++, gets(in));
  86. break;
  87. case 2:
  88. printf("id?\n");
  89. gets(in);
  90. id = atoi(in);
  91. printf("name?\n");
  92. add_user(id, gets(in));
  93. break;
  94. case 3:
  95. printf("id?\n");
  96. s = find_user(atoi(gets(in)));
  97. printf("user: %s\n", s ? s->name : "unknown");
  98. break;
  99. case 4:
  100. printf("id?\n");
  101. s = find_user(atoi(gets(in)));
  102. if (s) {
  103. delete_user(s);
  104. } else {
  105. printf("id unknown\n");
  106. }
  107. break;
  108. case 5:
  109. delete_all();
  110. break;
  111. case 6:
  112. sort_by_name();
  113. break;
  114. case 7:
  115. sort_by_id();
  116. break;
  117. case 8:
  118. print_users();
  119. break;
  120. case 9:
  121. num_users = HASH_COUNT(users);
  122. printf("there are %u users\n", num_users);
  123. break;
  124. case 10:
  125. running = 0;
  126. break;
  127. }
  128. }
  129. delete_all(); /* free any structures */
  130. return 0;
  131. }