1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| program Project6;
uses
WinCrt;
function StrToBin(const S: string): byte;
var V : byte;
N : byte;
begin
N := Length(S);
assert(N <= 8, 'Chaine trop longue');
V := $1;
result := 0;
repeat
if S[N] = '1' then
result := result or V;
V := V shl 1;
dec(N);
until N = 0;
end;
function BinToStr(const W: byte): string;
var
V: byte;
N: byte;
begin
result := '00000000';
N := 8;
V := $1;
repeat
if (W and V) = V then
result[N] := '1';
V := V shl 1;
dec(N);
until N = 0;
end;
var
sW1, sW2 : string;
W1, W2 : byte;
R : LongInt;
begin
write('Entrez le premier mot binaire (4 bits max) : ');
readln(sW1);
W1 := StrToBin(sW1);
write('Entrez le deuxieme mot binaire (4 bits max) : ');
readln(sW2);
W2 := StrToBin(sW2);
writeln;
R := W1 + W2;
writeln(sW1,' + ',sW2,' = ', BinToStr(R), ' (',W1,' + ',W2,' = ', R,')');
R := W1 - W2;
writeln(sW1,' - ',sW2,' = ', BinToStr(R), ' (',W1,' - ',W2,' = ', R,')');
R := W1 * W2;
writeln(sW1,' * ',sW2,' = ', BinToStr(R), ' (',W1,' * ',W2,' = ', R,')');
R := W1 div W2;
writeln(sW1,' / ',sW2,' = ', BinToStr(R), ' (',W1,' / ',W2,' = ', R,')');
readln;
end. |