Nugroho's blog.: Flexible Digital Counter using Delphi (with Recursive Procedure)

Friday, November 14, 2014

Flexible Digital Counter using Delphi (with Recursive Procedure)

Updated version from before

`unit Unit1;interfaceuses  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  Dialogs, Grids, StdCtrls, Math;type  TForm1 = class(TForm)    Button1: TButton;    StringGrid1: TStringGrid;    function toString(var a:boolean):string;    function denary:string;    procedure proses;    procedure tlsStrgrd;    procedure counter(l:integer);    procedure Button1Click(Sender: TObject);    procedure FormCreate(Sender: TObject);  private    { Private declarations }  public    { Public declarations }  end;const n=5;var Form1: TForm1;  Q:array[0..n-1]of boolean;  l:integer=0; jalan:boolean=false; clock:boolean=true;implementation{\$R *.dfm}function tform1.denary;var i,j:integer;begin  j:=0;  for i:=0 to n-1 do begin    j:=j+round(strtoint(toString(Q[i]))*Power(2,i));  end;  denary:=inttostr(j);end;function tform1.toString(var a:boolean):string;begin  toString:=inttostr(-1*strtoint(booltostr(a)));end;procedure tform1.counter(l:integer);begin  if l<=n-1 then begin    Q[l]:=not Q[l];    if Q[l]=false then begin      l:=l+1;      counter(l);    end;  end;end;procedure tform1.tlsStrgrd;var i:integer;begin  for i:=0 to n-1 do begin    stringgrid1.Cells[i+2,1]:=toString(Q[i]);  end;end;procedure tform1.proses;begin  clock:=not clock;  if clock=false then  begin    l:=0;    counter(l);    tlsStrgrd;  end;  stringgrid1.Cells[1,1]:=toString(clock);  stringgrid1.Cells[n+2,1]:=denary;end;procedure TForm1.Button1Click(Sender: TObject);begin  jalan:= not jalan;  while jalan=true do begin    proses;    application.ProcessMessages;sleep(300);  end;end;procedure TForm1.FormCreate(Sender: TObject);var i:integer;begin  stringgrid1.ColCount:=n+3;  stringgrid1.Cells[1,0]:='clock';  stringgrid1.Cells[n+2,0]:='denary';  stringgrid1.Cells[n+2,1]:=denary;  stringgrid1.Cells[1,1]:=toString(clock);  for i:=0 to n-1 do begin    Q[i]:=false;    stringgrid1.Cells[i+2,0]:='Q'+inttostr(i);    stringgrid1.Cells[i+2,1]:=toString(Q[i]);  end;end;end.`