0

这是我的 eiffel 程序,它基本上是在执行空格删除(删除给定文本文件中的冗余空格)以遵循正则表达式:A+(SA+)*EOL,其中对于文件中的每一行,它必须以字母开头,并且字母之间只有一个空格。

我的问题是,基于这个程序,我如何扩展它以使每个其他单词都变成大写?即第 2、第 4、第 6 等。

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
has_read_space: BOOLEAN
empty_line : BOOLEAN
do
empty_line: True
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
            if has_read_space then          -- this clause make sure the space will only place in between two words instead of end of lin
               output.putchar (Space_char)
            end
            output.putchar (ch)
            empty_line := False
            flag := 1
            read_char
        elseif ch = Space_char and flag = 1 then    -- see space after characters
            has_read_space := True          -- Don't output it right away
            flag := 0
            read_char
        elseif ch /= Space_char and flag = 1  then  -- see character after character
            output.putchar (ch)
            read_char
        end
    end
    if empty_line = False then
        output.putchar (EOL)        -- if line is not empty, then we place EOL at the end of line
    end
    flag := 0       -- reset it to 0 to make sure the next follow the same routine
    has_read_space := False -- reset it to avoid placing space in the beginning of line
    empty_line := True  -- reset to proceed to new line
    read_char
end

  -- At end of file, nothing to do in Eiffel except close the files
input.close
output.close
end
4

1 回答 1

0

刚刚想通了。只需设置另一个标志,比如 evenWord,将其初始化为 false。并在elseif ch = Space_char and flag = 1子句中翻转标志evenWord := not evenWord它有效

于 2015-10-03T19:41:50.800 回答