王炜麒的代码

顺便服务策略的代码实现 

三种功能分析思维导图

先来先服务策略

亿图脑图MindMasterhttps://www.edrawsoft.cn/viewer/public/s/c03b8122671795顺便服务策略

亿图脑图MindMasterhttps://www.edrawsoft.cn/viewer/public/s/0fd98238583172

最短时间优先策略

 亿图脑图MindMasterhttps://www.edrawsoft.cn/viewer/public/s/69037081371796

#include<stdio.h>
#include<stdlib.h>
int TOTAL_STATION = 5; //全局变量
int STRATEGY = 1;
int DISTANCE = 2;
int outtarget[10] = { 0 }; //初始化二进制数组
int outclockwise[10] = { 0 };
int outcounterclockwise[10] = { 0 };
int curtime = 0; //记录当前时间

struct status //公交车状态结构体
{
    int position;
    int direction;
    int status;
} Bus;

void output() //输出函数
{
    printf("TIME: %d\n", curtime);              //输出当前时间信息
    printf("BUS:\n");                           //开始输出公交状态信息
    printf("position: %d\n", Bus.position); //公交位置
    printf("target: ");                         //公交目的站点(二进制表示)
    for (int i = 0; i < TOTAL_STATION; i++)
        printf("%d", outtarget[i]);
    printf("\n");

    printf("STATION:\n");  //开始输出站点请求信息
    printf("clockwise: "); //顺时针乘坐车辆请求站点(二进制)
    for (int i = 0; i < TOTAL_STATION; i++)
        printf("%d", outclockwise[i]);
    printf("\n");
    printf("counterclockwise: "); //逆时针乘坐车辆请求站点(二进制)
    for (int i = 0; i < TOTAL_STATION; i++)
        printf("%d", outcounterclockwise[i]);
    printf("\n");
}

void DropBy(int cur_station)
{
    int stationcount = 0;

    if (Bus.direction == 1)                         //如果为顺时针方向行驶
    {
        for (stationcount = 0; stationcount < TOTAL_STATION; stationcount++)
        {
            if (stationcount + cur_station > TOTAL_STATION)//如果循环过程中超过了右边界
            {
                if (outclockwise[stationcount + cur_station - TOTAL_STATION - 1] == 1 || outcounterclockwise[stationcount + cur_station - TOTAL_STATION - 1] == 1 || outtarget[stationcount + cur_station - TOTAL_STATION - 1] == 1)
                {
                    if (TOTAL_STATION / 2 < stationcount)      //如果当前站点与当前方向上第一个有请求的站点的距离大于总站点数的一半
                    {
                        Bus.direction = 2;                                      //切换行驶方向
                    }
                    break;
                }
            }
            else if (stationcount + cur_station <= TOTAL_STATION)//如果循环过程中没有触碰到右边界
            {
                if (outclockwise[stationcount + cur_station - 1] == 1 || outcounterclockwise[stationcount + cur_station - 1] == 1 || outtarget[stationcount + cur_station - 1] == 1)
                {
                    if (TOTAL_STATION / 2 < stationcount)   //如果当前站点与当前方向上第一个有请求的站点的距离大于总站点数的一半
                    {
                        Bus.direction = 2;                                  //切换行驶方向
                    }
                    break;
                }
            }
        }
    }
    else if (Bus.direction == 2)                         //如果为逆时针方向行驶
    {
        for (stationcount = 0; stationcount < TOTAL_STATION; stationcount++)
        {
            if (cur_station - stationcount > 0)
            {
                if (outclockwise[cur_station - stationcount - 1] == 1 || outcounterclockwise[cur_station - stationcount - 1] == 1 || outtarget[cur_station - stationcount - 1] == 1)
                {
                    if (TOTAL_STATION / 2 < stationcount)
                    {
                        Bus.direction = 1;
                    }
                    break;
                }
            }
            else if (cur_station - stationcount <= 0)
            {
                if (outclockwise[cur_station - stationcount + TOTAL_STATION - 1] == 1 || outcounterclockwise[cur_station - stationcount + TOTAL_STATION - 1] == 1 || outtarget[cur_station - stationcount + TOTAL_STATION - 1] == 1)
                {
                    if (TOTAL_STATION / 2 < stationcount)
                    {
                        Bus.direction = 1;
                    }
                    break;
                }
            }
        }
    }

    if (stationcount == TOTAL_STATION)       //如果没有请求,则将公交车状态改为静止
    {
        Bus.status = 0;
    }
}

void FirstSearch()
{
    int clockwise = TOTAL_STATION - 1,counterclockwise = TOTAL_STATION - 1;
    int i = 0;
    for (i = 1; i <= TOTAL_STATION - 1; i++)
    {
        if (outcounterclockwise[i] == 1 || outclockwise[i] == 1 || outtarget[i] == 1)
        {
            clockwise = i;
            break;
        }
    }
    for (i = TOTAL_STATION - 1; i >= 0; i--)
    {
        if (outcounterclockwise[i] == 1 || outclockwise[i] == 1 || outtarget[i] == 1)
        {
            counterclockwise = TOTAL_STATION - i;
            break;
        }
    }
    if (counterclockwise < clockwise)
    {
        Bus.direction = 2;
    }
    else if (counterclockwise >= clockwise)
    {
        Bus.direction = 1;
    }
    Bus.status = 0;
}

void SCAN()
{
    output();
    int waittime[3][10] = { 0 };
    char str[21];  //用gets函数不能读取该行输入末尾的回车
    Bus.status = 1;//先将公交设置为可以接受请求的状态
    int first = 1;
    int init_status = -1;
    int key = 0;
    
    while (fgets(str, 20, stdin) && str[0] != 'e')         //如果读取到的指令不为end指令
    {
        //根据指令分类操作
        if (str[5] == '\n')    //如果读取到的指令为clock
        {
            if (key == 1)
            {
                key = 0;
            }
            
            if (init_status == 1)
            {
                init_status = 0;
                Bus.status = 1;
            }
            curtime++;        //当前时间加一
            int i = 0, j = 0, flag = 0;          //判断数组是否全空的标志flag,如果全空则为0,如果请求数组内部有请求,则为1

            for (i = 0; i < TOTAL_STATION; i++)
            {
                if (outclockwise[i] != 0 || outcounterclockwise[i] != 0 || outtarget[i] != 0)//如果有请求
                {
                    flag = 1;
                    break;
                }
                else
                {
                    continue;
                }
            }

            if (flag == 0)      //如果请求为空,列车处于空闲状态,直接打印,并进入下一个循环
            {
                output();
                Bus.status = 0;
                continue;
            }
            else if (flag == 1) //如果请求不为空,列车处于执行任务状态,跟据Bus.direction向对应方向将Bus.position改变
            {
                if (Bus.status == 1)//如果公交车此时处于运行状态
                {
                    if (Bus.direction == 1)//如果为顺时针行驶
                    {
                        if (Bus.position == DISTANCE * TOTAL_STATION - 1)//如果碰到了右边界
                        {
                            Bus.position = 0;
                        }
                        else
                        {
                            Bus.position++;
                        }
                    }
                    else if (Bus.direction == 2)//如果为逆时针行驶
                    {
                        if (Bus.position == 0)//如果碰到了左边界
                        {
                            Bus.position = TOTAL_STATION * DISTANCE - 1;
                        }
                        else
                        {
                            Bus.position--;
                        }
                    }

                    if (Bus.position % DISTANCE == 0)//判断下一个位置是否在站点,如果下一站是站点
                    {
                        for (int i = 0; i < TOTAL_STATION; i++)//先将对应的等待时间数组进行处理
                        {
                            if (outclockwise[i] == 1)
                            {
                                waittime[0][i]++;
                            }
                            else if (outclockwise[i] == 0)
                            {
                                waittime[0][i] = 0;
                            }
                            if (outcounterclockwise[i] == 1)
                            {
                                waittime[1][i]++;
                            }
                            else if (outcounterclockwise[i] == 0)
                            {
                                waittime[1][i] = 0;
                            }
                            if (outtarget[i] == 1)
                            {
                                waittime[2][i]++;
                            }
                            else if (outtarget[i] == 0)
                            {
                                waittime[2][i] = 0;
                            }
                        }

                        output();     //先输出此时的状态

                           //如果公交将要进站的站点有请求,改变对应数组中的元素
                        if (outclockwise[Bus.position / DISTANCE] == 1 || outcounterclockwise[Bus.position / DISTANCE] == 1 || outtarget[Bus.position / DISTANCE] == 1)
                        {
                            outclockwise[Bus.position / DISTANCE] = 0;               //消除将要进站的站点中的请求
                            outcounterclockwise[Bus.position / DISTANCE] = 0;
                            outtarget[Bus.position / DISTANCE] = 0;

                            Bus.status = 0;                                                           //将公交车设置为停靠状态
                        }
                    }
                    else if (Bus.position % DISTANCE != 0)//如果下一个位置不是站点
                    {
                        Bus.status = 1;
                        output();
                    }
                }
                else if(Bus.status == 0)//如果处于停车状态
                {
                    Bus.status = 1;        //因为此时还有任务,启动车辆
                    DropBy(Bus.position / DISTANCE + 1);//调用DropBy函数判断是否继承方向
                    output();
                    key = 1;
                }
            }
        }
        else if (str[5] == 'w')         //如果读取到的指令为clockwise
        {
            if (str[11] != '0')             //如果不是站点号不是10
            {
                if (Bus.status == 0 && Bus.position / DISTANCE + 1 == (str[10] - '0'))//如果此时公交处于不再接收此处请求的状态
                {
                    outclockwise[str[10] - '0' - 1] = 0;
                    outcounterclockwise[str[10] - '0' - 1] = 0;
                    outtarget[str[10] - '0' - 1] = 0;
                    continue;
                }
                else if (key == 1 && Bus.position / DISTANCE + 1 == (str[10] - '0'))
                {
                    outclockwise[str[10] - '0' - 1] = 0;
                    outcounterclockwise[str[10] - '0' - 1] = 0;
                    outtarget[str[10] - '0' - 1] = 0;
                    continue;
                }
                else
                {
                    if (first == 1)//如果是最开始确定目标的阶段
                    {
                        outclockwise[str[10] - '0' - 1] = 1;
                        FirstSearch();
                        first = 0;
                        init_status = 1;
                    }
                    else
                    {
                        outclockwise[str[10] - '0' - 1] = 1;
                    }
                }
            }
            else                    //如果站点号是10
            {
                if (Bus.status == 0 && Bus.position == 9 * DISTANCE)//如果公交此时已经进站停车,并且收到了该位置的请求,则立即完成,将该处请求全部置为0
                {
                    outclockwise[9] = 0;
                    outcounterclockwise[9] = 0;
                    outtarget[9] = 0;
                    continue;
                }
                else if (key == 1 && Bus.position == 9 * DISTANCE)
                {
                    outclockwise[9] = 0;
                    outcounterclockwise[9] = 0;
                    outtarget[9] = 0;
                    continue;
                }
                else
                {
                    if (first == 1)//如果是最开始确定目标的阶段
                    {
                        outclockwise[9] = 1;
                        FirstSearch();
                        first = 0;
                        init_status = 1;
                    }
                    else
                    {
                        outclockwise[9] = 1;
                    }
                }
            }
        }
        else if (str[5] == 'e')         //如果读取到的指令为counterclockwise
        {
            if (str[18] != '0')             //如果不是站点号不是10
            {   
                if (Bus.status == 0 && Bus.position / DISTANCE + 1 == (str[17] - '0'))//如果此时公交处于不再接收此处请求的状态
                {
                    outcounterclockwise[str[17] - '0' - 1] = 0;
                    outclockwise[str[17] - '0' - 1] = 0;
                    outtarget[str[17] - '0' - 1] = 0;
                    continue;
                }
                else if (key == 1 && Bus.position / DISTANCE + 1 == (str[17] - '0'))
                {
                    outclockwise[str[10] - '0' - 1] = 0;
                    outcounterclockwise[str[10] - '0' - 1] = 0;
                    outtarget[str[10] - '0' - 1] = 0;
                    continue;
                }
                else
                {
                    if (first == 1)
                    {
                        outcounterclockwise[str[17] - '0' - 1] = 1;
                        FirstSearch();
                        first = 0;
                        init_status = 1;
                    }
                    else
                    {
                        outcounterclockwise[str[17] - '0' - 1] = 1;
                    }
                }
            }
            else                    //如果站点号是10
            {
                if (Bus.status == 0 && Bus.position == 9 * DISTANCE)//如果公交此时已经进站并且停止接受此站的请求
                {
                    outcounterclockwise[9] = 0;
                    outclockwise[9] = 0;
                    outtarget[9] = 0;
                    continue;
                }
                else if (key == 1 && Bus.position == 9 * DISTANCE)
                {
                    outclockwise[str[10] - '0' - 1] = 0;
                    outcounterclockwise[str[10] - '0' - 1] = 0;
                    outtarget[str[10] - '0' - 1] = 0;
                    continue;
                }
                else
                {
                    if (first == 1)
                    {
                        outcounterclockwise[9] = 1;
                        FirstSearch();
                        first = 0;
                        init_status = 1;
                    }
                    else
                    {
                        outcounterclockwise[9] = 1;
                    }
                    
                }
            }
        }
        else if (str[5] == 't')         //如果读取到的指令为target
        {
            if (str[8] != '0')             //如果不是站点号不是10
            {
                if (Bus.status == 0 && Bus.position / DISTANCE + 1 == (str[7] - '0'))//如果此时公交处于不再接收此处请求的状态
                {
                    outtarget[str[7] - '0' - 1] = 0;
                    outclockwise[str[7] - '0' - 1] = 0;
                    outcounterclockwise[str[7] - '0' - 1] = 0;
                    continue;
                }
                else if (key == 1 && Bus.position / DISTANCE + 1 == (str[7] - '0'))
                {
                    outclockwise[str[10] - '0' - 1] = 0;
                    outcounterclockwise[str[10] - '0' - 1] = 0;
                    outtarget[str[10] - '0' - 1] = 0;
                    continue;
                }
                else
                {
                    if (first == 1)
                    {
                        outtarget[str[7] - '0' - 1] = 1;
                        FirstSearch();
                        first = 0;
                        init_status = 1;
                    }
                    else
                    {
                        outtarget[str[7] - '0' - 1] = 1;
                    }
                }
            }
            else                    //如果站点号是10
            {
                if (Bus.status == 0 && Bus.position == 9 * DISTANCE)//如果公交此时已经进站并且停止接受此站的请求
                {
                    outtarget[9] = 0;
                    outclockwise[9] = 0;
                    outcounterclockwise[9] = 0;
                    continue;
                }
                else if (key == 1 && Bus.position == 9 * DISTANCE)
                {
                    outclockwise[str[10] - '0' - 1] = 0;
                    outcounterclockwise[str[10] - '0' - 1] = 0;
                    outtarget[str[10] - '0' - 1] = 0;
                    continue;
                }
                else
                {
                    if (first == 1)
                    {
                        outtarget[9] = 1;
                        FirstSearch();
                        first = 0;
                        init_status = 1;
                    }
                    else
                    {
                        outtarget[9] = 1;
                    }
                }
            }
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值