- •Перевод чисел из одной системы счисления в другую в Delphi
- •Содержание
- •Введение
- •Понятие системы счисления. Классификация систем счисления. Позиционные и непозиционные системы счисления
- •Непозиционные системы счисления
- •Позиционные системы счисления
- •Перевод целых чисел из одной системы счисления в другую
- •Перевод дробных чисел из одной системы счисления в другую
- •Перевод произвольных чисел
- •Перевод чисел из системы счисления с основанием 2 в систему счисления с основанием 2n и обратно
- •Перевод двоичных, восьмеричных и шестнадцатеричных чисел в десятичную систему счисления.
- •Программа перевода чисел в Delphi
- •Заключение
- •Литература
Перевод двоичных, восьмеричных и шестнадцатеричных чисел в десятичную систему счисления.
Для перевода числа P-ичной системы в десятичную необходимо использовать следующую формулу разложения: аnan-1…а1а0=аnPn+ аn-1Pn-1+…+ а1P+a0
Пример
1. Перевести число 101,11(2) в десятичную
систему счисления.
Ответ:
101,11(2)= 5,75(10) .
Пример
2. Перевести число 57,24(8) в десятичную
систему счисления.
Ответ:
57,24(8) = 47,3125(10) .
Пример
3. Перевести число 7A,84(16) в десятичную
систему счисления.
Ответ:
7A,84(16)= 122,515625(10) .
Программа перевода чисел в Delphi
unit uMain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,Math;
type
TfrmMain = class(TForm)
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
edSrcBase: TEdit;
edSrcNum: TEdit;
edTrgPrecision: TEdit;
Label5: TLabel;
Label6: TLabel;
Label7: TLabel;
btnTransform: TButton;
edTrgBase: TEdit;
edTrgNum: TEdit;
procedure btnTransformClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var frmMain: TfrmMain;
implementation
uses StrUtils;
{$R *.dfm}
function IntToDigit(aNum : Byte) : String;
const SelfName : String = 'IntToDigit.';
begin
case aNum of
0..9 : Result := IntToStr(aNum);
10 : Result := 'A';
11 : Result := 'B';
12 : Result := 'C';
13 : Result := 'D';
14 : Result := 'E';
15 : Result := 'F';
else
Raise Exception.Create(SelfName + ' Числу не сопоставлена цифра!');
end;
end;
function DigitToInt(aDigit : AnsiChar; aBase : Byte) : Byte;
const
SelfName : String = 'DigitToInt.';
begin
if aBase < 2 then
Raise Exception.Create(SelfName + ' Основание системы счисления должно быть >= 2!') ;
case aDigit of
'0'..'9' : Result := StrToInt(aDigit);
'A', 'a' : Result := 10;
'B', 'b' : Result := 11;
'C', 'c' : Result := 12;
'D', 'd' : Result := 13;
'E', 'e' : Result := 14;
'F', 'f' : Result := 15;
else
Raise Exception.Create(SelfName + ' Неизвестный символ в представлении числа!');
end;
if Result > aBase - 1 then
Raise Exception.Create(SelfName + ' В данной системе счисления нет такой цифры!');
end;
function XcimalStrToNumber(aStrXcimal : String; aBase : Byte) : Extended;
const SelfName : String = 'XcimalStrToNumber.';
var i, j : Integer;
StrInt : String;
StrFrac : String;
Pos1 : Integer;
IntPart : Extended;
FracPart : Extended;
begin
if Length(aStrXcimal) = 0 then
Raise Exception.Create(SelfName + ' Не задано число!') ;
Pos1 := Pos(',', aStrXcimal);
if Pos1 = 0 then begin
StrInt := aStrXcimal;
StrFrac := '';
end else begin
StrInt := LeftStr(aStrXcimal, Pos1 - 1);
StrFrac := Copy(aStrXcimal, Pos1 + 1, Length(aStrXcimal) - Pos1);
end;
IntPart := 0;
for i := 1 to Length(StrInt) do begin
j := Length(StrInt) - i;
IntPart := IntPart + DigitToInt(StrInt[i], aBase) * Power(aBase, j);
end;
FracPart := 0;
for i := 1 to Length(StrFrac) do begin
j := Length(StrFrac) - i;
FracPart := FracPart + DigitToInt(StrFrac[i], aBase) * Power(aBase, j);
end;
FracPart := FracPart / Power(aBase, Length(StrFrac));
Result := IntPart + FracPart;
end;
function XcimalStrToYcimalStr ( aSrcBase : Byte; aSrcNumStr : String;
aTrgBase : Byte; aTrgPrecision : Byte) : String;
var SrcNum : Extended;
IntPart : Int64;
FracPart : Extended;
StrInt : String;
StrFrac : String;
i : Integer;
TempNum : Extended;
begin
SrcNum := XcimalStrToNumber(aSrcNumStr, aSrcBase);
IntPart := Trunc(SrcNum);
FracPart := Frac(SrcNum);
StrInt := '';
repeat
StrInt := IntToDigit(IntPart mod aTrgBase) + StrInt;
IntPart := IntPart div aTrgBase;
until IntPart = 0;
if FracPart = 0 then begin
Result := StrInt;
exit;
end;
StrFrac := '';
for i := 1 to aTrgPrecision do begin
TempNum := FracPart * aTrgBase;
StrFrac := StrFrac + IntToDigit(Trunc(TempNum));
FracPart := Frac(TempNum);
if FracPart = 0 then Break;
end;
Result := StrInt + ',' + StrFrac;
end;
procedure TfrmMain.btnTransformClick(Sender: TObject);
begin
edTrgNum.Text := XcimalStrToYcimalStr( StrToInt(edSrcBase.Text), edSrcNum.Text, StrToInt(edTrgBase.Text), StrToInt(edTrgPrecision.Text) );
end;
end.
