fork download
  1. program ideone;
  2.  
  3. {$MODE OBJFPC}{$LONGSTRINGS ON}
  4.  
  5. type
  6. TBinaryNumber = class(TObject)
  7. private type
  8. TBinaryDigits = array [0 .. 3] of Char;
  9. private
  10. FDigits: TBinaryDigits;
  11. private
  12. function BinaryToDecimal(ADigits: TBinaryDigits): UInt8;
  13. function DecimalToBinary(ANumber: UInt8): TBinaryDigits;
  14. private
  15. function GetAsBinary(): String;
  16. function GetAsDecimal(): UInt8;
  17. public
  18. constructor CreateAsBinary(ADigits: array of Char);
  19. constructor CreateAsDecimal(ANumber: UInt8);
  20. public
  21. procedure Add(ABinaryNumber: TBinaryNumber);
  22. procedure Multiply(ABinaryNumber: TBinaryNumber);
  23. public
  24. property AsBinary: String read GetAsBinary;
  25. property AsDecimal: UInt8 read GetAsDecimal;
  26. end;
  27.  
  28. constructor TBinaryNumber.CreateAsBinary(ADigits: array of Char);
  29. begin
  30. FDigits := ADigits;
  31. end;
  32.  
  33. constructor TBinaryNumber.CreateAsDecimal(ANumber: UInt8);
  34. begin
  35. FDigits := DecimalToBinary(ANumber);
  36. end;
  37.  
  38. function TBinaryNumber.BinaryToDecimal(ADigits: TBinaryDigits): UInt8;
  39. var
  40. intCode: Integer;
  41. begin
  42. Val('%' + ADigits, Result, intCode);
  43. end;
  44.  
  45. function TBinaryNumber.DecimalToBinary(ANumber: UInt8): TBinaryDigits;
  46. begin
  47. Result := BinStr(ANumber, Length(TBinaryDigits));
  48. end;
  49.  
  50. function TBinaryNumber.GetAsBinary(): String;
  51. begin
  52. Result := FDigits;
  53. end;
  54.  
  55. function TBinaryNumber.GetAsDecimal(): UInt8;
  56. begin
  57. Result := BinaryToDecimal(FDigits);
  58. end;
  59.  
  60. procedure TBinaryNumber.Add(ABinaryNumber: TBinaryNumber);
  61. begin
  62. FDigits := BinStr(UInt8(Self.AsDecimal + ABinaryNumber.AsDecimal), Length(TBinaryDigits));
  63. end;
  64.  
  65. procedure TBinaryNumber.Multiply(ABinaryNumber: TBinaryNumber);
  66. begin
  67. FDigits := BinStr(UInt8(Self.AsDecimal * ABinaryNumber.AsDecimal), Length(TBinaryDigits));
  68. end;
  69.  
  70. var
  71. Bin1st, Bin2nd: TBinaryNumber;
  72. begin
  73. Bin1st := TBinaryNumber.CreateAsBinary('0010');
  74. Bin2nd := TBinaryNumber.CreateAsDecimal(3);
  75. try
  76. WriteLn('Bin1st - 0b', Bin1st.AsBinary, ' (', Bin1st.AsDecimal, ')');
  77. WriteLn('Bin2nd - 0b', Bin2nd.AsBinary, ' (', Bin2nd.AsDecimal, ')', #10);
  78.  
  79. Bin1st.Add(Bin2nd);
  80.  
  81. WriteLn('Bin1st - 0b', Bin1st.AsBinary, ' (', Bin1st.AsDecimal, ')');
  82. WriteLn('Bin2nd - 0b', Bin2nd.AsBinary, ' (', Bin2nd.AsDecimal, ')', #10);
  83.  
  84. Bin2nd.Multiply(Bin1st);
  85.  
  86. WriteLn('Bin1st - 0b', Bin1st.AsBinary, ' (', Bin1st.AsDecimal, ')');
  87. WriteLn('Bin2nd - 0b', Bin2nd.AsBinary, ' (', Bin2nd.AsDecimal, ')', #10);
  88. finally
  89. Bin1st.Free();
  90. Bin2nd.Free();
  91. end;
  92.  
  93. ReadLn();
  94. end.
  95.  
Success #stdin #stdout 0s 280KB
stdin
Standard input is empty
stdout
Bin1st - 0b0010 (2)
Bin2nd - 0b0011 (3)

Bin1st - 0b0101 (5)
Bin2nd - 0b0011 (3)

Bin1st - 0b0101 (5)
Bin2nd - 0b1111 (15)