Coriic

Lista 1 kierunkowa

Jan 5th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5.  
  6. struct ksiazki{
  7. char *imie;
  8. char *nazwisko;
  9. int numer;
  10. double cena;
  11. char *tytul;
  12. struct ksiazki* next;
  13. };
  14.  
  15. struct ksiazki* dodaj_poczatek (struct ksiazki *poczatek1, int i){
  16. char bufor[30];
  17. int l;
  18. struct ksiazki *tmp;
  19. tmp=(struct ksiazki*)malloc(sizeof(struct ksiazki));
  20. printf("Ksiazka numer: %d\n", i+1);
  21. getchar();
  22. printf("Podaj imie: ");
  23. fgets(bufor, 30, stdin);
  24. l=strlen(bufor)+1;
  25. tmp->imie=(char*)malloc(l*sizeof(char));
  26. strcpy(tmp->imie, bufor);
  27. printf("Podaj nazwisko: ");
  28. fgets(bufor, 30, stdin);
  29. l=strlen(bufor)+1;
  30. tmp->nazwisko=(char*)malloc(l*sizeof(char));
  31. strcpy(tmp->nazwisko, bufor);
  32. printf("Podaj tytul: ");
  33. fgets(bufor, 30, stdin);
  34. l=strlen(bufor)+1;
  35. tmp->tytul=(char*)malloc(l*sizeof(char));
  36. strcpy(tmp->tytul, bufor);
  37. printf("Podaj cene: ");
  38. scanf("%lf", &(tmp->cena));
  39. printf("Podaj numer: ");
  40. scanf("%d", &(tmp->numer));
  41. tmp->next=poczatek1;
  42. poczatek1=tmp;
  43. return poczatek1;
  44. }
  45.  
  46. void wyswietl (struct ksiazki * poczatek){
  47. struct ksiazki *tmp;
  48. tmp=poczatek;
  49. while (tmp != NULL){
  50. printf("Imie: %s", tmp->imie);
  51. printf("Nazwisko: %s", tmp->nazwisko);
  52. printf("Tytul: %s", tmp->tytul);
  53. printf("Cena: %.2f\n", tmp->cena);
  54. printf("Numer: %d\n", tmp->numer);
  55. tmp=tmp->next;
  56. }
  57. }
  58.  
  59.  
  60. int main(void){
  61. struct ksiazki* poczatek = NULL;
  62. int n, i;
  63. printf("Ile ksiazek chcesz dodac: ");
  64. scanf("%d", &n);
  65. for (i=0; i<=n-1; ++i){
  66. poczatek=dodaj_poczatek(poczatek, i);
  67. }
  68. wyswietl(poczatek);
  69. return 0;
  70. }
Add Comment
Please, Sign In to add comment