Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. #define MAXVAL 100
  7.  
  8. int to_int(char array) {
  9. return ((int)array - '0');
  10. }
  11.  
  12. typedef struct Node {
  13. unsigned char name[MAXVAL];
  14. unsigned char address[MAXVAL];
  15. int number[MAXVAL];
  16. struct Node *next;
  17. }Node;
  18.  
  19. typedef struct {
  20. Node *head;
  21. } list;
  22.  
  23. void add_new(Node **head);
  24.  
  25. void list_foreach(list *list, void(*function)(Node** head));
  26.  
  27. int list_find(Node* list, int(*cmp_func)(char*, char*), unsigned char* value);
  28.  
  29. int cmp(const unsigned char* a, const unsigned char* b) {
  30. return strcmp(a, b);
  31. }
  32.  
  33. void print_all(Node** head);
  34.  
  35. Node* getNth(Node* head, int n);
  36.  
  37. void deleteNth(Node **head, int n);
  38.  
  39. void save(Node* head);
  40.  
  41. void help(void);
  42.  
  43. int input(Node **head,list* tmp, int count);
  44.  
  45. int main(int argc, char** argv) {
  46. int count;
  47. Node* head = malloc(sizeof(Node));
  48. head = NULL;
  49. list* tmp = malloc(sizeof(list));
  50. if (argc > 1 && strcmp(argv[1], "-i") != 0) {
  51. printf("neizvestnyi kluch");
  52. return 0;
  53. }
  54. if (argc == 2 && !strcmp(argv[1], "-i")) { //если введён ключ -i выполнять программу до ввода exit
  55. help();
  56. while (1) {
  57. count = 0;
  58. if (input(&head, tmp, count) == 1)
  59. break;
  60. }
  61. return 0;
  62. }
  63. }
  64.  
  65. int input(Node **head, list *tmp, int count){
  66. char buffer[MAXVAL];
  67. gets_s(buffer, MAXVAL);
  68. if (strcmp(buffer, "exit") == 0)
  69. return 1;
  70. if (strcmp(buffer, "add") == 0){
  71. count = 1;
  72. add_new(head);
  73. }
  74. if (strcmp(buffer, "printall") == 0) {
  75. count = 1;
  76. tmp->head = *head;
  77. list_foreach(tmp, &print_all);
  78. }
  79. if (strcmp(buffer, "find") == 0) {
  80. count = 1;
  81. char example[MAXVAL];
  82. gets_s(example, MAXVAL);
  83. list_find(*head, &cmp, example);
  84. }
  85. if (strcmp(buffer, "delete") == 0) {
  86. count = 1;
  87. tmp->head = *head;
  88. char example[MAXVAL];
  89. gets_s(example, MAXVAL);
  90. int position = list_find(*head, &cmp, example);
  91. deleteNth(head, position);
  92. }
  93. if (strcmp(buffer, "save") == 0){
  94. count = 1;
  95. save(*head);
  96. }
  97. if (count == 0) printf("neizvestnay komanda\n");
  98. return 0;
  99. }
  100.  
  101.  
  102.  
  103. void deleteNth(Node **head, int n) {
  104. if (n == 0) {
  105. Node *tmp = *head;
  106. (*head) = (*head)->next;
  107. free(tmp);
  108. }
  109. else {
  110. Node *prev = getNth(*head, n - 1);
  111. Node *elm = prev->next;
  112. prev->next = elm->next;
  113. free(elm);
  114. }
  115. printf("deleted");
  116. }
  117.  
  118. Node* getNth(Node* head, int n) {
  119. int counter = 1;
  120. while (counter < n && head) {
  121. head = head->next;
  122. counter++;
  123. }
  124. return head;
  125. }
  126.  
  127. void print_all(Node** head) {
  128. int i = 0;
  129. printf("\n");
  130. printf("name:");
  131. puts((*head)->name);
  132. printf("address:");
  133. puts((*head)->address);
  134. printf("number:");
  135. while ((*head)->number[i] >= 0) {
  136. printf("%d", (*head)->number[i++]);
  137. }
  138. printf("\n");
  139. }
  140.  
  141.  
  142. int list_find(Node* list, int(*cmp_func)(char*, char*), unsigned char* value) { // возвращает тот узел, для которого результат функции сравнения равен нулю
  143. int i = 0, counter = 0;
  144. while (list != NULL) {
  145. if ((*cmp_func)(value, list->name) == 0) {
  146. printf("name:");
  147. puts(list->name);
  148. printf("address:");
  149. puts(list->address);
  150. printf("number:");
  151. while (list->number[i] >= 0) {
  152. printf("%d", list->number[i++]);
  153. }
  154. printf("\n");
  155. return counter;
  156. }
  157. else {
  158. list = list->next;
  159. counter++;
  160. }
  161. }
  162. return -1;
  163. }
  164.  
  165. void list_foreach(list *list, void(*function)(Node** head)) //применаем функцию function к каждому узлу списка
  166. {
  167. Node *tmp = list->head;
  168. while (tmp != NULL) {
  169. function(&tmp);
  170. tmp = tmp->next;
  171. }
  172. }
  173.  
  174. void add_new(Node **head) {
  175. Node *tmp = (Node*)malloc(sizeof(Node));
  176. unsigned char name[MAXVAL];
  177. unsigned char address[MAXVAL];
  178. char number[MAXVAL];
  179.  
  180. printf("name:");
  181. gets_s(name, MAXVAL);
  182. strcpy(tmp->name, name);
  183.  
  184. printf("address:");
  185. gets_s(address, MAXVAL);
  186. strcpy(tmp->address, address);
  187.  
  188. printf("number:");
  189. gets_s(number, MAXVAL);
  190. int i = 0;
  191. while (1) {
  192. if (number[i] == '\0') break;
  193. tmp->number[i] = to_int(number[i]);
  194. i++;
  195. }
  196. tmp->next = (*head);
  197. (*head) = tmp;
  198. }
  199.  
  200. void save(Node* head){
  201. FILE *S1;
  202. int i=0;
  203. S1 = fopen("S1.txt", "ab");
  204. if (S1 == NULL)
  205. exit(1);
  206. while (head != NULL){
  207. fprintf(S1, "name:");
  208. fputs(head->name, S1);
  209. fprintf(S1, "\naddress:");
  210. fputs(head->address, S1);
  211. printf("\n");
  212. fprintf(S1, "number");
  213. while (head->number[i] >= 0) {
  214. fprintf(S1,"%d", head->number[i++]);
  215. }
  216. fprintf(S1,"\n");
  217. head=head->next;
  218. }
  219. fclose(S1);
  220. printf("saved");
  221. }
  222.  
  223. void help(void){
  224. printf("Available commands:\n");
  225. printf("add ");
  226. printf("printall ");
  227. printf("find ");
  228. printf("delete ");
  229. printf("save ");
  230. printf("exit\n");
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement