【问题标题】:How do I get rid of the last space when coding space removal编码空间删除时如何摆脱最后一个空格
【发布时间】:2015-09-27 22:20:13
【问题描述】:

这是我的空间删除任务的埃菲尔代码:

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

有人可以帮我吗?

【问题讨论】:

  • 如果发现单词后面有空格,请记住,但不要立即打印;等你找到另一个词。

标签: algorithm whitespace eiffel


【解决方案1】:

当您读取第一个空格字符时,不要立即打印它。等待下一个非空格字符打印它,如果你得到 EOL 字符,你不打印它。这是你的算法修改:

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
    has_read_space := False
    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
            if has_read_space then
                output.putchar (Space_char) -- Print the space when reading a character
                has_read_space := False
            end
            output.putchar (ch)
            flag := 1
            read_char
        elseif ch = Space_char and flag = 1 then    -- see space after characters
            has_read_space := True  -- Don't print it right now
            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

【讨论】:

  • 非常感谢,但是需要将has_read_space重置为false,否则代码还是一样,但是,谢谢你的想法!!
  • 是的。对不起,我修好了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-27
  • 1970-01-01
  • 1970-01-01
  • 2017-03-03
  • 2015-01-08
  • 1970-01-01
相关资源
最近更新 更多