Posted By


dhrgar on 11/12/12

Tagged


Statistics


Viewed 83 times
Favorited by 0 user(s)

Drazen_Hrgar_Zadatak_1_Strukture_Podataka


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

Datoteke zaglavlja (implementacija liste pomoću polja i pokazivača), te glavni dio programa


Copy this code and paste it in your HTML
  1. // Implementacija liste pomocu polja
  2.  
  3. L#define ERROR -1
  4.  
  5. struct tDatum {
  6. short dan, mjesec, godina;
  7. };
  8. typedef tDatum Datum;
  9.  
  10. struct tZiv {
  11. int sifra;
  12. char vrsta[50], naziv[50];
  13. float cijena;
  14. tDatum datum;
  15. };
  16. typedef tZiv Zivotinja;
  17.  
  18. struct tLista {
  19. tZiv el[10000];
  20. int kursor;
  21. };
  22. typedef int Element;
  23. typedef tLista Lista;
  24.  
  25. bool operator==(Zivotinja z, Zivotinja z2) {
  26. if(z.sifra != z2.sifra)
  27. return false;
  28. else if(z.cijena != z2.cijena)
  29. return false;
  30. else if(z.sifra != z2.sifra)
  31. return false;
  32. else if(z.sifra != z2.sifra)
  33. return false;
  34. else if(z.datum.dan!=z2.datum.dan || z.datum.mjesec!=z2.datum.mjesec ||
  35. z.datum.godina!=z2.datum.godina)
  36. return false;
  37. else
  38. return true;
  39. }
  40.  
  41. Element FirstL(Lista* L) {
  42. return 0;
  43. }
  44. Element EndL(Lista* L) {
  45. return L->kursor;
  46. }
  47.  
  48. Element NextL(Element p, Lista* L) {
  49. if(p==EndL(L) )
  50. return ERROR;
  51. return p+1;
  52. }
  53. Element PreviousL(Element p, Lista* L) {
  54. if(p==FirstL(L) )
  55. return ERROR;
  56. return p-1;
  57. }
  58.  
  59. Element LocateL(Zivotinja x, Lista* L) {
  60. int i=0;
  61. while(i<L->kursor) {
  62. if(L->el[i] == x)
  63. return i;
  64. i++;
  65. }
  66. return EndL(L);
  67. }
  68. bool InsertL(Zivotinja x, Element p, Lista* L) {
  69. if(p<0 || p>L->kursor)
  70. return 0;
  71.  
  72. int i=L->kursor;
  73. L->kursor++;
  74. while(i>p) {
  75. L->el[i] = L->el[i-1];
  76. i--;
  77. }
  78. L->el[p] = x;
  79. return 1;
  80. }
  81.  
  82. bool DeleteL(Element p, Lista* L) {
  83. if(p<0 || p>L->kursor)
  84. return 0;
  85.  
  86. int i = p+1;
  87. while(i<L->kursor) {
  88. L->el[i-1] = L->el[i];
  89. i++;
  90. }
  91. L->kursor--;
  92. return 1;
  93. }
  94. Zivotinja RetrieveL(Element p, Lista* L) {
  95. return L->el[p];
  96. }
  97.  
  98. void DeleteAllL(Lista* L) {
  99. L->kursor = 0;
  100. }
  101. void InitL(Lista* L) {
  102. L->kursor = 0;
  103. }
  104.  
  105. // Implementacija liste pomocu pokazivaca
  106.  
  107. #define ERROR NULL
  108.  
  109. struct tDatum {
  110. short dan, mjesec, godina;
  111. };
  112. typedef tDatum Datum;
  113.  
  114. struct tZiv {
  115. int sifra;
  116. char vrsta[50], naziv[50];
  117. float cijena;
  118. tDatum datum;
  119. };
  120. typedef tZiv Zivotinja;
  121.  
  122. struct tEl {
  123. tZiv el;
  124. tEl* sljedeci;
  125. };
  126. typedef tEl* Element;
  127. typedef tEl Lista;
  128.  
  129. bool operator==(Zivotinja z, Zivotinja z2) {
  130. if(z.sifra != z2.sifra)
  131. return false;
  132. else if(z.cijena != z2.cijena)
  133. return false;
  134. else if(z.sifra != z2.sifra)
  135. return false;
  136. else if(z.sifra != z2.sifra)
  137. return false;
  138. else if(z.datum.dan!=z2.datum.dan || z.datum.mjesec!=z2.datum.mjesec ||
  139. z.datum.godina!=z2.datum.godina)
  140. return false;
  141. else
  142. return true;
  143. }
  144.  
  145. Element FirstL(Lista* L) {
  146. return L;
  147. }
  148.  
  149. Element EndL(Lista* L) {
  150. Element trenutni = L;
  151. while(trenutni->sljedeci)
  152. trenutni=trenutni->sljedeci;
  153.  
  154. return trenutni;
  155. }
  156.  
  157. Element NextL(Element p, Lista* L) {
  158. if(p->sljedeci)
  159. return p->sljedeci;
  160. return ERROR;
  161. }
  162.  
  163. Element PreviousL(Element p, Lista* L) {
  164. if(p==FirstL(L) )
  165. return ERROR;
  166.  
  167. Element trenutni = FirstL(L);
  168. while(trenutni->sljedeci!=p)
  169. trenutni=trenutni->sljedeci;
  170.  
  171. return trenutni;
  172. }
  173.  
  174. Element LocateL(Zivotinja x, Lista* L) {
  175. Element trenutni=L;
  176. while(trenutni->sljedeci) {
  177. if(trenutni->sljedeci->el == x)
  178. return trenutni;
  179. trenutni = trenutni->sljedeci;
  180. }
  181. return EndL(L);
  182. }
  183.  
  184. bool InsertL(Zivotinja x, Element p, Lista* L) {
  185. if(!L) return 0;
  186. Element novi = new tEl;
  187.  
  188. memcpy(novi, &x, sizeof(Zivotinja) );
  189.  
  190. novi->sljedeci = p->sljedeci;
  191. p->sljedeci = novi;
  192. return 1;
  193. }
  194.  
  195. bool DeleteL(Element p, Lista* L) {
  196. if(!L) return 0;
  197. Element trenutni = p->sljedeci;
  198. p->sljedeci = trenutni->sljedeci;
  199. delete trenutni;
  200. return 1;
  201. }
  202. Zivotinja RetrieveL(Element p, Lista* L) {
  203. return p->sljedeci->el;
  204. }
  205.  
  206. void DeleteAllL(Lista* L) {
  207. if(!L) return;
  208. if(!L->sljedeci) return;
  209. Element prethodni = L->sljedeci;
  210. Element trenutni=prethodni->sljedeci;
  211. while(trenutni!=NULL) {
  212. delete prethodni;
  213. prethodni = trenutni;
  214. trenutni = prethodni->sljedeci;
  215. }
  216. delete prethodni;
  217. L->sljedeci = NULL;
  218. }
  219. void InitL(Lista* L) {
  220. L->sljedeci = NULL;
  221. }
  222.  
  223. // Glavni dio programa
  224.  
  225. #include <iostream>
  226. //#include "l_polje.h"
  227. #include "l_pokazivaci.h"
  228. using namespace std;
  229.  
  230. // mog 1 - Unos
  231. bool U(Lista* L) {
  232. cout << endl;
  233. cout << " Dodavanje zivotinje u listu " << endl;
  234. cout << "#############################" << endl;
  235.  
  236. static int sifra = 100;
  237.  
  238. Zivotinja* ziv = new Zivotinja;
  239. ziv->sifra = sifra;
  240.  
  241. sifra++;
  242. cout << "Sifra(*auto): " << ziv->sifra << endl;
  243.  
  244. cin.ignore();
  245. cout << "Naziv: ";
  246. cin.getline(ziv->naziv, 50);
  247.  
  248. cout << "Vrsta: ";
  249. cin.getline(ziv->vrsta, 50);
  250.  
  251. cout << "Cijena: ";
  252. cin >> ziv->cijena;
  253.  
  254.  
  255. cout << "Dan: ";
  256. cin >> ziv->datum.dan;
  257.  
  258. cout << "Mjesec: ";
  259. cin >> ziv->datum.mjesec;
  260.  
  261. cout << "Godina: ";
  262. cin >> ziv->datum.godina;
  263.  
  264.  
  265. int ok = InsertL(*ziv, EndL(L), L);
  266. return ok;
  267. }
  268.  
  269. // mog 3-1 - Ispis cijele liste
  270. void I1(Lista* L) {
  271. cout << endl;
  272. cout << " Ispis svih zivotinja iz liste " << endl;
  273. cout << "###############################" << endl;
  274.  
  275. Zivotinja ziv;
  276.  
  277. // prolazenje kroz cijelu listu i ispis
  278. Element trenutni = PreviousL(EndL(L), L) ;
  279. while(trenutni != ERROR) {
  280. ziv = RetrieveL(trenutni, L);
  281.  
  282. cout << endl;
  283. cout << "......................................" << endl;
  284.  
  285. cout << "Sifra: " << ziv.sifra << endl;
  286. cout << "Naziv: " << ziv.naziv << endl;
  287. cout << "Vrsta: " << ziv.vrsta << endl;
  288. cout << "Cijena: " << ziv.cijena << endl;
  289. cout << "Datum: " << ziv.datum.dan << ".";
  290.  
  291. cout << ziv.datum.mjesec << "." << ziv.datum.godina << "." << endl;
  292.  
  293. cout << endl;
  294.  
  295. trenutni = PreviousL(trenutni, L) ;
  296. }
  297. }
  298.  
  299. // mog 3-2 - Ispis svih zivotinja dostavljenih nakon 23.9.2012
  300. void I2(Lista* L) {
  301. cout << endl;
  302. cout << " Ispis zivotinja - svih poslije 23.9.2012. " << endl;
  303. cout << "###########################################" << endl;
  304.  
  305. Zivotinja ziv;
  306. int br = 0;
  307.  
  308. Element trenutni=FirstL(L);
  309. while(trenutni!=EndL(L)) {
  310. ziv = RetrieveL(trenutni, L);
  311. int dd = (ziv.datum.godina*10000) + (ziv.datum.mjesec*100) + ziv.datum.dan;
  312.  
  313. // ako je datum poslije (2012 09 23) - broj u if-u : ispis
  314. if(dd>20120923){
  315. cout << endl;
  316. cout << "Sifra: " << ziv.sifra << endl;
  317. cout << "Naziv: " << ziv.naziv << endl;
  318. cout << "Vrsta: " << ziv.vrsta << endl;
  319. cout << "Cijena: " << ziv.cijena << endl;
  320. cout << "Datum: " << ziv.datum.dan << ".";
  321. cout << ziv.datum.mjesec << "." << ziv.datum.godina << endl;
  322. cout << endl;
  323. br++;
  324. }
  325.  
  326. trenutni = NextL(trenutni, L);
  327. }
  328.  
  329. cout << "Broj ispisanih zivotinja: " << br << endl;
  330. }
  331.  
  332. bool StrJednako(char* s1, char* s2, int n, int n2) {
  333. // ako su rijeci razlicite duzine - onda nisu iste :D
  334. if(n != n2)
  335. return 0;
  336.  
  337. // prolazenje kroz sve znakova - ako postoji //razliciti znak - rijeci nisu iste
  338. int i=0; //razliciti znak - rijeci nisu iste
  339. while(i<n) {
  340. if(s1[i] != s2[i])
  341. return 0;
  342. i++;
  343. }
  344.  
  345. // ako do sad funkcija nije zavrsena - rijeci su iste (konacno iste)
  346. return 1; //su iste (konacno iste)
  347. }
  348.  
  349. // mog 5-1 - Brisanje prema nazivu
  350. int B1(Lista* L) {
  351. cout << endl;
  352. cout << " Brisanje unosa prema nazivu " << endl;
  353. cout << "#############################" << endl;
  354.  
  355. // unos naziva
  356. char naziv[50];
  357. cin.ignore();
  358.  
  359. cout << "Naziv zivotinje koja se zeli obrisati: ";
  360. cin.getline(naziv, 50);
  361.  
  362. // pretrazivanje liste sa zivotinjama
  363. Zivotinja ziv;
  364.  
  365. Element trenutni=FirstL(L);
  366. while(trenutni!=EndL(L)) {
  367. ziv = RetrieveL(trenutni, L);
  368.  
  369. // provjeri da li je naziv zivotinje jednak unesenom nazivu
  370. if(StrJednako(naziv, ziv.naziv, strlen(naziv), strlen(ziv.naziv))) {
  371. int ok = DeleteL(LocateL(ziv, L), L);
  372. if(ok==-1) return 0;
  373. else return 1;
  374. }
  375.  
  376. trenutni = NextL(trenutni, L);
  377. }
  378. return 0;
  379. }
  380.  
  381. // mog 5-2 - Brisanje prema vrsti
  382. int B2(Lista* L) {
  383. cout << endl;
  384. cout << " Brisanje vrste " << endl;
  385. cout << "################" << endl;
  386.  
  387. char vrsta[50];
  388. cin.ignore();
  389.  
  390. cout << "Vrsta zivotinja koja se zeli obrisati: ";
  391. cin.getline(vrsta, 50);
  392.  
  393. int br = 0;
  394. Zivotinja ziv;
  395.  
  396.  
  397. Element trenutni=FirstL(L);
  398. while(trenutni!=EndL(L)) {
  399. ziv = RetrieveL(trenutni, L);
  400.  
  401.  
  402. if(StrJednako(vrsta,ziv.vrsta,strlen(vrsta), strlen(ziv.vrsta))) {
  403.  
  404.  
  405. int ok = DeleteL(LocateL(ziv, L), L);
  406. if(ok==1) {
  407. br++;
  408. continue;
  409. }
  410. }
  411. trenutni=NextL(trenutni,L);
  412. }
  413.  
  414. cout << "Ukupno izbrisano " << br << " unosa." << endl;
  415.  
  416. if(br>0)
  417. return 1;
  418. else
  419. return 0;
  420. }
  421.  
  422. bool Provjera(Zivotinja z, Zivotinja z2) {
  423.  
  424. if(z.cijena<z2.cijena)
  425. return 1;
  426.  
  427.  
  428. if(z.cijena==z2.cijena) {
  429. int manji = (strlen(z.naziv)<strlen(z2.naziv)) ? strlen(z.naziv) :
  430. strlen(z2.naziv);
  431.  
  432.  
  433.  
  434. for(int i=0; i<manji; i++)
  435.  
  436. if(z.naziv[i]>z2.naziv[i])
  437. return 0;
  438.  
  439. else if(z.naziv[i]<z2.naziv[i])
  440. return 1;
  441. }
  442.  
  443.  
  444. return 0;
  445. }
  446.  
  447.  
  448. void SortiranjeSpoji(Zivotinja p[], int i, int k, int j) {
  449. int I=i, J=k+1, K=0;
  450. Zivotinja* p2 = new Zivotinja[j-i+1];
  451.  
  452. while(I<=k && J<=j)
  453. if(Provjera(p[I], p[J]))
  454. p2[K++]=p[I++];
  455. else
  456. p2[K++]=p[J++];
  457.  
  458. if(I>k)
  459. while(J<=j)
  460. p2[K++]=p[J++];
  461. else
  462. while(I<=k)
  463. p2[K++]=p[I++];
  464.  
  465. for(int m=i; m<=j; m++)
  466. p[m]=p2[m-i];
  467.  
  468. delete[] p2;
  469. }
  470. void SortiranjeSpajanjem(Zivotinja p[], int i, int j) {
  471. if(i<j) {
  472. int k = (i+j) / 2;
  473. SortiranjeSpajanjem(p, i, k);
  474. SortiranjeSpajanjem(p, k+1, j);
  475. SortiranjeSpoji(p, i, k, j);
  476. }
  477. }
  478.  
  479.  
  480. void S(Lista* L) {
  481. cout << endl;
  482. cout << " Sortiranje liste zivotinja " << endl;
  483. cout << "############################" << endl;
  484.  
  485. int brojac=0, i=0;
  486.  
  487.  
  488. Element trenutni=FirstL(L);
  489. while(trenutni!=EndL(L)) {
  490. brojac++;
  491. trenutni=NextL(trenutni, L);
  492. }
  493.  
  494. //
  495. Zivotinja* zivotinje = new Zivotinja[brojac];
  496. Element nova=FirstL(L);
  497. while( nova!=EndL(L)) {
  498. zivotinje[i++]=RetrieveL(nova, L);
  499.  
  500. nova=NextL(nova, L);
  501. }
  502.  
  503.  
  504. SortiranjeSpajanjem(zivotinje, 0, brojac-1);
  505.  
  506.  
  507. DeleteAllL(L);
  508.  
  509.  
  510. for(int i=brojac-1; i>=0; i--)
  511. InsertL(zivotinje[i], EndL(L), L);
  512.  
  513.  
  514. delete[] zivotinje;
  515. }
  516.  
  517. int main() {
  518. Lista* L = new Lista;
  519.  
  520. InitL(L);
  521.  
  522. short od;
  523.  
  524. do {
  525. cout << endl;
  526. cout << "#############################" << endl;
  527. cout << " Glavni izbornik " << endl;
  528. cout << "#############################" << endl;
  529. cout << " 1. Dodavanje zivotinje" << endl;
  530. cout << " 3. Ispis" << endl;
  531. cout << " 5. Brisanje vrste zivotinja" << endl;
  532. cout << " 7. Sortiranje" << endl;
  533. cout << " 9. Izlaz" << endl;
  534. cout << "Vas izbor: ";
  535. cin >> od;
  536.  
  537. switch(od) {
  538. case 1: {
  539. int ok = U(L);
  540. if(ok==1)
  541. cout << endl << "Unos je dodan u listu." << endl;
  542. break;
  543. }
  544. case 3: {
  545. int a;
  546. do {
  547. cout << "1. Ispis cijele liste" << endl;
  548. cout << "2. samo zivotinja s datumom poslije 23.9.2012." << endl;
  549. cin >> a;
  550.  
  551. if(a==1)
  552. I1(L);
  553.  
  554. if(a==2)
  555. I2(L);
  556. } while(a!=1 && a!=2);
  557.  
  558. break;
  559. }
  560.  
  561. case 5: {
  562. int a;
  563. do {
  564. cout << "1. Brisanje 1 zivotinje prema nazivu" << endl;
  565. cout << "2. Brisanje svih zivotinja prema vrsti" << endl;
  566. cin >> a;
  567.  
  568. if(a==1) {
  569. int ok = B1(L);
  570. if(ok==1)
  571. cout << "Brisanje je uspjesno obavljeno." << endl;
  572. }
  573.  
  574. if(a==2) {
  575. int ok = B2(L);
  576. if(ok)
  577. cout << "Brisanje je uspjesno obavljeno." << endl;
  578. }
  579. } while(a!=1 && a!=2);
  580.  
  581. break;
  582. }
  583.  
  584. case 7:
  585. S(L);
  586. break;
  587.  
  588. case 9:
  589. break;
  590.  
  591. default:
  592. cout << "Pogresan unos. Ponovite unos" << endl;
  593. }
  594. } while(od!=9);
  595.  
  596. return 0;
  597. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.