Posted By


mhudince on 11/10/12

Tagged


Statistics


Viewed 529 times
Favorited by 0 user(s)

Strukture_podataka_zad_1 (zivotinje.cpp)


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

zadatak 1 za SP


Copy this code and paste it in your HTML
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. #include "lista_pokazivac.h"
  5. //#include "lista_polje.h"
  6.  
  7. using namespace std;
  8.  
  9. void spoji(zivotinje A[], int i, int k, int j){
  10. int I=i, J=k+1, K=0;
  11. zivotinje *B = new zivotinje [j-i+1];
  12.  
  13. while (I <= k && J <= j)
  14. if (A[I].cijena<A[J].cijena)
  15. memcpy(&B[K++], &A[I++], sizeof(zivotinje) );
  16. else if (A[I].cijena == A[J].cijena) {
  17. int n = (strlen(A[I].naziv) < strlen(A[J].naziv)) ? strlen(A[I].naziv) : strlen(A[J].naziv);
  18. for (int in=0; in<n; in++)
  19. if (A[I].naziv[in]>A[J].naziv[in])
  20. memcpy(&B[K++], &A[J++], sizeof(zivotinje) );
  21. else if (A[I].naziv[i]<A[J].naziv[i])
  22. memcpy(&B[K++], &A[I++], sizeof(zivotinje) );
  23. }
  24. else
  25. memcpy(&B[K++], &A[J++], sizeof(zivotinje) );
  26.  
  27. if (I>k)
  28. while (J <= j)
  29. memcpy(&B[K++], &A[J++], sizeof(zivotinje) );
  30. else
  31. while (I<=k)
  32. memcpy(&B[K++], &A[I++], sizeof(zivotinje) );
  33. for (int I = 0; I <=j-i; I++) {
  34. memcpy(&A[i+I], &B[I], sizeof(zivotinje) );
  35. }
  36. delete []B;
  37. }
  38.  
  39. void MSort (zivotinje *A, int i, int j) {
  40. if (i<j) {
  41. int k=(i+j)/2;
  42. MSort(A,i,k);
  43. MSort (A, k+1, j);
  44. spoji (A,i,k,j);
  45. }
  46. }
  47.  
  48. void MSort (zivotinje A[], int N) {
  49. MSort (A,0,N-1);
  50. }
  51.  
  52.  
  53.  
  54. int dodaj (LIST *L, int sifra){
  55. bool u = 0;
  56. zivotinje ziv;
  57.  
  58. ziv.sifra = sifra;
  59. cout << "Naziv: ";
  60. cin.ignore();
  61. cin.getline(ziv.naziv, 20);
  62. cout << "Vrsta: ";
  63. cin.getline(ziv.vrsta, 30);
  64. cout << "Cijena: ";
  65. cin >> ziv.cijena;
  66.  
  67. do {
  68. cout << "Dan: ";
  69. cin >> ziv.d;
  70. } while (ziv.d <= 0 || ziv.d > 31);
  71. do {
  72. cout << "Mjesec: ";
  73. cin >> ziv.m;
  74. } while (ziv.m <= 0 || ziv.m > 12);
  75. do {
  76. cout << "Godina: ";
  77. cin >> ziv.g;
  78. } while (ziv.g <= 1900 || ziv.g > 2100);
  79.  
  80. if (INSERTL (ziv, ENDL(L), L) != 0) {
  81. u = 1;
  82. }
  83. cout << endl;
  84. if (u == 1) return 1;
  85. else return 0;
  86. }
  87.  
  88. void ispisi (LIST *L) {
  89. int br = 0;
  90. zivotinje z[1000];
  91. element position = FIRSTL(L);
  92.  
  93. while (position != ENDL(L)) {
  94. z[br] = RETRIEVEL (position, L);
  95. position = NEXTL (position, L);
  96. br++;
  97. }
  98.  
  99. for(int j = br-1; j >= 0; j--) {
  100. cout << "Sifra: " << z[j].sifra << endl;
  101. cout << "Naziv: " << z[j].naziv << endl;
  102. cout << "Vrsta: " << z[j].vrsta << endl;
  103. cout << "Datum: " << z[j].d << "." << z[j].m << "." << z[j].g << "." << endl;
  104. cout << "Cijena: " << z[j].cijena << endl;
  105. cout << "---------------" << endl;
  106. }
  107. }
  108.  
  109. void pretrazi (LIST *L) {
  110. int br = 0;
  111. int brojac = 0;
  112. zivotinje z[1000];
  113. element position = FIRSTL(L);
  114.  
  115. while (position != ENDL(L)) {
  116. z[br] = RETRIEVEL (position, L);
  117. position = NEXTL (position, L);
  118. br++;
  119. }
  120.  
  121. zivotinje polje[br];
  122.  
  123. for (int i = 0; i <= br; i++) {
  124. if(z[i].g > 2012){
  125. polje[brojac] = z[i];
  126. brojac ++;
  127. }
  128. else if(z[i].g == 2012){
  129. if(z[i].m > 9){
  130. polje[brojac] = z[i];
  131. brojac ++;
  132. }
  133. }
  134. else if(z[i].g == 2012){
  135. if(z[i].m == 9){
  136. if(z[i].d > 23){
  137.  
  138. polje[brojac] = z[i];
  139. brojac ++;}
  140. }
  141. }
  142. }
  143. for (int j = 0; j < brojac; j++) {
  144. cout << "Sifra: " << polje[j].sifra << endl;
  145. cout << "Naziv: " << polje[j].naziv << endl;
  146. cout << "Vrsta: " << polje[j].vrsta << endl;
  147. cout << "Datum: " << polje[j].d << "." << polje[j].m << "." << polje[j].g << "." << endl;
  148. cout << "Cijena: " << polje[j].cijena << endl;
  149. cout << "---------------" << endl;
  150. }
  151. cout << "---------------" << endl;
  152. cout << "Broj: " << brojac << endl;
  153. cout << "---------------" << endl;
  154. }
  155.  
  156. int brisi_naziv (LIST *L){
  157. char naz[20];
  158. cout << "Unesi naziv zivotinje: ";
  159. cin.ignore();
  160. cin.getline (naz, 20);
  161.  
  162. element position = FIRSTL(L);
  163.  
  164. zivotinje ziv;
  165.  
  166. while (position != ENDL(L)) {
  167. ziv = RETRIEVEL (position, L);
  168. if (strcmp(ziv.naziv, naz) != 0) {
  169. //position = position->sljedeci; // position = NEXTL (position, L); -> zbog polja
  170. position = NEXTL (position, L);
  171. }
  172. else {
  173. DELETEL (position, L);
  174. return 1;
  175. }
  176. }
  177. return 0;
  178. }
  179.  
  180. int brisi_vrstu (LIST *L) {
  181. char v[20];
  182. cout << "Unesi vrstu zivotinje: ";
  183. cin.ignore();
  184. cin.getline (v, 20);
  185.  
  186. element position = FIRSTL(L);
  187.  
  188. zivotinje ziv;
  189.  
  190. while (position != ENDL(L)) {
  191. ziv = RETRIEVEL (position, L);
  192. if (strcmp(ziv.vrsta, v) != 0) {
  193. //position = position->sljedeci; // position = NEXTL (position, L); -> zbog polja
  194. position = NEXTL (position, L);
  195. }
  196. else {
  197. DELETEL (position, L);
  198. }
  199. }
  200. return 0;
  201. }
  202.  
  203. void sortiraj (LIST *L) {
  204. zivotinje z[1000];
  205. element position = FIRSTL(L);
  206. int b=0;
  207. while (position != ENDL(L)) {
  208. z[b] = RETRIEVEL (position, L);
  209. position = NEXTL (position, L);
  210. b++;
  211. }
  212.  
  213. MSort (z, b);
  214.  
  215.  
  216. // ispisuje sortirano
  217. for(int j=0; j < b; j++) {
  218. cout << "Sifra: " << z[j].sifra << endl;
  219. cout << "Naziv: " << z[j].naziv << endl;
  220. cout << "Vrsta: " << z[j].vrsta << endl;
  221. cout << "Datum: " << z[j].d << "." << z[j].m << "." << z[j].g << "." << endl;
  222. cout << "Cijena: " << z[j].cijena << endl;
  223. cout << "---------------" << endl;
  224. }
  225. }
  226.  
  227. int main () {
  228.  
  229. int izbor, sifra = 100;
  230. LIST *L = INITL(L);
  231.  
  232. do {
  233. cout << "IZBORNIK" << endl;
  234. cout << "======================================================" << endl;
  235. cout << "1. Dodaj zapis." << endl;
  236. cout << "2. Ispisi sadrzaj." << endl;
  237. cout << "3. Pretrazi nakon 23.9.2012." << endl;
  238. cout << "4. Brisi po nazivu." << endl;
  239. cout << "5. Brisi vrstu." << endl;
  240. cout << "6. Sortiraj po cijeni i nazivu." << endl;
  241. cout << "0. IZLAZ" << endl;
  242. cout << "------------------------------------------------------" << endl;
  243. do {
  244. cout << "IZBOR: ";
  245. cin >> izbor;
  246. } while (izbor < 0 || izbor > 6);
  247.  
  248. cout << endl;
  249. switch (izbor) {
  250.  
  251. case 0: break;
  252.  
  253. case 1:
  254. dodaj(L, sifra);
  255. sifra ++;
  256. break;
  257.  
  258. case 2:
  259. ispisi(L);
  260. break;
  261.  
  262. case 3:
  263. pretrazi(L);
  264. break;
  265.  
  266. case 4:
  267. brisi_naziv(L);
  268. break;
  269. case 5:
  270. brisi_vrstu(L);
  271. break;
  272. case 6:
  273. sortiraj(L);
  274. break;
  275. }
  276.  
  277. } while (izbor != 0);
  278.  
  279. return 0;
  280. }

URL: mhudince1

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.