完成了SSTF和SCAN策略的控制函数及流程设计
修复了FCFS策略中连续在同一站的请求处理错误的bug
This commit is contained in:
		@@ -30,4 +30,44 @@ int FCFSDirection();
 | 
				
			|||||||
 * @return 需要处理的请求
 | 
					 * @return 需要处理的请求
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
bus_query_t *FCFSQuery();
 | 
					bus_query_t *FCFSQuery();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * 获得在SSTF策略下应该处理的请求
 | 
				
			||||||
 | 
					 * @return 指向需要处理的请求的指针
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					bus_query_t *SSTFGetQuery();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * 根据指定的请求获得前进的方向,也就是前往指定的请求最近的方向
 | 
				
			||||||
 | 
					 * 在SSTF策略中使用
 | 
				
			||||||
 | 
					 * @param target_query 指定完成的请求
 | 
				
			||||||
 | 
					 * @return 前进的方向
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					int SSTFDirection(bus_query_t* target_query);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * 在当前站上可以顺便服务的请求
 | 
				
			||||||
 | 
					 * @return 服务的请求指针
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					bus_query_t *SSTFBTWQuery();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * 获得在SCAN策略下应该处理的请求
 | 
				
			||||||
 | 
					 * @return 指向需要处理的请求的指针
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					bus_query_t *SCANGetQuery();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * 根据指定的请求获得前进的方向
 | 
				
			||||||
 | 
					 * 在SCAN策略中使用
 | 
				
			||||||
 | 
					 * @param target_query 指定完成的请求
 | 
				
			||||||
 | 
					 * @return 前进的方向
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					int SCANDirection(bus_query_t *target_query);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * 在当前站上可以顺便服务的请求
 | 
				
			||||||
 | 
					 * @return 服务的请求指针
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					bus_query_t *SCANBTWQuery();
 | 
				
			||||||
#endif //AUTO_PILOT_BUS_CONTROLLER_H
 | 
					#endif //AUTO_PILOT_BUS_CONTROLLER_H
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -37,6 +37,7 @@ extern bus_query_t *queries;
 | 
				
			|||||||
 * 创建请求链表节点
 | 
					 * 创建请求链表节点
 | 
				
			||||||
 * @param type 请求的类型
 | 
					 * @param type 请求的类型
 | 
				
			||||||
 * @param node 请求产生/指向的站点
 | 
					 * @param node 请求产生/指向的站点
 | 
				
			||||||
 | 
					 * @return 当前创建的链表节点地址
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
bus_query_t *CreateQuery(int type, rail_node_t *node);
 | 
					bus_query_t *CreateQuery(int type, rail_node_t *node);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										82
									
								
								main.c
									
									
									
									
									
								
							
							
						
						
									
										82
									
								
								main.c
									
									
									
									
									
								
							@@ -58,7 +58,6 @@ int main()
 | 
				
			|||||||
                    // 如果到站,处理请求和
 | 
					                    // 如果到站,处理请求和
 | 
				
			||||||
                    if(JudgeOnStation() == BUS_TRUE)
 | 
					                    if(JudgeOnStation() == BUS_TRUE)
 | 
				
			||||||
                    {
 | 
					                    {
 | 
				
			||||||
                        direction = FCFSDirection();
 | 
					 | 
				
			||||||
                        finished_query = FCFSQuery();
 | 
					                        finished_query = FCFSQuery();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                        if(finished_query != NULL) // 有请求就处理请求
 | 
					                        if(finished_query != NULL) // 有请求就处理请求
 | 
				
			||||||
@@ -69,6 +68,9 @@ int main()
 | 
				
			|||||||
                                DeleteQuery(finished_query);
 | 
					                                DeleteQuery(finished_query);
 | 
				
			||||||
                                finished_query = FCFSQuery();
 | 
					                                finished_query = FCFSQuery();
 | 
				
			||||||
                            }
 | 
					                            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                            // 请求处理完再进行方向的判断
 | 
				
			||||||
 | 
					                            direction = FCFSDirection();
 | 
				
			||||||
                        }
 | 
					                        }
 | 
				
			||||||
                        else //如果没有请求就继续前进
 | 
					                        else //如果没有请求就继续前进
 | 
				
			||||||
                        {
 | 
					                        {
 | 
				
			||||||
@@ -81,10 +83,83 @@ int main()
 | 
				
			|||||||
                    }
 | 
					                    }
 | 
				
			||||||
                    break;
 | 
					                    break;
 | 
				
			||||||
                case BUS_SSTF:
 | 
					                case BUS_SSTF:
 | 
				
			||||||
 | 
					                    // 如果没有指定的请求就获得指定的请求
 | 
				
			||||||
 | 
					                    if(target_query == NULL)
 | 
				
			||||||
 | 
					                    {
 | 
				
			||||||
 | 
					                        target_query = SSTFGetQuery();
 | 
				
			||||||
 | 
					                        direction = SSTFDirection(target_query);
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    if(JudgeOnStation() == BUS_TRUE)
 | 
				
			||||||
 | 
					                    {
 | 
				
			||||||
 | 
					                        // 如果到达目标的站点
 | 
				
			||||||
 | 
					                        if(the_bus->rail_node_pos == target_query->node)
 | 
				
			||||||
 | 
					                        {
 | 
				
			||||||
 | 
					                            DeleteQuery(target_query);
 | 
				
			||||||
 | 
					                            target_query = NULL;
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
 | 
					                        else
 | 
				
			||||||
 | 
					                        {
 | 
				
			||||||
 | 
					                            target_query = SSTFBTWQuery();
 | 
				
			||||||
 | 
					                            if(target_query != NULL)
 | 
				
			||||||
 | 
					                            {
 | 
				
			||||||
 | 
					                                while (target_query != NULL)
 | 
				
			||||||
 | 
					                                {
 | 
				
			||||||
 | 
					                                    DeleteQuery(target_query);
 | 
				
			||||||
 | 
					                                    target_query = SSTFBTWQuery();
 | 
				
			||||||
 | 
					                                }
 | 
				
			||||||
 | 
					                            }
 | 
				
			||||||
 | 
					                            else
 | 
				
			||||||
 | 
					                            {
 | 
				
			||||||
 | 
					                                RunBus(direction);
 | 
				
			||||||
 | 
					                            }
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                    else
 | 
				
			||||||
 | 
					                    {
 | 
				
			||||||
 | 
					                        RunBus(direction);
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
                    break;
 | 
					                    break;
 | 
				
			||||||
                case BUS_SCAN:
 | 
					                case BUS_SCAN:
 | 
				
			||||||
 | 
					                    // 如果没有指定的请求就获得指定的请求
 | 
				
			||||||
 | 
					                    if(target_query == NULL)
 | 
				
			||||||
 | 
					                    {
 | 
				
			||||||
 | 
					                        target_query = SCANGetQuery();
 | 
				
			||||||
 | 
					                        direction = SSTFDirection(target_query);
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    if(JudgeOnStation() == BUS_TRUE)
 | 
				
			||||||
 | 
					                    {
 | 
				
			||||||
 | 
					                        // 如果到达目标的站点
 | 
				
			||||||
 | 
					                        if(the_bus->rail_node_pos == target_query->node)
 | 
				
			||||||
 | 
					                        {
 | 
				
			||||||
 | 
					                            DeleteQuery(target_query);
 | 
				
			||||||
 | 
					                            target_query = NULL;
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
 | 
					                        else
 | 
				
			||||||
 | 
					                        {
 | 
				
			||||||
 | 
					                            target_query = SCANGetQuery();
 | 
				
			||||||
 | 
					                            if(target_query != NULL)
 | 
				
			||||||
 | 
					                            {
 | 
				
			||||||
 | 
					                                while (target_query != NULL)
 | 
				
			||||||
 | 
					                                {
 | 
				
			||||||
 | 
					                                    DeleteQuery(target_query);
 | 
				
			||||||
 | 
					                                    target_query = SCANBTWQuery();
 | 
				
			||||||
 | 
					                                }
 | 
				
			||||||
 | 
					                            }
 | 
				
			||||||
 | 
					                            else
 | 
				
			||||||
 | 
					                            {
 | 
				
			||||||
 | 
					                                RunBus(direction);
 | 
				
			||||||
 | 
					                            }
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                    else
 | 
				
			||||||
 | 
					                    {
 | 
				
			||||||
 | 
					                        RunBus(direction);
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
                    break;
 | 
					                    break;
 | 
				
			||||||
                default:
 | 
					                default:
 | 
				
			||||||
 | 
					                    // 这个分支只是为了符合代码规范而存在,理论上不会用到这个分支
 | 
				
			||||||
                    break;
 | 
					                    break;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            PrintState(output);
 | 
					            PrintState(output);
 | 
				
			||||||
@@ -93,6 +168,9 @@ int main()
 | 
				
			|||||||
        else if(result == IO_END)
 | 
					        else if(result == IO_END)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            printf("end\n");
 | 
					            printf("end\n");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            FreeRails(rails);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            break;
 | 
					            break;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        else
 | 
					        else
 | 
				
			||||||
@@ -100,4 +178,6 @@ int main()
 | 
				
			|||||||
            //在读取到创建请求的情况下,不做任何事
 | 
					            //在读取到创建请求的情况下,不做任何事
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return 0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -1,6 +1,7 @@
 | 
				
			|||||||
#include "bus.h"
 | 
					#include "bus.h"
 | 
				
			||||||
#include "math.h"
 | 
					
 | 
				
			||||||
bus_t *the_bus = NULL;
 | 
					bus_t *the_bus = NULL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void RunBus(int direction)
 | 
					void RunBus(int direction)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    if(direction == BUS_CLOCK_WISE)//顺时针
 | 
					    if(direction == BUS_CLOCK_WISE)//顺时针
 | 
				
			||||||
@@ -11,21 +12,25 @@ void RunBus(int direction)
 | 
				
			|||||||
    {
 | 
					    {
 | 
				
			||||||
        the_bus->distance--;
 | 
					        the_bus->distance--;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int GetBusPosition()
 | 
					int GetBusPosition()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    int a, b, c;
 | 
					    int a, b, c;
 | 
				
			||||||
    b = 0;
 | 
					    b = 0;
 | 
				
			||||||
    rail_node_t *p = rails;
 | 
					    rail_node_t *p = rails;
 | 
				
			||||||
    a = the_bus->rail_node_pos->id;//指向站点的指针以及这个指针对应的站台id
 | 
					    a = the_bus->rail_node_pos->id;//指向站点的指针以及这个指针对应的站台id
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    while (p->id != a){
 | 
					    while (p->id != a){
 | 
				
			||||||
        b += p->next_node_distance;
 | 
					        b += p->next_node_distance;
 | 
				
			||||||
        p = p->next_node;
 | 
					        p = p->next_node;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    c = b + (the_bus->distance);
 | 
					    c = b + (the_bus->distance);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return c;
 | 
					    return c;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int JudgeOnStation()
 | 
					int JudgeOnStation()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    if (abs(the_bus->distance) == rails->last_node_distance)//表示逆时针
 | 
					    if (abs(the_bus->distance) == rails->last_node_distance)//表示逆时针
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -48,3 +48,33 @@ bus_query_t *FCFSQuery()
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
    return NULL;
 | 
					    return NULL;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					bus_query_t *SSTFGetQuery()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    return NULL;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int SSTFDirection(bus_query_t* target_query)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    return BUS_STOP;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					bus_query_t *SSTFBTWQuery()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    return NULL;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					bus_query_t *SCANGetQuery()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    return NULL;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int SCANDirection(bus_query_t *target_query)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    return BUS_STOP;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					bus_query_t *SCANBTWQuery()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    return NULL;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user