Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.38 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include<ctype.h>
  5.  
  6. typedef struct Osoba
  7. {
  8. int ID;
  9. char imie[100];
  10. char nazwisko[100];
  11. int wiek;
  12. char plec[50];
  13. struct Osoba* next;
  14. } Osoba ;
  15.  
  16. // zlicza dlugosc listy
  17. int dlugosc_listy(Osoba* first)
  18. {
  19. int d = 0;
  20. Osoba* current = first;
  21. while(current != NULL)
  22. {
  23. d++;
  24. current = current->next;
  25. }
  26. return d;
  27. }
  28.  
  29. // dodaje osobe do listy
  30. void dodajOsobe(Osoba** first)
  31. { char line[500];
  32. Osoba* nowa = (Osoba*)malloc(sizeof(Osoba));
  33. printf("Podaj imie: ");
  34. scanf("%s", nowa->imie);
  35.  
  36. printf("Podaj nazwisko: ");
  37. scanf("%s", nowa->nazwisko);
  38.  
  39. printf("Podaj wiek: " );
  40. scanf("%d", &(nowa->wiek));
  41.  
  42. printf("Podaj plec: " );
  43. scanf("%s", nowa->plec);
  44. nowa->next=(*first);
  45. *first= nowa;
  46. }
  47.  
  48. //Wypisuje cala liste
  49. void wypisz_liste(Osoba* first)
  50. {
  51.  
  52. Osoba* current = first;
  53. if(first == NULL)
  54. printf("first JEST PUSTA");
  55. else
  56. printf("first zawiera %d elementow: \n", dlugosc_listy(first) );
  57.  
  58. int i=1;
  59. while( current != NULL)
  60. {
  61. printf("%d %s %s %d %s\n", i, current->imie, current->nazwisko, current->wiek, current->plec);
  62. current=current->next;
  63. i++;
  64. }
  65. }
  66.  
  67. //Usuwa element z listy
  68. void usun(Osoba** first, int ID)
  69. {
  70. Osoba* poprzedni = NULL;
  71. Osoba* current = *first;
  72. int i;
  73. for(i = 1; i < ID; i++)
  74. {
  75. poprzedni=current;
  76. current=current->next;
  77.  
  78. }
  79. if(poprzedni==NULL)
  80. {
  81. (*first)=(*first)->next;
  82. free(current);
  83. }
  84. else
  85. {
  86. poprzedni->next=current->next;
  87. free(current);
  88. }
  89. }
  90.  
  91. // Usuwa osobe po indexie
  92. void usun_osobe(Osoba** first)
  93. {
  94. int ID;
  95. printf("Podaj indeks osoby: " );
  96. scanf("%d", &ID);
  97.  
  98. if((ID > dlugosc_listy(*first)) || (ID < 1))
  99. {
  100. printf("Nie ma takiego indeksu");
  101.  
  102. }
  103. else
  104. {
  105. usun(first,ID);
  106. }
  107. }
  108. int usun_imnaz_first(char* imie, char* nazwisko, Osoba** first)
  109. {
  110. Osoba* poprzedni=NULL;
  111. Osoba* current= *first;
  112.  
  113. while((current != NULL) && ( ( strcmp(current->imie, imie)!=0) || (strcmp(current->nazwisko, nazwisko )!=0) ))
  114. {
  115. poprzedni=current;
  116. current=current->next;
  117. }
  118. if(current == NULL )
  119. {
  120. return 0;
  121. }
  122. else
  123. {
  124. if(poprzedni==NULL)
  125. {
  126. (*first)=(*first)->next;
  127. free(current);
  128. }
  129. else
  130. {
  131. poprzedni->next=current->next;
  132. free(current);
  133. }
  134. return 1;
  135. }
  136. }
  137.  
  138. //usuwa po imieniu i nazwisku
  139. /*void usun_imnaz(Osoba** first)
  140. {
  141. char imie[100];
  142. char nazwisko[100];
  143.  
  144. printf("Podaj imie: ");
  145. scanf("%s", imie);
  146.  
  147. printf("Podaj nazwisko: ");
  148. scanf("%s", nazwisko);
  149.  
  150. if(usun_imnaz_first(imie, nazwisko, first))
  151.  
  152. printf("Osoba zostala usunieta");
  153. else
  154. printf("Osoba nie zostala usunieta, nie ma takiej osoby");
  155.  
  156. }*/
  157.  
  158.  
  159. //wypisz osobe o podanym indexie
  160. void wypisz_osobe(Osoba* first)
  161. {
  162. int ID;
  163. printf("Podaj numer osoby: " );
  164. scanf("%d", &ID);
  165.  
  166. if((ID > dlugosc_listy(first)) || (ID < 1))
  167. {
  168. printf("Nie ma takiego indeksu");
  169. }
  170. else
  171. {
  172. int i;
  173. Osoba* current = first;
  174. for(i = 1; i < ID; i++)
  175. {
  176. current=current->next;
  177. }
  178. printf("%d %s %s %d %s\n", i, current->imie, current->nazwisko, current->wiek, current->plec);
  179. }
  180. }
  181.  
  182. int main()
  183. {
  184. Osoba* first = NULL;
  185. char imie[100],nazwisko[100];
  186.  
  187.  
  188.  
  189.  
  190. printf("\n\n MENU");
  191. printf("\n\n\n 0 Wyjscie z programu");
  192. printf("\n 1 Wpisz dane osoby do bazy");
  193. printf("\n 2 Usun osobe o podanym indeksie");
  194. printf("\n 3 Usun osobe o podanych imieniu i nazwisku");
  195. printf("\n 4 Wyswietl osobe o podanym indeksie");
  196. printf("\n 5 Wyswietl cala liste\n");
  197.  
  198. for(;;)
  199. {
  200. char option;
  201. scanf("%s", &option);
  202. while(getchar() != '\n');
  203.  
  204. switch(option)
  205. {
  206. case '0' :
  207.  
  208. return 0;
  209. case '1' :
  210.  
  211. printf("DODAWANIE OSOBY\n");
  212. dodajOsobe(&first);
  213. break;
  214.  
  215. case '2':
  216.  
  217. printf("USUWANIE OSOBY O PODANYM INDEKSIE\n");
  218. usun_osobe(&first);
  219. break;
  220.  
  221. case '3':
  222.  
  223. printf("USUWANIE OSOBY NA BAZIE IMIENIA I NAZWISKA");
  224. usun_imnaz_first(imie, nazwisko, &first);
  225. break;
  226.  
  227. case '4':
  228.  
  229. printf("WYSWIETL OSObe O PODANYM INDEKSIE\n");
  230. wypisz_osobe(first);
  231. break;
  232.  
  233. case '5':
  234.  
  235. printf("WYSWIETL CALA BAZE\n");
  236. wypisz_liste(first);
  237. break;
  238. default:printf("niepoprawna operacja\n");
  239. }
  240. }
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement