[syntax=Delphi]
For I := Length(V) downto 1 do
Begin
If V>'9' then
A := Ord(V)-48-7
else
A := Ord(V)-48;
PosR := 100-Length(V)+I; { Выщитываем текущую позицию в массиве Res }
Res[PosR] := Res[PosR] + A; { Прибавляем значение к текущей позиции }
If Res[PosR]>15 then { ГДе то в этой части кода находится баг }
Begin
Res[PosR-1] := Res[PosR-1] + 1; { Увеличиваем на 1 старшую позицию }
Res[PosR] := Res[PosR] - 16; { А в этой оставляем только остаток }
End;
End;
For I:= 100-Length(V) downto 2 do
if Res>15 then
Begin
Res[I-1] := Res[I-1] + 1; { Увеличиваем на 1 старшую позицию }
Res := Res - 16; { А в этой оставляем только остаток }
End;
[/syntax]
Сложение шестнадцатеричных чисел
Модераторы: Хыиуду, MOTOCoder, Medved, dr.Jekill
Рабочая программа.
Код: Выделить всё
Uses crt;
Const HexTab : array[0..15] of char = ('0','1','2','3','4','5','6','7',
'8','9','A','B','C','D','E','F');
Var Res : Array[1..100] of byte;
I : Integer;
S : String;
F : Text;
Procedure Sum(V : String); { adding new value to result }
Var A : Byte;
PosR : Integer;
Begin
For I := 1 to Length(V) do { convert to uppercase}
Begin
If V[i] in['a'..'f'] then Dec(V[i],32); { if letter is lowcase convert to uppercase }
If not(V[i] in['0'..'9','A'..'F']) then { display error if not hex }
Begin
WriteLn(V,' is not hex!');
Exit;
End;
End;
For I := Length(V) downto 1 do
Begin
If V[i]>'9' then { convert '0'..'F' into 0..15 }
A := Ord(V[i])-48-7
else
A := Ord(V[i])-48;
PosR := 100-Length(V)+I; { current position in massive Res }
Res[PosR] := Res[PosR] + A; { adding valueto current position }
If Res[PosR]>15 then { if too big, add 1 to next position }
Begin
Res[PosR-1] := Res[PosR-1] + 1; { inc next pos by 1 }
Res[PosR] := Res[PosR] - 16; { div}
End;
End;
{------------------ Здесь устраняем ошибку по переносу разрядов }
For I := 100 downto 2 do
Begin
If Res[i]>15 then
Begin
Res[I-1] := Res[I-1] + 1;
Res[i] := Res[i] - 16;
End;
End;
{--------------------}
End;
Begin
clrscr;
For I := 1 to 100 do Res[i] := 0; { make res 0 }
Assign(F,'a.txt');
Reset(F);
While not Eof(F) do
Begin
ReadLn(F,S); { reading next value }
If S='' then Continue; { skip empty lines }
Sum(S); { adding new line to result }
End;
Close(F);
{preparing for output }
S := '';
For I := 1 to 100 do
Begin
S := S + HexTab[Res[i]]; { convert from 0..15 to '0'..'F' }
End;
While (S<>'') and (S[1]='0') do Delete(S,1,1); { delete 0 from beginning}
WriteLn(S); { write hex }
readln;
End.
Отличный сайт у вас, красивая шапка
http://pulsz.ru/
http://pulsz.ru/