-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
575 lines (472 loc) · 16.7 KB
/
Copy pathmain.cpp
File metadata and controls
575 lines (472 loc) · 16.7 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
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
#include <iostream>
#include <iomanip>
#include <conio.h>
using namespace std;
//PROSEDUR UNTUK KEMBALI KE MENU UTAMA
void back_to_menu(){
cout << "\nTap any key to return to the menu...";
getch();
system("cls");
}
//UNTUK CASE 1
void case_1(){
string nama, nim, jurusan, kelas, nomor, alamat;
cout << "------------------------------------------\n";
cout << " Masukan Data Berikut \n";
cout << "------------------------------------------\n\n";
cout << "Nama : ";
getline(cin >> ws,nama);
cout << "NIM : " ;
cin >> nim;
cout << "Jurusan : ";
getline(cin >> ws,jurusan);
cout << "Kelas : ";
getline(cin >> ws,kelas);
cout << "Alamat : ";
getline(cin >> ws,alamat);
cout << "Nomor HP : ";
cin >> nomor;
system("cls");
cout <<endl;
cout << "------------------------------------------"<<endl;
cout << " DATA MAHASISWA "<<endl;
cout << "------------------------------------------"<<endl;
cout << "Nama : " << nama << endl;
cout << "NIM : " << nim << endl;
cout << "Jurusan : " << jurusan << endl;
cout << "Kelas : " << kelas << endl;
cout << "Alamat : " << alamat << endl;
cout << "Nomor HP : " << nomor << endl;
}
//UNTUK CASE 5 SORTING
//SORTING BUBBLE SORT
void bubble_sort(string arr[], int length){
bool not_sorted = true;
int j=0;
string tmp;
while (not_sorted){
not_sorted = false;
j++;
for (int i = 0; i < length - j; i++){
if (arr[i] > arr[i+1]){
tmp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = tmp;
not_sorted = true;
}
}
}
}
//SORTING SELECTION SORT 1
void selection_sort1(string arr[], int length){
int pos;
string temp;
for(int i=0; i<length-1; i++){
pos = i;
for(int j=i+1; j<length; j++){
if(arr[j] > arr[pos])
pos = j;
}
if(pos != i){
temp = arr[i];
arr[i] = arr[pos];
arr[pos] = temp;
}
}
}
//CETAK ISI ARRAY 1
void print_array1(string arr1[], int length){
for(int i=0; i<length; i++){
cout << arr1[i] << " ";
}
cout << endl;
}
//SORTING INSERTION SORT 2
void insertion_sort2(double arr2[], int length){
int i, j;
double temp;
for(i=1; i<length; i++){
j=i;
while(j>0 && arr2[j-1] > arr2[j]){
temp = arr2[j];
arr2[j] = arr2[j-1];
arr2[j-1] = temp;
j--;
}
}
}
//SORTING SELECTION SORT 2
void selection_sort2(double arr2[], int length){
int pos;
double temp;
for(int i=0; i<length-1; i++){
pos = i;
for(int j=i+1; j<length; j++){
if(arr2[j] > arr2[pos])
pos = j;
}
if(pos != i){
temp = arr2[i];
arr2[i] = arr2[pos];
arr2[pos] = temp;
}
}
}
//CETAK ISI ARRAY 2
void print_array2(double arr2[], int length){
for(int i=0; i<length; i++){
cout << arr2[i] << " ";
}
cout << endl;
}
//UNTUK CASE 6 : FUNGSI BINARY SEARCH DAN PROSEDUR BUBBLE SORT
//BINARY SEARCH
int case6_binary_search (double data[], int index, int n, double k) {
n = n - 1;
while (index <= n) {
int tengah = (index + n)/2;
if(k == data[tengah]) {
index = tengah;
return index;
}
else if(k < data[tengah]){ n = tengah - 1;}
else if(k > data[tengah]){ index = tengah + 1;}
}
index = -1;
return index;
}
//SORTING DATA SEBELUM KE BINARY SEARCH
void case6_bubble_sort(double arr[], int length){
bool not_sorted = true;
int j=0;
double tmp;
while (not_sorted){
not_sorted = false;
j++;
for (int i = 0; i < length - j; i++){
if (arr[i] > arr[i+1]){
tmp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = tmp;
not_sorted = true;
}
}
}
}
//UNTUK CASE 7 : FUNGSI FACTORIAL
int factorial(int n){
if (n > 1){
return n * factorial(n - 1);
}
else {
return 1;
}
}
//FUNGSI UTAMA
int main()
{
int pilih;
do
{
//TAMPILAN MENU
cout << "================================\n";
cout << "======== MENU MODUL C++ ========\n";
cout << "================================\n" << endl;
cout << "1. Input dan Output\n";
cout << "2. Percabangan\n";
cout << "3. Perulangan\n";
cout << "4. Array\n";
cout << "5. Sorting\n";
cout << "6. Searching\n";
cout << "7. Rekursif\n";
cout << "8. Exit\n\n";
cout << "Masukkan Pilihan Anda: "; cin >> pilih;
system("cls");
//CASE 1: Input dan Output
if (pilih == 1){
case_1();
back_to_menu();
}
//CASE 2: Percabangan
else if (pilih == 2){
//VARIABEL
int nilai;
string password, grade;
//LOGIN INPUT
cout << "========= Login =========" << endl;
cout << "Masukan password (kota asal ITTP) : ";
cin >> password;
//PERCABANGAN IF dan ELSE
if (password == "purwokerto" || "Purwokerto" || "PURWOKERTO"){
cout << "Selamat datang!" << endl << endl;
cout << "=== Program Grade Nilai ===" << endl;
cout << "Inputkan nilai Anda : ";
cin >> nilai;
if (nilai >= 90) {
grade = "A";
} else if (nilai >= 80) {
grade = "B+";
} else if (nilai >= 70) {
grade = "B";
} else if (nilai >= 60) {
grade = "C+";
} else if (nilai >= 50) {
grade = "C";
} else if (nilai >= 40) {
grade = "D";
} else if (nilai >= 30) {
grade = "E";
} else {
grade = "F";
}
//MENAMPILKAN TINGKAT BERDASARKAN NILAI INPUT
cout << "Grade anda: " << grade << endl << endl;
//TIPS
char tips;
cout << "Ingin tau tips mendapatkan nilai bagus? (Y/N) : \n";
cin >> tips;
//PERCABANGAN SWITCH
switch (toupper(tips)){
case 'Y':
cout << "\nTipsnya adalah Belajar dan berdoa" << endl << endl;
break;
case 'N':
cout << "Oke" << endl;
break;
default:
cout << "Input Salah!" << endl;
}
}
else {
cout << "Password salah, coba lagi!" << endl;
}
cout << "Terimakasih sudah menggunakan aplikasi ini!" << endl;
back_to_menu();
}
//CASE 3: PERULANGAN
else if (pilih == 3){
//VARIABEL
int angka, pilihOperasi;
//PENJELASAN TENTANG PROGRAM
cout << "Program ini akan melakukan penjumlahan, pengurangan, dan perkalian ^^\n";
cout << "Program akan melakukan perhitungan dari bilangan yang diinput dengan angka 1-10 ^^\n";
cout << "\nAyo, masukkan bilangan yang akan dihitung : ";
cin >> angka;
//PILIH OPERASI PERHITUNGAN
cout << "\nDaftar operasi perhitungan\n";
cout << "1. Penjumlahan\n";
cout << "2. Pengurangan\n";
cout << "3. Perkalian\n";
cout << "Pilih Operasi perhitungan : ";
cin >> pilihOperasi;
if ( pilihOperasi == 1 ){
cout << "\nPENJUMLAHAN\n";
for (int i = 0; i<10; i++){
cout << angka << " + " << i+1 << " = " << angka+(i+1)<< endl;
}
}
else if ( pilihOperasi == 2 ){
cout << "\nPENGURANGAN\n";
for (int i = 0; i<10; i++){
cout << angka << " - " << i+1 << " = " << angka - (i + 1)<< endl;
}
}
else {
cout << "\nPERKALIAN\n";
for (int i = 0; i<10; i++){
cout << angka << " x " << i+1 << " = " << angka * (i + 1)<< endl;
}
}
back_to_menu();
}
//CASE 4: ARRAY (PENERAPAN ARRAY DALAM GAME MINESWEEPER SEDERHANA)
else if (pilih == 4){
//PROGRAM MINESWEPER
//ARRAY UNTUK MENAMPUNG TEBAKAN PEMAIN
string A[5][5] ={{" "," "," "," "," "},{" "," "," "," "," "},{" "," "," "," "," "},{" "," "," "," "," "},{" "," "," "," "," "}};
//KUNCI JAWABAN GAME
string B[5][5] ={{"1","1","1","_","_"},{"1","*","2","1","1"},{"1","1","3","*","2"},{"_","_","2","*","2"},{"_","_","1","1","1"}};
string C[5][5] ={{"1","1","1","_","_"},{"1","*","2","1","1"},{"1","1","3","*","2"},{"_","_","2","*","2"},{"_","_","1","1","1"}};
//VARIABEL PARAMETER
int x,y;
int z=0;
string a,b,c;
//TAMPILAN AWAL
do{
cout<<"\t *MINESWEEPER*";
cout<<"\n===============================\n";
for (x=0;x<=4;x++){
cout<<"| ";
for (y=0;y<=4;y++){
cout<<A[x][y]<<" | ";
}
cout<<endl;
cout<<"===============================\n";
}
cout<<"\nMasukkan Kolom [1-5]: ";
cin>>y;
cout<<"Masukkan Baris [1-5]: ";
cin>>x;
//JIKA KONDISI SALAH DAN PEMAIN KALAH
if( B[x-1][y-1] == "*" ){
A[x-1][y-1] = "*" ;
cout<<"\n\n\t *MINESWEEPER*";
cout<<"\n===============================\n";
for (x = 0; x <= 4; x++){
cout<<"| ";
for (y = 0; y <= 4; y++){
cout<<A[x][y]<<" | ";
}
cout<<endl;
cout<<"===============================\n";
}
cout<<"\n\t Anda kalah\n";
break;
}
//JIKA KONDISI BENAR
else{
a=A[x-1][y-1];
b=B[x-1][y-1];
c=C[x-1][y-1];
if(b==c && b!=a){
A[x-1][y-1]=B[x-1][y-1];
B[x-1][y-1]="";
z++;
}
}
cout<<"\n\n\n";
} while (z != 22);
//JIKA PEMAIN MENANG
if (z == 22){
cout<<"\t Anda menang\n";
}
back_to_menu();
}
//CASE 5: Sorting
else if (pilih == 5){
//VARIABEL
int length1, length2;
int pilihan, i;
//MENU SORTING NAMA ATAU ANGKA
cout << "====== Sorting ======" << endl;
cout << "1. Nama" << endl;
cout << "2. Angka" << endl;
cout << "Masukkan Pilihan [1-2]: "; cin >> pilihan;
//JIKA PILIHAN MENSORTING NAMA
if (pilihan == 1){
cout << "1. Nama" << endl;
cout << " Berapa nama yang ingin di input : "; cin >> length1;
string arr1[length1];
//LOOPING INPUT NAMA
for(i=0; i<length1; i++){
cout << " Nama ke-" << i+1 << " : "; cin >> arr1[i];
}
//HASIL SEBELUM SORTING
cout << endl;
cout << " Urutan nama sebelum di sorting:" << endl;
cout << " ";
print_array1(arr1, length1);
//PENSORTINGAN DATA
bubble_sort(arr1, length1);
//HASIL ASCENDING SORTING
cout << endl;
cout << " Uruturan nama setelah ascending sort:" << endl;
cout << " ";
print_array1(arr1, length1);
//HASIL DESCENDING SORTING
cout << endl;
cout << " Uruturan nama setelah descending sort:" << endl;
selection_sort1(arr1, length1);
cout << " ";
print_array1(arr1, length1);
}
//JIKA PILIHAN MENSORTING ANGKA
else if(pilihan == 2){
cout << "2. Angka" << endl;
cout << " Berapa angka yang ingin di input : "; cin >> length2;
double arr2[length2];
//LOOPING INPUT ANGKA
for(i = 0; i < length2; i++){
cout << " Angka ke-" << i+1 << " : "; cin >> arr2[i];
}
//HASIL SEBELUM SORTING
cout << endl;
cout << " Urutan angka sebelum di sorting:" << endl;
cout << " ";
print_array2(arr2, length2);
//PENSORTINGAN DATA
insertion_sort2(arr2, length2);
//HASIL ASCENDING SORTING
cout << endl;
cout << " Uruturan angka setelah ascending sort:" << endl;
cout << " ";
print_array2(arr2, length2);
//HASIL DESCENDING SORTING
cout << endl;
cout << " Uruturan angka setelah descending sort:" << endl;
cout << " ";
selection_sort2(arr2, length2);
print_array2(arr2, length2);
}
back_to_menu();
}
//CASE 6: SEARCHING
else if (pilih == 6){
//BANNER CASE 6
cout << "Selamat Datang di Pemrograman Searching" << endl;
cout << " " << endl << endl;
//VARIABEL
double case6_cari;
int case6_arr_length;
//INPUT BANYAKNYA DATA
cout << " Masukkan Banyaknya Data : "; cin >> case6_arr_length;
double case6_arr[case6_arr_length];
//LOOPING INPUT DATA KE ARRAY
cout << " Masukkan Data Bilangan " << endl;
for(int i = 0; i < case6_arr_length; i++)
{
cout << " Data ke-" << i+1 <<" = "; cin >> case6_arr[i];
}
cout<<endl;
//SORTING DATA
case6_bubble_sort(case6_arr, case6_arr_length);
//INPUT DATA YANG INGIN DICARI
cout<<"Yuk input bilangan yang kamu cari : "; cin >> case6_cari;
cout<<endl;
//PENCARIAN DATA
int case6_result = case6_binary_search(case6_arr, 0, case6_arr_length, case6_cari);
if (case6_result != -1){
cout << "Data yang kamu cari ditemukan pada indeks ke-" << case6_result << endl;
}
else {
cout << "Yah, sayang sekali data yang kamu cari tidak ditemukan" << endl;
cout << " " << endl;
}
back_to_menu();
}
//CASE 7: RECURSIVE (IMPLEMENTASI RECURSIVE ALGORITHM DI PENGHITUNGAN FAKTORIAL)
else if (pilih == 7){
//VARIABEL
int case7_factorial_number;
//INPUT BILANGAN FAKTORIAL
cout << "====== Rekursif ======\n" << endl;
cout << "Masukkan bilangan Faktorial : ";
cin >> case7_factorial_number;
//HASIL FAKTORIAL
cout << "Hasil Faktorial dari " << case7_factorial_number << " adalah : " << factorial(case7_factorial_number) << endl;
back_to_menu();
}
//JIKA PILIH MENGAKHIRI PROGRAM
else if (pilih == 8){
cout << "\nTap any key to Exit...";
getch();
cout << endl;
}
else {
cout << "tidak ada pilihan ke-" << pilih << endl;
back_to_menu();
}
}
while (pilih !=8);
return 0;
}