Program merge_descending;
uses crt;
const
maks_mhs = 50;
type
Mahasiswa = record
nama : string;
NIM,Nilai : integer;
Indeks : char;
end;
rec_mhs = array[1..maks_mhs] of mahasiswa;
var
mhs,data : rec_mhs;
n,menu : integer;
procedure create(var mhs : rec_mhs; n : integer);
var
i : integer;
begin
for i := 1 to n do
begin
mhs[i].NIM := 0;
mhs[i].Nama := '';
mhs[i].Nilai := 0;
end;
end;
procedure isi_data(var n: integer; var mhs : rec_mhs);
var
i : integer;
begin
textcolor(15);
write('Masukan Jumlah Mahasiswa : ');readln(n);
clrscr;
for i := 1 to n do
begin
writeln('Data Mahasiswa ke-', i);
writeln('---------------------');
write('NIM : ');readln(mhs[i].NIM);
write('Nama : ');readln(mhs[i].Nama);
write('Nilai : ');readln(mhs[i].Nilai);
{validasi nilai}
while(mhs[i].nilai<0) or (mhs[i].nilai>100) do
begin
gotoxy(15,i*5);textcolor(red);
write('Nilai antara 0-100, ulangi! (Tekan Enter)');
readln;gotoxy(9,i*5);clreol;textcolor(15);
readln(mhs[i].Nilai);
end;
end;
end;
Function indeks_nilai(nilai : integer) : char;
begin
case (nilai) of
80..100 : Indeks_nilai := 'A';
70..79 : Indeks_nilai := 'B';
60..69 : Indeks_nilai := 'C';
50..59 : Indeks_nilai := 'D';
0..49 : Indeks_nilai := 'E';
end;
end;
Function rata_nilai( n : integer;total : real):real;
begin
Rata_nilai := total/n;
end;
Procedure tampil(n : integer; mhs : rec_mhs);
var
total : real;
i : integer;
begin
clrscr;textcolor(15);
writeln(' Daftar nilai mahasiswa');
writeln('================================================');
writeln('| NO | NIM | NAMA | NILAI | INDEKS |');
writeln('------------------------------------------------');
total := 0;
for i := 1 to n do
begin
mhs[i].indeks := indeks_nilai(mhs[i].nilai);
gotoxy(1,i+4);
write('| | | | | |');
gotoxy(3,i+4);write(i);
gotoxy(8,i+4);write(mhs[i].NIM);
gotoxy(18,i+4);write(mhs[i].Nama);
gotoxy(35,i+4);write(mhs[i].Nilai);
gotoxy(44,i+4);write(mhs[i].Indeks);
total := total + mhs[i].nilai;
end;
writeln;
writeln('------------------------------------------------');
writeln('rata-rata nilai : ', rata_nilai(n,total):0:2);
end;
Procedure tampil_merge(n : integer; var mhs : rec_mhs);
var
i : integer;
begin
clrscr;textcolor(15);
writeln(' NIM Tersusun Descending');
writeln('===============================');
writeln('| NO | NIM | NAMA |');
writeln('-------------------------------');
for i := 1 to n do
begin
gotoxy(1,i+4);
write('| | | |');
gotoxy(3,i+4);write(i);
gotoxy(8,i+4);write(mhs[i].NIM);
gotoxy(18,i+4);write(mhs[i].Nama);
end;
writeln;
writeln('-------------------------------');
end;
Procedure tampil_destroy(n : integer; var mhs : rec_mhs);
var
i : integer;
begin
clrscr;textcolor(15);
writeln(' Penghapusan Data Mahasiswa');
writeln('=======================================');
writeln('| NO | NIM | NAMA | NILAI |');
writeln('---------------------------------------');
for i := 1 to n do
begin
gotoxy(1,i+4);
write('| | | | |');
gotoxy(3,i+4);write(i);
gotoxy(8,i+4);write(mhs[i].NIM);
gotoxy(18,i+4);write(mhs[i].Nama);
gotoxy(35,i+4);write(mhs[i].Nilai);
end;
writeln;
writeln('---------------------------------------');
end;
Procedure binary_search(N : Integer; mhs : rec_mhs);
var
Ia,Ib,k : integer;
Ketemu : boolean;
x : integer;
begin
{binary search untuk data terurut secara descending}
write('Masukan NIM yang akan dicari!');readln(x);
Ia := 1;
Ib := N;
Ketemu := false;
while(not ketemu) and (Ia <= Ib) do
begin
k := (Ia + Ib) div 2;
if (mhs[k].NIM = x)
then
ketemu := true
else
if(mhs[k].NIM > x)
then
Ia := k + 1
else
Ib := k - 1;
end;
mhs[k].indeks := indeks_nilai(mhs[k].nilai);
if(ketemu)
then
begin
clrscr;
gotoxy(13,3);write(' Data Mahasiswa');
gotoxy(12,4);write('===========================');
gotoxy(13,5);write('NIM : ',x);
gotoxy(12,6);write('---------------------------');
gotoxy(13,7);write('Nama : ',mhs[k].nama);
gotoxy(12,8);write('---------------------------');
gotoxy(13,9);write('Nilai : ',mhs[k].nilai);
gotoxy(12,10);write('---------------------------');
gotoxy(13,11);write('Indeks : ',mhs[k].indeks);
gotoxy(12,12);write('---------------------------');
end
else
writeln('Mahasiswa dengan NIM ',x,' Tidak ditemukan');
end;
procedure Merge (var mhs: rec_mhs; p, q, r: integer);
var
i, j, k : integer;
B : rec_mhs;
begin { Merge }
i := p;
j := q + 1;
k := p;
while ((i <= q) and (j <= r)) do
begin
if (mhs[i].nim > mhs[j].nim)
then
begin
B[k] := mhs[i];
i := i + 1;
end
else
begin
B[k] := mhs[j];
j := j + 1;
end;
k := k + 1;
end;
while (i <= q) do
begin
B[k] := mhs[i];
k := k + 1;
i := i + 1;
end;
while (j <= r) do
begin
B[k] := mhs[j];
k := k + 1;
j := j + 1;
end;
for k := p to r do mhs[k] := B[k];
end;
procedure MergeSort (var mhs: rec_mhs; p, r: integer);
var
q: integer;
begin { MergeSort }
if (p < r) then
begin
q := (p + r) div 2;
MergeSort (mhs, p, q);
MergeSort (mhs, q + 1, r);
Merge (mhs, p, q, r);
end;
end;
{program utama}
begin
n := 0;
repeat
clrscr;
gotoxy(30,2);writeln('Pengolahan Data Mahasiswa');
gotoxy(29,3);writeln('===========================');
gotoxy(25,5);writeln('1.Isi Data Mahasiswa');
gotoxy(25,6);writeln('2.Tampilkan Daftar Nilai Mahasiswa');
gotoxy(25,7);writeln('3.Urutkan NIM Secara Descending');
gotoxy(25,8);writeln('4.Cari Data Mahasiswa Berdasarkan NIM');
gotoxy(25,9);writeln('5.Hapus data (Penghancuran Array)');
gotoxy(25,10);writeln('0.Keluar');
gotoxy(25,12);write('Silakan Memilih Menu! [1-5] ');readln(menu);
{validasi menu pilihan}
while(menu < 0) or (menu > 5) do
begin
gotoxy(25,13);
write('Nomor tidak ada dalam Menu, tekan enter untuk mengulang!');
readln;gotoxy(25,13);
readln(menu);
end;
case (menu) of
1 : begin
clrscr;
create(mhs,n);
isi_data(n,data);
end;
2 : begin
clrscr;
if (n = 0)
then
write('Data belum terdefinisi, isilah data terlebih dahulu!')
else
tampil(n,data);
readln;
end;
3 : begin
clrscr;
if (n = 0)
then
write('Data belum terdefinisi, isilah data terlebih dahulu!')
else
MergeSort(data, 1, N);
tampil_merge(n,data);
readln;
end;
4 : begin
clrscr;
if (n = 0)
then
write('Data belum terdefinisi, isilah data terlebih dahulu!')
else
MergeSort(data, 1, N);
binary_search(n,data);
readln;
end;
5 : begin
clrscr;
if (n = 0)
then
write('Data belum terdefinisi, isilah data terlebih dahulu!')
else
create(data,n);
tampil_destroy(n,data);
readln;
end;
end;
until(menu=0);
end.
Tidak ada komentar:
Posting Komentar