马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Math;
type
TForm1 = class(TForm)
edt1: TEdit;
btn1: TButton;
lbl1: TLabel;
procedure btn1Click(Sender: TObject);
procedure edt1Click(Sender: TObject);
procedure edt1KeyPress(Sender: TObject; var Key: Char);
private
{ Private declarations }
public
{ Public declarations }
end;
function spos(pos, g_length: Integer): Integer;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.btn1Click(Sender: TObject);
var
str: string; //编辑框的内容
i: Integer; //循环计次
Num: Extended; //转换后的值 因为POWER命令返回是EXTENDED类型
temp: Extended; //临时下每个位为1的值
Pos: Integer; //当前位置
lgh: Integer; //字符串总长度
begin
str := edt1.text;
lgh := Length(str);
num := 0;
for i := lgh downto 1 do
begin
if str[i] = '1' then
begin
Pos := spos(i, lgh);
temp := Power(2, pos - 1);
Num := Num + temp;
end;
end;
lbl1.Caption := '10进制的值为:'+floatToStr(Num);
end;
function spos(pos, g_length: Integer): Integer; //自定义的一个函数用来取从右到左的位置
var
nowpos: Integer;
begin
nowpos := g_length - pos + 1;
Result := nowpos;
end;
procedure TForm1.edt1Click(Sender: TObject);
begin
edt1.Text:=''
end;
procedure TForm1.edt1KeyPress(Sender: TObject; var Key: Char);
begin
if Key in ['0','1'] = False then //如果输入的不是0,1的话就不给显示
Key:=#0;
end;
end.
程序有三个组件 一个 叫edt1 的编辑框
一个叫btn1的按钮
一个叫lbl1的标签
|