飞机场调度(C语言)

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

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

整个模拟的时间以分钟为单位,从 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%


样例

输入(1)

1 2 3
1 2
2 1
0 0
0 0
0 0
-1 -1

输出(1)

Current Time:    0
airplane 5001 is ready to land on runway 01
Current Time:    1
Current Time:    2
runway 01 is free
airplane 5002 is ready to land on runway 01
Current Time:    3
Current Time:    4
runway 01 is free
airplane 5003 is ready to land on runway 01
Current Time:    5
Current Time:    6
runway 01 is free
airplane 0001 is ready to takeoff on runway 01
Current Time:    7
Current Time:    8
Current Time:    9
runway 01 is free
airplane 0002 is ready to takeoff on runway 01
Current Time:   10
Current Time:   11
Current Time:   12
runway 01 is free
airplane 0003 is ready to takeoff on runway 01
Current Time:   13
Current Time:   14
Current Time:   15
runway 01 is free
simulation finished
simulation time:   15
average waiting time of landing:  1.3
average waiting time of takeoff:  8.7
runway 01 busy time:   15
runway average busy time percentage: 100.0%

代码

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

typedef struct hikouki {
    int id, occurtime;
    struct hikouki* next;
} aeroplane; // 飞机

typedef struct {
    int occupytime, totaloccupytime;
} RUNWAY; // 跑道

aeroplane airplane;

aeroplane* head1 = NULL, *tail1 = NULL; // 降落
aeroplane* head2 = NULL, *tail2 = NULL; // 起飞

void cq1() { // 创造队列
    aeroplane* p;
    p = (aeroplane*)malloc(sizeof(aeroplane));
    p->id = -1;
    p->occurtime = -1;
    head1 = p;
    tail1 = p; // 空结点
}

void q1(int number, int time) { // 进队
    aeroplane* p;
    p = (aeroplane*)malloc(sizeof(aeroplane));
    p->next = NULL;
    p->id = number;
    p->occurtime = time;
    tail1->next = p;
    tail1 = tail1->next;
}

aeroplane outq1() { // 出队
    aeroplane* p;
    p = head1->next;
    head1->next = p->next;
    if (head1->next == NULL) tail1 = head1;
    return *p;
}

void cq2() { // 创造队列
    aeroplane* p;
    p = (aeroplane*)malloc(sizeof(aeroplane));
    p->id = -1;
    p->occurtime = -1;
    head2 = p;
    tail2 = p; // 空结点
}

void q2(int number, int time) { // 进队
    aeroplane* p;
    p = (aeroplane*)malloc(sizeof(aeroplane));
    p->next = NULL;
    p->id = number;
    p->occurtime = time;
    tail2->next = p;
    tail2 = tail2->next;
}

aeroplane outq2() { // 出队
    aeroplane* p;
    p = head2->next;
    head2->next = p->next;
    if (head2->next == NULL) tail2 = head2;
    return *p;
}

int empty(aeroplane* p) {
    if (p->next == NULL) return 0;
    else return 1;
}

int main() {
    int runwaynum, tc1, tc2;
    scanf("%d%d%d", &runwaynum, &tc1, &tc2);
    int currenttime = 0;
    int num1, num2, s1 = 5001, s2 = 1, numt1 = 0, numt2 = 0; // 每分钟数量,编号,总数量
    int end = 0, allrunwayfree = 1, i;
    int wait1 = 0, wait2 = 0; // 总等待时间

    RUNWAY runway[200];

    for (i = 1; i <= runwaynum; i++) {
        runway[i].occupytime = 0;
        runway[i].totaloccupytime = 0;
    }

    cq1();
    cq2(); // 初始化队列
    printf("Current Time: %4d\n", currenttime++); // 注意空格

    while (1) {
        if (end != 1) {
            scanf("%d%d", &num1, &num2);
            if (num1 < 0 && num2 < 0) {
                end = 1;
            }
        }

        for (i = 0; i < num1; i++) {
            q1(s1++, currenttime);
            numt1++;
        }

        for (i = 0; i < num2; i++) {
            q2(s2++, currenttime);
            numt2++;
        }

        for (int i = 1; i <= runwaynum; i++) {
            if (runway[i].occupytime == 0) { // 跑道空闲

                if (empty(head1)) { // 如果有下降的飞机
                    airplane = outq1();
                    runway[i].occupytime = tc1;
                    printf("airplane %04d is ready to land on runway %02d\n", airplane.id, i);
                    wait1 = wait1 + currenttime - airplane.occurtime;
                    runway[i].totaloccupytime += tc1;
                }

                else if (empty(head2)) {
                    airplane = outq2();
                    runway[i].occupytime = tc2;
                    printf("airplane %04d is ready to takeoff on runway %02d\n", airplane.id, i);
                    wait2 = wait2 + currenttime - airplane.occurtime;
                    runway[i].totaloccupytime += tc2;
                }
            }
        }

        printf("Current Time: %4d\n", currenttime++);
        allrunwayfree = 1;

        for (int i = 1; i <= runwaynum; i++) { // 时间流逝
            if (runway[i].occupytime != 0) {
                if (!(--runway[i].occupytime))
                    printf("runway %02d is free\n", i);
                else
                    allrunwayfree = 0;
            }
        }

        if (end && (!empty(head1)) && (!empty(head2)) && allrunwayfree) // 调度完成
            break;
    }

    printf("simulation finished\n");
    printf("simulation time: %4d\n", --currenttime);

    int runway_totaltime = 0;
    double average1, average2, per;

    average1 = (double)wait1 / (double)numt1;
    printf("average waiting time of landing: %4.1f\n", average1);

    average2 = (double)wait2 / (double)numt2;
    printf("average waiting time of takeoff: %4.1f\n", average2);

    for (i = 1; i <= runwaynum; i++) {
        printf("runway %02d busy time: %4d\n", i, runway[i].totaloccupytime);
        runway_totaltime += runway[i].totaloccupytime; // 加和
    }

    per = ((double)runway_totaltime) / runwaynum * 100 / currenttime; // 百分比
    printf("runway average busy time percentage: %4.1f%%\n", per);

    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值