Posted By


nzvorc on 11/12/12

Tagged


Statistics


Viewed 546 times
Favorited by 0 user(s)

Zadatak1_lista_polje.h


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

Ovo je datoteka zaglavlja lista_polje.h u kojoj se nalazi implementacija liste pomoću polja. Riješenje je realizirano pomoću polja.


Copy this code and paste it in your HTML
  1. #include<iostream>
  2. #include<cstring>
  3. using namespace std;
  4.  
  5. struct lista{
  6. int sifra;
  7. char vrsta[20], naziv[30];
  8. int dan, mj, god;
  9. float cijena;
  10. };
  11.  
  12. lista L[100];
  13. int sif=1;
  14.  
  15. //1. Dodavanje zapisa u listu
  16. int InsertL(int c, lista L[]){
  17. L[c].sifra=sif++;
  18. if(c!=0) cin.ignore();
  19. cout<<"Unesi vrstu zivotinje: ";
  20. cin.getline(L[c].vrsta, 20);
  21. cout<<"Unesi naziv zivotinje: ";
  22. cin.getline(L[c].naziv, 30);
  23. cout<<"Unesi cijenu zivotinje: ";
  24. cin>>L[c].cijena;
  25. cout<<"Unesi datum dostave:"<<endl;
  26. cout<<"Dan: "; cin>>L[c].dan;
  27. cout<<"Mjesec: "; cin>>L[c].mj;
  28. cout<<"Godina: "; cin>>L[c].god;
  29. if(L[c].dan>31 || L[c].dan<1 || L[c].mj<1 || L[c].mj>12 || L[c].god<1993) return 0;
  30. return 1;
  31. }
  32.  
  33. //2. Ispis zapisa liste
  34. void IspisL(int c, lista L[]){
  35. for(int x=c-1; x>=0; x--){
  36. cout<<"Sifra: ";
  37. if(L[x].sifra<10) cout<<"00";
  38. else if(L[x].sifra<100)cout<<"0";
  39. cout<<L[x].sifra<<endl;
  40. cout<<"Vrsta: "<<L[x].vrsta<<endl;
  41. cout<<"Naziv: "<<L[x].naziv<<endl;
  42. cout<<"Cijena: "<<L[x].cijena<<endl;
  43. cout<<"Datum: "<<L[x].dan<<"."<<L[x].mj<<"."<<L[x].god<<endl;
  44. cout<<"---------------------" << endl;
  45. }
  46. }
  47.  
  48. //3. Pretrazivaje liste
  49. int LocateL(int c, lista L[]){
  50. int bz=0;
  51. for(int x=0; x<c; x++){
  52. if(L[x].god<2012) continue;
  53. if(L[x].god==2012 && L[x].mj<9) continue;
  54. if(L[x].mj==9 && L[x].god==2012 && L[x].dan<=23) continue;
  55. cout<<"Sifra: ";
  56. if(L[x].sifra<10) cout<<"00";
  57. else if(L[x].sifra<100)cout<<"0";
  58. cout<<L[x].sifra<<endl;
  59. cout<<"Vrsta: "<<L[x].vrsta<<endl;
  60. cout<<"Naziv: "<<L[x].naziv<<endl;
  61. cout<<"Cijena: "<<L[x].cijena<<endl;
  62. cout<<"Datum dostave: "<<L[x].dan<<"."<<L[x].mj<<"."<<L[x].god<<endl;
  63. cout<<"-----------------------"<<endl;
  64. bz++;
  65. }
  66. return bz;
  67. }
  68.  
  69. //4. Brisanje prema nazivu
  70. int DeleteL(int c, char naziv[], lista L[]){
  71. int x;
  72. for(x=0; x<c; x++){
  73. if(!strcmp(naziv, L[x].naziv)) break;
  74. }
  75. if(x==c) return 0;
  76. for(int i=x; i<c; i++){
  77. L[i].sifra=L[i+1].sifra;
  78. strcpy(L[i].vrsta,L[i+1].vrsta);
  79. strcpy(L[i].naziv,L[i+1].naziv);
  80. L[i].cijena=L[i+1].cijena;
  81. L[i].dan=L[i+1].dan;
  82. L[i].mj=L[i+1].mj;
  83. L[i].god=L[i+1].god;
  84. }
  85. return 1;
  86. }
  87.  
  88. //5. Brisanje prema vrsti
  89. int DeleteVrstaL(int c, char vrsta[], lista L[]){
  90. int x;
  91. for(x=0; x<c; x++){
  92. if(!strcmp(vrsta, L[x].vrsta)) break;
  93. }
  94. if(x==c) return 0;
  95. for(int i=x; i<c; i++){
  96. L[i].sifra=L[i+1].sifra;
  97. strcpy(L[i].vrsta,L[i+1].vrsta);
  98. strcpy(L[i].naziv,L[i+1].naziv);
  99. L[i].cijena=L[i+1].cijena;
  100. L[i].dan=L[i+1].dan;
  101. L[i].mj=L[i+1].mj;
  102. L[i].god=L[i+1].god;
  103. }
  104. return 1;
  105. }
  106.  
  107.  
  108. //6. Sortiranje merge sort
  109. void Spajanje(lista *L, int i, int k, int j){
  110. int I=i, J=k+1, K=0;
  111. lista *A=new lista [j-i+1];
  112. while(I<=k && J<=j){
  113. if(L[I].cijena<=L[J].cijena){
  114. A[K++]=L[I++];
  115. }
  116. else {
  117. A[K++]=L[J++];
  118. }
  119. }//while
  120. if(I>k) while(J<=j) {
  121. A[K++]=L[J++];
  122. }//while
  123. else while(I<=k) {
  124. A[K++]=L[I++];
  125. }//while
  126. for(int I=0; I<=j-i; I++){
  127. L[i+I]=A[I];
  128. }
  129. }//spajanje
  130.  
  131. void MSort(lista *L, int i, int j){
  132. if(i<j){
  133. int k=(i+j)/2;
  134. MSort(L,i,k);
  135. MSort(L,k+1,j);
  136. Spajanje(L,i,k,j);
  137. }
  138. }
  139.  
  140. void SortL(int c, lista *L){
  141. MSort(L,0,c-1);
  142. cout<<"Lista je uspjesno sortirana!!!"<<endl;
  143. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.