lista_polje.h - Evidencija životinja


/ Published in: C++
Save to your folder(s)

Header lista_polje.h za main program Evidencija životinja


Copy this code and paste it in your HTML
  1. #include<iostream>
  2. using namespace std;
  3. typedef int element;
  4. struct zivotinja{
  5. int sifra;
  6. char vrsta[35];
  7. char naziv[35];
  8. float cijena;
  9. int datum[3];
  10. };
  11. struct Lista_zapis{
  12. zivotinja vrijednost[1000];
  13. int kursor;
  14. };
  15. typedef struct Lista_zapis lista;
  16. element EndL(lista *L){
  17. return (L->kursor);
  18. }
  19. element FirstL(lista *L){
  20. if(L->kursor==0)
  21. return EndL(L);
  22. else
  23. return 0;
  24. }
  25. zivotinja RetreiveL(element p, lista *L){
  26. return L->vrijednost[p];
  27. }
  28. element InsertL(zivotinja x,element p,lista *L){
  29. if(p>L->kursor || p<0) return 0;
  30. else{
  31. for( int q=L->kursor;q>=p;q--)
  32. L->vrijednost[q+1]=L->vrijednost[q];
  33. L->kursor++;
  34. L->vrijednost[p]=x;
  35. return 1; }
  36. }
  37. lista *InitL(lista *L){
  38. L=new lista;
  39. L->kursor=0;
  40. return L;
  41. }
  42. element NextL(element p, lista *L){
  43. if(p==EndL(L)-1) return EndL(L);
  44. if(p==EndL(L)) return 0;
  45. else
  46. return p+1;
  47. }
  48. element PreviousL(element p, lista *L){
  49. if(p==FirstL(L)) return -1;
  50. if(p==EndL(L)) return p-1;
  51. return p-1;
  52. }
  53. element DeleteL(element p, lista *L){
  54. if((L->kursor==0))
  55. return 0;
  56. if((p>=L->kursor)||(p<0))
  57. return 0;
  58. if(p==EndL(L))
  59. return 0;
  60. if(p==FirstL(L)){
  61. L->kursor--;
  62. for( int i=p; i<L->kursor; i++)
  63. L->vrijednost[i]=L->vrijednost[i+1];
  64. return 1;
  65. }
  66. else{
  67. L->kursor--;
  68. for( int i=p; i<L->kursor; i++)
  69. L->vrijednost[i]=L->vrijednost[i+1];
  70. }
  71. return 1;
  72. }
  73. element DeleteAll(lista *L){
  74. return L->kursor==0;
  75. }
  76. element LocateL(zivotinja x, lista *L){
  77. element pozicija=EndL(L);
  78. pozicija=PreviousL(pozicija,L);
  79. if(strlen(x.naziv)>0){
  80. if(FirstL(L)!=EndL(L)){
  81. while(5){
  82. zivotinja sad=RetreiveL(pozicija,L);
  83. if(strcmp(x.naziv,sad.naziv)==0)
  84. return pozicija;
  85. if(pozicija==FirstL(L))break;
  86. pozicija=PreviousL(pozicija,L);
  87. }
  88. }}
  89. if(strlen(x.vrsta)>0){
  90. if(FirstL(L)!=EndL(L)){
  91. while(5){
  92. zivotinja sad=RetreiveL(pozicija,L);
  93. if(strcmp(x.vrsta,sad.vrsta)==0)
  94. return pozicija;
  95. if(pozicija==FirstL(L))break;
  96. pozicija=PreviousL(pozicija,L);
  97. }
  98. }}
  99. return EndL(L);
  100. }
  101. void Merge(lista *L,int i,int k,int j){
  102. int beg=i, sec=k+1, br=0, vel=j-i+1;
  103. zivotinja *pomocno = new zivotinja [j-i+1];
  104. while (beg<=k && sec<=j){
  105. if(L->vrijednost[beg].cijena>L->vrijednost[sec].cijena)
  106. pomocno[br++]=L->vrijednost[beg++];
  107. else if(L->vrijednost[beg].cijena<L->vrijednost[sec].cijena)
  108. pomocno[br++]=L->vrijednost[sec++];
  109. if(L->vrijednost[beg].cijena==L->vrijednost[sec].cijena){
  110. if(strcmp(L->vrijednost[beg].naziv,L->vrijednost[sec].naziv)==1)
  111. pomocno[br++]=L->vrijednost[beg++];
  112. else
  113. pomocno[br++]=L->vrijednost[sec++];
  114. }
  115. }
  116.  
  117. while(beg<=k)
  118. pomocno[br++]=L->vrijednost[beg++];
  119. while(sec<=j)
  120. pomocno[br++]=L->vrijednost[sec++];
  121. for(int I=0;I<vel;I++)
  122. L->vrijednost[i+I]=pomocno[I];
  123. delete []pomocno;
  124. }
  125.  
  126.  
  127. void Sort(lista *L, int i,int j){
  128. int middle;
  129. if(i<j){
  130. middle=(i+j)/2;
  131. Sort(L,i,middle);
  132. Sort(L,middle+1,j);
  133. Merge(L,i,middle,j);
  134. }
  135.  
  136. }
  137. void MSort(lista *L,int i,int j){
  138. if(FirstL(L)==EndL(L)){
  139. cout<<"Lista je prazna "<<endl;
  140. return;
  141. }
  142. Sort(L,0,EndL(L)-1);
  143. zivotinja sad;
  144. for(int i=0;i<=EndL(L)-1;i++){
  145. sad=L->vrijednost[i];
  146. cout<<"Sifra "<<sad.sifra<<endl;
  147. cout<<"Naziv "<<sad.naziv<<endl;
  148. cout<<"Datum "<<sad.datum[0]<<"."<<sad.datum[1]<<"."<<sad.datum[2]<<endl;
  149. cout<<"Vrsta "<<sad.vrsta<<endl;
  150. cout<<"Cijena "<<sad.cijena<<endl;
  151. cout<<endl; }
  152. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.