Rabu, 01 April 2015

Program sorting dalam pascal

Tags

Contoh program pengurutan data mahasiswa berdasarkan nilai

Program sorting_data;
Uses crt;
Type data = record
  Nama:string[18];
  Nim:string[9];
  Nilai:real;
End;
Dtmhs=array[1..10] of data;

Var
  mhs:dtmhs;
  Plh:char;
  jlh,i,a:integer;

Procedure input;
Begin
  Clrscr;
  Write('Jumlah Data : '); readln(jlh);
  Clrscr;
  For a:=1 to jlh do
  Begin
    Writeln('Data ke-',a);
    Write('Nama  : '); readln(mhs[a].nama);
    Write('NIM   : '); readln(mhs[a].nim);
    Write('Nilai : '); readln(mhs[a].nilai);
    Writeln;
  End;
  Readkey;
End;

Procedure output;
Begin
  Clrscr;
  Writeln('Data yang belum diurutkan');
  writeln;
  Writeln(' ____________________________________________');
  Writeln('| NO |    NIM    |       NAMA        | NILAI |');
  Writeln('|--------------------------------------------|');
  Writeln('|    |           |                   |       |');
  For a:=1 to jlh do
  Begin
    Writeln('|    |           |                   |       |');
    Gotoxy(3,a+3); writeln(a);
    Gotoxy(8,a+3); writeln(mhs[a].nim);
    Gotoxy(20,a+3); writeln(mhs[a].nama);
    Gotoxy(40,a+3); writeln(mhs[a].nilai:2:2);
  End;
  Writeln('|____________________________________________|');
  Readkey;
End;

Procedure swap(var i,j:real);
Var
  temp:real;
Begin
  temp:=j;
  j:=i;
  i:=temp;
End;

Procedure swap2(var i,j:string);
Var
  temp:string;
Begin
  temp:=j;
  j:=i;
  i:=temp;
end;

Procedure Bubble(n:byte; var a:dtmhs);
Var
  i,j:byte;
Begin
  For i:=1 to n do
  Begin
    For j:=1 to i-1 do
    If (a[j].nilai < a[j+1].nilai) then
    Begin
      Swap (a[j].nilai, a[j+1].nilai);
      Swap2(a[j].nama, a[j+1].nama);
      Swap2(a[j].nim, a[j+1].nim);
    End;
  End;
End;

Procedure sorting;
Begin
  Clrscr;
  Bubble(jlh, mhs);
  Writeln('Data yang sudah diurutkan');
  Writeln;
  Writeln(' ____________________________________________');
  Writeln('| NO |    NIM    |       NAMA        | NILAI |');
  Writeln('|--------------------------------------------|');
  Writeln('|    |           |                   |       |');
  For a:=1 to jlh do
  Begin
    Writeln('|    |           |                   |       |');
    Gotoxy(3,a+3); writeln(a);
    Gotoxy(8,a+3); writeln(mhs[a].nim);
    Gotoxy(20,a+3); writeln(mhs[a].nama);
    Gotoxy(40,a+3); writeln(mhs[a].nilai:2:2);
  End;
  Writeln('|____________________________________________|');
  Writeln;
End;

Procedure Ranking;
Begin
  Writeln('Ranking : ');
  For a:=1 to jlh do
  Writeln(a,'. ',mhs[a].nama);
  Readkey;
End;

Begin
  Repeat
  Clrscr;
  Writeln('MENU');
  Writeln;
  Writeln('1. Input data');
  Writeln('2. Data yang belum di urutkan');
  Writeln('3. Data yang sudah di urutkan');
  Writeln('4. Exit');
  Writeln;
  Write('Pilihan : '); readln(plh);
  Case plh of
    '1':Input;
    '2':Output;
    '3':begin
        sorting;
        ranking;
        end;
    '4':Exit;
  End;
  Until plh='4';
  Readkey;
End.


EmoticonEmoticon