Задача на сложение двоичных чисел

program new;
var sum,ch1,j,ch2:integer;
s1,s2:string;
s:array[1..255]of char;

function preobr(s:string):integer;
var i,kol,m:integer;
begin
m:=1;
kol:=0;
for i:=length(s) downto 1 do begin
if ord(s[i])=ord('1') then
kol:=kol+m;
m:=m*2;
end;
preobr:=kol;
end;

begin
for j:=1 to 255 do
s[j]:='0';
writeln('vvedite chislo 1: ');
readln(s1);
writeln('vvedire chislo 2: ');
readln(s2);
sum:=preobr(s1)+preobr(s2);
write(s1,' + ',s2,' = ');
s1:='';
ch2:=1;
repeat
ch1:=1;
j:=1;
while sum>=ch1 do begin
ch1:=ch1*2;
j:=j+1;
end;
ch1:=ch1 div 2;
j:=j-1;
s1:=s1+'*';
s[j]:='1';
sum:=sum-ch1;
if (sum=1) then
s[1]:='1';
if j>ch2 then
ch2:=j;
until sum<=0;
for j:=ch2 downto 1 do
write(s[j]);
readln;
end.


На Главную

Hosted by uCoz