这里时,我摆脱过去的空间我艾菲尔代码为我的空间去除分配:如何编码空间去除
feature {NONE} -- Main routine
copy_file
-- Copy a file character by character from input to output
require
input_open: input.is_readable
output_open: output.is_writable
local flag: INTEGER
do
flag := 0 -- 0 for previous space, 1 for previous char
from read_char -- Must prime the pump by reading the first character
until ch = EOF
loop
from
ch := input.last_character
until
ch = EOL
loop
if ch = Space_char and flag = 0 then -- leading spaces
read_char
elseif ch /= Space_char and flag = 0 then -- see first charater after space
output.putchar (ch)
flag := 1
read_char
elseif ch = Space_char and flag = 1 then -- see space after characters
output.putchar (Space_char)
flag := 0
read_char
elseif ch /= Space_char and flag = 1 then -- see character after character
output.putchar (ch)
read_char
end
end
flag := 0
read_char
end
-- At end of file, nothing to do in Eiffel except close the files
input.close
output.close
end
这里是样本输入:
Leading spaces
Training spaces
Leading and trailing spaces
Only interword spaces
Leading, trailing and interword spaces
This line has correct spaces
Previous line was empty
Previous line has only leader spaces
OneWordLine
Three word line
我跑代码和我得到的输出是略有不同的要求,比如说,当线中有一个尾部空格时,我总是会得到一个额外的空间。
这里是我的输出:
Leading spaces
Training spaces
Leading and trailing spaces
Only interword spaces
Leading, trailing and interword spaces
This line has correct spaces
Previous line was empty
Previous line has only leader spaces
OneWordLine
Three word line
有人可以帮我吗?
如果你发现一个单词之后的空格,记住它,但不要马上打印;等到你找到另一个词。 – m69