kacxsku

Untitled

May 17th, 2020
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #pragma warning(disable:4996)
  5. #define MAX_BUFOR 80
  6.  
  7. typedef struct stud {
  8. char *imie;
  9. char *nazwisko;
  10. int rok;
  11. char *adres;
  12. double stypendium;
  13. struct stud *n;
  14. struct stud *prev;
  15. }STUDENT;
  16.  
  17. STUDENT *H;
  18. STUDENT *T;
  19.  
  20. FILE *fd = NULL;
  21.  
  22. void list1(FILE *plik)
  23. {
  24. unsigned len;
  25. STUDENT *B = NULL, *P = NULL;
  26. char bufor[MAX_BUFOR];
  27. H = (STUDENT *)NULL;
  28. T = (STUDENT *)NULL;
  29. while (fgets(bufor, MAX_BUFOR, fd)) {
  30. B = (STUDENT *)malloc(sizeof(STUDENT));
  31. if (H == (STUDENT *)NULL)
  32. {
  33. H = B;
  34. T = B;
  35. }
  36. else
  37. {
  38. P->n = B;
  39. T = P->n;
  40. }
  41. B->n = (STUDENT *)NULL;
  42. B->prev = (STUDENT *)NULL;
  43.  
  44. len = (unsigned)strlen(bufor);
  45. bufor[len - 1] = '\0';
  46. B->imie = (char*)malloc(len);
  47. strcpy(B->imie, bufor);
  48.  
  49. fgets(bufor, MAX_BUFOR, fd);
  50. len = (unsigned)strlen(bufor);
  51. bufor[len - 1] = '\0';
  52. B->nazwisko = (char*)malloc(len);
  53. strcpy(B->nazwisko, bufor);
  54.  
  55. fgets(bufor, MAX_BUFOR, fd);
  56. B->rok = atoi(bufor);
  57.  
  58. fgets(bufor, MAX_BUFOR, fd);
  59. len = (unsigned)strlen(bufor);
  60. bufor[len - 1] = '\0';
  61. B->adres = (char*)malloc(len);
  62. strcpy(B->adres, bufor);
  63.  
  64. fgets(bufor, MAX_BUFOR, fd);
  65. B->stypendium = atof(bufor);
  66. B->prev = P;
  67.  
  68. P = B;
  69. }
  70. }
  71.  
  72. STUDENT *max_s()
  73. {
  74. STUDENT *w, *x;
  75. double max;
  76. max = H->stypendium;
  77. x = H;
  78. for (w = H; w != (STUDENT *)NULL; w = w->n)
  79. {
  80. if (w->stypendium > max)
  81. {
  82. max = w->stypendium;
  83. x = w;
  84. }
  85. }
  86. return x;
  87. }
  88.  
  89. void dispList1()
  90. {
  91. STUDENT *w;
  92. w = H;
  93. if (H == NULL)
  94. {
  95. printf("Lista pusta");
  96. return;
  97. }
  98.  
  99. for (w = H; w != (STUDENT *)NULL; w = w->n)
  100. {
  101. printf("%d %d\n", (int)w, (int)w->n);
  102. printf("%12s| %12s| %4d| %25s| %7.2lf\n", w->imie, w->nazwisko, w->rok, w->adres, w->stypendium);
  103. }
  104. }
  105. void revdispList1() // wypisanie listy od konca
  106. {
  107. STUDENT *w;
  108. w = T;
  109. if (H == NULL)
  110. {
  111. printf("Lista pusta");
  112. return;
  113. }
  114.  
  115. for (w = T; w != (STUDENT *)NULL; w = w->prev)
  116. {
  117. printf("%d %d\n", (int)w, (int)w->n);
  118. printf("%12s| %12s| %4d| %25s| %7.2lf\n", w->imie, w->nazwisko, w->rok, w->adres, w->stypendium);
  119. }
  120.  
  121. }
  122.  
  123. void addHeadList1()
  124. {
  125. char bufor[MAX_BUFOR];
  126. unsigned len;
  127. STUDENT *head;
  128. head = (STUDENT *)malloc(sizeof(STUDENT));
  129. printf("podaj imie, nazwisko, rok, adres, stypendium\n");
  130. fgets(bufor, MAX_BUFOR, stdin);
  131. len = (unsigned)strlen(bufor);
  132. bufor[len - 1] = '\0';
  133. head->imie = (char*)malloc(len);
  134. strcpy(head->imie, bufor);
  135.  
  136. fgets(bufor, MAX_BUFOR, stdin);
  137. len = (unsigned)strlen(bufor);
  138. bufor[len - 1] = '\0';
  139. head->nazwisko = (char*)malloc(len);
  140. strcpy(head->nazwisko, bufor);
  141.  
  142. fgets(bufor, MAX_BUFOR, stdin);
  143. head->rok = atoi(bufor);
  144.  
  145. fgets(bufor, MAX_BUFOR, stdin);
  146. len = (unsigned)strlen(bufor);
  147. bufor[len - 1] = '\0';
  148. head->adres = (char*)malloc(len);
  149. strcpy(head->adres, bufor);
  150.  
  151. fgets(bufor, MAX_BUFOR, stdin);
  152. head->stypendium = atof(bufor);
  153.  
  154. head->n = H;
  155. H = head;
  156. }
  157.  
  158. int lenList1()
  159. {
  160. int count = 0;
  161. STUDENT *w;
  162. w = H;
  163. if (H == NULL)
  164. {
  165. printf("Lista pusta");
  166. return count;
  167. }
  168. while (w)
  169. {
  170. count += 1;
  171. w = w->n;
  172. }
  173. return count;
  174. }
  175.  
  176. void remHeadList1()
  177. {
  178. STUDENT *w = NULL;
  179. if (H == NULL)
  180. {
  181. printf("Lista pusta");
  182. return;
  183. }
  184. w = H->n;
  185. free(H);
  186. H = w;
  187. }
  188.  
  189. void addTailList1()
  190. {
  191. char bufor[MAX_BUFOR];
  192. unsigned len;
  193. STUDENT *tail, *w;
  194.  
  195. if (H == NULL)
  196. {
  197. printf("Lista pusta");
  198. return;
  199. }
  200. w = T;
  201. tail = (STUDENT *)malloc(sizeof(STUDENT));
  202. w->n = tail;
  203.  
  204. printf("podaj imie, nazwisko, rok, adres, stypendium\n");
  205. fgets(bufor, MAX_BUFOR, stdin);
  206. len = (unsigned)strlen(bufor);
  207. bufor[len - 1] = '\0';
  208. tail->imie = (char*)malloc(len);
  209. strcpy(tail->imie, bufor);
  210.  
  211. fgets(bufor, MAX_BUFOR, stdin);
  212. len = (unsigned)strlen(bufor);
  213. bufor[len - 1] = '\0';
  214. tail->nazwisko = (char*)malloc(len);
  215. strcpy(tail->nazwisko, bufor);
  216.  
  217. fgets(bufor, MAX_BUFOR, stdin);
  218. tail->rok = atoi(bufor);
  219.  
  220. fgets(bufor, MAX_BUFOR, stdin);
  221. len = (unsigned)strlen(bufor);
  222. bufor[len - 1] = '\0';
  223. tail->adres = (char*)malloc(len);
  224. strcpy(tail->adres, bufor);
  225.  
  226. fgets(bufor, MAX_BUFOR, stdin);
  227. tail->stypendium = atof(bufor);
  228.  
  229. tail->n = NULL;
  230. tail->prev = T;
  231. T = tail;
  232. }
  233. void remTailList1()
  234. {
  235. STUDENT *w;
  236.  
  237. if (H == NULL)
  238. {
  239. printf("Lista pusta");
  240. return;
  241. }
  242.  
  243. w = T->prev;
  244.  
  245.  
  246. free(w->n);
  247. w->n = NULL;
  248. }
  249.  
  250.  
  251.  
  252. int main()
  253. {
  254. STUDENT *ms;
  255.  
  256. fd = fopen("dane.txt", "r");
  257. if (fd == NULL) {
  258. printf("Nie mogę otworzyć pliku dane.txt do odczytu!\n");
  259. getchar();
  260. exit(1);
  261. }
  262.  
  263. list1(fd);
  264. //revdispList1();
  265. //ms = max_s();
  266. //printf("%s %.2lf\n", ms->nazwisko, ms->stypendium);
  267. //addHeadList1();
  268. addTailList1();
  269. //dispList1();
  270. //printf("dlugosc listy = %d\n", lenList1());
  271. //remHeadList1();
  272. revdispList1();
  273. }
Advertisement
Add Comment
Please, Sign In to add comment