-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMymapsFix-1.cpp
1078 lines (935 loc) · 29.9 KB
/
MymapsFix-1.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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <fstream>
#include <iostream>
#include <limits>
#include <queue>
#include <sstream>
#include <string>
#include <conio.h>
using namespace std;
// deklarasi maksimal lokasi
const int MAX_LOCATIONS = 100;
// deklarasi void menjadi global
void login();
void daftar();
struct Location {
string name;
string address;
int connections[MAX_LOCATIONS];
int weights[MAX_LOCATIONS];
int numConnections;
};
Location locations[MAX_LOCATIONS];
int numLocations = 0;
// Menambahkan lokasi ke dalam queue
void addLocation(string fileName) {
if (numLocations >= MAX_LOCATIONS) {
cout << "Kapasitas lokasi penuh." << endl;
return;
}
cout << "Lokasi ke-" << numLocations + 1 << endl;
Location location;
cout << "Masukkan nama lokasi: ";
cin.ignore(numeric_limits<streamsize>::max(), '\n');
getline(cin, location.name);
cout << "Masukkan alamat lokasi: ";
getline(cin, location.address);
location.numConnections = 0;
locations[numLocations] = location;
numLocations++;
cout << "Lokasi berhasil ditambahkan!" << endl;
// menambahkan data lokasi ke dalam txt
ifstream inFile(fileName.c_str());
ofstream outFile("temp.txt", ios::app);
// memasukkan file kedalam txt
if (inFile.is_open() && outFile.is_open()) {
string line;
while (getline(inFile, line)) {
if (line == "Koneksi Berarah") {
outFile << location.name << endl;
outFile << location.address << endl;
}
outFile << line << endl;
}
inFile.close();
outFile.close();
remove(fileName.c_str());
rename("temp.txt", fileName.c_str());
} else {
cout << "Gagal membuka file." << endl;
}
}
// Menampilkan lokasi yang sudah ada
void printLocations() {
cout << "Daftar Lokasi:" << endl;
for (int i = 0; i < numLocations; i++) {
cout << "Lokasi " << i + 1 << ":" << endl;
cout << "Nama: " << locations[i].name << endl;
cout << "Alamat: " << locations[i].address << endl;
cout << endl;
}
}
// Menambahkan koneksi antara lokasi yang sudah ada
void connectLocations(string fileName) {
if (numLocations < 2) {
cout << "Tidak ada cukup lokasi untuk melakukan koneksi." << endl;
return;
}
int source, target, weight;
cout << "Pilih sumber lokasi [1-" << numLocations << "]: ";
cin >> source;
if (source < 1 || source > numLocations) {
cout << "Indeks sumber lokasi tidak valid." << endl;
return;
}
cout << "Pilih target lokasi [1-" << numLocations << "]: ";
cin >> target;
if (target < 1 || target > numLocations) {
cout << "Indeks target lokasi tidak valid." << endl;
return;
}
if (source == target) {
cout << "Sumber dan target lokasi sama." << endl;
return;
}
// Periksa apakah sumber dan target sudah terhubung
bool isConnected = false;
for (int i = 0; i < locations[source - 1].numConnections; i++) {
if (locations[source - 1].connections[i] == target - 1) {
cout << "Lokasi " << source << " dan Lokasi " << target << " sudah terhubung." << endl;
cout << "Jarak: " << locations[source - 1].weights[i] << "km" << endl;
isConnected = true;
break;
}
}
if (!isConnected) {
penjaluran :
string jalur;
cout << "Apakah ini satu jalur?[y/t]: ";
cin >> jalur;
if (jalur == "y" || jalur == "Y") {
cout << "Masukkan jarak: ";
cin >> weight;
locations[source - 1].connections[locations[source - 1].numConnections] = target - 1;
locations[source - 1].weights[locations[source - 1].numConnections] = weight;
locations[source - 1].numConnections++;
// menambahkan data koneksi berarah ke dalam txt
ifstream inFile(fileName.c_str());
ofstream outFile("temp.txt", ios::app);
// memasukkan file kedalam txt
if (inFile.is_open() && outFile.is_open()) {
string line;
while (getline(inFile, line)) {
if (line == "Koneksi Tak Berarah") {
outFile << source - 1 << " " << target - 1 << " " << weight << endl;
}
outFile << line << endl;
}
inFile.close();
outFile.close();
remove(fileName.c_str());
rename("temp.txt", fileName.c_str());
} else {
cout << "Gagal membuka file." << endl;
}
}
else if (jalur == "t" || jalur == "T") {
cout << "Masukkan jarak: ";
cin >> weight;
locations[source - 1].connections[locations[source - 1].numConnections] = target - 1;
locations[source - 1].weights[locations[source - 1].numConnections] = weight;
locations[source - 1].numConnections++;
locations[target - 1].connections[locations[target - 1].numConnections] = source - 1;
locations[target - 1].weights[locations[target - 1].numConnections] = weight;
locations[target - 1].numConnections++;
// menambahkan data koneksi tak berarah ke dalam txt
ofstream outFile(fileName.c_str(), ios::app);
if (outFile.is_open()) {
outFile << source - 1 << " " << target - 1 << " " << weight << endl;
outFile.close();
} else {
cout << "Gagal membuka file." << endl;
}
}
else {
cout << "Silahkan input dengan benar! \n" << endl;
goto penjaluran;
}
cout << "Lokasi " << source << " dan Lokasi " << target << " berhasil terhubung dengan jarak " << weight << "km." << endl;
}
}
// Algoritma Dijkstra untuk mencari jalur terpendek
void dijkstra(int source, int target) {
int distance[MAX_LOCATIONS];
int parent[MAX_LOCATIONS];
for (int i = 0; i < numLocations; i++) {
distance[i] = numeric_limits<int>::max();
parent[i] = -1;
}
distance[source] = 0;
priority_queue<pair<int, int>, vector<pair<int, int>>,
greater<pair<int, int>>> pq;
pq.push(make_pair(0, source));
while (!pq.empty()) {
int u = pq.top().second;
pq.pop();
for (int i = 0; i < locations[u].numConnections; i++) {
int v = locations[u].connections[i];
int weight = locations[u].weights[i];
if (distance[v] > distance[u] + weight) {
distance[v] = distance[u] + weight;
parent[v] = u;
pq.push(make_pair(distance[v], v));
}
}
}
if (distance[target] == numeric_limits<int>::max()) {
cout << "Tidak ada jalur yang tersedia." << endl;
return;
}
cout << "Jalur terpendek dari " << locations[source].name << " ke "
<< locations[target].name << ":" << endl;
int current = target;
vector<int> path;
while (current != -1) {
path.push_back(current);
current = parent[current];
}
// Menampilkan jalur terpendek
for (int i = path.size() - 1; i >= 0; i--) {
cout << locations[path[i]].name;
if (i > 0) {
cout << " -> ";
}
}
cout << endl;
cout << "Jarak yang ditempuh: " << distance[target] << "km" << endl;
}
// Mencari jalur terpendek antara dua lokasi
void shortestPath(string fileName) {
if (numLocations < 2) {
cout << "Tidak ada cukup lokasi untuk mencari jalur terpendek." << endl;
return;
}
int source, target;
cout << "Pilih lokasi asal [1-" << numLocations << "]: ";
cin >> source;
if (source < 1 || source > numLocations) {
cout << "Indeks lokasi asal tidak valid." << endl;
return;
}
cout << "Pilih lokasi tujuan [1-" << numLocations << "]: ";
cin >> target;
if (target < 1 || target > numLocations) {
cout << "Indeks lokasi tujuan tidak valid." << endl;
return;
}
source--;
target--;
// setelah memilih lokasi asal dan tujuan, lalu jalankan algoritma dijkstra
dijkstra(source, target);
}
// Memuat data lokasi dari file
void loadData(const string &fileName) {
ifstream inFile(fileName);
if (inFile.is_open()) {
numLocations = 0;
string line, name, address;
// pengecekan apakah sedang membaca bagian lokasi atau koneksi
bool isLocationSection = false;
bool isDirectionalConnectionSection = false;
bool isPointlessConnectionSection = false;
while (getline(inFile, line)) {
if (line == "Lokasi") {
isLocationSection = true;
isDirectionalConnectionSection = false;
isPointlessConnectionSection = false;
continue;
}
else if (line == "Koneksi Berarah") {
isLocationSection = false;
isDirectionalConnectionSection = true;
isPointlessConnectionSection = false;
continue;
}
else if (line == "Koneksi Tak Berarah") {
isLocationSection = false;
isDirectionalConnectionSection = false;
isPointlessConnectionSection = true;
continue;
}
if (isLocationSection) {
if (name.empty()) {
name = line;
}
else {
address = line;
Location location;
location.name = name;
location.address = address;
location.numConnections = 0;
locations[numLocations] = location;
numLocations++;
name.clear();
address.clear();
}
}
else if (isDirectionalConnectionSection) {
istringstream iss(line);
int source, target, weight;
iss >> source >> target >> weight;
locations[source].connections[locations[source].numConnections] = target;
locations[source].weights[locations[source].numConnections] = weight;
locations[source].numConnections++;
}
else if (isPointlessConnectionSection) {
istringstream iss(line);
int source, target, weight;
iss >> source >> target >> weight;
locations[source].connections[locations[source].numConnections] = target;
locations[source].weights[locations[source].numConnections] = weight;
locations[source].numConnections++;
locations[target].connections[locations[target].numConnections] = source;
locations[target].weights[locations[target].numConnections] = weight;
locations[target].numConnections++;
}
}
inFile.close();
cout << "Data berhasil dimuat." << endl;
} else {
cout << "Gagal membuka file." << endl;
}
}
// menghapus lokasi
void deleteLocation(string fileName) {
if (numLocations == 0) {
cout << "Tidak ada lokasi yang tersedia." << endl;
return;
}
int index;
cout << "Pilih lokasi yang ingin dihapus [1-" << numLocations << "]: ";
cin >> index;
if (index < 1 || index > numLocations) {
cout << "Indeks lokasi tidak valid." << endl;
return;
}
index--;
// Menghapus koneksi yang terhubung dengan lokasi yang dihapus
for (int i = 0; i < numLocations; i++) {
for (int j = 0; j < locations[i].numConnections; j++) {
if (locations[i].connections[j] == index) {
// Geser koneksi ke kiri
for (int k = j; k < locations[i].numConnections - 1; k++) {
locations[i].connections[k] = locations[i].connections[k + 1];
locations[i].weights[k] = locations[i].weights[k + 1];
}
locations[i].numConnections--;
j--; // Penyesuaian indeks setelah penghapusan
// Perbarui indeks koneksi pada lokasi 1 jika diperlukan
if (locations[i].connections[j] > index) {
locations[i].connections[j]--; // Kurangi indeks
}
}
}
}
for (int i = index; i < numLocations - 1; i++) {
locations[i] = locations[i + 1];
}
numLocations--;
// Menulis ulang data ke file
ofstream outFile(fileName.c_str(), ios::trunc);
if (outFile.is_open()) {
outFile << "Lokasi" << endl;
for (int i = 0; i < numLocations; i++) {
outFile << locations[i].name << endl;
outFile << locations[i].address << endl;
}
outFile << "Koneksi Berarah" << endl;
for (int i = 0; i < numLocations; i++) {
for (int j = 0; j < locations[i].numConnections; j++) {
int target = locations[i].connections[j];
int weight = locations[i].weights[j];
// Cek apakah koneksi berarah
bool isDirected = false;
for (int k = 0; k < locations[target].numConnections; k++) {
if (locations[target].connections[k] == i && locations[target].weights[k] == weight) {
isDirected = true;
break;
}
}
if (!isDirected) {
outFile << i << " " << target << " " << weight << endl;
}
}
}
outFile << "Koneksi Tak Berarah" << endl;
for (int i = 0; i < numLocations; i++) {
for (int j = 0; j < locations[i].numConnections; j++) {
int target = locations[i].connections[j];
int weight = locations[i].weights[j];
// Cek apakah koneksi tak berarah
bool isUndirected = false;
for (int k = 0; k < locations[target].numConnections; k++) {
if (locations[target].connections[k] == i && locations[target].weights[k] == weight) {
isUndirected = true;
break;
}
}
bool connectionExists = false;
for (int k = 0; k < i; k++) {
if (locations[k].connections[j] == i && locations[i].connections[j] == k && locations[i].weights[j] == locations[k].weights[j]) {
connectionExists = true;
break;
}
}
if (isUndirected && connectionExists) {
outFile << i << " " << target << " " << weight << endl;
}
}
}
outFile.close();
cout << "Lokasi berhasil dihapus!" << endl;
}
else {
cout << "Gagal membuka file." << endl;
}
}
// mengedit bobot lokasi yang sudah ada
void changeWeight(string fileName) {
if (numLocations < 2) {
cout << "Tidak ada cukup lokasi untuk melakukan perubahan jarak." << endl;
return;
}
int source, target, weight;
cout << "Pilih sumber lokasi [1-" << numLocations << "]: ";
cin >> source;
if (source < 1 || source > numLocations) {
cout << "Indeks sumber lokasi tidak valid." << endl;
return;
}
cout << "Pilih target lokasi [1-" << numLocations << "]: ";
cin >> target;
if (target < 1 || target > numLocations) {
cout << "Indeks target lokasi tidak valid." << endl;
return;
}
if (source == target) {
cout << "Sumber dan target lokasi sama." << endl;
return;
}
cout << "Masukkan jarak baru: ";
cin >> weight;
// mengecek apakah ada koneksi antara sumber dan target
bool foundConnection = false;
for (int i = 0; i < locations[source - 1].numConnections; i++) {
if (locations[source - 1].connections[i] == target - 1) {
locations[source - 1].weights[i] = weight;
foundConnection = true;
break;
}
}
if (!foundConnection) {
cout << "Tidak ditemukan koneksi antara sumber dan target lokasi." << endl;
return;
}
for (int i = 0; i < locations[target - 1].numConnections; i++) {
if (locations[target - 1].connections[i] == source - 1) {
locations[target - 1].weights[i] = weight;
break;
}
}
// menulis ulang data ke file
ofstream outFile(fileName.c_str(), ios::trunc);
if (outFile.is_open()) {
outFile << "Lokasi" << endl;
for (int i = 0; i < numLocations; i++) {
outFile << locations[i].name << endl;
outFile << locations[i].address << endl;
}
outFile << "Koneksi Berarah" << endl;
for (int i = 0; i < numLocations; i++) {
for (int j = 0; j < locations[i].numConnections; j++) {
int target = locations[i].connections[j];
int weight = locations[i].weights[j];
// Cek apakah koneksi berarah
bool isDirected = false;
for (int k = 0; k < locations[target].numConnections; k++) {
if (locations[target].connections[k] == i && locations[target].weights[k] == weight) {
isDirected = true;
break;
}
}
if (!isDirected) {
outFile << i << " " << target << " " << weight << endl;
}
}
}
outFile << "Koneksi Tak Berarah" << endl;
for (int i = 0; i < numLocations; i++) {
for (int j = 0; j < locations[i].numConnections; j++) {
int target = locations[i].connections[j];
int weight = locations[i].weights[j];
// Cek apakah koneksi tak berarah
bool isUndirected = false;
for (int k = 0; k < locations[target].numConnections; k++) {
if (locations[target].connections[k] == i && locations[target].weights[k] == weight) {
isUndirected = true;
break;
}
}
bool connectionExists = false;
for (int k = 0; k < i; k++) {
if (locations[k].connections[j] == i && locations[i].connections[j] == k && locations[i].weights[j] == locations[k].weights[j]) {
connectionExists = true;
break;
}
}
if (isUndirected && connectionExists) {
outFile << i << " " << target << " " << weight << endl;
}
}
}
outFile.close();
cout << "Jarak berhasil diubah!" << endl;
} else {
cout << "Gagal membuka file." << endl;
}
}
// mengedit lokasi yang sudah ada
void editLocation(string fileName) {
if (numLocations == 0) {
cout << "Tidak ada lokasi yang tersedia." << endl;
return;
}
int index;
cout << "Pilih lokasi yang ingin diubah [1-" << numLocations << "]: ";
cin >> index;
if (index < 1 || index > numLocations) {
cout << "Indeks lokasi tidak valid." << endl;
return;
}
index--;
int pilihan;
cout << "Pilih data yang ingin diubah:" << endl;
cout << "[1] Nama Lokasi" << endl;
cout << "[2] Alamat Lokasi" << endl;
cout << "Silahkan pilih opsi [1-2]: ";
cin >> pilihan;
if (pilihan == 1) {
cout << "Masukkan nama lokasi baru: ";
cin.ignore(numeric_limits<streamsize>::max(), '\n');
getline(cin, locations[index].name);
cout << "Nama lokasi berhasil diubah!" << endl;
} else if (pilihan == 2) {
cout << "Masukkan alamat lokasi baru: ";
cin.ignore(numeric_limits<streamsize>::max(), '\n');
getline(cin, locations[index].address);
cout << "Alamat lokasi berhasil diubah!" << endl;
} else {
cout << "Opsi tidak valid." << endl;
return;
}
// menulis ulang data ke file
ofstream outFile(fileName.c_str(), ios::trunc);
if (outFile.is_open()) {
outFile << "Lokasi" << endl;
for (int i = 0; i < numLocations; i++) {
outFile << locations[i].name << endl;
outFile << locations[i].address << endl;
}
outFile << "Koneksi Berarah" << endl;
for (int i = 0; i < numLocations; i++) {
for (int j = 0; j < locations[i].numConnections; j++) {
int target = locations[i].connections[j];
int weight = locations[i].weights[j];
// Cek apakah koneksi berarah
bool isDirected = false;
for (int k = 0; k < locations[target].numConnections; k++) {
if (locations[target].connections[k] == i && locations[target].weights[k] == weight) {
isDirected = true;
break;
}
}
if (!isDirected) {
outFile << i << " " << target << " " << weight << endl;
}
}
}
outFile << "Koneksi Tak Berarah" << endl;
for (int i = 0; i < numLocations; i++) {
for (int j = 0; j < locations[i].numConnections; j++) {
int target = locations[i].connections[j];
int weight = locations[i].weights[j];
// Cek apakah koneksi tak berarah
bool isUndirected = false;
for (int k = 0; k < locations[target].numConnections; k++) {
if (locations[target].connections[k] == i && locations[target].weights[k] == weight) {
isUndirected = true;
break;
}
}
bool connectionExists = false;
for (int k = 0; k < i; k++) {
if (locations[k].connections[j] == i && locations[i].connections[j] == k && locations[i].weights[j] == locations[k].weights[j]) {
connectionExists = true;
break;
}
}
if (isUndirected && connectionExists) {
outFile << i << " " << target << " " << weight << endl;
}
}
}
outFile.close();
} else {
cout << "Gagal membuka file." << endl;
}
}
// mengedit data lokasi dan koneksi yang sudah ada sebelumnya
void editData(string fileName) {
bool backToMenu = false;
int pilih, opsi;
do {
home:
// menu edit data
system ("cls");
cout << "===============================================" << endl;
cout << " EDIT DATA " << endl;
cout << "===============================================" << endl;
cout << "[1] Ubah nama dan alamat lokasi" << endl;
cout << "[2] Ubah jarak antar lokasi" << endl;
cout << "[3] Kembali ke menu utama" << endl;
cout << "===============================================" << endl;
cout << "Silahkan pilih menu [1-3]: ";
cin >> pilih;
if (cin.fail()) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Silahkan input dengan benar!" << endl;
system("pause");
goto home;
}
switch (pilih) {
case 1:
printLocations();
editLocation(fileName);
goto akhir;
break;
case 2:
printLocations();
changeWeight(fileName);
goto akhir;
break;
case 3:
goto last;
break;
default:
cout << "Silahkan input dengan benar!" << endl;
goto home;
break;
}
akhir:
cout << "===============================================" << endl;
cout << "[1] Kembali ke menu awal" << endl;
cout << "[2] Ubah data lagi" << endl;
cout << "Silahkan pilih opsi [1-2]: ";
cin >> opsi;
if (cin.fail()) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Silahkan input dengan benar!" << endl;
system("pause");
goto akhir;
}
if (opsi == 1) {
backToMenu = true;
}
else if (opsi == 2) {
goto home;
}
else {
cout << "Silahkan input dengan benar!" << endl;
goto akhir;
}
last:
backToMenu = true;
} while (!backToMenu);
}
// menu utama
void menu(string username) {
int pilihan, opsi;
string optLogout, fileName = username + ".txt";
home:
system("cls");
loadData(fileName);
cout << "=====================================================" << endl;
cout << " " << endl;
cout << " Selamat Datang di MyMaps " << endl;
cout << " " << endl;
cout << "=====================================================" << endl;
cout << "[1] Input Lokasi" << endl;
cout << "[2] Lihat Lokasi" << endl;
cout << "[3] Hubungkan Lokasi" << endl;
cout << "[4] Cari Rute Terpendek" << endl;
cout << "[5] Hapus Lokasi" << endl;
cout << "[6] Edit Data" << endl;
cout << "[7] Logout" << endl;
cout << "[8] Exit" << endl;
cout << "=====================================================" << endl;
cout << "Silahkan pilih menu [1-8]: ";
cin >> pilihan;
if (cin.fail()) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Silahkan input dengan benar!" << endl;
system("pause");
goto home;
}
switch (pilihan) {
case 1:
addLocation(fileName);
goto akhir;
break;
case 2:
printLocations();
goto akhir;
break;
case 3:
printLocations();
connectLocations(fileName);
goto akhir;
break;
case 4: {
printLocations();
shortestPath(fileName);
goto akhir;
break;
}
case 5:
printLocations();
deleteLocation(fileName);
goto akhir;
break;
case 6:
editData(fileName);
goto home;
break;
case 7:
logout:
system("cls");
cout << "Apakah anda yakin ingin logout?(y/t): ";
cin >> optLogout;
if (optLogout == "y" || optLogout == "Y") {
login();
} else if (optLogout == "t" || optLogout == "T") {
goto home;
} else {
cout << "Silahkan input dengan benar!" << endl;
goto logout;
}
break;
case 8:
goto end;
break;
default:
cout << "Silahkan input dengan benar!" << endl;
goto home;
break;
}
akhir:
cout << "=====================================================" << endl;
cout << "[1] Kembali ke menu awal" << endl;
cout << "[2] Exit Program" << endl;
cout << "Silahkan pilih opsi [1-2]: ";
cin >> opsi;
if (cin.fail()) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Silahkan input dengan benar!" << endl;
system("pause");
goto akhir;
}
if (opsi == 1) {
goto home;
}
else if (opsi == 2) {
goto end;
}
else {
cout << "Silahkan input dengan benar!" << endl;
goto akhir;
}
end:
abort();
}
// halaman login
void login() {
string username, password, fileUsn, filePass;
int i;
char ch;
for (i = 2; i >= 0; i--) {
bool found = false;
system("cls");
cout << "=====================================================" << endl;
cout << " Selamat Datang di MyMaps " << endl;
cout << " " << endl;
cout << " Silahkan Login " << endl;
cout << "=====================================================" << endl;
cout << "Masukkan username: ";
cin >> username;
cout << "Masukkan password: ";
ch = _getch();
while (ch != 13) {
if (ch == 8) {
if (password.size() > 0) {
password.pop_back();
cout << "\b \b";
}
} else {
password.push_back(ch);
cout << "*";
}
ch = _getch();
}
ifstream infile("accounts.txt");
if (infile.is_open()) {
while (infile >> fileUsn >> filePass) {
if (username == fileUsn && password == filePass) {
found = true;
break;
}
}
infile.close();
if (found) {
system("cls");
cout << "Login Berhasil" << endl;
system("pause");
menu(username);
return;
}
}
if (i > 0) {
system("cls");
cout << "Username atau Password salah!" << endl;
cout << "Kesempatan anda tersisa " << i << " lagi" << endl;
password.clear();
system("pause");
}
}
system("cls");
cout << "Anda tidak dapat mengakses aplikasi ini!" << endl;
}
// halaman daftar
void daftar() {
string username, password, verify;
char ch;
system("cls");
cout << "=====================================================" << endl;
cout << " Selamat Datang di MyMaps " << endl;
cout << " " << endl;
cout << " Silahkan Daftar " << endl;
cout << "=====================================================" << endl;
cout << "Masukan username: ";
cin >> username;
ifstream infile("accounts.txt");
string existingUsername;
bool usernameExist = false;
while (infile >> existingUsername) {
if (username == existingUsername) {
usernameExist = true;
break;
}
}
if (usernameExist) {
system("cls");
cout << "Username sudah terdaftar, silahkan gunakan username yang lain" << endl;
system("pause");
return daftar();
}
cout << "Masukan password: ";
ch = _getch();
while (ch != 13) {
if (ch == 8) {
if (password.size() > 0) {
password.pop_back();
cout << "\b \b";
}
}
else {
password.push_back(ch);
cout << "*";
}
ch = _getch();
}
cout << endl;
cout << "Verifikasi password anda: ";
ch = _getch();
while (ch != 13) {
if (ch == 8) {