Advertisement
Guest User

Untitled

a guest
Oct 18th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.69 KB | None | 0 0
  1. program Arr2;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6.   SysUtils;
  7.  
  8. Type
  9.    TArr = array of integer;
  10.  
  11. procedure GetBin(var Num, Digit: Integer);
  12. begin
  13.    Digit := Num mod 2;
  14.    Num := Num div 2;
  15. end;
  16.  
  17. function ReverseBin(Bin: TArr): TArr;
  18. var
  19.    Index, Len: Integer;
  20. begin
  21.    Len := Length(Bin);
  22.    SetLength(Result, Len);
  23.    for Index := (Len - 1) downto 0 do
  24.       ReverseBin[Len - Index - 1] := Bin[Index];
  25. end;
  26.  
  27. procedure Main();
  28. const
  29.    CMaxNatural = 2147483647;
  30.    CMinNatural = 1;
  31.  
  32. var
  33.    P, ArrSize, Num, Digit, Index: Integer;
  34.    Bin: TArr;
  35.    IsCorrect: Boolean;
  36. begin
  37.    Write('Please, enter natural number to ', CMaxNatural,' : ');
  38.    IsCorrect := false;
  39.    repeat
  40.       try
  41.          Readln(P);
  42.          if (P < CMinNatural) then
  43.             Write('It is not a number from ', CMinNatural,' to ', CMaxNatural,' , please, enter valid number : ')
  44.          else
  45.             IsCorrect := true;
  46.       except
  47.          Write('Invalid input ! Please, enter natural number to ', CMaxNatural,' : ');
  48.       end;
  49.    until IsCorrect;
  50.    Digit := 0;
  51.    Num := P;
  52.    ArrSize := 1;
  53.    repeat
  54.       GetBin(Num, Digit);
  55.       SetLength(Bin, ArrSize);
  56.       Bin[ArrSize-1] := Digit;
  57.       inc(ArrSize);
  58.    until (Num = 1);
  59.    if (P > CMinNatural) then
  60.    begin
  61.    ArrSize := Length(Bin);
  62.    inc(ArrSize);
  63.    SetLength(Bin, ArrSize);
  64.    Bin[ArrSize-1] := 1;
  65.    Bin := ReverseBin(Bin);
  66.    end;
  67.  
  68.    Write(P,' in binary digit system equals : ');
  69.    for Index := 0 to high(Bin) do
  70.       Write(Bin[Index]);
  71.    Readln;
  72. end;
  73.  
  74. var
  75.    IsContinue, IsCorrect: Boolean;
  76.    Answer: Char;
  77.  
  78. begin
  79.    Writeln('This program converts a natural number to binary digit system.');
  80.    Main;
  81. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement