如何在单个 read() 中获取由 space 分隔的多个 char 和 longint 的输入?
How to get input of multiple char and longint separated by a space in single read()?
我试过了,但它给了我运行时错误 106。
var
a, c: char;
b, d: longint;
begin
read(a, b, c, d);
write(a, ' ', b, ' ', c, ' ', d);
end.
输入是
A 1 B 2
正如@DavidHeffernan 所评论的那样,正如@lurker 所解释的那样,您的代码将无法按原样运行。
假设你有一个 FreePascal 版本知道 TStringList
单元 Classes
中的类型(我想所有最近的都知道),你可以做以下:
uses
Classes, SysUtils;
procedure Test;
var
a, c: Char;
b, d: Longint;
s: string;
sl: TStringList;
begin
sl := TStringList.Create;
try
Writeln('Enter a char, a number, a char and a number again, separated by spaces:');
// Read entire line
Readln(s);
// Split s into four entries in the string list
sl.Delimiter := ' ';
sl.DelimitedText := s;
if sl.Count >= 4 then
begin
a := sl[0][1]; // first string item, convert to char
b := StrToInt(sl[1]); // second string item, convert to integer
c := sl[2][1]; // third string item, convert to char
d := StrToInt(sl[3]); // fourth string item, convert to integer
end;
finally
sl.Free;
end;
Writeln(a, ' ', b, ' ', c, ' ', d);
end;
begin
Test;
end.
您可能应该为输入错误的情况添加错误检查,也许使用 TryStrToInt
而不是 StrToInt
,但我将把它留作练习。
正如@lurker所说,在你的问题中,你试图读取字符,这会导致问题,因为例如 space 和制表符也是字符,所以 space 分隔符不要像你预期的那样工作。这会弄乱整个输入。获取这些项目的唯一方法是读取整个字符串,然后对其进行解析(拆分)。
如果您的 FreePascal 版本有一个带有 SplitString
函数的单元 StrUtils
,您可以使用它来代替 TStringList
。它使用给定的分隔符(此处为 ' '
)将字符串拆分为动态数组。那可能更简单一些,但也不多。
我尝试了这个并且成功了,但它需要更多未使用的变量。
var
a, c: char;
b, d: char;
e, f, g, h: char; // take care of space
p, q: char;
x, y: longint;
begin
read(a, b, c, d, e, f, g, h);
p := a;
q := e;
x := ord(c) - 48;
y := ord(g) - 48;
write(p, ' ', x + 1, ' ', q, ' ', y + 1);
end.
我试过了,但它给了我运行时错误 106。
var
a, c: char;
b, d: longint;
begin
read(a, b, c, d);
write(a, ' ', b, ' ', c, ' ', d);
end.
输入是
A 1 B 2
正如@DavidHeffernan 所评论的那样,正如@lurker 所解释的那样,您的代码将无法按原样运行。
假设你有一个 FreePascal 版本知道 TStringList
单元 Classes
中的类型(我想所有最近的都知道),你可以做以下:
uses
Classes, SysUtils;
procedure Test;
var
a, c: Char;
b, d: Longint;
s: string;
sl: TStringList;
begin
sl := TStringList.Create;
try
Writeln('Enter a char, a number, a char and a number again, separated by spaces:');
// Read entire line
Readln(s);
// Split s into four entries in the string list
sl.Delimiter := ' ';
sl.DelimitedText := s;
if sl.Count >= 4 then
begin
a := sl[0][1]; // first string item, convert to char
b := StrToInt(sl[1]); // second string item, convert to integer
c := sl[2][1]; // third string item, convert to char
d := StrToInt(sl[3]); // fourth string item, convert to integer
end;
finally
sl.Free;
end;
Writeln(a, ' ', b, ' ', c, ' ', d);
end;
begin
Test;
end.
您可能应该为输入错误的情况添加错误检查,也许使用 TryStrToInt
而不是 StrToInt
,但我将把它留作练习。
正如@lurker所说,在你的问题中,你试图读取字符,这会导致问题,因为例如 space 和制表符也是字符,所以 space 分隔符不要像你预期的那样工作。这会弄乱整个输入。获取这些项目的唯一方法是读取整个字符串,然后对其进行解析(拆分)。
如果您的 FreePascal 版本有一个带有 SplitString
函数的单元 StrUtils
,您可以使用它来代替 TStringList
。它使用给定的分隔符(此处为 ' '
)将字符串拆分为动态数组。那可能更简单一些,但也不多。
我尝试了这个并且成功了,但它需要更多未使用的变量。
var
a, c: char;
b, d: char;
e, f, g, h: char; // take care of space
p, q: char;
x, y: longint;
begin
read(a, b, c, d, e, f, g, h);
p := a;
q := e;
x := ord(c) - 48;
y := ord(g) - 48;
write(p, ' ', x + 1, ' ', q, ' ', y + 1);
end.