-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLatres_123180078_M. DAFFA ALFANDY P .cpp
302 lines (293 loc) · 7.18 KB
/
Latres_123180078_M. DAFFA ALFANDY P .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
#include <iostream>
#include <string.h>
#include <fstream>
#include <windows.h>
#define true 1
#define false 0
#define max 100
using namespace std;
void input(), archive(), del();
int jumlah, cek, ceks;
// PTB
typedef int typeinfo;
typedef struct typenode *typeptr;
struct typenode{
typeinfo info;
typeptr kiri, kanan;
};
typeptr akar,p,b;
int NH;
void buat_ptb();
int ptb_kosong();
void sisipnode(typeinfo IB);
void cetak();
void preorder(typeptr akar);
void inorder(typeptr akar);
void postorder(typeptr akar);
void hapusnode(typeinfo IH);
void hapus();
// Data Mahasiswa
struct data {
int nim;
string nama;
float uts, uas, akhir;
};
data mhs[100], temp[100];
// End
int main() {
int pilih;
buat_ptb();
menu:
cout<<"========================================"<<endl;
cout<<"\tSimpan Data Mahasiswa Dengan PTB"<<endl;
cout<<"========================================"<<endl;
cout<<"Menu : "<<endl;
cout<<"[1] Input Data Mahasiswa"<<endl;
cout<<"[2] Tampilkan Data"<<endl;
cout<<"[3] Hapus Data"<<endl;
cout<<"[4] Archive"<<endl;
cout<<"[5] Exit"<<endl;
cout<<"PILIH: "; cin>>pilih;
switch(pilih) {
case 1:
input();
system("pause");
system("CLS");
goto menu;
case 2:
cetak();
system("pause");
system("CLS");
goto menu;
case 3:
del();
system("pause");
goto menu;
case 4:
archive();
system("pause");
system("cls");
goto menu;
default:
return 0;
break;
}
}
void input(){
cout<<"=====INPUT DATA MAHASISWA====="<<endl;
cout<<"Masukkan jumlah data yang ingin diinput: "; cin>>jumlah; cin.ignore();
for(int i=0; i<jumlah; i++){
cout<<"\t=====Mahasiswa ke-"<<i+1<<"====="<<endl;
cout<<"\tNIM Mahasiswa : "; cin>>mhs[i].nim; cin.ignore();
cout<<"\tNama Mahasiswa : "; getline(cin, mhs[i].nama);
cout<<"\tNilai UTS : "; cin>>mhs[i].uts; cin.ignore();
cout<<"\tNilai UAS : "; cin>>mhs[i].uas; cin.ignore();
mhs[i].akhir = (mhs[i].uts + mhs[i].uas) / 2;
sisipnode(mhs[i].nim);
}
}
void del(){
int nim;
cout<<"Masukkan NIM yang ingin dihapus: "; cin>>nim;
for(int i = 0; i<jumlah; i++){
if(mhs[i].nim == nim){
ceks++;
temp[i] = mhs[i];
hapusnode(nim);
cout<<"Berhasil menghapus "<<endl;
break;
} else {
cek++;
break;
}
if(cek > 0){
cout<<"NIM tidak terdapat dalam data"<<endl;
}
}
}
void archive(){
cout<<"=====History Hapus Data====="<<endl;
for(int i = 0; i<ceks; i++){
cout<<"NIM Mahasiswa : "<<temp[i].nim<<endl;
cout<<"Nama Mahasiswa : "<<temp[i].nama<<endl;
cout<<"Hasil UTS : "<<temp[i].uts<<endl;
cout<<"Hasil UAS : "<<temp[i].uas<<endl;
cout<<"Hasil AKHIR : "<<temp[i].akhir<<endl;
}
}
void buat_ptb()
{ typeptr ptb;
ptb=NULL;
akar=ptb; }
int ptb_kosong()
{ if(akar==NULL)
return(true);
else
return(false); }
void sisipnode(typeinfo IB)
{ typeptr NB;
NB=(typenode *) malloc(sizeof(typenode));
NB->info=IB;
NB->kiri=NULL;
NB->kanan=NULL;
if (ptb_kosong())
akar=NB;
else
{ b=akar;
p=akar;
// mencari tempat untuk menyisipkan node
while(p!=NULL && IB!=p->info)
{ b=p;
if (IB<p->info)
p=b->kiri;
else
p=b->kanan; }
if (IB==b->info)
cout << "\n\nNode " << IB << " sudah ada !\n\n";
else
{ if (IB<b->info)
b->kiri=NB;
else
b->kanan=NB; }
}
}
void cetak()
{
cout << "\n\tPre-order : "<<endl;
cout<<"---------------------------------------------------------------------------------"<<endl;
cout<<"\t|\tNIM\t|\tNama\t|\tUTS\t|\tUAS\t|\tAKHIR\t|"<<endl;
cout<<"---------------------------------------------------------------------------------"<<endl;
preorder(akar);
cout << "\n\tIn-order : "<<endl;
cout<<"---------------------------------------------------------------------------------"<<endl;
cout<<"\t|\tNIM\t|\tNama\t|\tUTS\t|\tUAS\t|\tAKHIR\t|"<<endl;
cout<<"---------------------------------------------------------------------------------"<<endl;
inorder(akar);
cout << "\n\tPost-order: "<<endl;
cout<<"---------------------------------------------------------------------------------"<<endl;
cout<<"\t|\tNIM\t|\tNama\t|\tUTS\t|\tUAS\t|\tAKHIR\t|"<<endl;
cout<<"---------------------------------------------------------------------------------"<<endl;
postorder(akar);
return;
}
void preorder(typeptr akar)
{
if (akar!=NULL){
for(int i=0; i<jumlah; i++){
if(mhs[i].nim == akar->info){
cout<<"\t|\t"<<mhs[i].nim<<"\t|";
cout<<"\t"<<mhs[i].nama<<"\t|";
cout<<"\t"<<mhs[i].uts<<"\t|";
cout<<"\t"<<mhs[i].uas<<"\t|";
cout<<"\t"<<mhs[i].akhir<<"\t|"<<endl;
}
}
preorder(akar->kiri);
preorder(akar->kanan); }
}
void inorder(typeptr akar)
{
if (akar!=NULL)
{ inorder(akar->kiri);
for(int i=0; i<jumlah; i++){
if(mhs[i].nim == akar->info){
cout<<"\t|\t"<<mhs[i].nim<<"\t|";
cout<<"\t"<<mhs[i].nama<<"\t|";
cout<<"\t"<<mhs[i].uts<<"\t|";
cout<<"\t"<<mhs[i].uas<<"\t|";
cout<<"\t"<<mhs[i].akhir<<"\t|"<<endl;
}
}
inorder(akar->kanan); }
}
void postorder(typeptr akar)
{
if (akar!=NULL)
{ postorder(akar->kiri);
postorder(akar->kanan);
for(int i=0; i<jumlah; i++){
if(mhs[i].nim == akar->info){
cout<<"\t|\t"<<mhs[i].nim<<"\t|";
cout<<"\t"<<mhs[i].nama<<"\t|";
cout<<"\t"<<mhs[i].uts<<"\t|";
cout<<"\t"<<mhs[i].uas<<"\t|";
cout<<"\t"<<mhs[i].akhir<<"\t|"<<endl;
}
}
}
}
void hapusnode(typeinfo IH)
{
if (ptb_kosong())
cout << "PTB Kosong !\n\n";
else
{ b=akar;
p=akar;
// mencari tempat hapus node
while(p!=NULL && IH!=p->info)
{ b=p;
if (IH<p->info)
p=b->kiri;
else
p=b->kanan; }
}
hapus();
}
void hapus()
{ typeptr temp;
// Bila PTB terdiri dari akar saja atau akar dengan 1 anak kiri/kanan
if (p->kiri==NULL && p->kanan==NULL)
{
if (b==akar && p==akar)
akar=NULL;
else
{
if (p==b->kiri)
b->kiri=NULL;
else
b->kanan=NULL;
}
free(p);
}
// Bila PTB memiliki anak kiri dan anak kanan dgn banyak anak cabang
else if (p->kiri!=NULL && p->kanan!=NULL)
{
temp=p->kiri;
b=p;
while (temp->kanan != NULL)
{ b=temp;
temp=temp->kanan; }
p->info=temp->info;
if (b==p)
b->kiri = temp->kiri;
else
b->kanan = temp->kiri;
free(temp);
}
// Bila PTB memiliki anak kiri saja dgn banyak anak cabang
else if (p->kiri!=NULL && p->kanan==NULL)
{
if (b==p)
akar=p->kiri;
else
{ if (p==b->kiri)
b->kiri=p->kiri;
else
b->kanan=p->kiri;
}
free(p);
}
// Bila PTB memiliki anak kanan saja dgn banyak anak cabang
else if (p->kiri==NULL && p->kanan!=NULL)
{
if (b==p)
akar=p->kanan;
else
{ if (p==b->kanan)
b->kanan=p->kanan;
else
b->kiri=p->kanan;
}
free(p);
}
}