Advertisement
Guest User

Delphi Modulo 11

a guest
Apr 26th, 2016
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.00 KB | None | 0 0
  1. function Modulo11LinhaDigitavel(Valor: String; Base: Integer = 9; Resto : boolean = false) : string;
  2. var
  3.    Soma : integer;
  4.    Contador, Peso, Digito : integer;
  5.    I: integer;
  6. begin
  7.   for I := 1 to Length(Valor) do
  8.   begin
  9.     if Pos(Valor[I], '0123456789')=0 then
  10.     begin
  11.       Delete(Valor, I, 1);
  12.     end;
  13.   end;
  14.  
  15.   Valor := Copy(Valor, 1, 4) +
  16.            Copy(Valor, 33,15) +
  17.            Copy(Valor, 5,5) +
  18.            Copy(Valor, 11,10) +
  19.            Copy(Valor, 22,10);
  20.  
  21.   Valor := Copy(Valor, 1, 4) + Copy(Valor, 6, 39);
  22.  
  23.   Soma := 0;
  24.   Peso := 2;
  25.   for Contador := Length(Valor) downto 1 do
  26.   begin
  27.     Soma := Soma + (StrToInt(Valor[Contador]) * Peso);
  28.     if Peso < Base then
  29.       Peso := Peso + 1
  30.     else
  31.       Peso := 2;
  32.   end;
  33.   if Resto then
  34.     Result := IntToStr(Soma mod 11)
  35.   else
  36.   begin
  37.     Digito := 11 - (Soma mod 11);
  38.     if (Digito > 9) then
  39.       Digito := 0;
  40.     Result := IntToStr(Digito);
  41.   end;
  42.   // correção
  43.   if Result = '0' then Result := '1';
  44. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement