I wrote this simple program that will ask the user to give a number in binary and then our program will convert the number to its decimal equivalent using Delphi as my programming language.
Add me at Facebook my address is jakerpomperada@gmail.com and jakerpomperada@yahoo.com.
My mobile number here in the Philippines is 09173084360.
Sample Program Output
Program Listing
binary.pas
unit binary;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
Edit1: TEdit;
Button1: TButton;
Label3: TLabel;
Button2: TButton;
Label4: TLabel;
Label5: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function Pow(i, k: Integer): Integer;
var
j, Count: Integer;
begin
if k>0 then j:=2
else j:=1;
for Count:=1 to k-1 do
j:=j*2;
Result:=j;
end;
function BinToDec(Str: string): Integer;
var
Len, Res, i: Integer;
Error: Boolean;
begin
Error:=False;
Len:=Length(Str);
Res:=0;
for i:=1 to Len do
if (Str[i]='0')or(Str[i]='1') then
Res:=Res+Pow(2, Len-i)*StrToInt(Str[i])
else
begin
MessageDlg('It is not a binary number', mtInformation, [mbOK], 0);
Error:=True;
Break;
end;
if Error=True then Result:=0
else Result:=Res;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if edit1.text ='' then
begin
ShowMessage('Emtpy not allow');
Edit1.text :='';
Label3.Caption := '';
Edit1.SetFocus;
end
else
Label3.Caption:= 'The equivalent value of ' + Edit1.Text
+ ' to Binary is ' + IntToStr(BinToDec(Edit1.Text))+'.'
End;
procedure TForm1.Button2Click(Sender: TObject);
begin
Edit1.text :='';
Label3.Caption := '';
Edit1.SetFocus
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
button1.Hint := 'Click here to convert binary number into its decimal equivalent.';
button1.ShowHint := true;
end;
end.
No comments:
Post a Comment