Posted By

itutavac on 11/12/12


Tagged

lista program funkcije zaglavlja


Versions (?)

main.cpp


 / Published in: C++
 

Datoteka kod koje se nalazi main funkcija. Glavna datoteka kako bi se pokrenuo program

  1. #include <iostream>
  2. #include <string>
  3. //#include "lista_polje.h"
  4. #include "lista_pokazivac.h"
  5.  
  6. using namespace std;
  7.  
  8. int n = 0;
  9.  
  10. bool AddNewElement(_list *list)
  11. {
  12. _tempList temp;
  13. cin.clear();
  14. cin.ignore();
  15. cout<<"Naziv: ";
  16. getline(cin,temp.naziv);
  17. cout<<"Vrsta: ";
  18. getline(cin,temp.vrsta);
  19. cout<<"Datum: ";
  20. getline(cin,temp.datum);
  21. cout<<"Cijena: ";
  22. cin>>temp.cijena;
  23. temp.sifra = n;
  24. n++;
  25. if(InsertL(temp,EndL(list),list))
  26. return true;
  27. n--;
  28. return false;
  29. }
  30.  
  31. bool Print(_list *list)
  32. {
  33. if (list->next == 0)
  34. return false;
  35. List last = EndL(list); // zbog typdef svejedno dal _list* ili int
  36. _tempList search;
  37. while (last != FirstL(list))
  38. {
  39. last = PreviousL(last,list);
  40. search = RetrieveL(last,list);
  41. cout<<"Sifra: "<<search.sifra<<endl;
  42. cout<<"Naziv: "<<search.naziv<<endl;
  43. cout<<"Vrsta: "<<search.vrsta<<endl;
  44. cout<<"Datum: "<<search.datum<<endl;
  45. cout<<"Cijena: "<<search.cijena<<endl<<endl;
  46. }
  47. return true;
  48. }
  49.  
  50. bool Print1(_list *list) // format datuma mora biti XX.XX.XXXX
  51. {
  52. if (list->next == 0)
  53. return false;
  54. List index = FirstL(list);
  55. _tempList search;
  56. string buff;
  57. string temp;
  58. bool print = false;
  59. int nPrinted = 0;
  60. while (index != EndL(list))
  61. {
  62. search.datum.clear();
  63. buff.clear();
  64. temp.clear();
  65. search = RetrieveL(index,list);
  66. index = NextL(index,list);
  67. // XX.XX.XXXX
  68. // 0123456789
  69. for (int i = 6;i<10;i++)
  70. {
  71. buff = search.datum[i];
  72. temp+=buff;
  73. }
  74. if (temp>"2012")
  75. print = true;
  76. else if (temp == "2012")
  77. {
  78. buff.clear();
  79. temp.clear();
  80. for (int i = 3;i<5;i++)
  81. {
  82. buff = search.datum[i];
  83. temp+=buff;
  84. }
  85. if (temp>"09")
  86. print = true;
  87. else if ( temp == "09")
  88. {
  89. buff.clear();
  90. temp.clear();
  91. for (int i = 0;i<2;i++)
  92. {
  93. buff = search.datum[i];
  94. temp+=buff;
  95. }
  96. if (temp>"23")
  97. print = true;
  98. }
  99. }
  100. if (print)
  101. {
  102. cout<<"Sifra: "<<search.sifra<<endl;
  103. cout<<"Naziv: "<<search.naziv<<endl;
  104. cout<<"Vrsta: "<<search.vrsta<<endl;
  105. cout<<"Datum: "<<search.datum<<endl;
  106. cout<<"Cijena: "<<search.cijena<<endl<<endl;
  107. nPrinted++;
  108. }
  109. print = false;
  110. }
  111. cout<<"Ukupan broj: "<<nPrinted<<endl;
  112. return true;
  113. }
  114.  
  115. bool Delete(_list *list)
  116. {
  117. string temp;
  118. cout<<"Unesite naziv zivotinje koju zelite izbrisati: ";
  119. cin.clear();
  120. cin.ignore();
  121. getline(cin,temp);
  122. if(DeleteL(LocateL(temp,list),list))
  123. return true;
  124. cout<<"Krivo uneseni naziv ! "<<endl;
  125. return false;
  126. }
  127.  
  128. bool DeleteVrsta(_list *list)
  129. {
  130. string temp;
  131. cout<<"Unesite naziv vrste koju zelite izbrisati: ";
  132. cin>>temp;
  133. List first = FirstL(list);
  134. _tempList search;
  135. bool deleted = false;
  136. while (first != EndL(list))
  137. {
  138. search = RetrieveL(first,list);
  139. if (search.vrsta == temp)
  140. {
  141. DeleteL(first,list);
  142. first = FirstL(list);
  143. deleted = true;
  144. }
  145. else
  146. first = NextL(first,list);
  147. }
  148. if (deleted)
  149. return true;
  150. cout<<"Krivo unesena vrsta ! "<<endl;
  151. return false;
  152. }
  153.  
  154. void MergeSort2(_tempList *tempList,int tempN);
  155. void MergeSort2(_tempList *tempList,int i,int j);
  156. void Merge2(_tempList *tempList,int i,int k,int j);
  157.  
  158. bool Sort2(_list *list)
  159. {
  160. int tempN = 0;
  161. List searchTempN = FirstL(list);
  162. while (searchTempN != EndL(list))
  163. {
  164. searchTempN = NextL(searchTempN,list);
  165. tempN++;
  166. }
  167. _tempList *tempList = new _tempList[tempN];
  168. List search = FirstL(list);
  169. _tempList temp;
  170. int i = 0;
  171. while (search != EndL(list))
  172. {
  173. temp = RetrieveL(search,list);
  174. tempList[i] = temp;
  175. i++;
  176. search = NextL(search,list);
  177. }
  178. MergeSort2(tempList,tempN);
  179. for (int i = 0; i< tempN;i++)
  180. {
  181. cout<<"Sifra: "<<tempList[i].sifra<<endl;
  182. cout<<"Naziv: "<<tempList[i].naziv<<endl;
  183. cout<<"Vrsta: "<<tempList[i].vrsta<<endl;
  184. cout<<"Datum: "<<tempList[i].datum<<endl;
  185. cout<<"Cijena: "<<tempList[i].cijena<<endl<<endl;
  186. }
  187. return true;
  188. }
  189.  
  190. void Merge2(_tempList *tempList,int i,int k,int j)
  191. {
  192. int I = i, J = k+1, K = 0;
  193. _tempList *tempArray = new _tempList [j-i+1];
  194. while ( I <= k && J <= j)
  195. {
  196. if (tempList[I].cijena > tempList[J].cijena)
  197. tempArray[K++] = tempList[I++];
  198. else if (tempList[I].cijena == tempList[J].cijena)
  199. {
  200. if (tempList[I].naziv > tempList[J].naziv)
  201. tempArray[K++] = tempList[I++];
  202. else
  203. tempArray[K++] = tempList[J++];
  204. }
  205. else
  206. tempArray[K++] = tempList[J++];
  207. }
  208. if ( I > k)
  209. while ( J <= j)
  210. tempArray[K++] = tempList[J++];
  211. else
  212. while ( I <= k)
  213. tempArray[K++] = tempList[I++];
  214. for (int l = 0; l <= j-i; l++)
  215. tempList[i+l] = tempArray[l];
  216. delete []tempArray;
  217. }
  218.  
  219. void MergeSort2(_tempList *tempList,int i,int j)
  220. {
  221. if (i<j)
  222. {
  223. int k = (i+j)/2;
  224. MergeSort2(tempList,i,k);
  225. MergeSort2(tempList,k+1,j);
  226. Merge2(tempList,i,k,j);
  227. }
  228. }
  229.  
  230. void MergeSort2(_tempList *tempList,int tempN)
  231. {
  232. MergeSort2(tempList,0,tempN-1);
  233. }
  234.  
  235. int main ()
  236. {
  237. _list *list = new _list;
  238. InitL(list);
  239. int choice = 0;
  240. do
  241. {
  242. cout<<"1. Dodaj novi element\n";
  243. cout<<"2. Ispis liste\n";
  244. cout<<"3. Ispis zivotinja dostavljenih nakon 23.09.2012.\n";
  245. cout<<"4. Brisanje elementa\n";
  246. cout<<"5. Brisanje vrste\n";
  247. cout<<"6. Merge sort\n";
  248. cout<<"9. Kraj programa\n";
  249. cin>>choice;
  250. switch (choice)
  251. {
  252. case 1:
  253. if(!AddNewElement(list))
  254. cout<<"Error\n";
  255. break;
  256. case 2:
  257. if(!Print(list))
  258. cout<<"Error\n";
  259. break;
  260. case 3:
  261. if(!Print1(list))
  262. cout<<"Error\n";
  263. break;
  264. case 4:
  265. if (!Delete(list))
  266. cout<<"Error\n";
  267. break;
  268. case 5:
  269. if (!DeleteVrsta(list))
  270. cout<<"Error\n";
  271. break;
  272. case 6:
  273. if (!Sort2(list))
  274. cout<<"Error\n";
  275. break;
  276. }
  277. }while(choice != 9);
  278. DeleteAllL(list);
  279. system("pause");
  280. return 0;
  281. }

Report this snippet  

You need to login to post a comment.