Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2018
1,000
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.51 KB | None | 0 0
  1. // 9. Структура «Пациент» : фамилия, имя, отчество; домашний адрес; номер медицинской карты; номер страхового полиса.
  2. #include <stdio.h>
  3. #include <windows.h>
  4. #include <stdlib.h>
  5. #include <iostream>
  6. using namespace std;//открывает пространство имен библиотеки iostream
  7.  
  8. typedef struct patients {
  9. char Name[20];
  10. char LastName[20];
  11. char Patronymic[20];
  12. char Address[256];
  13. char HealthCardNumber [20];
  14. char InsuranceNumber [20];
  15. struct patients *next;
  16. //объявляем шаблон структуры
  17. } Patients;
  18.  
  19. Patients* list = NULL;
  20.  
  21. /*ОБЪЯВЛЕНИЕ ПРОТОТИПОВ ФУНКЦИЙ*/
  22. int Menu();
  23. void Input(void);
  24. void Output(void);
  25. void Del();
  26. void CountList();
  27. //void FindMax();//////////////////////////
  28. //void FindMin();///////////////////
  29. void DeleteDouble();/////////////////////
  30. void DeleteList();
  31. void clear_stream();
  32. void clear_stream()
  33. {
  34. while (getchar() != '\n');
  35. }
  36.  
  37. int main()
  38. {
  39. while (1)
  40. {
  41. switch (Menu())//вызов функции menu
  42. {
  43. case 1: Input(); break;//вызов функции ввода
  44. case 2: Output(); break;//вызов функции сортировки
  45. case 3: Del(); break;//вызов функции поиска
  46. case 4: DeleteList(); break;
  47. case 5: CountList(); break;
  48. case 6: DeleteDouble(); break;
  49. case 7: return 0;
  50. }
  51. puts("\n----------------------");
  52. }
  53. }
  54.  
  55.  
  56. int Menu() //определение функции menu
  57. {
  58. int choice;
  59. do {
  60. printf_s("\n Menu \n\n");
  61. printf_s("1. Input an element of a list; \n"); //Ввод массива структур
  62. printf_s("2. Output the list; \n"); //Сортировка массива структур
  63. printf_s("3. Delete an element of the list;\n"); //Поиск
  64. printf_s("4. Delete the list;\n");
  65. printf_s("5. Count all the elements of the list;\n");
  66. printf_s("6. Delete double elements;\n");
  67. printf_s("7. Exit;\n");
  68. //Изменение заданной структуры
  69. printf_s("\n Your choise: ");
  70. scanf_s("%d", &choice);
  71. puts("\n----------------------\n");
  72. } while (choice>7);
  73. return choice;
  74. }
  75.  
  76. void Input()//определение ф-ции ввода новой структуры все ок
  77. {
  78. Patients *p, *previous, *current;
  79. p = (Patients *)malloc(sizeof(Patients));
  80. printf_s("--- New Patient-- \n"); //выводим номер записи
  81. printf_s("Input Name:\n");
  82. clear_stream();
  83. gets_s (p->Name);
  84. printf_s("Input Last name:\n");
  85. //clear_stream();
  86. gets_s(p->LastName);
  87. printf_s("Input Patronymic:\n");
  88. //clear_stream();
  89. gets_s (p->Patronymic);
  90. printf_s("Input Address:\n");
  91. //clear_stream();
  92. gets_s(p->Address);
  93. printf_s("Input Health card number:\n");
  94. //clear_stream();
  95. gets_s(p->HealthCardNumber);
  96. printf_s("Insurance number\n");
  97. //clear_stream();
  98. gets_s(p->InsuranceNumber);
  99. printf_s("_______________________________________________\n");
  100. if (p != NULL)
  101. {
  102. p->next = NULL;
  103. Patients *previous = NULL;
  104. Patients *current = list;
  105. //while (current != NULL && (p->LastName > current->LastName))
  106. while (current != NULL && (_stricmp(p->LastName, current->LastName)>0))
  107. {
  108. previous = current;
  109. current = current->next;
  110. }
  111. if (previous == NULL)
  112. {
  113. p->next = list;
  114. list = p;
  115. }
  116. else
  117. {
  118. previous->next = p;
  119. p->next = current;
  120. }
  121. }
  122. else
  123. printf("No free space!!\n");
  124. }
  125.  
  126. void Output()//функция вывода массива структур
  127. {
  128. Patients *p = list;
  129. if (p == NULL)
  130. printf("List is empty\n");
  131. else
  132. {
  133. printf("Your list:\n");
  134. printf_s("_______________________________________________\n");
  135. int num = 1;
  136. while (p != NULL)
  137. {
  138. printf("Patient Number ");
  139. cout<<num;
  140. printf("\n");
  141. printf("Name: ");
  142. printf(p->Name);
  143. printf("\n");
  144. printf("LastName: ");
  145. printf(p->LastName);
  146. printf("\n");
  147. printf("Patronymic: ");
  148. printf(p->Patronymic);
  149. printf("\n");
  150. printf("Address: ");
  151. printf(p->Address);
  152. printf("\n");
  153. printf("HealthCardNumber: ");
  154. printf(p->HealthCardNumber);
  155. printf("\n");
  156. printf("InsuranceNumber: ");
  157. printf(p->InsuranceNumber);
  158. printf("\n");
  159. p = p->next;
  160. num++;
  161. printf_s("_______________________________________________\n");
  162. }
  163. }
  164.  
  165.  
  166. }
  167.  
  168. void Del()
  169. {
  170. Patients *prev = list, *cur = list->next, *p;
  171. char value[20];
  172. printf_s("Input Last name of Patient you want to delete:\n");
  173. clear_stream();
  174. gets_s(value);
  175. if (strcmp(value, list->LastName) == 0) { /* если нужно удалить */
  176. /* начальный элемент списка */
  177. p = list; /* p указывает на удаляемый элемент*/
  178. list = list->next; /* list указывает на след. элемент */
  179. free(p); /* удалить элемент */
  180. printf_s("Element is deleted\n");
  181. }
  182. else { /* иначе... */
  183. /* С помощью цикла while найти элемент */
  184. while (cur != NULL && strcmp(value, cur->LastName) != 0) {
  185. prev = cur;
  186. cur = cur->next;
  187. }
  188. if (cur != NULL) { /* если элемент найден... */
  189. p = cur; /* p указывает на удаляемый элемент*/
  190. prev->next = cur->next; /* перенастроить связи */
  191. free(p); /* удалить элемент */
  192. printf_s("Element is deleted\n");
  193. }
  194. else {
  195.  
  196. printf_s("There is no such an element\n");
  197. }
  198. }
  199. }
  200. void DeleteList()
  201. {
  202. Patients *prev = list, *p;
  203. if (list != NULL)
  204. {
  205. while (list != NULL)
  206. {
  207. p = list; /* p указывает на удаляемый элемент*/
  208. list = list->next; /* list указывает на след. элемент */
  209. free(p); /* удалить элемент */
  210. printf_s("Element has been deleted\n");
  211. }
  212. printf_s("The list has been deleted");// почему не пишет что список удален?????
  213. }
  214. else if (list = NULL)
  215. {
  216. printf_s("The list is empty");// почему не пишет что список удален?????
  217. }
  218. }
  219. void CountList()//все ок
  220. {
  221. int count = 0;
  222. Patients *prev = list, *p;
  223. p = list;
  224. if (p != NULL)
  225. {
  226. while (p != NULL)
  227. {
  228. p = p->next;
  229. count++;
  230. }
  231. printf_s("The list has %d elements", count);
  232. }
  233. else {
  234. printf_s("The list is empty");
  235. }
  236. }
  237. void DeleteDouble()
  238. {
  239.  
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement