diff --git a/Campus_tour_guide_consulting_system.pro b/Campus_tour_guide_consulting_system.pro new file mode 100644 index 0000000000000000000000000000000000000000..b383532600899c8c86f1cdc5ad13300b5e975fcf --- /dev/null +++ b/Campus_tour_guide_consulting_system.pro @@ -0,0 +1,29 @@ +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +CONFIG += c++17 + +# You can make your code fail to compile if it uses deprecated APIs. +# In order to do so, uncomment the following line. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +SOURCES += \ + graph.cpp \ + main.cpp \ + mainwindow.cpp + +HEADERS += \ + graph.h \ + mainwindow.h + +FORMS += \ + mainwindow.ui + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target + +RESOURCES += \ + images/images.qrc diff --git a/Campus_tour_guide_consulting_system.pro.user b/Campus_tour_guide_consulting_system.pro.user new file mode 100644 index 0000000000000000000000000000000000000000..c845e92ba0a0703af5fe008d2a135f2205291786 --- /dev/null +++ b/Campus_tour_guide_consulting_system.pro.user @@ -0,0 +1,268 @@ + + + + + + EnvironmentId + {5ee1fd6c-afe8-46e4-91b8-3f613c228dd1} + + + ProjectExplorer.Project.ActiveTarget + 0 + + + ProjectExplorer.Project.EditorSettings + + true + false + true + + Cpp + + CppGlobal + + + + QmlJS + + QmlJSGlobal + + + 2 + UTF-8 + false + 4 + false + 80 + true + true + 1 + false + true + false + 0 + true + true + 0 + 8 + true + false + 1 + true + true + true + *.md, *.MD, Makefile + false + true + true + + + + ProjectExplorer.Project.PluginSettings + + + true + false + true + true + true + true + + + 0 + true + + true + true + Builtin.DefaultTidyAndClazy + 8 + + + + true + + + false + true + + + + + ProjectExplorer.Project.Target.0 + + Desktop + Desktop Qt 6.5.0 MinGW 64-bit + Desktop Qt 6.5.0 MinGW 64-bit + qt.qt6.650.win64_mingw_kit + 0 + 0 + 0 + + 0 + E:\dataStructure\work\test\build-Campus_tour_guide_consulting_system-Desktop_Qt_6_5_0_MinGW_64_bit-Debug + E:/dataStructure/work/test/build-Campus_tour_guide_consulting_system-Desktop_Qt_6_5_0_MinGW_64_bit-Debug + + + true + QtProjectManager.QMakeBuildStep + false + + + + true + Qt4ProjectManager.MakeStep + + 2 + 构建 + 构建 + ProjectExplorer.BuildSteps.Build + + + + true + Qt4ProjectManager.MakeStep + clean + + 1 + 清除 + 清除 + ProjectExplorer.BuildSteps.Clean + + 2 + false + + false + + Debug + Qt4ProjectManager.Qt4BuildConfiguration + 2 + + + E:\dataStructure\work\test\build-Campus_tour_guide_consulting_system-Desktop_Qt_6_5_0_MinGW_64_bit-Release + E:/dataStructure/work/test/build-Campus_tour_guide_consulting_system-Desktop_Qt_6_5_0_MinGW_64_bit-Release + + + true + QtProjectManager.QMakeBuildStep + false + + + + true + Qt4ProjectManager.MakeStep + + 2 + 构建 + 构建 + ProjectExplorer.BuildSteps.Build + + + + true + Qt4ProjectManager.MakeStep + clean + + 1 + 清除 + 清除 + ProjectExplorer.BuildSteps.Clean + + 2 + false + + false + + Release + Qt4ProjectManager.Qt4BuildConfiguration + 0 + 0 + + + 0 + E:\dataStructure\work\test\build-Campus_tour_guide_consulting_system-Desktop_Qt_6_5_0_MinGW_64_bit-Profile + E:/dataStructure/work/test/build-Campus_tour_guide_consulting_system-Desktop_Qt_6_5_0_MinGW_64_bit-Profile + + + true + QtProjectManager.QMakeBuildStep + false + + + + true + Qt4ProjectManager.MakeStep + + 2 + 构建 + 构建 + ProjectExplorer.BuildSteps.Build + + + + true + Qt4ProjectManager.MakeStep + clean + + 1 + 清除 + 清除 + ProjectExplorer.BuildSteps.Clean + + 2 + false + + false + + Profile + Qt4ProjectManager.Qt4BuildConfiguration + 0 + 0 + 0 + + 3 + + + 0 + 部署 + 部署 + ProjectExplorer.BuildSteps.Deploy + + 1 + + false + ProjectExplorer.DefaultDeployConfiguration + + 1 + + true + true + true + + 2 + + Campus_tour_guide_consulting_system2 + Qt4ProjectManager.Qt4RunConfiguration:E:/dataStructure/work/write-tld/Campus_tour_guide_consulting_system/Campus_tour_guide_consulting_system.pro + E:/dataStructure/work/write-tld/Campus_tour_guide_consulting_system/Campus_tour_guide_consulting_system.pro + false + true + true + false + true + E:/dataStructure/work/test/build-Campus_tour_guide_consulting_system-Desktop_Qt_6_5_0_MinGW_64_bit-Debug + + 1 + + + + ProjectExplorer.Project.TargetCount + 1 + + + ProjectExplorer.Project.Updater.FileVersion + 22 + + + Version + 22 + + diff --git a/graph.cpp b/graph.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d7785d8e46500dcecf37d6afed5169e38d2599e0 --- /dev/null +++ b/graph.cpp @@ -0,0 +1,174 @@ + +#include "graph.h" +#include +using namespace std; +//添加一条边函数 +//addEdge(int src, int dest, T weight) 用于在图中添加一条边。首先,它会检查传入的顶点索引 src 和 dest (两个顶点)是否在有效的范围内 +//如果是有效的顶点,它将在邻接矩阵中的 adjacencyMatrix[src][dest] 位置设置边的权重为 weight +//在 adjacencyMatrix[dest][src] 位置设置相同的权重。如果顶点索引无效,它会输出错误消息。 +template +void graph::addEdge(roadNode road)//在src和dest顶点之间建立weight的权重 +{ + if ((road.src >= 0 && road.src < numVertices )&&( road.dest >= 0 && road.dest < numVertices)) + { + adjacencyMatrix[road.src][road.dest] = road; + + adjacencyMatrix[road.dest][road.src] = road; + } else { + cout << "[error]顶点索引无效" << endl; + } +} + +//删除一条边函数 +//函数 removeEdge(int src, int dest) 用于从图中移除一条边 +//首先检查传入的顶点索引是否有效。如果有效,它会将邻接矩阵中的 adjacencyMatrix[src][dest] 位置的边权重设为 0 +//将 adjacencyMatrix[dest][src] 位置的边权重设为 0。如果顶点索引无效,它会输出错误消息。 +template +void graph::removeEdge(int src, int dest) { + if (src >= 0 && src < numVertices && dest >= 0 && dest < numVertices) { + adjacencyMatrix[src][dest] = {0,"0",0,0,0}; + + adjacencyMatrix[dest][src] = {0,"0",0,0,0}; + } else { + cout << "[error]顶点索引无效" << endl; + } +} + + + + +//dijkstra算法 +// Dijkstra 算法计算给定图中从起始顶点 src +//其中,distances 是一个 vector,用于存储每个顶点的最短路径长度 +//算法首先初始化 distances 数组,并创建一个优先队列 pq 存储顶点和最短路径长度的 pair。 +//在每次循环中,从优先队列中取出最短路径长度最小的顶点 u,然后遍历其邻接顶点 v +//如果通过顶点 u 可以获得到顶点 v 更短的路径,则更新 distances[v] 为新的路径长度 +//并将 v 和其最短路径长度加入优先队列 pq +//最终,当优先队列为空时,算法结束,distances 中存储的就是从起始顶点 src 到其他每个顶点的最短路径长度。 + + + +template +void graph::dijkstra(int src)//起始顶点 +{ + // 记录顶点的最短路径长度 + //设置 distances 向量的大小为 numVertices,并用 numeric_limits::max() 初始化所有元素的值。这是为了存储每个顶点的最短路径长度。 + + //初始化路径 + //由src到i点的路径为“src,length” + for(int j = 0;j,int> data0 = {{interest[src]},-1}; + path.push_back(data0); + } + + + + //创建队列 + //存储顶点和最短路径长度的 pair + //创建一个优先路径队列 pq + //priority_queue: priority_queue 是一个 STL(标准模板库)中提供的容器适配器。 + //priority_queue 是一个模板类,它用于实现一个优先队列。在这个队列中,元素按照一定的优先级顺序出队,队列中的元素是通过比较其关联的值来确定优先级的。 + //以一种特定的方式组织元素,使得可以通过特定的规则定位最值元素。这里使用优先队列来存储顶点和最短路径长度的 pair。(方便快速查找) + //这里使用 pair 作为元素类型 [把地图节点T类型和最短路径长度int类型组合成新数据类型] + //vector>: 这是 priority_queue 内部使用的底层容器类型,用于存储元素。在这里,我们选择使用 vector 作为底层容器 + //greater> 用于比较 pair 的大小。 [较大int值的pair会被优先出队] + //根据给定的比较器 greater 来组织元素的优先级 + struct cmp { + bool operator()(const std::pair& a, const std::pair& b) { + return a.second > b.second; + } + }; + priority_queue, vector>,cmp> pq; + bool* arrived = new bool[numVertices]; + + //将起始顶点 src 和其最短路径长度(初始为 0)加入优先队列 pq。这表示从起始顶点开始的最短路径。 + //make_pair 是一个辅助函数模板,用于创建一个 pair 对象。允许我们以更简洁的方式创建一个 pair 对象,并自动推导出 pair 中成员的类型。 + //(由于使用的是类模板,这样可以更方便)(你可以理解为pair中创建了一个0, src) + //可以帮助我们避免显式指定 pair 的模板参数类型,编译器会根据传递给 make_pair 的参数自动推导出正确的类型。 + for(int j = 0;j 0){ //把src的所有路导入pq + pair data = {adjacencyMatrix[src][j],adjacencyMatrix[src][j].length}; + pq.push(data); + } + } + arrived[src] =true; //src视为到达 + + + + while (!pq.empty())//当优先队列 pq 不为空时(说明没有结束),继续执行循环。循环的目的是通过不断更新路径来寻找最短路径(比较)。 + { + //从优先队列里面输出目前的最短道路 + roadNode shortRoad = pq.top().first; + int u = shortRoad.src; //最短道路是从谁出发的(必与已经到达的点相连) + int v = shortRoad.dest; //最短道路是到达哪里的 + pq.pop();//移除路径长度最小的顶点。 + if(arrived[v] ==true){ //由于是无向图 + if(arrived[u] == true){ //路的两边都走过了 + qDebug()<<"走过了:"<调换 + int temp =u; + u = v; + v = temp; + } + } + qDebug()<同样更新为这一次的路径 + qDebug()<<"以前有路来这里,现在路径更短:"< data = {adjacencyMatrix[v][i],adjacencyMatrix[v][i].length}; + pq.push(data); //把新路径放进pq,长度作为比较依据 + } + } + //结束操作,进行下一次比对 + } + int count = 0; + for (vector,int>>::iterator iter = path.begin(); + iter != path.end(); iter++) + { + pair,int> vec_temp = *iter; + qDebug()<<"this is the route of the"<::iterator it = vec_temp.first.begin(); it != vec_temp.first.end(); it++) + { + qDebug() << (*it).name<<" "; + } + + } +// for (const auto &element : path) { +// // 访问 pair 的第一个元素,也就是兴趣点向量 +// const std::vector &interestNodes = element.first; +// // 遍历兴趣点向量,并输出每个兴趣点的 name 成员变量 +// for (const auto &node : interestNodes) { +// std::cout << "Interest point name: " << node.name << std::endl; +// } + +// } + + delete[] arrived; +} diff --git a/graph.h b/graph.h new file mode 100644 index 0000000000000000000000000000000000000000..b4b397a4e650848b3c7deb3f9f67f0cea45bb30b --- /dev/null +++ b/graph.h @@ -0,0 +1,106 @@ + +#ifndef GRAPH_H +#define GRAPH_H + +#include +#include +#include +#include +//注: 是 C++ 标准库中的头文件,提供了各种与类型的极限值相关的常量和函数 +//该头文件中定义了各种模板,用于获取不同类型的极限值,例如整数类型的最大值和最小值、浮点数类型的最大值和最小值、字符类型的最大值和最小值等。(方便处理) +#include +using namespace std; + + + + +struct InterestNode{ + int id; + string name; + string inf; +}; + +struct roadNode{ + int id; + string name; + int src; + int dest; + int length; +}; + + + +//图 +template +class graph { +private: + vector interest; + vector> adjacencyMatrix;//邻接矩阵 0表示没有路径 + int numVertices;//顶点个数 + vector,int>> path; //存储某一点到各点之间的最短路径和长度 path.first为路径(用景点表示顺序),path.second为长度 + +public: + //构造函数 + //在构造函数 graph中,根据传入的顶点数量 numVertices 来初始化邻接矩阵 + //注:resize 是一个 vector 的成员函数,用于调整 vector 的大小,第一个参数是代表numVertices行,相当于每一行都是一个大小为numVertices的vector + //通过调用 adjacencyMatrix.resize(numVertices, vector(numVertices, 0)) + //将邻接矩阵的大小调整为 numVertices * numVertices,并将所有元素初始化为 0。 + graph(int numVertices) : numVertices(numVertices) { + adjacencyMatrix.resize(numVertices, vector(numVertices, {0,"0",0,0,-1})); + for(int i = 0;i++;i>::iterator iter = adjacencyMatrix.begin(); + iter != adjacencyMatrix.end(); iter++) // typename用于消除歧义,显示告诉编译器vector>::iterator是类型名,而不是某类的成员 + { + vector vec_temp = *iter; + for (vector::iterator it = vec_temp.begin(); it != vec_temp.end(); it++) + { + cout << (*it).length << "\t"; + } + cout << endl; + } + } +}; + + + + + + + +#endif // GRAPH_H + + diff --git a/images/images.qrc b/images/images.qrc new file mode 100644 index 0000000000000000000000000000000000000000..0e88eb78e4c08e431a8008c6ea2128844c9a6b41 --- /dev/null +++ b/images/images.qrc @@ -0,0 +1,5 @@ + + + map.jpg + + diff --git a/images/map.jpg b/images/map.jpg new file mode 100644 index 0000000000000000000000000000000000000000..20452721505c8831d1aec637d28b9715b181f0fc Binary files /dev/null and b/images/map.jpg differ diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ac327b21e59f2f56a4f15889f5f000941e838094 --- /dev/null +++ b/main.cpp @@ -0,0 +1,29 @@ + +#include "mainwindow.h" + +#include"graph.h" +#include"graph.cpp" +#include +#include +#include + + +int main(int argc, char *argv[]) +{ + roadNode roadA= {1,"a",0,1,8}; + roadNode roadB= {2,"b",1,2,2}; + roadNode roadC= {3,"c",0,2,4}; + graph test(3); + test.addEdge(roadA); + test.addEdge(roadB); + test.addEdge(roadC); + test.print(); + test.dijkstra(0); + + + + QApplication a(argc, argv); + MainWindow w; + w.show(); + return a.exec(); +} diff --git a/mainwindow.cpp b/mainwindow.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4f526669a6f3a7b2bf49d6a6e762f0bcb6de9191 --- /dev/null +++ b/mainwindow.cpp @@ -0,0 +1,24 @@ + +#include "mainwindow.h" +#include "ui_mainwindow.h" + + +MainWindow::MainWindow(QWidget *parent) + : QMainWindow(parent) + , ui(new Ui::MainWindow) +{ + QPixmap pixmap(":/map.jpg"); //创建一个 `QPixmap` 对象,将需要显示的图片加载到其中 + static QGraphicsScene scene; //创建一个 `QGraphicsScene` 对象,并将 `pixmap` 添加到其中 + scene.addPixmap(pixmap); //将 `scene` 设置给 `QGraphicsView` 对象: + ui->setupUi(this); + //ui->mapview = new QGraphicsView; + ui->mapview->setScene(&scene); + ui->noteLabel->setPixmap(pixmap); +} + +MainWindow::~MainWindow() +{ + delete ui; +} + + diff --git a/mainwindow.h b/mainwindow.h new file mode 100644 index 0000000000000000000000000000000000000000..b6bae316a037e7d6428dfca1f7d61ccf40b79226 --- /dev/null +++ b/mainwindow.h @@ -0,0 +1,26 @@ +//this is a note +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include + + + +QT_BEGIN_NAMESPACE +namespace Ui { class MainWindow; } +QT_END_NAMESPACE + +class MainWindow : public QMainWindow + +{ + Q_OBJECT + +public: + MainWindow(QWidget *parent = nullptr); + ~MainWindow(); + +private: + Ui::MainWindow *ui; +}; + +#endif // MAINWINDOW_H diff --git a/mainwindow.ui b/mainwindow.ui new file mode 100644 index 0000000000000000000000000000000000000000..4885be411b3b453ca2e7be9ad7f440707ec2f4f8 --- /dev/null +++ b/mainwindow.ui @@ -0,0 +1,105 @@ + + + MainWindow + + + + 0 + 0 + 1044 + 737 + + + + MainWindow + + + + + + 50 + 40 + 69 + 20 + + + + TextLabel + + + + + + 880 + 90 + 141 + 591 + + + + + + + + + + + + + + + + + + + + + + 添加 + + + + + + + 删除 + + + + + + + 导航 + + + + + + + + + + + 10 + 90 + 851 + 591 + + + + + + + + 0 + 0 + 1044 + 26 + + + + + + + +