⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 untutils.pas

📁 此程序实现对汉明码的编码与译码算法。汉明码是一种多重(复式)奇偶检错系统。它将信息用逻辑形式编码
💻 PAS
字号:
unit untUtils;

interface

uses
  SysUtils,
  StrUtils,
  Math;

function pModDouble(const dNumIn, dDivNum: Double): Double;
function pConvertBase(const sNumIn: string; const nBaseIn, nBaseOut: Integer):
  string;
function pAnyBaseAdd(const sNumIn: string; const nBaseIn, nValue: Integer):
  string; overload;
function pAnyBaseAdd(const sNumIn: string; const nBaseIn: Integer): string;
overload;

implementation

function pAnyBaseAdd(const sNumIn: string; const nBaseIn: Integer): string;
var
  nValueAdd, n, nSrcValue, nLength: Integer;
begin
  (* 任意进制加一转换 *)

  Result:=EmptyStr;
  nValueAdd := 1;
  nLength := Length(sNumIn);

  for n := nLength downto 1 do
  begin
    nSrcValue := StrToInt(sNumIn[n]);
    Inc(nSrcValue, nValueAdd);
    if nSrcValue >= nBaseIn then
      nSrcValue := nSrcValue mod nBaseIn
    else
      nValueAdd := 0;
    Result := IntToStr(nSrcValue) + Result;
  end;
end;

function pAnyBaseAdd(const sNumIn: string; const nBaseIn, nValue: Integer):
  string;
var
  sDecimal: string;
begin
  (* 任意进制加法转换 *)

  sDecimal := pConvertBase(sNumIn, nBaseIn, 10);
  sDecimal := IntToStr(StrToInt(sDecimal) + nValue);
  Result := pConvertBase(sDecimal, 10, nBaseIn);
end;

function pModDouble(const dNumIn, dDivNum: Double): Double;
begin
  Result := dNumIn - (Int(dNumIn / dDivNum) * dDivNum);
end;

function pConvertBase(const sNumIn: string; const nBaseIn, nBaseOut: Integer):
  string;
var
  sNumInCaps, sRemainder: string;
  chCurrentCharacter: Char;
  n, nCharacterValue, nPlaceValue: Integer;
  dRunningTotal, dRemainder, dBaseOutDouble: Double;
begin
  (* 任意进制转换函数 *)

  Result := EmptyStr;

  if (sNumIn = EmptyStr) or (nBaseIn < 2) or (nBaseIn > 36) or
    (nBaseOut < 1) or (nBaseOut > 36) then
    Exit;

  sNumInCaps := UpperCase(sNumIn);
  nPlaceValue := Length(sNumInCaps);
  dRunningTotal := 0;

  for n := 1 to Length(sNumInCaps) do
  begin
    Dec(nPlaceValue, 1);
    chCurrentCharacter := sNumInCaps[n];
    nCharacterValue := 0;

    if (Ord(chCurrentCharacter) > 64) and (Ord(chCurrentCharacter) < 91) then
      nCharacterValue := Ord(chCurrentCharacter) - 55;

    if nCharacterValue = 0 then
      if (Ord(chCurrentCharacter) < 48) or (Ord(chCurrentCharacter) > 57) then
        Exit
      else
        nCharacterValue := StrToInt(chCurrentCharacter);

    if (nCharacterValue < 0) or (nCharacterValue > nBaseIn - 1) then
      Exit;

    dRunningTotal := dRunningTotal + nCharacterValue * Power(nBaseIn,
      nPlaceValue);
  end;

  repeat
    dBaseOutDouble := nBaseOut;
    dRemainder := pModDouble(dRunningTotal, dBaseOutDouble);
    sRemainder := ' ' + FloatToStr(dRemainder);
    dRunningTotal := (dRunningTotal - dRemainder) / nBaseOut;

    if (dRemainder >= 10) or (Length(sRemainder) = 1) then
      chCurrentCharacter := Chr(Round(Int(dRemainder) + 55))
    else
      chCurrentCharacter := RightBStr(sRemainder, Length(sRemainder) - 1)[1];

    Result := chCurrentCharacter + Result;
  until (dRunningTotal <= 0);
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -