|
The following example program inputs two denary (decimal) numbers, converts them to
binary, then adds them in binary and outputs the result, also in binary. If you do not
understand what this means, or how the program works, do some research! One of the main
skills of being a computer programmer is being able to find information, especially on the
internet (the rest of this site is a good place to start for general programming information).
Look through the code and see if you can work out what it does.
Program BinaryDenaryAdder;
{$mode objfpc}{$H+}
Uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Classes, SysUtils
{ add your units here };
Function AddBinary(BinaryNum1, BinaryNum2 : String) : String;
Var
NumberOfDigits, i, Diff : Integer;
Carry : Char;
BinaryResult : String;
Begin
If Not (Length(BinaryNum1) = Length(BinaryNum2))
Then
If Length(BinaryNum1) > Length(BinaryNum2)
Then
Begin
NumberOfDigits := Length(BinaryNum1);
Diff := NumberOfDigits - Length(BinaryNum2);
For i := 1 To Diff
Do BinaryNum2 := '0' + BinaryNum2;
End
Else
Begin
NumberOfDigits := Length(BinaryNum2);
Diff := NumberOfDigits - Length(BinaryNum1);
For i := 1 To Diff
Do BinaryNum1 := '0' + BinaryNum1;
End
Else NumberOfDigits := Length(BinaryNum1);
Writeln('Binary number input 1: ', BinaryNum1);
Writeln('Binary number input 2: ', BinaryNum2);
BinaryResult := '';
For i := 1 To NumberOfDigits
Do BinaryResult := BinaryResult + ' ';
Carry := '0';
For i := NumberOfDigits DownTo 1
Do
If Carry = '0'
Then
If (BinaryNum1[i] = '1') And (BinaryNum2[i] = '1')
Then
Begin
Carry := '1';
BinaryResult[i] := '0'
End
Else
If (BinaryNum1[i] = '0') And (BinaryNum2[i] = '0')
Then BinaryResult[i] := '0'
Else BinaryResult[i] := '1'
Else
If (BinaryNum1[i] = '0') And (BinaryNum2[i] = '0')
Then
Begin
BinaryResult[i] := '1';
Carry := '0';
End
Else
If (BinaryNum1[i] = '1') And (BinaryNum2[i] = '1')
Then BinaryResult[i] := '1'
Else BinaryResult[i] := '0';
If Carry = '1'
Then BinaryResult := '1' + BinaryResult;
AddBinary := BinaryResult;
End;
Function ConvertDenaryToBinary(DenaryNum : Integer) : String;
Var
TempResult, BinaryResult : String;
Count : Integer;
Begin
TempResult := '';
Repeat
TempResult := TempResult + IntToStr(DenaryNum Mod 2);
DenaryNum := DenaryNum Div 2;
Until DenaryNum = 0;
BinaryResult := '';
For Count := Length(TempResult) DownTo 1
Do BinaryResult := BinaryResult + TempResult[Count];
ConvertDenaryToBinary := BinaryResult;
End;
Var
NumIn1, NumIn2 : Integer;
BinaryIn1, BinaryIn2 : String;
Result : String;
Begin
Writeln('Combined Denary To Binary Convertor and Binary Adder');
Writeln;
Write('Input denary number 1: ');
Readln(NumIn1);
Write('Input denary number 2: ');
Readln(NumIn2);
BinaryIn1 := ConvertDenaryToBinary(NumIn1);
BinaryIn2 := ConvertDenaryToBinary(NumIn2);
Result := AddBinary(BinaryIn1, BinaryIn2);
Writeln('Result in Binary: ', Result);
Readln;
End.
Looked through the code yet? (hint: start with the main Begin End block at the end)
Now download the
Lazarus project file [4KB]
(you may have to right-click the link and then click "Save Target As" or "Save Link As"),
run the program, and see if you were correct.
If you understood the program, then well done, you are now thinking like a computer programmer!
If not, then don't worry, just do some more research and try again.
|