1. 程式人生 > >離散事件模擬——銀行佇列問題//資料結構作業

離散事件模擬——銀行佇列問題//資料結構作業

//銀行——離散事件模擬
#include<stdio.h>
#include<stdlib.h>

struct event{
    int type,occurtime,money;//type為0,到達;為1,離開視窗;occurtime為0到600;
}ev1,ev2;
struct eventnode{
    int type;
    int occurtime;
    int money;
    struct eventnode *next;
} *evlist;
struct queuenode{
    int money,duringtime,arrivetime;//arrivetime用於最後計算總時間
    struct queuenode *next;
};
struct Queue{
    struct queuenode *front;
    struct queuenode *rear;
}queue1,queue2;
int totaltime,customernum,closetime,timemark,totalmoney;



void evlistinsert(struct event temp){
    struct eventnode *p1,*p2,*p3;
    p1=(struct eventnode *)malloc(sizeof(struct eventnode));
    p1->type=temp.type;
    p1->occurtime=temp.occurtime;
    p1->money=temp.money;
    p1->next=NULL;
    if(!evlist){
        evlist=p1;
    }
    else{
        p2=evlist;
    while(p2->next)p2=p2->next;
    p2->next=p1;
    }

}

void arrive(){
    int duringtime,intertime;
    ++customernum;

    duringtime=rand()%30+1;
    if(ev1.money+totalmoney>=0){//能滿足
        struct queuenode *p;
        p=(struct queuenode*)malloc(sizeof(struct queuenode));
        queue1.rear->next=p;
        queue1.rear=p;
        p->next=NULL;
        p->money=ev1.money;
        p->duringtime=duringtime;
        p->arrivetime=ev1.occurtime;//加入佇列1
        printf("顧客%d\n到達時間:%d\n業務用時:%d\n金額:%d\n",customernum,ev1.occurtime,duringtime,ev1.money);

        ev2.type=1;
        ev2.occurtime=timemark+duringtime;
        timemark=ev2.occurtime;
        ev2.money=ev1.money;
        evlistinsert(ev2);//離開事件插入事件表
        totaltime+=ev2.occurtime-ev1.occurtime;

    }
    else{
        struct queuenode *p;
        p=(struct queuenode *)malloc(sizeof(struct queuenode));
        queue2.rear->next=p;
        queue2.rear=p;
        p->next=NULL;
        p->money=ev1.money;
        p->duringtime=duringtime;
        p->arrivetime=ev1.occurtime;//加入佇列2
        printf("顧客%d\n到達時間:%d\n欲交易金額:%d,不能滿足,進入佇列2\n",customernum,ev1.occurtime,ev1.money);
        printf("-----------------------------------------------\n");
    }


    intertime=rand()%10+1;
    ev2.type=0;
    ev2.occurtime=ev1.occurtime+intertime;
    ev2.money=rand()%10000-5000;
    if(ev2.occurtime>timemark)timemark=ev2.occurtime;
    if(ev2.occurtime<closetime&&timemark<closetime)evlistinsert(ev2);//下一個顧客


}

void leave(){
    struct queuenode *p;
    if(ev1.type==1){
        p=queue1.front->next;
        queue1.front->next=p->next;
        if(!queue1.front->next)queue1.rear=queue1.front;
        free(p);//刪除佇列1頭的顧客
        totalmoney+=ev1.money;
        printf("離開時間:%d分鐘\n銀行金額:%d\n",ev1.occurtime,totalmoney);
        printf("-----------------------------------------------\n");

    }
    if(ev1.money<0)return;

    struct queuenode *temp=queue2.front->next;
    if(!temp)return;
    int k=0,flag;
    while(temp!=NULL){
        k++;
        temp=temp->next;
    }
    flag=k;
    k=0;
    int temptime=ev1.occurtime,tempmoney;
    temp=queue2.front->next;
    while(k<flag){
        if(totalmoney+temp->money>=0){
            tempmoney=temp->money;
            totalmoney+=temp->money;
            temptime+=temp->duringtime;
            totaltime+=temptime-temp->arrivetime;
            queue2.front->next=temp->next;
            if(!queue2.front->next)queue2.rear=queue2.front;
            temp=queue2.front->next;
            free(p);//刪除佇列2頭的顧客
            printf("佇列2有顧客離開了,時間為%d,交易金額為%d,銀行金額為%d\n",temptime,tempmoney,totalmoney);
            printf("-----------------------------------------------\n");
        }
        else if(temp->next){
                queue2.front->next=temp->next;
                temp->next=NULL;
                queue2.rear->next=temp;
                queue2.rear=temp;
                temp=queue2.front->next;//隊頭掉到隊尾
                }
        k++;
        }
        if(temptime>timemark)timemark=temptime;
    }



void openforday(){


    queue1.front=(struct queuenode *)malloc(sizeof(struct queuenode));
    queue1.rear=queue1.front;
    queue1.rear->next=NULL;
    queue2.front=(struct queuenode *)malloc(sizeof(struct queuenode));
    queue2.rear=queue2.front;
    queue2.rear->next=NULL;


    totaltime=0;
    customernum=0;
    totalmoney=1000;
    closetime=1000;

    evlist=(struct eventnode*)malloc(sizeof(struct eventnode));
    evlist->occurtime=0;
    evlist->type=0;
    evlist->money=5000;

}

int main(){
    struct eventnode *p;
        srand(time(NULL));
    openforday();//初始化

    while(evlist){
        ev1.occurtime=evlist->occurtime;
        ev1.type=evlist->type;
        ev1.money=evlist->money;

        p=evlist;
        evlist=evlist->next;
        free(p);

        if(ev1.type==0)arrive();
        else leave();

    }
    struct queuenode *temp=queue2.front->next;
    while(temp){
        totaltime+=closetime-temp->arrivetime;
        temp=temp->next;
    }
    printf("The average time is %f\n",(float)totaltime/customernum);
}