-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConsoleInterface.cpp
158 lines (132 loc) · 3.34 KB
/
ConsoleInterface.cpp
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//
// Created by MrPark on 08.02.2019.
//
#include "ConsoleInterface.h"
int ConsoleInterface::init()
{
cout << "Input number of vertices K:" << endl;
cin >> k;
cout << "Input number of edges T:" << endl;
cin >> l;
cout << "Input edges:" << endl;
g.setParameters(k,l);
if(!g.isConnected())
{
throw DisconnectedGraphException();
}
cout << "Input number of vectors N:" << endl;
cin >> n;
cout << "Input size of vector M:" << endl;
cin >> m;
vector<int> temp(m, 0);
db.assign(n, temp);
cout << "Input vectors:" << endl;
for(auto i=0; i < n; i++)
for(int j=0; j<m; j++)
cin >> db[i][j];
query.assign(m, 0);
}
int ConsoleInterface::dijkstraStep()
{
int result = 0;
for(auto i=0; i<m; i++)
{
g.dijkstra(query[i]);
result += g.shortest_distance[db[vector_index][i]];
}
return result;
}
int ConsoleInterface::floydWarshallStep()
{
if(!vector_index)
g.floydWarshall();
int result = 0;
for(auto i=0; i<m; i++)
result += g.shortest_distances[query[i]][db[vector_index][i]];
return result;
}
int ConsoleInterface::step()
{
int current_result = INF;
if(mode==DIJKSTRA_MODE)
{
current_result = dijkstraStep();
}
else if(mode==FLOYD_WARSHALL_MODE)
{
current_result = floydWarshallStep();
}
if(current_result < best_result)
{
best_result = current_result;
best_index = vector_index;
}
if(++vector_index == n)
{
return 0;
}
return vector_index;
}
int ConsoleInterface::run()
{
cout << "Input query vector:" << endl;
for(auto i=0; i<m; i++)
{
cin >> query[i];
}
char userKey=0;
while(step() && userKey!=USER_KEY_CANCEL)
{
cout << best_result << endl;
// отобразить состояние задачи
switch(mode)
{
case DIJKSTRA_MODE:
cout << "DIJKSTRA MODE" << endl;
break;
case FLOYD_WARSHALL_MODE:
cout << "FLOYD-WARSHALL MODE" << endl;
break;
default:
break;
};
cout << "Checked vector is: ";
for(auto i=0; i<m; i++)
{
cout << db[vector_index-1][i] << ' ';
}
cout << endl;
cout << "Best vector is: ";
for(auto i=0; i<m; i++)
{
cout << db[best_index][i] << ' ';
}
cout << endl;
cout<<"Press any key to continue (to cancel press 'c'):" << endl;
userKey = static_cast<char>(getchar());
}
cout << "The nearest vector from database:" << endl;
for(auto i=0; i<m; i++)
{
cout << db[best_index][i] << ' ';
}
cout << endl;
}
void ConsoleInterface::setData(unsigned long long k, unsigned long long l, unsigned long long m, unsigned long long n, vector<vector<int> > &edges, vector<vector<int> > &db, vector<int> &query)
{
this->k = k;
this->l = l;
this->m = m;
this->n = n;
this->g.setGraph(k, l, edges);
this->db.insert(this->db.begin(), db.begin(), db.end());
this->query.insert(this->query.begin(), query.begin(), query.end());
}
void ConsoleInterface::solve()
{
int c = 0;
while(step())
{
c++; // c++ is a general-purpose programming language.
}
}