glavni_program_zadatak_1 Strukture podataka


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

U glavnom programu se nalaze dvije biblijoteke lista_pokazivaca i lista_polje. Pomocu nje sam napravio program za evidenciju zivotinja.


Copy this code and paste it in your HTML
  1. #include <iostream>
  2. #include "lista_polje1.h"
  3. //#include "lista_pokazivaca.h"
  4. using namespace std;
  5.  
  6. void unos_niza(char *polje){
  7. cin.getline(polje, 100);
  8. if(cin.gcount()==1) cin.getline(polje,100);
  9. }
  10. lista *lis=InitL(lis);
  11. int broj_ziv=1;
  12. int broj_zivotinja=0;
  13. int ukupni_datum;
  14. int unos(lista *lis){
  15.  
  16. zivotinje zivotinja;
  17. cout<<"Sifra zivotinje: "<<broj_ziv<<endl;
  18. zivotinja.sifra=broj_ziv;
  19. cout<<"Naziv zivotinje: ";
  20. unos_niza(zivotinja.naziv);
  21. cout<<"Vrsta zivotinje: ";
  22. unos_niza(zivotinja.vrsta);
  23. cout<<"Datum ispuruke: "<<endl;
  24. do{
  25. cout<<"\t dan: ";
  26. cin>>zivotinja.d;}while(zivotinja.d<=0 || zivotinja.d>32);
  27. do{
  28. cout<<"\t mjesec: ";
  29. cin>>zivotinja.m;}while(zivotinja.m<=0 || zivotinja.m>12);
  30. do{
  31. cout<<"\t godina: ";
  32. cin>>zivotinja.g;}while(zivotinja.g<=1970 || zivotinja.g>2020);
  33. cout<<"Unesite cijenu: ";
  34. cin>>zivotinja.cijena;
  35. cout<<"Da li je vece od 23.09.2012?"<<endl;
  36. ukupni_datum=zivotinja.d+(31*(zivotinja.m-1))+(12*31*(zivotinja.g-1));
  37. zivotinja.dat=ukupni_datum;
  38. cout<<"*****************"<<endl;
  39. int provjeri = InsertL(zivotinja, EndL(lis), lis);
  40. if(provjeri==1){
  41. cout<<endl<<"Zapis je unesen" << endl;
  42. broj_zivotinja++;
  43. broj_ziv++;
  44. return 1;
  45. }
  46. else return 0;
  47.  
  48. }
  49. void obrnuti_redosljed(lista *lis){
  50.  
  51. element zadnji = PreviousL(EndL(lis), lis);
  52. zivotinje zivotinja;
  53.  
  54. if(EndL(lis) == FirstL(lis)){
  55. cout << "Lista je prazna";
  56. return;
  57. }
  58. while(zadnji >= FirstL(lis)){
  59.  
  60. zivotinja = RetrieveL(zadnji, lis);
  61. cout<<"Sifra zivotinje: "<<zivotinja.sifra<<endl;
  62. cout<<"naziv zivotinje: "<<zivotinja.naziv<<endl;
  63. cout<<"vrsta zivotine : "<<zivotinja.vrsta<<endl;
  64. cout<<"Datum ispuruke : "<<zivotinja.d<<"."<<zivotinja.m<<"."<<zivotinja.g<<endl;
  65. cout<<"Unesena cijena : "<<zivotinja.cijena<<endl;
  66. cout<<"-----------------"<<endl;
  67. zadnji = PreviousL(zadnji, lis);
  68. }
  69. }
  70.  
  71. void ispis() {
  72. zivotinje a;
  73. element p=FirstL(lis);
  74. while (p!=EndL(lis)){
  75. a=RetrieveL(p,lis);
  76. cout<<"Sifra: "<<a.sifra<<endl;
  77. cout<<"Vrsta: "<<a.vrsta<<endl;
  78. cout<<"Naziv: "<<a.naziv<<endl;
  79. cout<<"Cijena: "<<a.cijena<<endl;
  80. cout<<"Datum ispuruke : "<<a.d<<"."<<a.m<<"."<<a.g<<endl;
  81.  
  82. cout<<"-------------------------------\n";
  83. p = NextL(p,lis);
  84. }
  85. }
  86.  
  87.  
  88. void pretraga(){
  89. system("cls");
  90. int brojac=0;
  91. zivotinje ziv;
  92. element last=PreviousL(EndL(lis), lis);
  93. if(EndL(lis) !=FirstL(lis))
  94. while(true){
  95. ziv=RetrieveL(last, lis);
  96.  
  97. if(ziv.dat>748363){
  98. cout<<"Sifra zivotinje: "<<ziv.sifra<<endl;
  99. cout<<"naziv zivotinje: "<<ziv.naziv<<endl;
  100. cout<<"vrsta zivotine : "<<ziv.vrsta<<endl;
  101. cout<<"Datum ispuruke : "<<ziv.d<<"."<<ziv.m<<"."<<ziv.g<<endl;
  102. cout<<"Unesena cijena : "<<ziv.cijena<<endl;
  103. cout<<"-----------------"<<endl;
  104. brojac++;
  105. }
  106. if(last==FirstL(lis)) break;
  107. last=PreviousL(last, lis);
  108. }
  109.  
  110. cout<<"Zbroj zivotinja nakon 23.rujna 2012 : " <<brojac<<endl;
  111.  
  112. }
  113.  
  114.  
  115. int brisanje_naziv (lista *lis){
  116. char naz[20];
  117. cout << "Unesi naziv zivotinje: ";
  118. unos_niza(naz);
  119. element pozicija=FirstL(lis);
  120. zivotinje ziv;
  121.  
  122. while (pozicija != EndL(lis)) {
  123.  
  124. ziv = RetrieveL(pozicija, lis);
  125. if (strcmp(ziv.naziv, naz) != 0) {
  126. pozicija = NextL (pozicija, lis);
  127. }
  128. else {
  129.  
  130. DeleteL (pozicija, lis);
  131. broj_ziv--;
  132. return 1;
  133.  
  134. }
  135. }
  136. return 0;
  137. }
  138.  
  139. int brisanje_vrsta(lista *lis){
  140. char vrsta[30];
  141. cout << "Unesite vrstu zivotinja za brisanje: "; unos_niza(vrsta);
  142. element pozicija=FirstL(lis);
  143. zivotinje ziv;
  144. bool postoji=false;
  145.  
  146. while (pozicija != EndL(lis)) {
  147.  
  148. ziv = RetrieveL(pozicija, lis);
  149. if (strcmp(ziv.vrsta, vrsta) != 0) {
  150. pozicija = NextL (pozicija, lis);
  151. }
  152. else {
  153.  
  154. DeleteL (pozicija, lis);
  155. broj_ziv--;
  156.  
  157. postoji=true;
  158. }
  159. }
  160. if(postoji) return 1;
  161. else return 0;
  162. }
  163.  
  164. void mergesort(zivotinje*a, zivotinje *b, int l, int r){
  165. int i, j, k, m;
  166. if(l < r){
  167. m = (r+l) / 2;
  168. mergesort(a, b, l, m);
  169. mergesort(a, b, m+1, r);
  170. i = l; j = m+1; k = l;
  171. while((i <= m) && (j <= r)){
  172.  
  173. if(a[i].cijena>a[j].cijena || ((a[i].cijena==a[j].cijena)&&(strncmp(a[i].naziv, a[j].naziv, sizeof(a[i].naziv))>0)))
  174. memcpy(&b[k++], &a[i++], sizeof(zivotinje));
  175. else
  176. memcpy(&b[k++], &a[j++], sizeof(zivotinje));
  177. }
  178. while(i <= m)
  179. memcpy(&b[k++], &a[i++], sizeof(zivotinje));
  180. while(j <= r)
  181. memcpy(&b[k++], &a[j++], sizeof(zivotinje));
  182. for(k = l; k <= r; k++)
  183. memcpy(&a[k], &b[k], sizeof(zivotinje));
  184. }
  185. }
  186.  
  187. void sort(){
  188. zivotinje*a = new zivotinje[broj_zivotinja];
  189. element tekuci = FirstL(lis);
  190. int i = 0;
  191. while(tekuci != EndL(lis)){
  192.  
  193. a[i] = RetrieveL(tekuci, lis);
  194. tekuci = NextL(tekuci, lis);
  195. i++;
  196. }
  197. zivotinje *b = new zivotinje[broj_zivotinja];
  198. mergesort(a, b, 0, broj_zivotinja-1);
  199. DeleteAllL(lis);
  200. InitL(lis);
  201.  
  202. for(i=0; i<broj_zivotinja; i++)
  203. InsertL(a[i], EndL(lis), lis);
  204. delete []a;
  205. delete []b;
  206. }
  207. int main(){
  208.  
  209.  
  210. int izbor;
  211. do{ cout<<"IZBORNIK"<<endl;
  212. cout<<"------------------"<<endl;
  213. cout<<"1.dodavanje zapisa u listu "<<endl;
  214. cout<<"2.Ispis zapisa "<<endl;
  215. cout<<"3.ispis svih zivotinja nakon 23.rujna 2012"<<endl;
  216. cout<<"4.brisanje prema nazivu "<<endl;
  217. cout<<"5.brisanje prema vrsti"<<endl;
  218. cout<<"6.Sortiranje prema cijeni i nazivu"<<endl;
  219. cout<<"9.kraj"<<endl;
  220. cout<<"Vas izbor je: "<<endl;
  221. cout<<"------------------"<<endl;
  222.  
  223. cin>>izbor;
  224. cout<<"------------------"<<endl;
  225. switch(izbor){
  226. case 1: system("cls"); unos(lis);obrnuti_redosljed(lis);break;
  227. case 2: system("cls");obrnuti_redosljed(lis);break;
  228. case 3: system("cls");pretraga();break;
  229. case 4: system("cls");brisanje_naziv(lis);obrnuti_redosljed(lis);break;
  230. case 5: system("cls");brisanje_vrsta(lis);obrnuti_redosljed(lis);break;
  231. case 6: system("cls");sort();ispis();break;
  232. case 9: system("cls");cout<<"Izradio: Nikola Miljancic"<<endl;
  233. cout<<"Fakultet organizacije i informatike,Varazdin"<<endl;
  234.  
  235.  
  236. }
  237.  
  238.  
  239. }while(izbor!=9);
  240.  
  241.  
  242.  
  243. system("pause");
  244. return 0;
  245. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.