Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.91 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. #define MAX 150
  6.  
  7. struct Talbum {
  8. char gatunek[MAX];
  9. char wytwornia[MAX];
  10. char wykonawca[MAX];
  11. float dlugosc; //dlugosc albumu
  12. char nazwa_albumu[MAX];//nazwa albumu
  13. struct Talbum *nast;
  14. struct Talbum *poprz;
  15. };
  16.  
  17. struct Tlista {
  18. struct Talbum *pocz; //adres pierwszego elementu listy
  19. struct Talbum *kon; //adres ostatniego elementu listy
  20. };
  21.  
  22. struct Talbum * nowy_el(char *gat, char *wytw, char *wyko, float dlug, char *naza)
  23. {
  24. struct Talbum *ptr = NULL;
  25. // printf("Adding line: %s %s %s %f %s\n", gat, wytw, wyko, dlug, naza);
  26.  
  27. if (!(ptr = (struct Talbum*)malloc(sizeof(struct Talbum))))
  28. {
  29. printf("\nMemory allocation failure while adding a new element.\n");
  30. return NULL;
  31. }
  32. strcpy(ptr->gatunek, gat);
  33. strcpy(ptr->wykonawca, wyko);
  34. strcpy(ptr->wytwornia, wytw);
  35. strcpy(ptr->nazwa_albumu, naza);
  36. ptr->dlugosc = dlug;
  37.  
  38. return ptr;
  39. }
  40.  
  41.  
  42.  
  43. void dodaj_za_ostatnim(struct Tlista *lista, struct Talbum *el)
  44. {
  45.  
  46. struct Talbum *wsk = lista->pocz;
  47.  
  48. if (!wsk)
  49. {
  50. lista->pocz = lista->kon = el;
  51. lista->pocz->poprz = NULL;
  52. lista->kon->nast = NULL;
  53. }
  54. else
  55. {
  56. lista->kon->nast = el;
  57. el->poprz = lista->kon;
  58. lista->kon = el;
  59. }
  60.  
  61. strcpy(lista->kon->gatunek, el->gatunek);
  62. strcpy(lista->kon->wytwornia, el->wytwornia);
  63. strcpy(lista->kon->wykonawca, el->wykonawca);
  64. lista->kon->dlugosc = el->dlugosc;
  65. strcpy(lista->kon->nazwa_albumu, el->nazwa_albumu);
  66. lista->kon->nast = NULL;
  67. }
  68.  
  69. void wypisz(struct Tlista lista)
  70. {
  71. struct Talbum *temp = lista.pocz;
  72.  
  73. printf("\nList:\n\n");
  74. while (temp)
  75. {
  76. printf("%s %s %s %f %s\n", temp->gatunek, temp->wytwornia, temp->wykonawca, temp->dlugosc, temp->nazwa_albumu);
  77. temp = temp->nast;
  78. }
  79. }
  80.  
  81. void usun(struct Tlista lista, char *usuwana_nazwa)
  82. {
  83. struct Talbum *temp = lista.pocz;
  84. struct Talbum *poprz;
  85. if(strcmp(temp->nazwa_albumu,usuwana_nazwa)==0){
  86. lista.pocz=lista.pocz->nast;
  87.  
  88. free(temp);
  89. }
  90. else{
  91.  
  92. while(temp, strcmp(usuwana_nazwa,temp->nazwa_albumu)!=0)
  93. {
  94. poprz=temp;
  95. temp=temp->nast;
  96. }
  97. if(strcmp(usuwana_nazwa,temp->nazwa_albumu)==0)
  98. {
  99. poprz->nast=temp->nast;
  100. free(temp);
  101. }
  102. }
  103.  
  104. }
  105. //void wypisz(struct Tlista lista)
  106. //{
  107. // struct Talbum *temp = lista.pocz;
  108. // printf("\nList:\n\n");
  109. //
  110. // if (lista.pocz == 0)
  111. // {
  112. // printf("LISTA JEST PUSTA.");
  113. // }
  114. // else while (temp)
  115. // {
  116. // printf("%s %s %s %f %s \n ", &temp->gatunek, temp->wytwornia, temp->wykonawca, temp->dlugosc, temp->nazwa_albumu);
  117. // temp = temp->nast;
  118. // }
  119. //}
  120.  
  121. struct Tlista wczytaj_z_pliku(char *nazwa)
  122. {
  123. FILE *input;
  124. if (!(input = fopen(nazwa, "r")))
  125. {
  126. printf("\nCouldn't open input file %s. End of program.\n\n", nazwa);
  127. exit(0);
  128. }
  129. else
  130. {
  131. fseek(input, 0, SEEK_END);
  132. long pom1 = ftell(input);
  133. fseek(input, 0, SEEK_SET);
  134. long pom2 = ftell(input);
  135. if (pom1 == pom2)
  136. {
  137. printf("\nInput file %s is empty.\n\n", nazwa);
  138. if (fclose(input))
  139. {
  140. printf("Couldn't close input file %s. End of program.\n\n", nazwa);
  141. exit(0);
  142. }
  143. printf("End of program.\n\n");
  144. exit(0);
  145. }
  146. }
  147.  
  148. struct Tlista lista;
  149. lista.pocz = NULL;
  150. lista.kon = NULL;
  151. struct Talbum bufor, *pom = NULL;
  152.  
  153.  
  154. while (fscanf(input, "%s %s %s %f %s", bufor.gatunek, bufor.wytwornia, bufor.wykonawca, &bufor.dlugosc, bufor.nazwa_albumu)==5)
  155. {
  156.  
  157. if (!(pom = nowy_el(bufor.gatunek, bufor.wytwornia, bufor.wykonawca, bufor.dlugosc, bufor.nazwa_albumu)))
  158. {
  159. printf("Error while adding new element to the list.");
  160. if (fclose(input))
  161. {
  162. printf("Couldn't close input file %s. End of program.\n\n", nazwa);
  163. exit(0);
  164. }
  165. printf("End of program.\n\n");
  166. exit(0);
  167. }
  168. dodaj_za_ostatnim(&lista, pom);
  169.  
  170. }
  171.  
  172.  
  173.  
  174. return lista;
  175. }
  176.  
  177.  
  178. int main()
  179. {
  180. printf("***********WITAM W PROGRAMIE DOMINIKI JAROSZ************\n ******WCZYTYWANIE PLIKU.******* \n");
  181. printf("JEZELI CHCESZ DODAC WLASNE DANE WCISNIJ 1 \n");
  182. int czy=0;
  183. struct Tlista test;
  184. test.pocz = NULL;
  185. test.kon = NULL;
  186.  
  187. char nazwa[20];
  188. char wytwornia_nazwa[20];
  189. char wykonawca[20];
  190. char gatunek[20];
  191. float dl;
  192. char filename[]="input.txt";
  193. test = wczytaj_z_pliku(&filename);
  194. scanf("%d",&czy);
  195. while (getchar()!='\n')
  196. ;
  197. while(czy==1)
  198. {
  199.  
  200. printf("Podaj gatunek:\n");
  201. scanf("%s", gatunek);
  202. while (getchar()!='\n')
  203. ;
  204. printf("\nPodaj nazwe wytworni:\n");
  205. scanf("%s", wytwornia_nazwa);
  206. while (getchar()!='\n')
  207. ;
  208. printf("\nPodaj wykonawce:\n");
  209. scanf("%s", wykonawca);
  210. while (getchar()!='\n')
  211. ;
  212. printf("\nPodaj dlugosc:\n");
  213. scanf("%f", &dl);
  214. while (getchar()!='\n')
  215. ;
  216. printf("\nPodaj nazwe:\n");
  217. scanf("%s", nazwa);
  218. while (getchar()!='\n')
  219. ;
  220. struct Talbum *temp = nowy_el(&gatunek, &wytwornia_nazwa, &wykonawca, dl, &nazwa);
  221. dodaj_za_ostatnim(&test, temp);
  222. printf("JEZELI CHCESZ DODAC KOLEJNY ELEMENT DO LISTY KLIKNIJ 1, JEZELI NIE - KLIKNIJ 0");
  223. scanf("%d",&czy);
  224. while (getchar()!='\n')
  225. ;
  226.  
  227. }
  228.  
  229. // test = wczytaj_z_pliku(&filename);
  230. //dodaj_za_ostatnim(&test, temp);
  231.  
  232. wypisz(test);
  233. usun(test, "nazwa2");
  234. printf("\n\n Po usunieciu: \n\n");
  235. wypisz(test);
  236. printf("\n\nEnd of program.\n\n");
  237. return 0;
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement