Advertisement
Guest User

Untitled

a guest
May 21st, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.41 KB | None | 0 0
  1. #include "pch.h"
  2. #include <assert.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. struct Node
  8. {
  9. char* data;
  10. Node* next;
  11. int* marks;
  12. int k;
  13. };
  14.  
  15. struct List
  16. {
  17. Node* head;
  18. };
  19.  
  20. FILE *S;
  21.  
  22. //нахождение последнего элемента
  23. Node* getLastNodeIfNotEmpty(List* list)
  24. {
  25. /*assert(list != NULL);
  26. assert(!isEmpty(list));*/
  27. Node* i = list->head;
  28. while (i->next != NULL)
  29. {
  30. i = i->next;
  31. }
  32.  
  33. return i;
  34. }
  35.  
  36.  
  37. // нахождение предпоследнего
  38. Node* getLastButOne(List* list)
  39. {
  40. /*assert(list != NULL);
  41. assert(!isEmpty(list));*/
  42. Node* i = list->head;
  43. while (i->next != getLastNodeIfNotEmpty(list))
  44. {
  45. i = i->next;
  46. }
  47. return i;
  48. }
  49.  
  50. void Marks(FILE* S)
  51. {
  52. char ch[1];
  53. ch[0] = fgetc(S);
  54. printf("\n ch! = %c\n ", ch);
  55. Node* tmp = (Node*)malloc(sizeof(Node*));
  56. int* a = NULL;
  57. int* b;
  58. int i = 0;
  59. int k = 0;
  60. if (!feof(S))
  61. {
  62. do
  63. {
  64. //printf("\n ch = %c\n", ch[0]);
  65. if (ch[0] == '/')
  66. {
  67. continue;
  68. }
  69. b = (int*)realloc(a, (i + 1) * sizeof(int));
  70. a = b;
  71. *a = atoi(ch);
  72. printf("%d", *a);
  73. i++;
  74. k++;
  75. } while (((ch[0] = fgetc(S)) != '#') && (!feof(S)));
  76. tmp->marks = a;
  77. tmp->k = k;
  78. }
  79. }
  80. //чтение файла
  81. void readfile(List* list, FILE *S)
  82. {
  83. fopen_s(&S, "input.txt", "rt");
  84. Node* tmp = (Node*)malloc(sizeof(Node));
  85. char * tmpc;
  86. tmp->data = (char*)malloc(1000);
  87.  
  88. while (!feof(S))
  89. {
  90. //фамилия
  91. tmpc = tmp->data;
  92. int ch;
  93. while(((ch = fgetc(S)) != ' ') && (!feof(S)))
  94. {
  95. *(tmpc) = ch;
  96. tmpc++;
  97. printf("end");
  98. }
  99. *(tmpc) = '\0';
  100. // оценки
  101. Marks(S);
  102. tmp->next = NULL;
  103. if (list->head == NULL)
  104. {
  105. list->head = tmp;
  106. }
  107. else
  108. {
  109. Node* last = getLastNodeIfNotEmpty(list);
  110. last->next = tmp;
  111. }
  112. }
  113. fclose(S);
  114. }
  115.  
  116. //enter после чтения файла
  117. void Enter(List* list)
  118. {
  119. Node* p = getLastNodeIfNotEmpty(list);
  120. char* str = p->data;
  121. int lengStr = strlen(str);
  122. if (str[lengStr] == '\0')
  123. {
  124. str[lengStr] = '\n';
  125. str[lengStr + 1] = '\0';
  126. }
  127. }
  128.  
  129.  
  130. // вставка в конец
  131. void insertBack(List* list, char* value, int* assessment)
  132. {
  133. assert(list != NULL);
  134. Node* tmp = (Node*)malloc(sizeof(Node));
  135. tmp->data = value;
  136. tmp->marks = assessment;
  137. tmp->next = NULL;
  138. if (list->head == NULL)
  139. {
  140. list->head = tmp;
  141.  
  142. }
  143. else
  144. {
  145. Node* last = getLastNodeIfNotEmpty(list);
  146. last->next = tmp;
  147. }
  148. }
  149.  
  150. // пустой список
  151. bool isEmpty(List* list)
  152. {
  153. assert(list != NULL);
  154.  
  155. if (list->head == NULL)
  156. return true;
  157. else
  158. return false;
  159. }
  160.  
  161. //размер списка
  162. int getSize(List* list)
  163. {
  164. assert(list != NULL);
  165. int size = 0;
  166.  
  167. if (list->head != NULL)
  168. {
  169. size++;
  170. Node* p = list->head;
  171. while (p->next != NULL)
  172. {
  173. size++;
  174. p = p->next;
  175. }
  176. }
  177. return size;
  178. }
  179.  
  180. //вставка в любое место
  181. void insertMiddle(List* list, char* value, int number)
  182. {
  183. char *s;
  184. Node* tmp = (Node*)malloc(sizeof(Node));
  185. tmp->data = value;
  186. Node* current = list->head;
  187. for (int i = 0; i < number; i++)
  188. {
  189. current = current->next;
  190. }
  191. tmp->next = current->next->next;
  192. current->next = tmp;
  193. }
  194.  
  195.  
  196. void swap(char* a, char* b)
  197. {
  198. char c;
  199. c = *a;
  200. *a = *b;
  201. *b = c;
  202. }
  203.  
  204. bool sort_two(Node* first_node)
  205. {
  206. assert(first_node != NULL);
  207. assert(first_node->next != NULL);
  208. if (strcmp(first_node->data, first_node->next->data) > 0)
  209. //if (first_node->data > first_node->next->data)
  210. {
  211. swap(first_node->data, first_node->next->data);
  212. return true;
  213. }
  214. return false;
  215. }
  216.  
  217. //нахождение последнего элемента
  218. bool isLast(Node* node)
  219. {
  220. if (node->next == NULL)
  221. return true;
  222. else
  223. return false;
  224. }
  225.  
  226. void sort(List* list)
  227. {
  228. if (isEmpty(list))
  229. return;
  230.  
  231. int size = getSize(list);
  232. if (size == 1)
  233. return;
  234. else if (size > 1)
  235. {
  236. for (int k = 0; k < size; k++)
  237. {
  238. Node* i = list->head;
  239. while (!isLast(i))
  240. {
  241. sort_two(i);
  242. i = i->next;
  243. }
  244. }
  245. }
  246. }
  247.  
  248. void printList(List* list)
  249. {
  250. printf("Zahod");
  251. assert(list != NULL);
  252. Node* i = list->head;
  253. while (i != NULL)
  254. {
  255. printf("%s ", i->data);
  256. for (int m = 0; m < i->k; m++)
  257. {
  258. printf("m");
  259. printf("%d", i->marks);
  260. }
  261. i = i->next;
  262. }
  263. }
  264.  
  265. List makeEmptyList()
  266. {
  267. List empty_list;
  268. empty_list.head = NULL;
  269. return empty_list;
  270. }
  271.  
  272. void writeList(List* list, int number)
  273. {
  274. for (int i = 0; i < number; i++)
  275. {
  276. printf("Vvedite element: ");
  277. Node* UsingMem = (Node*)malloc(sizeof(Node));
  278. char* str = (char*)malloc(1000);
  279. gets_s(str, 1000);
  280. UsingMem->data = str;
  281. insertBack(list, UsingMem->data, UsingMem->marks);
  282. }
  283. }
  284.  
  285. void Delete(List* list, int number)
  286. {
  287. Node* current = list->head;
  288. if (number == 0)
  289. {
  290. list->head = current->next;
  291. free(current);
  292. }
  293. else
  294. {
  295. if (number == getSize(list))
  296. {
  297. Node* p = getLastButOne(list);// нахождение препоследнего элемента
  298. Node* d = p->next; //хранение адреса последнего элемента
  299. p->next = NULL;
  300. free(d);
  301. }
  302. else
  303. {
  304. for (int i = 0; i < number; i++)
  305. {
  306. current = current->next;
  307. }
  308. Node* elm = current->next;
  309. current->next = elm->next;
  310. free(elm);
  311. }
  312. }
  313. }
  314. //Разделение строки
  315. void SplitStr(char* str)
  316. {
  317. char sep[10] = " ";
  318. char* istr;
  319. printf("Исходная строка: ", str);
  320. //printf("Разделенная строка: ");
  321. char* next_token1 = NULL;
  322. istr = strtok_s(str, sep, &next_token1);
  323. /*while (istr != NULL)
  324. {
  325. // Вывод очередной выделенной части
  326. printf("%s\n", i str);
  327. // Выделение очередной части строки
  328. istr = strtok_s(NULL, sep, &next_token1);
  329. }*/
  330. }
  331.  
  332. int main()
  333. {
  334. List l = makeEmptyList();
  335. //isEmpty(&l) == true;
  336. //Marks(&l);
  337. readfile(&l, S);
  338. //Enter(&l);
  339. printList(&l);
  340. printf("Vvedite element: ");
  341. //gets_s(value, 1000);
  342.  
  343. char* str = (char*)malloc(1000);
  344. char* value = (char*)malloc(1000);
  345. //char value[];
  346. printf("Vvedite element: ");
  347. gets_s(str, 1000);
  348. gets_s(value, 1000);
  349. int value1 = atoi(value);
  350. insertBack(&l, str, &value1);
  351. printList(&l);
  352. int n;
  353. scanf_s("%d", &n);
  354. Delete(&l, n);
  355. printList(&l);
  356. int number;
  357. scanf_s("%d", &number);
  358. printf("Before sort:\n ");
  359. printList(&l);
  360. sort(&l);
  361. printf("After sort:\n ");
  362. printList(&l);
  363. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement