Advertisement
Guest User

Organizacaij v4z1

a guest
Dec 12th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define FAKTOR_BLOKIRANJA 3
  5.  
  6. typedef struct Zatvorenik {
  7. int evidencioni_broj;
  8. char sifra_zatvorenika[8];
  9. char datum[11];
  10. char celija[6];
  11. short int kazna;
  12. int del;
  13. } Zatvorenik;
  14.  
  15. typedef struct Blok {
  16. Zatvorenik niz[FAKTOR_BLOKIRANJA];
  17. int popunjenost;
  18. } Blok;
  19.  
  20. void displayz(Zatvorenik z){
  21. printf("%d %s \n", z.evidencioni_broj, z.sifra_zatvorenika);
  22. printf("------\n");
  23. }
  24.  
  25. void displayb(Blok* b) {
  26. int i = 0;
  27. for (;i<3;i++){
  28. displayz(b->niz[i]);
  29. }
  30. }
  31.  
  32. FILE* formiranje(char* dat) {
  33. FILE *f = fopen(dat, "wb+");
  34. if (f == NULL){
  35. printf("Neuspesno citanje datoteke %s", dat);
  36. exit(1);
  37. }
  38. return f;
  39. }
  40.  
  41. FILE* dopisi(FILE *f, Blok* blok){
  42.  
  43. fwrite(blok, sizeof(Blok), 1, f);
  44.  
  45. //printf("OKE");
  46. return f;
  47. }
  48.  
  49. int load_blok(FILE *f, Blok* cur){
  50. int broj;
  51. broj = fread(cur, sizeof(Blok), 1, f);
  52. fseek(f, sizeof(cur), ftell(f));
  53. //printf("--%d--", broj);
  54. return broj;
  55. }
  56.  
  57. int search(FILE *f, int key, Blok* retb){
  58. rewind(f);
  59. Blok bl;
  60. int keyfirst, keylast, i;
  61. Zatvorenik zat;
  62. i = 0;
  63. int ucitano;
  64. do{
  65.  
  66. ucitano = load_blok(f, &bl);
  67.  
  68. keyfirst = bl.niz[0].evidencioni_broj;
  69. keylast = bl.niz[2].evidencioni_broj;
  70. } while (ucitano == 1 && keylast <= key);
  71.  
  72. *retb = bl;
  73. for (i = 0;i<FAKTOR_BLOKIRANJA;i++){
  74. zat = bl.niz[i];
  75. if (zat.evidencioni_broj == key){
  76. return i;
  77. }
  78. }
  79. return -1;
  80. }
  81.  
  82. void dodaj(FILE *f, Zatvorenik z) {
  83.  
  84. }
  85.  
  86.  
  87. int main()
  88. {
  89. char datoteka[20] = "database.bin";
  90. FILE *db = formiranje(datoteka);
  91.  
  92. Blok* current_blok;
  93.  
  94.  
  95. Zatvorenik* z1 = malloc(sizeof(Zatvorenik));
  96. z1->evidencioni_broj = 10;
  97. strcpy(z1->sifra_zatvorenika, "P23JSD0");
  98. strcpy(z1->datum, "11/02/2018");
  99. strcpy(z1->celija, "CEL15");
  100. z1->kazna = 220;
  101. z1->del = 0;
  102.  
  103. Zatvorenik* z2 = malloc(sizeof(Zatvorenik));
  104. z2->evidencioni_broj = 12;
  105. strcpy(z2->sifra_zatvorenika, "DP2DDDD");
  106. strcpy(z2->datum, "14/6/2018");
  107. strcpy(z2->celija, "CEL16");
  108. z2->kazna = 190;
  109. z2->del = 0;
  110.  
  111. Zatvorenik* z3 = malloc(sizeof(Zatvorenik));
  112. z3->evidencioni_broj = 15;
  113. strcpy(z3->sifra_zatvorenika, "1231237");
  114. strcpy(z3->datum, "14/6/2018");
  115. strcpy(z3->celija, "CEL16");
  116. z3->kazna = 190;
  117. z3->del = 0;
  118.  
  119. Blok* bl = malloc(sizeof(Blok));
  120. bl->niz[0] = *z1;
  121. bl->niz[1] = *z2;
  122. bl->niz[2] = *z3;
  123.  
  124.  
  125. Zatvorenik* z4 = malloc(sizeof(Zatvorenik));
  126. z4->evidencioni_broj = 20;
  127. strcpy(z4->sifra_zatvorenika, "1111110");
  128. strcpy(z4->datum, "14/6/2019");
  129. strcpy(z4->celija, "AAASS");
  130. z4->kazna = 190;
  131. z4->del = 0;
  132.  
  133. Blok* bl2 = malloc(sizeof(Blok));
  134. bl2->niz[0] = *z4;
  135. bl2->niz[1] = *z4;
  136. bl2->niz[2] = *z4;
  137.  
  138. dopisi(db, bl);
  139. dopisi(db, bl2);
  140.  
  141.  
  142.  
  143. rewind(db);
  144. load_blok(db, bl2);
  145. displayb(bl2);
  146.  
  147. load_blok(db, bl2);
  148. displayb(bl2);
  149.  
  150.  
  151.  
  152. //strcpy(*z1.)
  153. Blok retbl;
  154.  
  155. int ser = search(db,20,&retbl);
  156. printf("%d world!\n", ser);
  157. return 0;
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement