This repository was archived by the owner on Jan 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnomor2.cpp
More file actions
189 lines (161 loc) · 4.46 KB
/
nomor2.cpp
File metadata and controls
189 lines (161 loc) · 4.46 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
// Diberikan sebuah kasus untuk enkripsi password yang memanfaatkan Linked List,
// dengan spesifikasi : Panjang password 6 digit, Isi password terserah dari
// user dan password diinputkan terlebih dahulu. Enkripsi dilakukan dengan
// menukarkan n node terakhir, menjadi node terdepan. Kemudian sisipkan karakter
// baru sebagai kunci (misal karakter @) setelah n node dari yang dipindahkan
// tersebut. Silahkan tentukan nilai n, dan boleh menambahkan asumsi pribadi.
// Buatlah tampilan aplikasi semenarik mungkin
#include <iostream>
using namespace std;
// struct node
struct Node {
char data;
Node *next;
};
Node *head, *tail, *cur, *newNode, *del, *temp;
// digit password
int digitPassword = 6;
// password sebelum dan sesudah
string passwordSebelum, passwordSesudah;
// push
void push(char data) {
Node *newNode = new Node;
newNode->data = data;
newNode->next = NULL;
// penentuan sebagai head atau tail
if (head == NULL) {
head = newNode;
tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
}
}
// remove
void remove() {
for (int i = 0; i < 7; i++) {
del = head;
head = head->next;
del->next = NULL;
delete del;
}
}
// print
void print(string pesan) {
cout << pesan << endl;
// traversing
cur = head;
while (cur != NULL) {
cout << cur->data;
cur = cur->next;
if (cur != NULL) {
cout << " >> ";
}
}
cout << endl << endl;
}
// input
string input() {
bool valid = false;
string hasil;
passwordSebelum = "";
while (!valid) {
cout << "Masukkan password yang ingin dienkripsi [" << digitPassword
<< " digit karakter]: ";
cin >> hasil;
if (hasil.length() == digitPassword) {
valid = true;
} else {
system("cls");
cout << "Password yang Anda masukkan terlalu";
if (hasil.length() < digitPassword) {
cout << " [pendek].";
} else {
cout << " [panjang].";
}
cout << endl << endl;
}
cout << endl;
}
// pembentukan single linked list & menyimpan password sebelum terenkripsi
for (int i = 0; i < digitPassword; i++) {
push(hasil[i]);
// penyimpanan password sebelum
passwordSebelum += hasil[i];
}
// menampilkan password
print("Password sebelum dienkripsi: ");
return hasil;
}
// enkripsi
void enkripsi(string data) {
bool check = false;
int counter = 1;
passwordSesudah = "";
int nilaiN = 4;
// int nilaiN;
// while (!check) {
// cout << "Nilai n [ 0 < digit < " << digitPassword << " ] : ";
// cin >> nilaiN;
// if (nilaiN > 0 && nilaiN < digitPassword) {
// check = true;
// } else {
// system("cls");
// cout << "Tidak valid." << endl;
// }
// }
char kunci = '#';
// char kunci;
// cout << "Karakter kunci : ";
// cin >> kunci;
push(kunci);
// traversing menetapkan letak node yang hubungannya akan di putus
cur = head;
while (counter != abs(digitPassword - nilaiN)) {
cur = cur->next;
counter++;
}
// proses pengenkripsian
temp = head;
head = cur->next;
tail->next = temp;
cur->next = NULL;
tail = cur;
print("\nPassword sesudah dienkripsi: ");
// traversing
cur = head;
while (cur != NULL) {
passwordSesudah += cur->data;
cur = cur->next;
}
}
int main() {
bool lagi = true;
bool valid = false;
string konfirmasi;
while (lagi) {
enkripsi(input());
while (!valid) {
cout << "Enkripsi password lainnya? [y/n]: ";
cin >> konfirmasi;
if (tolower(konfirmasi[0]) == 'y') {
system("cls");
remove();
lagi = true;
valid = true;
} else if (tolower(konfirmasi[0]) == 'n') {
cout << "Terima kasih telah menggunakan aplikasi ini." << endl;
lagi = false;
valid = true;
} else {
system("cls");
cout << "Input tidak valid." << endl;
valid = false;
}
}
valid = false;
}
// cout << "Sebelum enkripsi: " << passwordSebelum << endl;
// cout << "Sesudah enkripsi: " << passwordSesudah << endl;
return 0;
}