Advertisement
Guest User

Untitled

a guest
Oct 18th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.44 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. procedure Main();
  25. const
  26.    CMaxNatural = 2147483647;
  27.    CMinNatural = 1;
  28.  
  29. var
  30.    P,Num, Digit: Integer;
  31.    Bin: String;
  32.    IsCorrect: Boolean;
  33.  
  34. begin
  35.    Write('Please, enter natural number to ', CMaxNatural,' : ');
  36.    IsCorrect := false;
  37.    repeat
  38.       try
  39.          Readln(P);
  40.          if (P < CMinNatural) then
  41.             Write('It is not a number from ', CMinNatural,' to ', CMaxNatural,' , please, enter valid number : ')
  42.          else
  43.             IsCorrect := true;
  44.       except
  45.          Write('Invalid input ! Please, enter natural number to ', CMaxNatural,' : ');
  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;
  63.  
  64. var
  65.    IsContinue, IsCorrect: Boolean;
  66.    Answer: Char;
  67.  
  68. begin
  69.    Writeln('This program converts a natural number to binary digit system.');
  70.    Main;
  71. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement