Posted By


kduga on 11/14/12

Tagged


Statistics


Viewed 65 times
Favorited by 0 user(s)

SP_zadatak1_kduganic


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

zadatak


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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.