飞机场调度

在本实验中,需要同学们利用队列实现一个飞机场调度模拟,根据不同的输入参数得到不同的模拟结果。程序运行开始,首先需要输入以下参数:

机场跑道数,飞机降落占用跑道时间(整数), 飞机起飞占用跑道时间(整数)

整个模拟的时间以分钟为单位,从 0 开始,每分钟的开始需要输入:

该分钟要求降落飞机数, 该分钟要求起飞飞机数

机场调度原则是降落优先起飞,在此原则下按来的顺序排队;每驾飞机都有一个编号,要起飞飞机从 1 开始,要降落飞机从 5001 开始;每驾飞机需要等待的时间是从其提要求开始到分配跑道为止;每个跑道都有一个编号(从 1 开始),都可以用来降落和起飞,但同一时间只能被一架飞机占用,占用时间为该飞机降落(起飞)占用跑道时间。

当输入的要求降落飞机数和要求起飞飞机数都小于 0 时,表示机场关闭,不再接受新的请求,但余下没有降落(起飞)的飞机需照常进行。

模拟过程中需要随时输出以下数据:

1. 当前时间 (%4d)

2. 所有从占用变为空闲的跑道编号 (在输入降落、起飞飞机数前输出)

3. 可以降落(起飞)飞机编号(% 04d )、跑道编号(% 02d ) (在输入降落、起飞飞机数后输出)

模拟结束后,程序需输出以下统计结果:

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%

  测试输入关于“测试输入”的帮助 期待的输出关于“期待的输出”的帮助 时间限制关于“时间限制”的帮助 内存限制关于“内存限制”的帮助 额外进程关于“{$a} 个额外进程”的帮助
测试用例 1 以文本方式显示
  1. 1 2 3↵
  2. 1 2↵
  3. 2 1↵
  4. 0 0↵
  5. 0 0↵
  6. 0 0↵
  7. -1 -1↵
以文本方式显示
  1. Current Time:    0↵
  2. airplane 5001 is ready to land on runway 01↵
  3. Current Time:    1↵
  4. Current Time:    2↵
  5. runway 01 is free↵
  6. airplane 5002 is ready to land on runway 01↵
  7. Current Time:    3↵
  8. Current Time:    4↵
  9. runway 01 is free↵
  10. airplane 5003 is ready to land on runway 01↵
  11. Current Time:    5↵
  12. Current Time:    6↵
  13. runway 01 is free↵
  14. airplane 0001 is ready to takeoff on runway 01↵
  15. Current Time:    7↵
  16. Current Time:    8↵
  17. Current Time:    9↵
  18. runway 01 is free↵
  19. airplane 0002 is ready to takeoff on runway 01↵
  20. Current Time:   10↵
  21. Current Time:   11↵
  22. Current Time:   12↵
  23. runway 01 is free↵
  24. airplane 0003 is ready to takeoff on runway 01↵
  25. Current Time:   13↵
  26. Current Time:   14↵
  27. Current Time:   15↵
  28. runway 01 is free↵
  29. simulation finished↵
  30. simulation time:   15↵
  31. average waiting time of landing:  1.3↵
  32. average waiting time of takeoff:  8.7↵
  33. runway 01 busy time:   15↵
  34. runway average busy time percentage: 100.0%↵
1秒 64M 0

#include <stdlib.h>
#include "stdio.h"
#include "malloc.h"
#include "string.h"
#define MinQueueSize ( 3 )
#define Error( Str )        FatalError( Str )
#define FatalError( Str )   fprintf( stderr, "%s\n", Str ), exit( 1 )
typedef struct {
	int airplaneID;
	int appearTime;
}ElementType;

/* START: fig3_57.txt */
#ifndef _Queue_h
#define _Queue_h

struct QueueRecord;
typedef struct QueueRecord *Queue;

int IsEmpty(Queue Q);
int IsFull(Queue Q);
Queue CreateQueue(int MaxElements);
void DisposeQueue(Queue Q);
void MakeEmpty(Queue Q);
void Enqueue(ElementType X, Queue Q);
ElementType Front(Queue Q);
ElementType Dequeue(Queue Q);

#endif  /* _Queue_h */
/* END */

struct QueueRecord
{
	int Capacity;
	int Front;
	int Rear;
	int Size;
	ElementType *Array;
};

/* START: fig3_58.txt */
int
IsEmpty(Queue Q)
{
	return Q->Size == 0;
}
/* END */

int
IsFull(Queue Q)
{
	return Q->Size == Q->Capacity;
}

Queue
CreateQueue(int MaxElements)
{
	Queue Q;

	/* 1*/      if (MaxElements < MinQueueSize)
		/* 2*/          Error("Queue size is too small");

	/* 3*/      Q = (Queue)malloc(sizeof(struct QueueRecord));
	/* 4*/      if (Q == NULL)
		/* 5*/          FatalError("Out of space!!!");

	/* 6*/      Q->Array = (ElementType *)malloc(sizeof(ElementType)* MaxElements);
	/* 7*/      if (Q->Array == NULL)
		/* 8*/          FatalError("Out of space!!!");
	/* 9*/      Q->Capacity = MaxElements;
	/*10*/      MakeEmpty(Q);

	/*11*/      return Q;
}

/* START: fig3_59.txt */
void
MakeEmpty(Queue Q)
{
	Q->Size = 0;
	Q->Front = 1;
	Q->Rear = 0;
}
/* END */

void
DisposeQueue(Queue Q)
{
	if (Q != NULL)
	{
		free(Q->Array);
		free(Q);
	}
}

/* START: fig3_60.txt */

static int
Succ(int Value, Queue Q)
{
	if (++Value == Q->Capacity)
		Value = 0;
	return Value;
}

void
Enqueue(ElementType X, Queue Q)
{
	if (IsFull(Q))
		Error("Full queue");
	else
	{
		Q->Size++;
		Q->Rear = Succ(Q->Rear, Q);
		Q->Array[Q->Rear] = X;
	}
}
/* END */



ElementType
Front(Queue Q)
{
	if (!IsEmpty(Q))
		return Q->Array[Q->Front];
	Error("Empty queue");
}

ElementType
Dequeue(Queue Q)
{
	ElementType X;

	if (IsEmpty(Q))
		Error("Empty queue");
	else
	{
		Q->Size--;
		X = Q->Array[Q->Front];
		Q->Front = Succ(Q->Front, Q);
	}
	return X;
}

typedef struct {
	int freeTime; //0 means free
	int busyTimeTotal;
} RunwayType;

#define MAXRUNWAYS 100

int main()
{
	//freopen("1.txt", "r", stdin);
	RunwayType runways[MAXRUNWAYS];
	Queue arrivalQueue, departureQueue;
	int landWaitTimeTotal = 0;
	int takeoffWaitTimeTotal = 0;
	int busyTimeTotal = 0;
	int arrivalTotal = 0;
	int departureTotal = 0;
	int currentTime = 0;
	int landingTime, takeoffTime, numRunways;
	int i, numArrival, numDeparture;
	int finishFlag = 0, allRunwaysFree = 1;
	ElementType airplane;

	arrivalQueue = CreateQueue(5006);//1
	departureQueue = CreateQueue(5006);//2


	//printf("Please input number of runways, landing time, takeoff time:\n");
	scanf("%d %d  %d", &numRunways, &landingTime, &takeoffTime);
	for (i = 1; i <= numRunways; i++) {
		runways[i].freeTime = 0;
		runways[i].busyTimeTotal = 0;
	}
	printf("Current Time: %4d\n", currentTime);

	for (;;) {
		//1 min start
		if (!finishFlag) {
			//printf("Please input number of arrivals, number of departures required in next minute:\n");
			scanf("%d %d", &numArrival, &numDeparture);
			if ((numArrival < 0) && (numDeparture < 0))
				finishFlag = 1;
		}
		for (i = 0; i < numArrival; i++)  {
			airplane.airplaneID = ++arrivalTotal + 5000;
			airplane.appearTime = currentTime;
			Enqueue(airplane, arrivalQueue);//3
		}
		for (i = 0; i < numDeparture; i++)  {
			airplane.airplaneID = ++departureTotal;
			airplane.appearTime = currentTime;
			Enqueue(airplane, departureQueue);//4
		}
		for (i = 1; i <= numRunways; i++) {
			if (runways[i].freeTime == 0) {
				if (!IsEmpty(arrivalQueue)){   //5
					airplane = Dequeue(arrivalQueue);//6
					runways[i].freeTime = landingTime;
					runways[i].busyTimeTotal += landingTime;
					printf("airplane %04d is ready to land on runway %02d\n", airplane.airplaneID, i);
					landWaitTimeTotal += currentTime - airplane.appearTime;
				}
				else if (!IsEmpty(departureQueue)){   //7
					airplane = Dequeue(departureQueue);  //8
					runways[i].freeTime = takeoffTime;
					runways[i].busyTimeTotal += takeoffTime;
					printf("airplane %04d is ready to takeoff on runway %02d\n", airplane.airplaneID, i);
					takeoffWaitTimeTotal += currentTime - airplane.appearTime;
				}
			}//if (runways[i].freeTime == 0) {
		} //for ( i = 1; i <= numRunways; i++) 

		//1 min past
		currentTime++;
		printf("Current Time: %4d\n", currentTime);
		allRunwaysFree = 1;
		for (i = 1; i <= numRunways; i++)
		if (runways[i].freeTime) {
			runways[i].freeTime--;
			if (!runways[i].freeTime)
				printf("runway %02d is free\n", i);
			else
				allRunwaysFree = 0;
		}
		if (finishFlag && IsEmpty(arrivalQueue) && IsEmpty(departureQueue) && allRunwaysFree)   //9  //10
			break;
	} //for (;;)

	printf("simulation finished\n");
	printf("simulation time: %4d\n", currentTime);
	float avLandWaitTime = (float)landWaitTimeTotal / arrivalTotal;
	printf("average waiting time of landing: %4.1f\n", avLandWaitTime);
	float avTakeoffWaitTime = (float)takeoffWaitTimeTotal / departureTotal;
	printf("average waiting time of takeoff: %4.1f\n", avTakeoffWaitTime);
	for (i = 1; i <= numRunways; i++) {
		busyTimeTotal += runways[i].busyTimeTotal;
		printf("runway %02d busy time: %4d\n", i, runways[i].busyTimeTotal);
	}
	float busyPercentage = ((float)busyTimeTotal) / numRunways * 100 / currentTime;
	printf("runway average busy time percentage: %4.1f%%\n", busyPercentage);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水之积也不厚,则其负大舟也无力

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值