Revision: 53882
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at December 3, 2011 05:05 by bstivic
Initial Code
//Implementacija reda pomocu polja
struct tpacijent{
int x;
int y;
int prioritet;
};
struct tred{
tpacijent svi[10000];
int f,r;
};
typedef struct tred Queue;
typedef struct tpacijent element;
tred* InitQ(tred *Q){
Q = new tred;
Q->f = 0;
Q->r = 9999;
return Q;
}
int AddOne(int n) {
return ((n+1)%10000);
}
element FrontQ(tred *Q){
return Q->svi[Q->f];
}
void EnQueueQ(element x, tred *Q){
Q->r=AddOne(Q->r);
Q->svi[Q->r] = x;
}
void DeQueueQ(tred *Q){
Q->f=AddOne(Q->f);
}
bool IsEmptyQ(tred *Q){
if(AddOne(Q->r)==Q->f)
return true;
return false;
}
//implementacija pomocu pokazivaca
struct tpacijent{
int x;
int y;
int prioritet;
};
struct el{
tpacijent svi;
el *s;
};
struct tred{
el *f,*r;
};
typedef struct tpacijent element;
typedef struct tred Queue;
tred* InitQ(tred *Q){
el *novi=new el;
Q = new tred;
novi->s=NULL;
Q->r=novi;
Q->f=novi;
return Q;
}
tpacijent FrontQ(tred *Q){
return ((Q->f)->s)->svi;
}
void EnQueueQ(element x, tred *Q){
el *novi=new el;
novi->svi = x;
novi->s=NULL;
Q->r->s=novi;
Q->r=novi;
}
void DeQueueQ(tred *Q){
el *brisi = Q->f;
Q->f = brisi->s;
delete brisi;
}
bool IsEmptyQ(tred *Q){
if(Q->r==Q->f)
return true;
return false;
}
//main
#include <iostream>
//#include "i_polje.h"
#include "i_pokazivaci.h"
using namespace std;
int n;
unsigned int vrijeme;
tpacijent polje[1000];
void gen(){
cout<<"Koliko parova zelite generirati? ";
cin>>n;
srand(time(0));
for(int i=0;i<n;i++){
polje[i].x=rand()%10000+1;
polje[i].y=rand()%10000+1;
polje[i].prioritet=rand()%4+1;
}
}
void pri(tred *Q){
tred *Q1=InitQ(Q1);
tpacijent spremnik[1000];
int br=1,br1=n;
int f=0;
while(!IsEmptyQ(Q)){
spremnik[f]=FrontQ(Q);
DeQueueQ(Q);
f++;
}
for (int m = 1; m < f; m++) { //Sortiranje umetanjem
int j = m-1;
tpacijent pom = spremnik[m];
while (j >= 0 && spremnik[j].prioritet> pom.prioritet)
spremnik[j+1] = spremnik[j--];
spremnik[j+1] = pom;
}
int m=0;
while(m<f){
EnQueueQ(spremnik[m],Q);
m++;
}
}
void ispis_red(tred *Q,bool k=false){
tpacijent temp;
int i=1;
tred *Q1=InitQ(Q1);
cout<<"-------<Red u cekaonici>-------\n"
<<"|.br........Xi.......Yi.......(p)|\n\n";
while(!IsEmptyQ(Q)){
temp=FrontQ(Q);
cout<<i++<<"\t"<<temp.x<<"\t"<<temp.y;
if(k) cout<<" "<<temp.prioritet<<endl;
else cout<<endl;
DeQueueQ(Q);
EnQueueQ(temp,Q1);
}
cout<<"\n\n";
while(!IsEmptyQ(Q1)){
temp=FrontQ(Q1);
DeQueueQ(Q1);
EnQueueQ(temp,Q);
}
system("pause");
system("cls");
}
void ispis_ord(tpacijent novi,bool k=false){
cout<<"\n-------<Ordinacija>-------\n"
<<"........X........Y.........(p)\n"
<<"\t"<<novi.x<<"\t"<<novi.y;
if(k) cout<<" "<<novi.prioritet<<endl;
else cout<<endl;
}
void b(tred *Q){ //opcija b, bez prioriteta
tpacijent pacijent;
vrijeme=0;
int i=0;
if(!n){
cout<<"Prvo generirajte polja!!\n\n";
return;
}
while(i<n || !IsEmptyQ(Q)){
if(i==0){
pacijent=polje[i];
vrijeme+=pacijent.y;
ispis_ord(pacijent);
ispis_red(Q);
i++;
}//if
else if(i<n){
if(pacijent.y<polje[i].x){
pacijent.y=pacijent.y-polje[i].x;
EnQueueQ(polje[i],Q);
i++;
}//if
else if(pacijent.y<polje[i].x){
polje[i].x=polje[i].x-pacijent.y;
if(IsEmptyQ(Q)){
pacijent=polje[i++];
}//if2
else{
pacijent=FrontQ(Q);
DeQueueQ(Q);
}//else2
vrijeme+=pacijent.y;
ispis_ord(pacijent);
ispis_red(Q);
}//else if
else{
EnQueueQ(polje[i],Q);
i++;
pacijent=FrontQ(Q);
DeQueueQ(Q);
ispis_ord(pacijent);
ispis_red(Q);
vrijeme+=pacijent.y;
}//else
}
else{
pacijent=FrontQ(Q);
DeQueueQ(Q);
ispis_ord(pacijent);
ispis_red(Q);
vrijeme+=pacijent.y;
}//else
}//while
cout<<"\nKraj\n\n";
}//kraj opcije b, bez prioriteta!
void c(tred *Q){ //opcija c, sa prioritetima
tpacijent pacijent;
vrijeme=0;
int i=0;
if(!n){
cout<<"Prvo generirajte polja!!\n\n";
return;
}
while(i<n || !IsEmptyQ(Q)){
if(i==0){
pacijent=polje[i];
ispis_ord(pacijent,true);
ispis_red(Q,true);
i++;
vrijeme+=pacijent.y;
}//if
else if(i<n){
if(pacijent.y>polje[i].x){
pacijent.y=pacijent.y-polje[i].x;
EnQueueQ(polje[i],Q);
pri(Q);
i++;
}//if
else if(pacijent.y<polje[i].x){
polje[i].x=polje[i].x-pacijent.y;
if(IsEmptyQ(Q)){
pacijent=polje[i++];
}//if2
else{
pacijent=FrontQ(Q);
DeQueueQ(Q);
}//else2
ispis_ord(pacijent,true);
ispis_red(Q,true);
}//else if
else{
EnQueueQ(polje[i],Q);
pri(Q);
i++;
pacijent=FrontQ(Q);
DeQueueQ(Q);
ispis_ord(pacijent,true);
ispis_red(Q,true);
}//else
vrijeme+=pacijent.y;
}
else{
pacijent=FrontQ(Q);
DeQueueQ(Q);
ispis_ord(pacijent,true);
ispis_red(Q,true);
vrijeme+=pacijent.y;
}//else
}//while
cout<<"\nKraj\n\n";
}//kraj opcije c, sa prioritetima!
void v(){
cout<<"Vrijeme potrebno za obradu svih pacijenata u ordinaciji\n(koristeno u zadnjoj simulaciji)\niznosi "
<<vrijeme<<" jedinica vremena\n\n";
}
int main(){
int u;
tred *Q = InitQ(Q);
do{
cout<<"1. Generiranje parova slucajnih brojeva\n"
<<"2. Simulacija reda u ordinaciji\n"
<<"3. Simulacija reda sa prioritetima\n"
<<"4. Ukupno vrijeme izvrsavanja b ili c\n"
<<"9. Izlaz iz programa\n\n";
cin>>u;
switch(u){
case 1:gen();break;
case 2:b(Q);break;
case 3:c(Q);break;
case 4:v();
}
}while(u!=9);
return 0;
}
Initial URL
Initial Description
Red u ordinaciji, strukture podataka
Initial Title
Red u ordinaciji
Initial Tags
Initial Language
C++