Strukture podataka-Zadatak_1-Glavni_Program


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

Glavni dio programa"Životinjska_farma"

Ovaj program se ističe od sličnih riješenja više s svojim vizualnim izričajem no samim načinom riješavanja zadatka.
Razlog tome je prije svega Å¡to je velik broj studenata dobio isti zadatak.


Copy this code and paste it in your HTML
  1. #include <iostream>
  2. #include <cstring>
  3. #include <windows.h>
  4. #include "l_polj.h"
  5. //#include "l_pok.h"
  6. using namespace std;
  7. //_______________________________________________________________________________________________
  8. void last_to_st() {
  9. HANDLE hConsole;
  10. hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  11. elem a;
  12. int k=0;
  13. int p = PreviousL(EndL(list),list);
  14. while(k<br_elem){
  15. a=RetrieveL(p,list);
  16. SetConsoleTextAttribute(hConsole, 12);
  17. cout<<"\n________________________________________________________________________________\n";
  18. SetConsoleTextAttribute(hConsole, 15);
  19. cout<<"\tSifra:"<<a.sifra<<endl;
  20. cout<<"\tVrsta:"<<a.vrsta<<endl;
  21. cout<<"\tNaziv:"<<a.naziv<<endl;
  22. cout<<"\tCijena:"<<a.cijena<<endl;
  23. cout<<"\tDatum:"<<a.date.d<<".";
  24. cout<<a.date.m<<"."<<a.date.y<<".\n";
  25. SetConsoleTextAttribute(hConsole, 12);
  26. cout<<"\n________________________________________________________________________________\n";
  27. SetConsoleTextAttribute(hConsole, 15);
  28. p = PreviousL(p,list);
  29. k++;
  30. }
  31. }
  32.  
  33. //_______________________________________________________________________________________________
  34. void after_date() {
  35. HANDLE hConsole;
  36. hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  37. int numero=0;
  38. elem a;
  39. int p = FirstL(list);
  40. while(p!=EndL(list)) {
  41. a = RetrieveL(p,list);
  42. if(a.date.y>=2012)
  43. if((!strcmp(a.date.m,"rujan")&&a.date.d>23)||!strcmp(a.date.m,"listopad")||
  44. !strcmp(a.date.m,"studeni")||!strcmp(a.date.m,"prosinac")) {
  45. numero++;
  46. SetConsoleTextAttribute(hConsole, 12);
  47. cout<<"\n________________________________________________________________________________\n";
  48. SetConsoleTextAttribute(hConsole, 15);
  49. cout<<"\tSifra:"<<a.sifra<<endl;
  50. cout<<"\tVrsta:"<<a.vrsta<<endl;
  51. cout<<"\tNaziv:"<<a.naziv<<endl;
  52. cout<<"\tCijena:"<<a.cijena<<endl;
  53. cout<<"\tDatum:"<<a.date.d<<"."<<a.date.m<<"."<<a.date.y<<".\n";
  54. SetConsoleTextAttribute(hConsole, 12);
  55. cout<<"\n________________________________________________________________________________\n";
  56. SetConsoleTextAttribute(hConsole, 15);
  57. }
  58. p = NextL(p,list);
  59. }
  60. SetConsoleTextAttribute(hConsole, 10);
  61. cout<<"\tBroj pronadenih zivotinja:"<<numero<<"\n \n ";
  62. SetConsoleTextAttribute(hConsole, 15);
  63. }
  64.  
  65.  
  66. //_______________________________________________________________________________________________
  67. int spawn_new_creature() {
  68. HANDLE hConsole;
  69. hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  70. elem novi;
  71. novi.sifra=sifra++;
  72. SetConsoleTextAttribute(hConsole, 12);
  73. cout<<"\n________________________________________________________________________________\n";
  74. SetConsoleTextAttribute(hConsole, 15);
  75. cout<<"\tVrsta:";
  76. cin.ignore();
  77. cin.getline(novi.vrsta,50);
  78. cout<<"\tNaziv:";
  79. cin.getline(novi.naziv,50);
  80. cout<<"\tCijena:";cin>>novi.cijena;
  81. if(novi.cijena<=0) {sifra--;return 0;}
  82. cout<<"\tUnesite datum dostave:\n";
  83. cout<<"\tDan:";cin>>novi.date.d;
  84. if(novi.date.d<=0||novi.date.d>31) {sifra--;return 0;}
  85. cout<<"\tMjesec:";
  86. cin.ignore();
  87. cin.getline(novi.date.m,10);
  88. if(novi.date.d>29&&!strcmp(novi.date.m,"veljaca")) {sifra--;return 0;}
  89. cout<<"\tGodina:";cin>>novi.date.y;
  90. if(novi.date.y<1523||novi.date.y>2023) {sifra--;return 0;}
  91. InsertL(novi,br_elem,list);
  92. br_elem++;
  93. SetConsoleTextAttribute(hConsole, 12);
  94. cout<<"\n________________________________________________________________________________\n";
  95. SetConsoleTextAttribute(hConsole, 15);
  96. return 1;
  97. }
  98.  
  99. //_______________________________________________________________________________________________
  100. int del_name() {
  101. HANDLE hConsole;
  102. hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  103. char naziv[50];
  104. SetConsoleTextAttribute(hConsole, 12);
  105. cout<<"\n________________________________________________________________________________\n";
  106. SetConsoleTextAttribute(hConsole, 15);
  107. cout<<"\tUnesite naziv zivotinje:";
  108. cin.ignore();
  109. cin.getline(naziv,50);
  110. SetConsoleTextAttribute(hConsole, 12);
  111. cout<<"\n________________________________________________________________________________\n";
  112. SetConsoleTextAttribute(hConsole, 15);
  113. int stuff = LocateL(naziv,list);
  114. if(stuff==EndL(list)) return 0;
  115. else {
  116. DeleteL(stuff,list);
  117. br_elem--;
  118. return 1;
  119. }
  120. }
  121.  
  122. //_______________________________________________________________________________________________
  123. int del_spi() {
  124. HANDLE hConsole;
  125. hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  126. char vrsta[50];
  127. int stuff;
  128. bool obrisan=0;
  129. v=1;
  130. SetConsoleTextAttribute(hConsole, 12);
  131. cout<<"\n________________________________________________________________________________\n";
  132. SetConsoleTextAttribute(hConsole, 15);
  133. cout<<"\tUnesite vrstu zivotinje:";
  134. cin.ignore();
  135. cin.getline(vrsta,35);
  136. SetConsoleTextAttribute(hConsole, 12);
  137. cout<<"\n________________________________________________________________________________\n";
  138. SetConsoleTextAttribute(hConsole, 15);
  139. while (LocateL(vrsta,list)!=EndL(list)) {
  140. stuff = LocateL(vrsta,list);
  141. DeleteL(stuff,list);
  142. br_elem--;
  143. obrisan=1;
  144. }
  145. v=0;
  146. if(!obrisan) return 0;
  147. else return 1;
  148. }
  149.  
  150. //_______________________________________________________________________________________________
  151. void output() {
  152. HANDLE hConsole;
  153. hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  154. elem a;
  155. int p=FirstL(list);
  156. while (p!=EndL(list)){
  157. a=RetrieveL(p,list);
  158. SetConsoleTextAttribute(hConsole, 12);
  159. cout<<"\n________________________________________________________________________________\n";
  160. SetConsoleTextAttribute(hConsole, 15);
  161. cout<<"\tSifra: "<<a.sifra<<endl;
  162. cout<<"\tVrsta: "<<a.vrsta<<endl;
  163. cout<<"\tNaziv: "<<a.naziv<<endl;
  164. cout<<"\tCijena: "<<a.cijena<<endl;
  165. cout<<"\tDatum: "<<a.date.d<<".";
  166. cout<<a.date.m<<"."<<a.date.y<<".\n";
  167. SetConsoleTextAttribute(hConsole, 12);
  168. cout<<"\n________________________________________________________________________________\n";
  169. SetConsoleTextAttribute(hConsole, 15);
  170. p = NextL(p,list);
  171. }
  172. }
  173.  
  174. //_______________________________________________________________________________________________
  175. void spoji(int truble, int wub_wub, int buble) {
  176. int I=truble, J=buble+1, K=0;
  177. elem *b = new elem [buble-truble+1];
  178. elem prvi,drugi;
  179.  
  180. while(I<=wub_wub && J<=buble){
  181.  
  182. prvi=RetrieveL(I,list);
  183. drugi=RetrieveL(J,list);
  184.  
  185. if(prvi.cijena > drugi.cijena) {
  186. b[K++]=prvi;
  187. I++;
  188. }
  189.  
  190. else if(prvi.cijena < drugi.cijena){
  191. b[K++]=drugi;
  192. J++;
  193. }
  194. else if(strcmp(prvi.naziv,drugi.naziv)==-1) {
  195. b[K++]=drugi;
  196. J++;
  197. }
  198. else {
  199. b[K++]=prvi;
  200. I++;
  201. }
  202. }
  203.  
  204. if(I>wub_wub) while(J<=buble) {
  205. drugi=RetrieveL(J,list);
  206. b[K++] = drugi;
  207. J++;
  208. }
  209.  
  210. else while(I<=wub_wub) {
  211. prvi=RetrieveL(I,list);
  212. b[K++] = prvi;
  213. I++;
  214. }
  215.  
  216. for(int I=0;I<=buble-truble;I++)Return(b[I],truble+I,list);
  217.  
  218. delete [] b;
  219. }
  220.  
  221. //_______________________________________________________________________________________________
  222. void msort(int truble, int buble) {
  223. if(truble<buble) {
  224. int wub_wub=(truble+buble)/2;
  225. msort(truble,wub_wub);
  226. msort(wub_wub+1,buble);
  227. spoji(truble,buble,wub_wub);
  228. }
  229. }
  230.  
  231. //_______________________________________________________________________________________________
  232. void msort() {
  233. msort(0,br_elem-1);
  234. }
  235.  
  236. int main() {
  237. HANDLE hConsole;
  238. hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  239.  
  240. SetConsoleTextAttribute(hConsole, 12);
  241. cout<<"\n\t\t\t\t N";
  242. SetConsoleTextAttribute(hConsole, 11);
  243. cout<<"Y";
  244. SetConsoleTextAttribute(hConsole, 10);
  245. cout<<"A";
  246. SetConsoleTextAttribute(hConsole, 15);
  247. cout<<"N ";
  248. SetConsoleTextAttribute(hConsole, 12);
  249. cout<<"C";
  250. SetConsoleTextAttribute(hConsole, 11);
  251. cout<<"O";
  252. SetConsoleTextAttribute(hConsole, 10);
  253. cout<<"R";
  254. SetConsoleTextAttribute(hConsole, 15);
  255. cout<<"P \n";
  256. //_____________________________START____________________________________________________________
  257. int choice=9001;
  258. InitL(list);
  259. do {
  260.  
  261.  
  262. HANDLE hConsole;
  263. hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  264.  
  265.  
  266.  
  267. // cout<<"\n\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<Izbornik>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>";
  268. SetConsoleTextAttribute(hConsole, 12);
  269. cout<<"\n\n_____________________________________Izbornik___________________________________";
  270. SetConsoleTextAttribute(hConsole, 15);
  271. cout<<"\n\t | [1] Dodaj zivotinju. |";
  272. cout<<"\n\t | [2] Ispis od zadnje dodane zivotinje do prvo dodane. |";
  273. cout<<"\n\t | [3] Ispis dodanih zivotinja koje su dostavljene nakon 23.9.2012. |";
  274. cout<<"\n\t | [4] Brisanje unesenih zivotinja [naziv]. |";
  275. cout<<"\n\t | [5] Brisanje unesenih zivotinja [vrsta]. |";
  276. cout<<"\n\t | [6] Silazno sortiranje [naziv/cijena]. |";
  277. cout<<"\n\t | [7] Clear screen. (brisanje prethodnog ispisa sa ekrana) |";
  278. cout<<"\n\t | [0] Zatvaranje programa. |";
  279. cout<<"\n\t |___________________________________________________________________|";
  280. SetConsoleTextAttribute(hConsole, 12);
  281. cout<<"\n________________________________________________________________________________\n";
  282. SetConsoleTextAttribute(hConsole, 15);
  283. cout<<"\n\t Izbor[0-7]:";
  284. cin>>choice;
  285. cout<<"\n\n";
  286.  
  287. switch(choice) {
  288. //_______________________________________________________________________________________________
  289. case 1:
  290. if(spawn_new_creature())
  291. {
  292. SetConsoleTextAttribute(hConsole, 10);
  293. cout<<"\tZapis je uspjesno dodan.\n";
  294. SetConsoleTextAttribute(hConsole, 15);
  295. }
  296. else
  297. { SetConsoleTextAttribute(hConsole, 12);
  298. cout<<"\t<<Error!>>\n\t";
  299. SetConsoleTextAttribute(hConsole, 15);
  300. cout<< "Pogresan unos!";
  301. }
  302. break;
  303. //_______________________________________________________________________________________________
  304. case 2:last_to_st();
  305.  
  306. break;
  307. //_______________________________________________________________________________________________
  308. case 3:after_date();
  309.  
  310. break;
  311. //_______________________________________________________________________________________________
  312. case 4:
  313. if(del_name()) {
  314.  
  315. SetConsoleTextAttribute(hConsole, 10);
  316. cout<<"\n\tTrazena zivotinja je obrisana.\n\n";
  317. SetConsoleTextAttribute(hConsole, 15);
  318. output();
  319. }
  320. else
  321. { SetConsoleTextAttribute(hConsole, 12);
  322. cout<<"\t<<Error!>>\n\t";
  323. SetConsoleTextAttribute(hConsole, 15);
  324. cout<<"Trazena zivotinja nije obrisana.\n\tZivotinja pod tim nazivom ne postoji u zapisu.\n\n";
  325. }
  326. break;
  327. //_______________________________________________________________________________________________
  328. case 5:
  329. if(del_spi()) {
  330.  
  331. SetConsoleTextAttribute(hConsole, 10);
  332. cout<<"\n\tTrazena vrsta je obrisana.\n\n";
  333. SetConsoleTextAttribute(hConsole, 15);
  334. output();
  335. }
  336. else
  337. { SetConsoleTextAttribute(hConsole, 12);
  338. cout<<"\t<<Error!>>\n\t";
  339. SetConsoleTextAttribute(hConsole, 15);
  340. cout<<"Trazena vrsta nije obrisana.\n\tVrsta ne postoji u zapisu..\n\n";
  341. }
  342. break;
  343.  
  344. //_______________________________________________________________________________________________
  345. case 6:
  346. msort();
  347. output();
  348.  
  349. break;
  350.  
  351. case 7:
  352. system("cls");
  353.  
  354. break;
  355. }
  356. }while(choice!=0);
  357. DeleteAll(list);
  358.  
  359. system("pause");
  360. return 0;
  361. }
  362.  
  363.  
  364. //made by Mugo

URL: Zadatak1_Glavni dio programa

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.