添加了轨道类

This commit is contained in:
jackfiled 2022-06-10 21:21:35 +08:00
parent 2eb368ddfa
commit 6380265faa
3 changed files with 130 additions and 0 deletions

View File

@ -18,9 +18,11 @@ include_directories(
${PROJECT_SOURCE_DIR}/include ${PROJECT_SOURCE_DIR}/include
${PROJECT_SOURCE_DIR}/src/header ${PROJECT_SOURCE_DIR}/src/header
) )
aux_source_directory(${PROJECT_SOURCE_DIR}/src SRCS) aux_source_directory(${PROJECT_SOURCE_DIR}/src SRCS)
add_executable(auto_bus_gui main.cpp ${SRCS}) add_executable(auto_bus_gui main.cpp ${SRCS})
target_link_libraries(auto_bus_gui target_link_libraries(auto_bus_gui
Qt::Core Qt::Core
Qt::Gui Qt::Gui

51
include/railsModel.h Normal file
View File

@ -0,0 +1,51 @@
//
// Created by ricardo on 2022/6/10.
//
#ifndef AUTO_BUS_GUI_RAILSMODEL_H
#define AUTO_BUS_GUI_RAILSMODEL_H
#include "cstdlib"
struct rail_node{
/**
*
*/
int id;
/**
*
*/
int last_node_distance;
/**
*
*/
int next_node_distance;
/**
*
*/
struct rail_node* last_node;
/**
*
*/
struct rail_node* next_node;
};
typedef struct rail_node rail_node_t;
class RailsModel
{
public:
/**
*
*/
rail_node_t *rails;
explicit RailsModel(int distance = 2, int node_number = 5);
rail_node_t *FindNode(int node_num) const;
~RailsModel();
};
#endif //AUTO_BUS_GUI_RAILSMODEL_H

77
src/railsModel.cpp Normal file
View File

@ -0,0 +1,77 @@
//
// Created by ricardo on 2022/6/10.
//
#include "railsModel.h"
RailsModel::RailsModel(int distance, int node_number)
{
rail_node_t *head;
rail_node_t *node;
head = (rail_node_t *) malloc(sizeof(rail_node_t));
head->id = 1;
head->last_node_distance = distance;
head->next_node_distance = distance;
node = head;
// 循环创建每一个节点
for(int i = 2; i <= node_number; i++)
{
auto *p = (rail_node_t*) malloc(sizeof (rail_node_t));
p->id = i;
p->last_node_distance = distance;
p->next_node_distance = distance;
p->last_node = node;
node->next_node = p;
node = p;
}
// 将链表头尾链接起来
node->next_node = head;
head->last_node = node;
rails = head;
}
RailsModel::~RailsModel()
{
rail_node_t *node = rails;
// 断开头尾节点
node->last_node->next_node = nullptr;
while (node != nullptr)
{
rail_node_t *p = node;
node = node->next_node;
free(p);
}
// 将指针置为空
rails = nullptr;
}
rail_node_t *RailsModel::FindNode(int node_num) const
{
if (rails == nullptr)
{
return nullptr;
}
rail_node_t *result = nullptr;
rail_node_t *node = rails;
do
{
if(node->id == node_num)
{
result = node;
break;
}
node = node->next_node;
} while (node != rails);
return result;
}