模擬銀行排隊時間—4佇列

酷行僧發表於2019-05-12
//銀行排隊


#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<iostream>

typedef struct nima{
int go;
int type;   
}event;

typedef struct hh{
    event thing;
    int lenght;
    struct hh* next;
}evlist;

typedef struct oo{
    int arrtiem;
    int durtime;
}qelem;

typedef struct kk{
    qelem tt;
    struct kk*next; 

}data,*queptr;

typedef struct {

     queptr front;
     queptr rear;
     int lenght;
}linkque;

void fistarry(linkque &fist )
{
    fist.front = fist.rear = (queptr)malloc(sizeof(data));
    if(!fist.front)exit(0);
    fist.front->next = NULL;

}
void enarry(linkque &fist,int go,int dur)
{
    data*p  = (data*)malloc(sizeof(data));
    p->tt.arrtiem = go;
    p->tt.durtime = dur;
    p->next = NULL;
    if(!p){
        printf("error
");
        exit(0);
    }   
    fist.rear->next = p;
    fist.rear = p;
    fist.lenght++;

}

int minimum(linkque fist[])
{int min =fist[0].lenght;
    int k;
    for(int i = 0;i<4;i++)
    {
    if(min>fist[i].lenght){
        min = fist[i].lenght;
        k = i;} 
    }
    return k;
}


void delarry(linkque& fist,qelem *cumter)
{data*k;
    if(fist.lenght>0)
    {k=fist.front->next;
    *cumter =k->tt ;
    fist.front->next = k->next;
    if(fist.rear == k)fist.rear = fist.front;
    free(k);
    fist.lenght--;
    }
}


void getarry(linkque fist,qelem*cumter)
{
    printf("%d
",fist.lenght);
    *cumter = fist.front->next->tt;
}


//事件 
void init(evlist**fist)
{
    *fist =(evlist*)malloc(sizeof(evlist));
    (*fist)->lenght = 0;
    (*fist)->next = NULL;

}


int diskong(evlist*fist)
{
    if(fist->lenght == 0)return 1;
    else return 0;
}


int cmp(event i,event j)
{
 int ii = i.go;
 int jj = j.go;
    if(ii>=jj)return 1;
     else   
    return 0;   
}


void insert(evlist* fist,event en,int(*cmpen)(event  i,event j))
{
    int j,woca;
    woca = 0;
    evlist*p = (evlist*)malloc(sizeof(evlist));
    p->thing = en;
        if(fist->next == NULL)
        {   fist->next = p;
        p->next = NULL;

        }
    else{
        evlist*key = fist;
        evlist*q = fist->next;
              for(;q;q= q->next)
            {
            j = (*cmpen)(p->thing,en);
            if(j == 1)
            {   
                woca = 1;
                p->next = q;
                key->next = p;break;
            }
            key = q;
            }

        if(woca == 0){
                key->next = p;
                p->next = NULL;     
            }

    }   
    fist->lenght++; 

}


void sunji(int* dur,int *wait)
{
    *dur = 10000;
    *wait = ;
}

evlist*shijian;
event ee;
qelem cc;
int tot,ccnumber;
linkque dui[4];


void openforday()
{
    tot = 0;ccnumber = 0;
    init(&shijian);
    printf("dad
");    
    ee.go = 0;ee.type = 0;
    insert(shijian,ee,cmp);


    for(int i= 0;i<4;i++)
    {
    fistarry(dui[i]);
    }   
}

void custarr()
{  int i,j,t;
    event ll;
    event ll2;
    ccnumber++;
    sunji(&i,&j);
    t = ee.go+j;
    if(t<RAND_MAX%100000){
    ll2.go = t;
    ll2.type = 0;   
    insert(shijian,ll2,cmp);

    }
    int ii = minimum(dui);
    enarry(dui[ii],ee.go,i);
    if(dui[ii].lenght ==1){
        ll.go = ee.go+i;
        ll.type = ii;
        insert(shijian,ll,cmp);
        printf("1   %d
",shijian->lenght);
    }   
}


void custdet()
{
    int i = ee.type;
    event ll;
    delarry(dui[i],&cc);
    tot += ee.go-cc.arrtiem;
    if(dui[i].lenght!=0)
    {
        getarry(dui[i],&cc);
        ll.go = ee.go+cc.durtime;
        ll.type = i;
        insert(shijian,ll,cmp);
        printf("2     %d
",shijian->lenght);
    }
}


void gethead(evlist*fist,event*en)
{   
    evlist* p;
    if(fist->next!=NULL)
    p = fist->next;
    *en = p->thing;
    fist->next = p->next;
    free(p);
    fist->lenght--;

}


int main()
{ 
    srand(time(0));
    openforday();printf("%d     
",shijian->lenght);   
    while(!diskong(shijian))
    {///printf("dadad");    
        gethead(shijian ,&ee);
        if(ee.type  == 0)
        {
            custarr();//printf("ddd
");
        }
        else
            custdet();

        printf("%d     
",shijian->lenght);
        printf("%d  %d  %d  %d
",dui[0].lenght,dui[1].lenght,dui[2].lenght,dui[3].lenght);

    }
    printf("%d     %d     %f
",tot,ccnumber,(float)tot/ccnumber);
    return 0;
}

相關文章