Advertisement
huiubiascwv

Untitled

Oct 16th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.35 KB | None | 0 0
  1. program Lab2Z2V31;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6.   SysUtils;
  7.  
  8.    procedure GetBin(var Num, Digit: Integer);
  9.    begin
  10.       Digit := Num mod 2;
  11.       Num := Num div 2;
  12.    end;
  13.  
  14.    function ReverseBin(const Str: string): string;
  15.    var
  16.       Index, Len: Integer;
  17.    begin
  18.       Len := Length(Str);
  19.       SetLength(Result, Len);
  20.       for Index := Len downto 1 do
  21.          ReverseBin[Len - Index + 1] := Str[Index];
  22.    end;
  23.  
  24. const
  25.    CMaxInt = 2147483647;
  26.    CMinNatural = 1;
  27.  
  28. var
  29.    P,Num, Digit: Integer;
  30.    Bin: String;
  31.    IsCorrect: Boolean;
  32.  
  33. begin
  34.    Writeln('This program converts a natural number to binary digit system.');
  35.    Write('Please, enter natural number to ', CMaxInt,' : ');
  36.    IsCorrect := false;
  37.    repeat
  38.       try
  39.          Readln(P);
  40.          if (P >= CMinNatural) then
  41.             IsCorrect := true
  42.          else
  43.             Write('It is not a natural number to ', CMaxInt,' , please, enter valid number : ');
  44.       except
  45.          Write('Invalid input ! Please, enter natural number to ', CMaxInt,' : ');
  46.       end;
  47.    until IsCorrect;
  48.  
  49.    Num := P;
  50.    repeat
  51.       GetBin(Num, Digit);
  52.       Bin := Bin + IntToStr(Digit);
  53.    until (Num < 2);
  54.    if (P > CMinNatural) then
  55.    begin
  56.    Bin := Bin + '1';
  57.    Bin := ReverseBin(Bin);
  58.    end;
  59.  
  60.    Writeln(P,' in binary digit system equals : ', Bin);
  61.    Readln;
  62. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement