Create GUI Branch

This commit is contained in:
2022-06-10 19:52:43 +08:00
parent 1dad2ed57a
commit 2eb368ddfa
349 changed files with 297 additions and 107159 deletions

111
src/bus.c
View File

@@ -1,111 +0,0 @@
#include "bus.h"
bus_t *the_bus = NULL;
void RunBus(int direction)
{
if(direction == BUS_CLOCK_WISE)//顺时针
{
the_bus->distance++;
}
else if(direction == BUS_COUNTER_CLOCK_WISE)
{
the_bus->distance--;
}
}
int GetBusPosition()
{
int distance = 0;
rail_node_t *now_pos = the_bus->rail_node_pos;
rail_node_t *p = rails;
// 先计算当前所在站点距离起始站点的距离
distance += p ->next_node_distance;
p = p->next_node;
while (p != now_pos)
{
distance += p->next_node_distance;
p = p->next_node;
}
if(now_pos == rails) // 起始点特殊处理
{
// 公交车偏离起始点的位置
int length = the_bus->distance;
if(length >= 0)
{
return length;
}
else
{
return distance + length;
}
}
else if(now_pos == rails->last_node)
{
int length = the_bus->distance;
if(length == now_pos->next_node_distance) // 处理一种极为特殊的情况 公交车在即将到达起始站时
{
return 0;
}
else
{
return distance + length;
}
}
else
{
return distance + the_bus->distance;
}
}
int JudgeOnStation()
{
if (the_bus->distance == - the_bus->rail_node_pos->last_node_distance)//表示逆时针
{
the_bus->distance = 0;
the_bus->rail_node_pos = the_bus->rail_node_pos->last_node;//逆时针往上一个
return BUS_TRUE;
}
else if (the_bus->distance == the_bus->rail_node_pos->next_node_distance)//表示顺时针
{
the_bus->distance = 0;
the_bus->rail_node_pos = the_bus->rail_node_pos->next_node;//顺时针往下一个
return BUS_TRUE;
}
else if(the_bus->distance == 0) //在站点上原地不动
{
return BUS_TRUE;
}
else
{
return BUS_FAlSE;
}
}
int GetQueryDistance(bus_query_t *query, int orientation)
{
rail_node_t *target_node = query->node;
rail_node_t *now_node = the_bus->rail_node_pos;
int distance = 0;
if(orientation == BUS_CLOCK_WISE)
{
while (now_node != target_node)
{
distance += now_node->next_node_distance;
now_node = now_node->next_node;
}
}
else if(orientation == BUS_COUNTER_CLOCK_WISE)
{
while (now_node != target_node)
{
distance += now_node->last_node_distance;
now_node = now_node->last_node;
}
}
return distance;
}

View File

@@ -1,264 +0,0 @@
//
// Created by ricardo on 2022/5/6.
//
#include "bus_io.h"
int ReadInput(char *inputString)
{
char src[20];
int num;
sscanf(inputString, "%[a-z] %d", src, &num);
if (0 == strcmp("clock", src))
{
return IO_CLOCK;
}
else if (0 == strcmp("counterclockwise", src))
{
CreateQuery(BUS_COUNTER_CLOCK_WISE, FindNode(rails, num));
return IO_READING;
}
else if (0 == strcmp("clockwise", src))
{
CreateQuery(BUS_CLOCK_WISE, FindNode(rails, num));
return IO_READING;
}
else if (0 == strcmp("target", src))
{
CreateQuery(BUS_TARGET, FindNode(rails, num));
return IO_READING;
}
else if (0 == strcmp("end", src))
{
return IO_END;
}
else
{
// 匹配失败则返回-1
return -1;
}
}
rail_node_t *ReadConfigFile()
{
FILE *config_file = NULL;
char buffer[30];
int total_station = 0;
int distance = 0;
config_file = fopen("dict.dic", "r");
// 循环读取文件的每一行
while (fgets(buffer, sizeof buffer, config_file) != NULL)
{
char first_char = buffer[0];
char *p;
switch (first_char)
{
case '#':
// 如果读取到#什么都不做
break;
case 'T':
// TOTAL_STATION
p = buffer;
// 把数字前面的所有字符全部干掉
while (*p < '0' || *p > '9')
{
p++;
}
// 讲道理,应该只有两位数,所以就这样处理了
if (*(p + 1) == '\n' || *(p + 1) == '\0')
{
total_station = *p - 48;
}
else
{
total_station = (*p - 48) * 10 + *(p + 1) - 48;
}
break;
case 'S':
// STRATEGY
p = buffer;
// 将=前的字符全部略去
while (*p != '=')
{
p++;
}
// =也去掉
p++;
// =和策略之间的空格也去掉
while (*p == ' ')
{
p++;
}
if (*p == 'F' && *(p + 1) == 'C') //FCFS
{
chosen_strategy = BUS_FCFS;
}
else if (*p == 'S' && *(p + 1) == 'S') //SSTF
{
chosen_strategy = BUS_SSTF;
}
else if (*p == 'S' && *(p + 1) == 'C') //SCAN
{
chosen_strategy = BUS_SCAN;
}
else
{
// 读取失败
chosen_strategy = -1;
}
break;
case 'D':
// DISTANCE
p = buffer;
// 把数字前面的所有字符全部干掉
while (*p < '0' || *p > '9')
{
p++;
}
if (*(p + 1) == '\n' || *(p + 1) == '\0')
{
distance = *p - 48;
}
break;
default:
continue;
}
}
// 处理参数去缺省值的情况
if (distance == 0)
{
distance = 2;
}
if (total_station == 0)
{
total_station = 5;
}
if(chosen_strategy == -1)
{
chosen_strategy = BUS_FCFS;
}
all_distance = distance * total_station;
rail_node_t *head = CreateRails(distance, total_station);
return head;
}
void PrintState()
{
int count, flag = 1; //flag用于标记为使下面第一个循环能够进入
rail_node_t *p = NULL;
char target[25], clockwise[25], counterclockwise[25];
for (count = 0, p = rails; flag == 1 || p != rails; p = p->next_node, count++)
{
flag=0;
target[count] = '0';
clockwise[count] = '0';
counterclockwise[count] = '0';
} //遍历轨道链表将所有站点初始化为0无任何请求
// 在字符串的末尾填0
target[count] = '\0';
clockwise[count] = '\0';
counterclockwise[count] = '\0';
bus_query_t *t = NULL;
int i;
for (t = queries; t != NULL; t = t->next_node)
{
i = t->node->id - 1;
if (t->type == 0)
{
clockwise[i] = '1';
}
else if(t->type==BUS_COUNTER_CLOCK_WISE)
{
counterclockwise[i] = '1';
}
else if(t->type==BUS_TARGET)
{
target[i] = '1';
}
} //遍历请求链表将有请求的站点按照不同类型标记为1
printf("TIME:%d\n", bus_time);
printf("BUS:\n");
printf("position:%d\n", GetBusPosition());
printf("target:%s\n", target);
printf("STATION:\n");
printf("clockwise:%s\n", clockwise);
printf("counterclockwise:%s\n", counterclockwise);
}
void PrintStateInner(char *str)
{
memset(str, 0, 150);
int count, flag = 1; //flag用于标记为使下面第一个循环能够进入
rail_node_t *p = NULL;
char target[25], clockwise[25], counterclockwise[25];
for (count = 0, p = rails; flag == 1 || p != rails; p = p->next_node, count++)
{
flag=0;
target[count] = '0';
clockwise[count] = '0';
counterclockwise[count] = '0';
} //遍历轨道链表将所有站点初始化为0无任何请求
// 在字符串的末尾填0
target[count] = '\0';
clockwise[count] = '\0';
counterclockwise[count] = '\0';
bus_query_t *t = NULL;
int i;
for (t = queries; t != NULL; t = t->next_node)
{
i = t->node->id - 1;
if (t->type == 0)
{
clockwise[i] = '1';
}
else if(t->type==BUS_COUNTER_CLOCK_WISE)
{
counterclockwise[i] = '1';
}
else if(t->type==BUS_TARGET)
{
target[i] = '1';
}
} //遍历请求链表将有请求的站点按照不同类型标记为1
char line1[100], line2[10], line3[15], line4[100], line5[10], line6[100], line7[100];
sprintf(line1, "TIME:%d\n", bus_time);
sprintf(line2, "BUS:\n");
sprintf(line3, "position:%d\n", GetBusPosition());
sprintf(line4, "target:%s\n", target);
sprintf(line5, "STATION:\n");
sprintf(line6, "clockwise:%s\n", clockwise);
sprintf(line7, "counterclockwise:%s\n", counterclockwise); //分别得到每一行的字符串
strcat(line1, line2);
strcat(line1, line3);
strcat(line1, line4);
strcat(line1, line5);
strcat(line1, line6);
strcat(line1, line7);//将7行字符串合并在一起
strcat(str, line1);
}

20
src/centralwidget.cpp Normal file
View File

@@ -0,0 +1,20 @@
//
// Created by ricardo on 2022/6/10.
//
// You may need to build the project (run Qt uic code generator) to get "ui_CentralWidget.h" resolved
#include "header/moc_centralwidget.cpp"
#include "form/ui_CentralWidget.h"
CentralWidget::CentralWidget(QWidget *parent) :
QWidget(parent), ui(new Ui::CentralWidget)
{
ui->setupUi(this);
}
CentralWidget::~CentralWidget()
{
delete ui;
}

View File

@@ -1,286 +0,0 @@
//
// Created by ricardo on 2022/5/6.
//
#include "controller.h"
bus_query_t *target_query = NULL;
int chosen_strategy = -1;
int FCFSDirection()
{
bus_query_t *p = queries;
if(p == NULL)
{
return BUS_STOP;
} //如果没有请求,公交车停止
else
{
int clockwise = 0;
int counterclockwise = 0; //用于顺,逆时针方向所经站台计数
/**
* 公交车当前所在位置
*/
rail_node_t *now_position = the_bus->rail_node_pos;
/**
* 公交车应该前进的方向
*/
rail_node_t *target_position = p->node;
rail_node_t *pos = now_position;
while (pos != target_position) //顺时针计数
{
clockwise++;
pos = pos->next_node;
}
pos = now_position;
while (pos != target_position) //逆时针计数
{
counterclockwise++;
pos = pos->last_node;
}
if(clockwise <= counterclockwise)
{
return BUS_CLOCK_WISE;
}//若顺时针距离短(或顺逆相等),公交车顺时针运行
else
{
return BUS_COUNTER_CLOCK_WISE;
}//若逆时针距离短,公交车逆时针运行
}
}
bus_query_t *FCFSQuery()
{
bus_query_t *result = NULL;
if(queries != NULL)
{
if(the_bus->rail_node_pos == queries->node)
{
result = queries;
}
}
return result;
}
bus_query_t *SSTFGetQuery()
{
// 当前没有请求
if(queries == NULL)
{
return NULL;
}
int distance = 9999;
bus_query_t *query = NULL;
bus_query_t *p = queries;
// 遍历顺时针方向
// 在两个方向路程相同时选择顺时针方向
// 所以先遍历顺时针方向
while (p != NULL)
{
int temp = GetQueryDistance(p, BUS_CLOCK_WISE);
if(temp < distance)
{
distance = temp;
query = p;
}
p = p->next_node;
}
// 遍历逆时针方向
p = queries;
while (p != NULL)
{
int temp = GetQueryDistance(p, BUS_COUNTER_CLOCK_WISE);
if(temp < distance)
{
distance = temp;
query = p;
}
p = p->next_node;
}
return query;
}
int SSTFDirection(bus_query_t* query)
{
if (query == NULL)
{
return BUS_STOP;
}
int distance = GetQueryDistance(query, BUS_CLOCK_WISE);
if(distance > all_distance / 2)
{
return BUS_COUNTER_CLOCK_WISE;
}
else if(distance == 0)
{
return BUS_STOP;
}
else
{
return BUS_CLOCK_WISE;
}
}
bus_query_t *SSTFBTWQuery(int direction)
{
bus_query_t *query = queries;
bus_query_t *allow_query = NULL;
rail_node_t *now_node = the_bus->rail_node_pos;
while (query != NULL)
{
if(query->node == now_node)
{
// 这里是设计上的缺陷在bus_time显示时间的前一秒公交车就实际上到达站台了
if(query->time < bus_time - 1)
{
if(query->type == direction || query->type == BUS_TARGET)
{
allow_query = query;
break;
}
}
}
query = query->next_node;
}
return allow_query;
}
bus_query_t *SCANGetQuery(int direction)
{
// 当前没有请求
if(queries == NULL)
{
return NULL;
}
if(direction == BUS_STOP)
{
// 在停止的状态下第一次开始选择方向
int distance = 9999;
bus_query_t *query = NULL;
bus_query_t *p = queries;
// 遍历顺时针方向
// 在两个方向路程相同时选择顺时针方向
// 所以先遍历顺时针方向
while (p != NULL)
{
int temp = GetQueryDistance(p, BUS_CLOCK_WISE);
if(temp < distance)
{
distance = temp;
query = p;
}
p = p->next_node;
}
// 遍历逆时针方向
p = queries;
while (p != NULL)
{
int temp = GetQueryDistance(p, BUS_COUNTER_CLOCK_WISE);
if(temp < distance)
{
distance = temp;
query = p;
}
p = p->next_node;
}
return query;
}
else
{
// 在已经有方向的情况下处理方向
int distance = 9999;
bus_query_t *query = NULL;
bus_query_t *p = queries;
while (p != NULL)
{
int temp = GetQueryDistance(p, direction);
if(temp < distance)
{
query = p;
distance = temp;
}
p = p->next_node;
}
return query;
}
}
int SCANDirection(bus_query_t *query, int orientation)
{
if(query == NULL)
{
return BUS_STOP;
}
if(orientation == BUS_STOP)
{
int distance = GetQueryDistance(query, BUS_CLOCK_WISE);
if(distance > all_distance / 2)
{
return BUS_COUNTER_CLOCK_WISE;
}
else
{
return BUS_CLOCK_WISE;
}
}
else
{
int distance = GetQueryDistance(query, orientation);
if(distance > all_distance / 2)
{
if(orientation == BUS_CLOCK_WISE)
{
return BUS_COUNTER_CLOCK_WISE;
}
else
{
return BUS_CLOCK_WISE;
}
}
else
{
return orientation;
}
}
}
bus_query_t *SCANBTWQuery()
{
rail_node_t *now_position = the_bus->rail_node_pos;
//获取公交车当前所在站点
bus_query_t *p = queries;
while(p != NULL)
{
if(p->node == now_position)
{
if(p->time < bus_time - 1)
{
return p;
}
}
p = p->next_node;
}//遍历请求链表,判断是否有可以顺便处理的请求
return NULL;
}

71
src/form/centralwidget.ui Normal file
View File

@@ -0,0 +1,71 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>CentralWidget</class>
<widget class="QWidget" name="CentralWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>865</width>
<height>635</height>
</rect>
</property>
<property name="windowTitle">
<string>CentralWidget</string>
</property>
<widget class="QGraphicsView" name="graphicsView">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>661</width>
<height>421</height>
</rect>
</property>
</widget>
<widget class="QWidget" name="verticalLayoutWidget">
<property name="geometry">
<rect>
<x>659</x>
<y>-1</y>
<width>201</width>
<height>421</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>控制面板</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QScrollArea" name="scrollArea">
<property name="geometry">
<rect>
<x>0</x>
<y>420</y>
<width>861</width>
<height>221</height>
</rect>
</property>
<property name="widgetResizable">
<bool>true</bool>
</property>
<widget class="QWidget" name="scrollAreaWidgetContents">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>859</width>
<height>219</height>
</rect>
</property>
</widget>
</widget>
</widget>
<resources/>
<connections/>
</ui>

47
src/form/mainwindow.ui Normal file
View File

@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>900</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>900</width>
<height>22</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">
<property name="title">
<string>File</string>
</property>
<addaction name="actionRead_ConfigFile"/>
<addaction name="actionExit"/>
</widget>
<addaction name="menuFile"/>
</widget>
<action name="actionRead_ConfigFile">
<property name="text">
<string>Read ConfigFile</string>
</property>
</action>
<action name="actionExit">
<property name="text">
<string>Exit</string>
</property>
</action>
</widget>
<resources/>
<connections/>
</ui>

View File

@@ -0,0 +1,32 @@
//
// Created by ricardo on 2022/6/10.
//
#ifndef AUTO_BUS_GUI_CENTRALWIDGET_H
#define AUTO_BUS_GUI_CENTRALWIDGET_H
#include <QWidget>
QT_BEGIN_NAMESPACE
namespace Ui
{
class CentralWidget;
}
QT_END_NAMESPACE
class CentralWidget : public QWidget
{
Q_OBJECT
public:
explicit CentralWidget(QWidget *parent = nullptr);
~CentralWidget() override;
private:
Ui::CentralWidget *ui;
};
#endif //AUTO_BUS_GUI_CENTRALWIDGET_H

36
src/header/mainwindow.h Normal file
View File

@@ -0,0 +1,36 @@
//
// Created by ricardo on 2022/6/10.
//
#ifndef AUTO_BUS_GUI_MAINWINDOW_H
#define AUTO_BUS_GUI_MAINWINDOW_H
#include <QMainWindow>
#include "centralwidget.h"
QT_BEGIN_NAMESPACE
namespace Ui
{
class MainWindow;
}
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow() override;
private:
Ui::MainWindow *ui;
CentralWidget *central_widget;
void SetMenuBarConnection();
};
#endif //AUTO_BUS_GUI_MAINWINDOW_H

31
src/mainwindow.cpp Normal file
View File

@@ -0,0 +1,31 @@
//
// Created by ricardo on 2022/6/10.
//
// You may need to build the project (run Qt uic code generator) to get "ui_MainWindow.h" resolved
#include "header/moc_mainwindow.cpp"
#include "form/ui_MainWindow.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
ui = new Ui::MainWindow;
central_widget = new CentralWidget;
ui->setupUi(this);
this->setCentralWidget(central_widget);
SetMenuBarConnection();
}
MainWindow::~MainWindow()
{
delete ui;
delete central_widget;
}
void MainWindow::SetMenuBarConnection()
{
QObject::connect(ui->actionExit, SIGNAL(triggered()), this, SLOT(close()));
}

View File

@@ -1,96 +0,0 @@
//
// Created by ricardo on 2022/5/6.
//
#include "query.h"
bus_query_t *queries = NULL;
bus_query_t *CreateQuery(int type, rail_node_t *node)
{
bus_query_t *p = (bus_query_t *)malloc(sizeof (bus_query_t));
p->node = node;
p->type = type;
p->time = bus_time;
p->next_node = NULL;
if (queries == NULL)
{
// 如果头指针是空指针,便没有必要排查是否重复
queries = p;
return p;
}
else
{
bus_query_t *query = queries;
int flag = BUS_FAlSE;
// 排查是否有重复的请求
while (query != NULL)
{
if(query->node == p->node && query->type == p->type)
{
flag = BUS_TRUE;
break;
}
query = query->next_node;
}
if(flag == BUS_TRUE)
{
// 如果存在相同的请求,这个请求就被放弃
free(p);
return NULL;
}
else
{
// 找到请求链表的最后一个节点
bus_query_t *last_query = queries;
while (last_query->next_node != NULL)
{
last_query = last_query->next_node;
}
last_query->next_node = p;
return p;
}
}
}
void DeleteQuery(bus_query_t *target)
{
if(target == queries)
{
queries = target->next_node;
}
else
{
bus_query_t *node = queries;
// 找到被删除节点的上一个节点
while(node->next_node != target)
{
node = node->next_node;
}
node->next_node = target->next_node;
}
free(target);
}
void FreeQueries(bus_query_t *head)
{
bus_query_t *p = head;
while (p != NULL)
{
bus_query_t *temp = p;
p = p->next_node;
free(temp);
}
// 将全局的请求列表头指针置为空
queries = NULL;
}

View File

@@ -1,88 +0,0 @@
#include "rail.h"
rail_node_t *rails = NULL;
int bus_time = 0;
int all_distance = 0;
rail_node_t *CreateRails(int length, int node_num)
{
/**
* 表示轨道的头节点
*/
rail_node_t *head = NULL;
rail_node_t *node = NULL;
head = (rail_node_t*) malloc(sizeof (rail_node_t));
head->id = 1;
head->last_node_distance = length;
head->next_node_distance = length;
node = head;
// 循环创建每一个节点
for(int i = 2; i <= node_num; i++)
{
rail_node_t *p = (rail_node_t*) malloc(sizeof (rail_node_t));
p->id = i;
p->last_node_distance = length;
p->next_node_distance = length;
p->last_node = node;
node->next_node = p;
node = p;
}
// 循环结束时node就是最后一个节点
node->next_node = head;
head->last_node = node;
return head;
}
rail_node_t *FindNode(rail_node_t *head, int id)
{
// 排除头节点为空的情况
if(head == NULL)
{
return NULL;
}
rail_node_t *result = NULL;
rail_node_t *p = head;
do
{
if(p->id == id)
{
result = p;
break;
}
p = p->next_node;
}
while (p != head);
return result;
}
void FreeRails(rail_node_t *head)
{
rail_node_t *p = head;
// 断开头尾节点
head->last_node->next_node = NULL;
while (p != NULL)
{
rail_node_t *temp = p;
p = p->next_node;
free(temp);
}
// 将全局的轨道指针置为空
rails = NULL;
}
void AddTime()
{
bus_time++;
}