机场调度

来源:互联网 发布:淘宝旗下99 编辑:程序博客网 时间:2024/05/11 02:21

 机场调度(15分)
成绩: 15 / 折扣: 0.8
在本实验中,需要同学们利用队列实现一个飞机场调度模拟,根据不同的输入参数得到不同的模拟结果。程序运行开始,首先需要输入以下参数:
机场跑道数,飞机降落占用跑道时间(整数), 飞机起飞占用跑道时间(整数)
整个模拟的时间以分钟为单位,从 0 开始,每分钟的开始需要输入:
该分钟要求降落飞机数, 该分钟要求起飞飞机数
机场调度原则是降落优先起飞,在此原则下按来的顺序排队;每驾飞机都有一个编号,要起飞飞机从 1 开始,要降落飞机从 5001 开始;每驾飞机需要等待的时间是从其提要求开始到分配跑道为止;每个跑道都有一个编号(从 1 开始),都可以用来降落和起飞,但同一时间只能被一架飞机占用,占用时间为该飞机降落(起飞)占用跑道时间。
当输入的要求降落飞机数和要求起飞飞机数都小于 0 时,表示机场关闭,不再接受新的请求,但余下没有降落(起飞)的飞机需照常进行。
模拟过程中需要随时输出以下数据:
1. 当前时间 (%4d)
2. 可以降落(起飞)飞机编号(% 04d )、跑道编号(% 02d )
3. 由被占用变为空闲的跑道编号
模拟结束后,程序需输出以下统计结果:
1. 模拟时间(% 4d )
2. 降落平均等待时间(% 4.1f )
3. 起飞平均等待时间(% 4.1f )
4. 每条跑道被占用时间(% 4d )
5. 跑道平均被占用的百分比(% 4.1f , 平均占用时间× 100/ 模拟时间)
例: (下面的黑斜体为输入)
4 3 5
Current Time: 0
1 4
airplane 5001 is ready to land on runway 01
airplane 0001 is ready to takeoff on runway 02
airplane 0002 is ready to takeoff on runway 03
airplane 0003 is ready to takeoff on runway 04
Current Time: 1
0 0
Current Time: 2
0 2
Current Time: 3
runway 01 is free
3 0
airplane 5002 is ready to land on runway 01
Current Time: 4
0 0
Current Time: 5
runway 02 is free
runway 03 is free
runway 04 is free
0 0
airplane 5003 is ready to land on runway 02
airplane 5004 is ready to land on runway 03
airplane 0004 is ready to takeoff on runway 04
Current Time: 6
runway 01 is free
2 4
airplane 5005 is ready to land on runway 01
Current Time: 7
-1 -1
Current Time: 8
runway 02 is free
runway 03 is free
airplane 5006 is ready to land on runway 02
airplane 0005 is ready to takeoff on runway 03
Current Time: 9
runway 01 is free
airplane 0006 is ready to takeoff on runway 01
Current Time: 10
runway 04 is free
airplane 0007 is ready to takeoff on runway 04
Current Time: 11
runway 02 is free
airplane 0008 is ready to takeoff on runway 02
Current Time: 12
Current Time: 13
runway 03 is free
airplane 0009 is ready to takeoff on runway 03
Current Time: 14
runway 01 is free
airplane 0010 is ready to takeoff on runway 01
Current Time: 15
runway 04 is free
Current Time: 16
runway 02 is free
Current Time: 17
Current Time: 18
runway 03 is free
Current Time: 19
runway 01 is free
simulation finished
simulation time: 19
average waiting time of landing: 1.0
average waiting time of takeoff: 4.2
runway 01 busy time: 19
runway 02 busy time: 16
runway 03 busy time: 18
runway 04 busy time: 15
runway average busy time percentage: 89.5%

 

#include <stdio.h>#include <stdlib.h>struct airline{        int id;        int wtime;        int busytime;        struct airline *next;};struct air{        int id;        int type;        int wtime;        struct air *next;};void main(){        int n,time=0,landtime,taketime,landnum,takenum,airlineid=1,landid=5001,takeid=1,i,j,k,or1=1,or2=1,flag;        float wland=0,wtake=0,plane1=0,plane2=0,sum=0,time1,n1;        struct airline *lhead=NULL,*lp,*lq;        struct air *ahead1=NULL,*arear1,*ap1,*aq1,*asearch1,*ahead2=NULL,*arear2,*ap2,*aq2,*asearch2;        scanf("%d %d %d",&n,&landtime,&taketime);        for (i=0;i<n;i++)        {                lp=(struct airline*)malloc(sizeof(struct airline));                lp->busytime=0;                lp->id=airlineid++;                lp->wtime=0;                lp->next=NULL;                if (lhead==NULL) lhead=lq=lp;                else                 {                        lq->next=lp;                        lq=lp;                }        }        while(1)        {                printf("Current Time: %4d\n",time++);                lp=lhead;                while (lp!=NULL)                {                        if (lp->wtime!=0)                        {                                lp->wtime--;                                if (lp->wtime==0)                                {                                        printf("runway %02d is free\n",lp->id);                                }                        }                        lp=lp->next;                }                if (or1>=0 && or2>=0)                {                        scanf("%d %d",&landnum,&takenum);                        or1=landnum;                        or2=takenum;                        plane1+=landnum;                        plane2+=takenum;                }                        for (i=0;i<landnum;i++)                {                        ap1=(struct air*)malloc(sizeof(struct air));                        ap1->id=landid++;                        ap1->type=1;                        ap1->next=NULL;                        if (ahead1 == NULL)                        {                                ahead1=arear1=ap1;                        }                        else                         {                                arear1->next=ap1;                                arear1=ap1;                        }                                }                for (i=0;i<takenum;i++)                {                        ap2=(struct air*)malloc(sizeof(struct air));                        ap2->id=takeid++;                        ap2->type=2;                        ap2->next=NULL;                        if (ahead2 == NULL)                        {                                ahead2=arear2=ap2;                        }                        else                         {                                arear2->next=ap2;                                arear2=ap2;                        }                                }                aq1=asearch1=ahead1;                aq2=asearch2=ahead2;                while (asearch1!=NULL)                {                        lq=lhead;                        flag=0;                        while (lq!=NULL)                        {                                if (lq->wtime == 0)                                {                                        flag=1;                                        printf("airplane %04d is ready to land on runway %02d\n",asearch1->id,lq->id);                                        lq->wtime=landtime;                                        lq->busytime+=landtime;                                        if (ahead1->next!=NULL)                                            ahead1=ahead1->next;                                        else ahead1=NULL;                                        break;                                }                                lq=lq->next;                        }                        if (flag == 0)                        {                                wland++;                        }                        asearch1=asearch1->next;                }                while (asearch2!=NULL)                {                        lq=lhead;                        flag=0;                        while (lq!=NULL)                        {                                if (lq->wtime == 0)                                {                                        flag=1;                                        printf("airplane %04d is ready to takeoff on runway %02d\n",asearch2->id,lq->id);                                        lq->wtime=taketime;                                        lq->busytime+=taketime;                                        if (ahead2->next!=NULL)                                            ahead2=ahead2->next;                                        else ahead2=NULL;                                        break;                                }                                lq=lq->next;                        }                        if (flag == 0)                        {                                wtake++;                        }                        asearch2=asearch2->next;                }                flag=0;                lp=lhead;                while (lp!=NULL)                {                        if (lp->wtime==0)                        {                                flag++;                        }                        else break;                        lp=lp->next;                }                if (ahead1==NULL &&ahead2==NULL && flag==n && or1==-1 && or2==-1)                {                        break;                }        }        time1=time;        n1=n;        printf("simulation finished\n");        printf("simulation time: %4d\n",time-1);        printf("average waiting time of landing: %4.1f\n",(float)(wland/(plane1+1)));        printf("average waiting time of takeoff: %4.1f\n",(float)(wtake/(plane2+1)));        lq=lhead;        while (lq!=NULL)        {                printf("runway %02d busy time: %4d\n",lq->id,lq->busytime);                sum+=lq->busytime;                lq=lq->next;        }        printf("runway average busy time percentage: %4.1f%%\n",sum/n1*100.0/(time1-1));}


 

原创粉丝点击