-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtouristwindow.cpp
More file actions
102 lines (83 loc) · 3.08 KB
/
Copy pathtouristwindow.cpp
File metadata and controls
102 lines (83 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "touristwindow.h"
#include "ui_touristwindow.h"
#include <QVBoxLayout>
#include <QLabel>
#include<QDebug>
TouristWindow::TouristWindow(Graph *graph, QWidget *parent)
: QMainWindow(parent), graph(graph)
{
QWidget *centralWidget = new QWidget(this);
this->setWindowTitle("路线查询");
setFixedSize(626,402);
move(785,185);
QVBoxLayout *layout = new QVBoxLayout(centralWidget);
QLabel *startLabel = new QLabel("起点:", this);
startLineEdit = new QLineEdit(this);
QLabel *endLabel = new QLabel("终点:", this);
endLineEdit = new QLineEdit(this);
queryButton = new QPushButton("查询", this);
connect(queryButton, &QPushButton::clicked, this, &TouristWindow::onQueryClicked);
resultTextEdit = new QTextEdit(this);
resultTextEdit->setReadOnly(true);
layout->addWidget(startLabel);
layout->addWidget(startLineEdit);
layout->addWidget(endLabel);
layout->addWidget(endLineEdit);
layout->addWidget(queryButton);
layout->addWidget(resultTextEdit);
setCentralWidget(centralWidget);
}
TouristWindow::~TouristWindow() {}
void TouristWindow::onQueryClicked()
{
QString start = startLineEdit->text();
QString end = endLineEdit->text();
if (start.isEmpty() || end.isEmpty()) {
resultTextEdit->setText("请输入有效的起点和终点");
return;
}
std::vector<int> shortestPath = graph->getShortestPathByName(start.toStdString(), end.toStdString());
std::vector<std::vector<int>> allPaths = graph->getAllPathsByName(start.toStdString(), end.toStdString());
QString result;
if (shortestPath.empty()) {
resultTextEdit->setText("起点或终点不存在");
} else {
result += "从" + start + "到" + end + "的最短路径为: ";
for (int i = 0; i < shortestPath.size(); ++i) {
result += QString::fromStdString(graph->getVertexInfo(shortestPath[i]));
if (i != shortestPath.size() - 1) {
result += " -> ";
}
}
result += "; 最短路径距离为:";
int dist=0;
std::vector<int>::iterator vert1 = shortestPath.begin();
std::vector<int>::iterator vert2 = vert1++;
for(int i=1;i<shortestPath.size();i++){
dist += graph->getDist(*vert1,*vert2);
vert1++;
vert2++;
}
result += QString::number(dist);
result += " 米";
result += "\n";
}
if (allPaths.empty()) {resultTextEdit->setText("起点或终点不存在");}
else {
result += "从" + start + "到" + end + "的所有路径:\n";
std::vector<int> prevPath;
for (const auto &path : allPaths) {
if(path!=prevPath){
for (int i = 0; i < path.size(); ++i) {
result += QString::fromStdString(graph->getVertexInfo(path[i]));
if (i != path.size() - 1) {
result += " -> ";
}
}
result += "\n";
prevPath=path;
}
}
}
resultTextEdit->setText(result);
}