Posted By


nleskovar on 12/03/11

Tagged


Statistics


Viewed 96 times
Favorited by 0 user(s)

rad lijecnicke ordinacije


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

simulacija rada lijecnicke ordinacije


Copy this code and paste it in your HTML
  1. //red_pokazivac.h
  2.  
  3. #include <cstdlib>
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8.  
  9. struct tpacijent
  10. { int rbp;
  11. int x;
  12. int y;
  13. int z;
  14. };
  15.  
  16. struct t{
  17. tpacijent vrij;
  18. t *sljedeci;
  19. };
  20.  
  21. struct q{
  22. t *front, *rear;
  23. };
  24.  
  25. void initq(q *queue){
  26. queue->front=(t*)malloc(sizeof(t));
  27. queue->front->sljedeci=NULL;
  28. queue->rear=queue->front;
  29. cout<<"Red je inicijaliziran"<<endl;
  30. };
  31.  
  32. bool isemptyq(q*queue){
  33. if(queue->front==queue->rear) return true;
  34. else return false;
  35. };
  36.  
  37. void enqueueq(tpacijent x,q *queue){
  38. t *pom;
  39. pom=new t;
  40. pom->vrij=x;
  41. pom->sljedeci=NULL;
  42. queue->rear->sljedeci=pom;
  43. queue->rear=pom;
  44. };
  45.  
  46. void dequeueq(q*queue){
  47. t *pom;
  48. if(isemptyq(queue)) cout<<"Red je prazan";
  49. else{
  50. pom=queue->front;
  51. queue->front=queue->front->sljedeci;
  52. free(pom);
  53. }
  54. };
  55.  
  56. tpacijent frontq(q *queue){
  57. if(isemptyq(queue)) cout<<"Red je prazan";
  58. else return (queue->front->sljedeci->vrij);
  59. };
  60.  
  61.  
  62. //red_polje.h
  63.  
  64. #include <cstdlib>
  65. #include <iostream>
  66.  
  67. using namespace std;
  68.  
  69. struct tpacijent
  70. { int rbp;
  71. int x;
  72. int y;
  73. int z;
  74. };
  75.  
  76. struct q{
  77. tpacijent vrij[10000];
  78. int front, rear;
  79. };
  80.  
  81. int addone(int i){
  82. return ((i+1)%10000);
  83. };
  84.  
  85. void initq(q*queue){
  86. queue->front=0;
  87. queue->rear=9999; //zadnji el polja
  88. cout<<"red je inicijaliziran"<<endl;
  89. };
  90.  
  91. bool isemptyq(q *queue){
  92. if(addone(queue->rear)==queue->front) return true;
  93. else return false;
  94. };
  95.  
  96. void enqueueq(tpacijent x, q*queue){
  97. if(addone(addone(queue->rear))==(queue->front)) cout<<"red je pun";
  98. else {
  99. queue->rear=addone(queue->rear);
  100. queue->vrij[queue->rear]=x;
  101. }
  102. };
  103.  
  104. void dequeueq(q *queue){
  105. if(isemptyq(queue)) cout<<"red je prazan";
  106. else queue->front=addone(queue->front);
  107. };
  108.  
  109. tpacijent frontq(q*queue){
  110. if(isemptyq(queue)) cout<<"red je prazan";
  111. else return (queue->vrij[queue->front]);
  112. };
  113.  
  114.  
  115.  
  116. //glavni
  117.  
  118. #include <cstdlib>
  119. #include <iostream>
  120. #include <ctime>
  121. #include "red_pokazivac.h"
  122. //#include "red_polje.h"
  123.  
  124. using namespace std;
  125.  
  126. int n;
  127. clock_t time1,time2;
  128. double pocetak(){
  129. time1=clock();
  130. return time1;
  131. };
  132. double kraj(){
  133. time2=clock();
  134. return time2;
  135. };
  136. double proteklo(){
  137. return time2-time1;
  138. };
  139.  
  140. void generiranje(q*pacijenti){
  141. srand(time(0));
  142. cout<<"Koliko parova zelite generirati? (1-10000)"<<endl;
  143. cin>>n;
  144. tpacijent novi;
  145. for(int i=0;i<n;i++){
  146. novi.rbp=i+1;
  147. novi.x=((rand()%10001)/1000)+1;
  148. novi.y=((rand()%10001)/1000)+1;
  149. novi.z=(rand()%4)+1;
  150. enqueueq(novi,pacijenti);
  151. }
  152. };
  153. void primanje(q *pacijenti){
  154. pocetak();
  155. cout<<"Ispis svih pacijenata"<<endl;
  156. tpacijent tekuci;
  157. if(isemptyq(pacijenti)) cout<<"Red je prazan"<<endl;
  158. for(int i=0;i<n;i++){
  159. tekuci=frontq(pacijenti);
  160. dequeueq(pacijenti);
  161. cout<<"pacijent br....."<<tekuci.rbp<<endl;
  162. cout<<"cekanje:........"<<tekuci.x<<" min"<<endl;
  163. cout<<"u ordinaciji:..."<<tekuci.y<<" min"<<endl;
  164. cout<<"*************************"<<endl;
  165. enqueueq(tekuci,pacijenti);
  166. }
  167. kraj();
  168. cout<<"Vrijeme trajanja u sekundama: "<<proteklo()/1000<<endl;
  169. };
  170.  
  171. void modifikacija(q *pacijenti){
  172. pocetak();
  173. tpacijent ord[10000];
  174. int brojac=0;
  175. while(!isemptyq(pacijenti)){
  176. ord[brojac]=frontq(pacijenti);
  177. brojac++;
  178. dequeueq(pacijenti);}
  179.  
  180. bool zamjena=true;
  181. for(int i=n-1;i>0&&zamjena; i--){
  182. zamjena=false;
  183. for(int j=0;j<i;j++){
  184. if(ord[j].z>ord[j+1].z){
  185. tpacijent pom=ord[j];
  186. ord[j]=ord[j+1];
  187. ord[j+1]=pom;
  188. zamjena=true;
  189. }
  190. if(ord[j].z==ord[j+1].z){
  191. if(ord[j].x<ord[j+1].x){
  192. tpacijent pom=ord[j];
  193. ord[j]=ord[j+1];
  194. ord[j+1]=pom;
  195. zamjena=true;
  196. }
  197. }
  198. }
  199. }
  200.  
  201. for(int i=0;i<n;i++) enqueueq(ord[i],pacijenti);
  202.  
  203. cout<<"Stanje reda"<<endl;
  204. tpacijent tekuci;
  205. if(isemptyq(pacijenti)) cout<<"red je prazan"<<endl;
  206. while(!isemptyq(pacijenti)){
  207. tekuci=frontq(pacijenti);
  208. dequeueq(pacijenti);
  209. cout<<"pacijent br....."<<tekuci.rbp<<endl;
  210. cout<<"cekanje:........"<<tekuci.x<<" min"<<endl;
  211. cout<<"u ordinaciji:..."<<tekuci.y<<" min"<<endl;
  212. cout<<"prioritet:......"<<tekuci.z<<endl;
  213. cout<<"***********************"<<endl;
  214. }
  215. enqueueq(tekuci,pacijenti);
  216. kraj();
  217. cout<<"Vrijeme trajanja u sekundama: "<<proteklo()/1000<<endl;
  218.  
  219. }
  220.  
  221. int main()
  222. {
  223. int izbor;
  224. q *pacijenti;
  225. pacijenti=new q;
  226. initq(pacijenti);
  227. do{
  228. cout<<"*********I Z B O R N I K**********: "<<endl;
  229. cout<<"1) generiranje pacijenata"<<endl;
  230. cout<<"2) Primanje pacijenata"<<endl;
  231. cout<<"3) Primanje pacijenata prema prioritetu"<<endl;
  232. cout<<"9) za kraj programa"<<endl;
  233. cin>>izbor;
  234. switch(izbor){
  235. case 1:
  236. generiranje(pacijenti); break;
  237. case 2:
  238. primanje(pacijenti);break;
  239. case 3:
  240. modifikacija(pacijenti);break;
  241. default: break;
  242. }}
  243. while(izbor!=9);
  244. system("PAUSE");
  245. return 0;
  246. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.