-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoop_graph.cpp
More file actions
244 lines (213 loc) · 6.36 KB
/
oop_graph.cpp
File metadata and controls
244 lines (213 loc) · 6.36 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include <iostream>
#include <list>
#include <string>
#include <stack>
#include <math.h>
#include <stdio.h>
using namespace std;
class Titik
{
public:
int id;
string nama_tempat;
double x;
double y;
Titik() : id(0), nama_tempat(""), x(0.0), y(0.0) {}
Titik(int id, string nama_tempat, double x, double y)
{
this->id = id;
this->nama_tempat = nama_tempat;
this->x = x;
this->y = y;
}
};
// Mendefinisikan "Class" Peta yang mengimplementasikan struktur data "Graph"
// Ini "Class Peta"
class Peta
{
private:
// Property list
int jumlah_titik;
list<int> *adjacency_list;
// Property matrix
int **adjacency_matrix;
Titik *titik_array;
public:
// Constructor
Peta(int jumlah_titik)
{
this->jumlah_titik = jumlah_titik;
adjacency_list = new list<int>[jumlah_titik];
inisialisasiAdjMatrix(jumlah_titik);
titik_array = new Titik[jumlah_titik];
}
// Destructor
~Peta()
{
delete[] adjacency_list;
for (int i = 0; i < jumlah_titik; i++)
{
delete[] adjacency_matrix[i];
}
delete[] adjacency_matrix;
delete[] titik_array;
}
void inisialisasiAdjMatrix(int jumlah_titik)
{
adjacency_matrix = new int *[jumlah_titik];
for (int i = 0; i < jumlah_titik; i++)
{
adjacency_matrix[i] = new int[jumlah_titik];
for (int j = 0; j < jumlah_titik; j++)
{
adjacency_matrix[i][j] = 0;
}
}
}
// Fungsi untuk menambahkan koneksi dari titik awal ke tujuan
void tambahLintasan(int asal, int tujuan)
{
adjacency_list[asal].push_back(tujuan);
adjacency_matrix[asal][tujuan] = 1;
adjacency_matrix[tujuan][asal] = 1;
}
// Fungsi untuk menampilkan adjacency list
void tampilkanAdjList()
{
list<int>::iterator i;
for (int v = 0; v < jumlah_titik; v++)
{
cout << v + 1 << " -> ";
for (i = adjacency_list[v].begin(); i != adjacency_list[v].end(); ++i)
{
cout << (*i);
if (next(i, 1) != adjacency_list[v].end())
{
cout << " -> ";
}
}
cout << endl;
}
}
void tampilkanAdjMatrix()
{
for (int i = 0; i < jumlah_titik; i++)
{
for (int j = 0; j < jumlah_titik; j++)
{
cout << adjacency_matrix[i][j] << " ";
}
cout << endl;
}
}
void tambahTitik(int id, string nama_tempat, double x, double y)
{
titik_array[id - 1] = Titik(id, nama_tempat, x, y);
}
void tampilkanTitik()
{
for (int i = 0; i < jumlah_titik; i++)
{
cout << "Titik: " << titik_array[i].id << endl;
cout << "Nama Tempat: " << titik_array[i].nama_tempat << endl;
cout << "Koordinat (x, y): (" << titik_array[i].x << ", " << titik_array[i].y << ")" << endl;
cout << endl;
}
}
double hitungJarak(int titik_awal, int titik_tujuan)
{
if (titik_awal < 1 || titik_awal > jumlah_titik || titik_tujuan < 1 || titik_tujuan > jumlah_titik)
{
cout << "ID titik tidak valid." << endl;
return -1.0; // Nilai negatif menunjukkan kesalahan
}
double x1 = titik_array[titik_awal - 1].x;
double y1 = titik_array[titik_awal - 1].y;
double x2 = titik_array[titik_tujuan - 1].x;
double y2 = titik_array[titik_tujuan - 1].y;
// Menghitung jarak dengan rumus Euclidean distance
double jarak = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
return jarak;
}
};
int main()
{
int asal, tujuan;
int choice;
cout << "Peta Rumah" << endl;
int jumlah_titik = 10;
// Ini "Object petaKu"
// Object adalah instansiasi dari Class
Peta petaKu(jumlah_titik);
// Mendefinisikan data untuk graf Peta
petaKu.tambahLintasan(0, 1); // dari titik 0 ke 1
petaKu.tambahLintasan(0, 4); // dari titik 0 ke 4
petaKu.tambahLintasan(1, 2);
petaKu.tambahLintasan(1, 4);
petaKu.tambahLintasan(1, 5);
petaKu.tambahLintasan(2, 3);
petaKu.tambahLintasan(4, 6);
petaKu.tambahLintasan(3, 8);
petaKu.tambahLintasan(6, 9);
petaKu.tambahLintasan(6, 7);
petaKu.tambahLintasan(7, 9);
petaKu.tambahLintasan(7, 8);
petaKu.tambahLintasan(5, 4);
petaKu.tambahLintasan(5, 1);
petaKu.tambahLintasan(4, 1);
petaKu.tambahLintasan(4, 0);
petaKu.tambahLintasan(6, 4);
petaKu.tambahLintasan(7, 6);
petaKu.tambahLintasan(9, 6);
petaKu.tambahLintasan(9, 7);
petaKu.tambahLintasan(8, 7);
petaKu.tambahTitik(0, "Universitas Widya Kartika", 0.0, 5.0);
petaKu.tambahTitik(1, "Spectrum", 1.0, 3.0);
petaKu.tambahTitik(2, "Mcd", 1.0, 2.0);
petaKu.tambahTitik(3, "SAS", 0.0, 2.0);
petaKu.tambahTitik(4, "Home", 3.0, 5.0);
petaKu.tambahTitik(5, "Mulyosari Tengah", 3.0, 3.0);
petaKu.tambahTitik(6, "San Antonio", 5.0, 4.0);
petaKu.tambahTitik(7, "San Diego", 6.0, 2.0);
petaKu.tambahTitik(8, "Palem Cinde", 7.0, 1.0);
petaKu.tambahTitik(9, "Hokky", 8.0, 5.0);
cout << endl;
cout << "Adjacency List" << endl;
petaKu.tampilkanAdjList();
cout << endl;
cout << "Adjacency Matrix" << endl;
petaKu.tampilkanAdjMatrix();
cout << endl;
cout << "Informasi Titik" << endl;
petaKu.tampilkanTitik();
while (true)
{
cout << endl;
cout << "=== MENU ===" << endl;
cout << "1. Find distance between two places" << endl;
cout << "2. Exit program" << endl;
cout << "Input Choice: ";
cin >> choice;
if (choice == 1)
{
cout << "ID of origin: ";
cin >> asal;
cout << "ID of destination: ";
cin >> tujuan;
double jarak = petaKu.hitungJarak(asal, tujuan);
if (jarak >= 0)
{
cout << "Distance between " << asal << " and " << tujuan << " is " << jarak << " units." << endl;
}
}
else if (choice == 2)
{
cout << "Exiting the program..." << endl;
break;
}
else
{
cout << "Pilihan tidak valid. Silakan pilih 1 atau 2." << endl;
}
}
}