Buatlah form program seperti ini
Kemudian klik F9 pada keyboard,
masukkan nilai, misal:2 klik button Tambah
Klik button Proses, jadinya seperti di bawah ini
Contoh List Program
unit UContoh;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
Trina_2b = class(TForm)
ListBox1: TListBox;
ListBox2: TListBox;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Button1: TButton;
Edit1: TEdit;
Button2: TButton;
Button3: TButton;
Label4: TLabel;
procedure Button3Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
Data : array of integer;
public
{ Public declarations }
end;
var
rina_2b: Trina_2b;
implementation
{$R *.dfm}
procedure QuickSort(var A: array of Integer; iLo, iHi: Integer) ;
var
Lo, Hi, Pivot, T: Integer;
begin
Lo := iLo;
Hi := iHi;
Pivot := A[(Lo + Hi) div 2];
repeat
while A[Lo] < Pivot do Inc(Lo) ;
while A[Hi] > Pivot do Dec(Hi) ;
if Lo <= Hi then
begin
T := A[Lo];
A[Lo] := A[Hi];
A[Hi] := T;
Inc(Lo) ;
Dec(Hi) ;
end;
until Lo > Hi;
if Hi > iLo then QuickSort(A, iLo, Hi) ;
if Lo < iHi then QuickSort(A, Lo, iHi) ;
end;
procedure Trina_2b.Button1Click(Sender: TObject);
var
nilai : Integer;
begin
//menambahkan nilai ke listbox
if TryStrToInt(Edit1.Text,Nilai) then
ListBox1.Items.Add(IntToStr(nilai));
//membersihkan komponen inputan nilai
Edit1.Clear;
//focus kursor ke inputan nilai
Edit1.SetFocus;
end;
procedure Trina_2b.Button3Click(Sender: TObject);
begin
Close;
end;
procedure Trina_2b.Button2Click(Sender: TObject);
var
indek : Word;
begin
// Menentukan panjang array
SetLength(Data,ListBox1.Items.Count) ;
// Pemasukan nilai ke array
for indek := 0 to ListBox1.Items.Count - 1 do
Data[indek] := StrToInt(ListBox1.Items[indek]);
// Proses pengurutan / sorting
QuickSort(data,Low(Data),High(Data));
// Kosongkan hasil
ListBox2.Items.Clear;
// Memasukkan nilai hasil
for indek := 0 to ListBox1.Items.Count - 1 do
ListBox2.Items.Add(IntToStr(data[indek]));
end;
end.
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
Trina_2b = class(TForm)
ListBox1: TListBox;
ListBox2: TListBox;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Button1: TButton;
Edit1: TEdit;
Button2: TButton;
Button3: TButton;
Label4: TLabel;
procedure Button3Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
Data : array of integer;
public
{ Public declarations }
end;
var
rina_2b: Trina_2b;
implementation
{$R *.dfm}
procedure QuickSort(var A: array of Integer; iLo, iHi: Integer) ;
var
Lo, Hi, Pivot, T: Integer;
begin
Lo := iLo;
Hi := iHi;
Pivot := A[(Lo + Hi) div 2];
repeat
while A[Lo] < Pivot do Inc(Lo) ;
while A[Hi] > Pivot do Dec(Hi) ;
if Lo <= Hi then
begin
T := A[Lo];
A[Lo] := A[Hi];
A[Hi] := T;
Inc(Lo) ;
Dec(Hi) ;
end;
until Lo > Hi;
if Hi > iLo then QuickSort(A, iLo, Hi) ;
if Lo < iHi then QuickSort(A, Lo, iHi) ;
end;
procedure Trina_2b.Button1Click(Sender: TObject);
var
nilai : Integer;
begin
//menambahkan nilai ke listbox
if TryStrToInt(Edit1.Text,Nilai) then
ListBox1.Items.Add(IntToStr(nilai));
//membersihkan komponen inputan nilai
Edit1.Clear;
//focus kursor ke inputan nilai
Edit1.SetFocus;
end;
procedure Trina_2b.Button3Click(Sender: TObject);
begin
Close;
end;
procedure Trina_2b.Button2Click(Sender: TObject);
var
indek : Word;
begin
// Menentukan panjang array
SetLength(Data,ListBox1.Items.Count) ;
// Pemasukan nilai ke array
for indek := 0 to ListBox1.Items.Count - 1 do
Data[indek] := StrToInt(ListBox1.Items[indek]);
// Proses pengurutan / sorting
QuickSort(data,Low(Data),High(Data));
// Kosongkan hasil
ListBox2.Items.Clear;
// Memasukkan nilai hasil
for indek := 0 to ListBox1.Items.Count - 1 do
ListBox2.Items.Add(IntToStr(data[indek]));
end;
end.
0 komentar:
Posting Komentar