Posted By


bstivic on 12/03/11

Tagged


Statistics


Viewed 295 times
Favorited by 0 user(s)

Red u ordinaciji


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

Red u ordinaciji, strukture podataka


Copy this code and paste it in your HTML
  1. //Implementacija reda pomocu polja
  2. struct tpacijent{
  3. int x;
  4. int y;
  5. int prioritet;
  6. };
  7.  
  8. struct tred{
  9. tpacijent svi[10000];
  10. int f,r;
  11. };
  12.  
  13. typedef struct tred Queue;
  14. typedef struct tpacijent element;
  15.  
  16. tred* InitQ(tred *Q){
  17. Q = new tred;
  18. Q->f = 0;
  19. Q->r = 9999;
  20. return Q;
  21. }
  22.  
  23. int AddOne(int n) {
  24. return ((n+1)%10000);
  25. }
  26.  
  27. element FrontQ(tred *Q){
  28. return Q->svi[Q->f];
  29. }
  30.  
  31. void EnQueueQ(element x, tred *Q){
  32. Q->r=AddOne(Q->r);
  33. Q->svi[Q->r] = x;
  34. }
  35.  
  36. void DeQueueQ(tred *Q){
  37. Q->f=AddOne(Q->f);
  38. }
  39.  
  40. bool IsEmptyQ(tred *Q){
  41. if(AddOne(Q->r)==Q->f)
  42. return true;
  43. return false;
  44. }
  45. //implementacija pomocu pokazivaca
  46. struct tpacijent{
  47. int x;
  48. int y;
  49. int prioritet;
  50. };
  51.  
  52. struct el{
  53. tpacijent svi;
  54. el *s;
  55. };
  56.  
  57. struct tred{
  58. el *f,*r;
  59. };
  60.  
  61. typedef struct tpacijent element;
  62. typedef struct tred Queue;
  63.  
  64. tred* InitQ(tred *Q){
  65. el *novi=new el;
  66. Q = new tred;
  67. novi->s=NULL;
  68. Q->r=novi;
  69. Q->f=novi;
  70. return Q;
  71. }
  72.  
  73. tpacijent FrontQ(tred *Q){
  74. return ((Q->f)->s)->svi;
  75. }
  76.  
  77. void EnQueueQ(element x, tred *Q){
  78. el *novi=new el;
  79. novi->svi = x;
  80. novi->s=NULL;
  81. Q->r->s=novi;
  82. Q->r=novi;
  83. }
  84.  
  85. void DeQueueQ(tred *Q){
  86. el *brisi = Q->f;
  87. Q->f = brisi->s;
  88. delete brisi;
  89. }
  90.  
  91. bool IsEmptyQ(tred *Q){
  92. if(Q->r==Q->f)
  93. return true;
  94. return false;
  95. }
  96.  
  97.  
  98. //main
  99. #include <iostream>
  100. //#include "i_polje.h"
  101. #include "i_pokazivaci.h"
  102. using namespace std;
  103. int n;
  104. unsigned int vrijeme;
  105. tpacijent polje[1000];
  106. void gen(){
  107. cout<<"Koliko parova zelite generirati? ";
  108. cin>>n;
  109. srand(time(0));
  110. for(int i=0;i<n;i++){
  111. polje[i].x=rand()%10000+1;
  112. polje[i].y=rand()%10000+1;
  113. polje[i].prioritet=rand()%4+1;
  114. }
  115. }
  116. void pri(tred *Q){
  117. tred *Q1=InitQ(Q1);
  118. tpacijent spremnik[1000];
  119. int br=1,br1=n;
  120. int f=0;
  121. while(!IsEmptyQ(Q)){
  122. spremnik[f]=FrontQ(Q);
  123. DeQueueQ(Q);
  124. f++;
  125. }
  126. for (int m = 1; m < f; m++) { //Sortiranje umetanjem
  127. int j = m-1;
  128. tpacijent pom = spremnik[m];
  129. while (j >= 0 && spremnik[j].prioritet> pom.prioritet)
  130. spremnik[j+1] = spremnik[j--];
  131. spremnik[j+1] = pom;
  132. }
  133.  
  134. int m=0;
  135. while(m<f){
  136.  
  137. EnQueueQ(spremnik[m],Q);
  138. m++;
  139. }
  140. }
  141. void ispis_red(tred *Q,bool k=false){
  142. tpacijent temp;
  143. int i=1;
  144. tred *Q1=InitQ(Q1);
  145.  
  146. cout<<"-------<Red u cekaonici>-------\n"
  147. <<"|.br........Xi.......Yi.......(p)|\n\n";
  148. while(!IsEmptyQ(Q)){
  149. temp=FrontQ(Q);
  150. cout<<i++<<"\t"<<temp.x<<"\t"<<temp.y;
  151. if(k) cout<<" "<<temp.prioritet<<endl;
  152. else cout<<endl;
  153. DeQueueQ(Q);
  154. EnQueueQ(temp,Q1);
  155. }
  156. cout<<"\n\n";
  157.  
  158. while(!IsEmptyQ(Q1)){
  159. temp=FrontQ(Q1);
  160. DeQueueQ(Q1);
  161. EnQueueQ(temp,Q);
  162. }
  163. system("pause");
  164. system("cls");
  165. }
  166. void ispis_ord(tpacijent novi,bool k=false){
  167. cout<<"\n-------<Ordinacija>-------\n"
  168. <<"........X........Y.........(p)\n"
  169. <<"\t"<<novi.x<<"\t"<<novi.y;
  170. if(k) cout<<" "<<novi.prioritet<<endl;
  171. else cout<<endl;
  172. }
  173. void b(tred *Q){ //opcija b, bez prioriteta
  174. tpacijent pacijent;
  175. vrijeme=0;
  176. int i=0;
  177. if(!n){
  178. cout<<"Prvo generirajte polja!!\n\n";
  179. return;
  180. }
  181. while(i<n || !IsEmptyQ(Q)){
  182.  
  183. if(i==0){
  184. pacijent=polje[i];
  185. vrijeme+=pacijent.y;
  186. ispis_ord(pacijent);
  187. ispis_red(Q);
  188. i++;
  189. }//if
  190. else if(i<n){
  191. if(pacijent.y<polje[i].x){
  192. pacijent.y=pacijent.y-polje[i].x;
  193. EnQueueQ(polje[i],Q);
  194. i++;
  195. }//if
  196. else if(pacijent.y<polje[i].x){
  197. polje[i].x=polje[i].x-pacijent.y;
  198.  
  199. if(IsEmptyQ(Q)){
  200. pacijent=polje[i++];
  201. }//if2
  202. else{
  203. pacijent=FrontQ(Q);
  204. DeQueueQ(Q);
  205. }//else2
  206. vrijeme+=pacijent.y;
  207. ispis_ord(pacijent);
  208. ispis_red(Q);
  209. }//else if
  210. else{
  211. EnQueueQ(polje[i],Q);
  212. i++;
  213. pacijent=FrontQ(Q);
  214. DeQueueQ(Q);
  215. ispis_ord(pacijent);
  216. ispis_red(Q);
  217. vrijeme+=pacijent.y;
  218. }//else
  219. }
  220. else{
  221. pacijent=FrontQ(Q);
  222. DeQueueQ(Q);
  223. ispis_ord(pacijent);
  224. ispis_red(Q);
  225. vrijeme+=pacijent.y;
  226. }//else
  227. }//while
  228. cout<<"\nKraj\n\n";
  229. }//kraj opcije b, bez prioriteta!
  230.  
  231. void c(tred *Q){ //opcija c, sa prioritetima
  232. tpacijent pacijent;
  233. vrijeme=0;
  234. int i=0;
  235. if(!n){
  236. cout<<"Prvo generirajte polja!!\n\n";
  237. return;
  238. }
  239.  
  240. while(i<n || !IsEmptyQ(Q)){
  241.  
  242. if(i==0){
  243. pacijent=polje[i];
  244. ispis_ord(pacijent,true);
  245. ispis_red(Q,true);
  246. i++;
  247. vrijeme+=pacijent.y;
  248. }//if
  249. else if(i<n){
  250. if(pacijent.y>polje[i].x){
  251. pacijent.y=pacijent.y-polje[i].x;
  252. EnQueueQ(polje[i],Q);
  253. pri(Q);
  254. i++;
  255. }//if
  256. else if(pacijent.y<polje[i].x){
  257. polje[i].x=polje[i].x-pacijent.y;
  258. if(IsEmptyQ(Q)){
  259. pacijent=polje[i++];
  260. }//if2
  261. else{
  262. pacijent=FrontQ(Q);
  263. DeQueueQ(Q);
  264. }//else2
  265. ispis_ord(pacijent,true);
  266. ispis_red(Q,true);
  267. }//else if
  268. else{
  269. EnQueueQ(polje[i],Q);
  270. pri(Q);
  271. i++;
  272. pacijent=FrontQ(Q);
  273. DeQueueQ(Q);
  274. ispis_ord(pacijent,true);
  275. ispis_red(Q,true);
  276. }//else
  277. vrijeme+=pacijent.y;
  278. }
  279. else{
  280. pacijent=FrontQ(Q);
  281. DeQueueQ(Q);
  282. ispis_ord(pacijent,true);
  283. ispis_red(Q,true);
  284. vrijeme+=pacijent.y;
  285. }//else
  286. }//while
  287. cout<<"\nKraj\n\n";
  288. }//kraj opcije c, sa prioritetima!
  289. void v(){
  290. cout<<"Vrijeme potrebno za obradu svih pacijenata u ordinaciji\n(koristeno u zadnjoj simulaciji)\niznosi "
  291. <<vrijeme<<" jedinica vremena\n\n";
  292. }
  293. int main(){
  294. int u;
  295.  
  296. tred *Q = InitQ(Q);
  297. do{
  298. cout<<"1. Generiranje parova slucajnih brojeva\n"
  299. <<"2. Simulacija reda u ordinaciji\n"
  300. <<"3. Simulacija reda sa prioritetima\n"
  301. <<"4. Ukupno vrijeme izvrsavanja b ili c\n"
  302. <<"9. Izlaz iz programa\n\n";
  303. cin>>u;
  304. switch(u){
  305. case 1:gen();break;
  306. case 2:b(Q);break;
  307. case 3:c(Q);break;
  308. case 4:v();
  309. }
  310.  
  311. }while(u!=9);
  312. return 0;
  313. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.