algorithm - 编码空格删除时如何删除最后一个空格

标签 algorithm whitespace eiffel

这是我的空间移除作业的 Eiffel 代码:

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

有人可以帮我吗?

最佳答案

当您读取第一个空格字符时,不要立即打印它。等待下一个非空格字符打印它,如果你得到 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

关于algorithm - 编码空格删除时如何删除最后一个空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32813449/

相关文章:

algorithm - Google Code Jam 2008 第 1A 轮第 3 题

c - 在 C 中读取输入文件时忽略空格

import - Eiffel 文本中的 "Using"或 "import"子句

html - 在 CSS 中使用 "top"标签后删除空白

multidimensional-array - 尝试遍历Eiffel中的数组时出错

.net - 在调用例程之后和之前在哪里评估不变量?

javascript - 将数字转换为罗马数字

Python 计算错误的大 float

algorithm - 函数式语言中的等价类和联合/查找

python - strip() 和 strip(string.whitespace) 给出不同的结果,尽管文档表明它们应该相同