История изменений
Исправление bormant, (текущая версия) :
Первое впечатление: непрактично и сильно странно.
Вообще хорошо бы придерживаться правила: сперва ТЗ, потом код.
Допустим, нужно читать 10-е или 16-е (префикс '$') числа.
Вот ее и реализуем:
function ReadLong(var N: Longint): Boolean;
var
Res: Longint;
Base: Integer;
c: Char;
begin
ReadLong:=False;
repeat Read(c)
until c>' ';
Base:=10; Res:=0;
if c='$' then begin
Base:=16; Read(c);
end;
while c>' ' do begin
c:=UpCase(c);
case c of
'0'..'9':
Res:=Res*Base+Ord(c)-Ord('0');
'A'..'F':
if base=16 then
Res:=Res*Base+Ord(c)-Ord('A')+10
else
Exit;
else
Exit;
end;
Read(c);
end;
N:=Res;
ReadLong:=True;
end;
var x, y: Longint;
begin
WriteLn('Use prefix "$" for hexadecimal numbers.');
repeat Write('x: ');
until ReadLong(x);
repeat Write('y: ');
until ReadLong(y);
WriteLn(x,' times ',y,' is ',x*y);
end.
Нужно что-то добавить: меняем ТЗ (обязательно до непротиворечивого состояния!), меняем код.
Но в целом, вот эта конкретная задача реализуется куда как проще:
var x, y: Longint;
begin
WriteLn('Use prefix "$" for hexadecimal numbers.');
repeat Write('x: '); Read(x);
until IOResult=0;
repeat Write('y: '); Read(y);
until IOResult=0;
WriteLn(x,' times ',y,' is ',x*y);
end.
Исправление bormant, :
Первое впечатление: непрактично и сильно странно.
Вообще хорошо бы придерживаться правила: сперва ТЗ, потом код.
Допустим, нужно читать 10-е или 16-е (префикс '$') числа.
Вот ее и реализуем:
function ReadLong(var N: Longint): Boolean;
var
Res: Longint;
Base: Integer;
c: Char;
begin
ReadLong:=False;
repeat Read(c)
until c>' ';
Base:=10; Res:=0;
if c='$' then begin
Base:=16; Read(c);
end;
while c>' ' do begin
c:=UpCase(c);
case c of
'0'..'9':
Res:=Res*Base+Ord(c)-Ord('0');
'A'..'F':
if base=16 then
Res:=Res*Base+Ord(c)-Ord('A')+10
else
Exit;
else
Exit;
end;
Read(c);
end;
N:=Res;
ReadLong:=True;
end;
var x, y: Longint;
begin
WriteLn('Use prefix "$" for hexadecimal numbers.');
repeat Write('x: ');
until ReadLong(x);
repeat Write('y: ');
until ReadLong(y);
WriteLn(x,' times ',y,' is ',x*y);
end.
Нужно что-то добавить: меняем ТЗ (обязательно до непротиворечивого состояния!), меняем код.
Но в целом, вот эта конкретная задача реализуется куда как проще:
var x, y: Longint;
begin
WriteLn('Use prefix "$" for hexadecimal numbers.');
repeat Write('x: '); Read(x);
until IOResult=0;
repeat Write('y: '); Read(y);
until IOResult=0;
end.
Исходная версия bormant, :
Первое впечатление: непрактично и сильно странно.
Вообще хорошо бы придерживаться правила: сперва ТЗ, потом код.
Допустим, нужно читать 10-е или 16-е (префикс '$') числа.
Вот ее и реализуем:
function ReadLong(var N: Longint): Boolean;
var
Res: Longint;
Base: Integer;
c: Char;
begin
ReadLong:=False;
repeat Read(c)
until c>' ';
Base:=10; Res:=0;
if c='$' then begin
Base:=16; Read(c);
end;
while c>' ' do begin
c:=UpCase(c);
case c of
'0'..'9':
Res:=Res*Base+Ord(c)-Ord('0');
'A'..'F':
if base=16 then
Res:=Res*Base+Ord(c)-Ord('A')+10
else
Exit;
else
Exit;
end;
Read(c);
end;
N:=Res;
ReadLong:=True;
end;
var x, y: Longint;
begin
WriteLn('Use prefix "$" for hexadecimal numbers.');
repeat Write('x: ');
until ReadLong(x);
repeat Write('y: ');
until ReadLong(y);
WriteLn(x,' times ',y,' is ',x*y);
end.
Нужно что-то добавить: меняем ТЗ (обязательно до непротиворечивого состояния!), меняем код.
Но в целом, вот эта конкретная задача реализуется куда как проще:
var x, y: Longint;
begin
WriteLn('Use prefix "$" for hexadecimal numbers.');
repeat Write('x: '); Read(x);
until IOResult=0;
repeat Write('y: '); Read(y);
until IOResult=0;
end.