这是我的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
答案 0 :(得分:0)
刚想通了。
只需设置另一个标志,比如evenWord,将其初始化为false。
并在elseif ch = Space_char and flag = 1
子句中翻转标志evenWord := not evenWord
它有效