2015-09-27 100 views
0

這裏時,我擺脫過去的空間我艾菲爾代碼爲我的空間去除分配:如何編碼空間去除

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 

有人可以幫我嗎?

+0

如果你發現一個單詞之後的空格,記住它,但不要馬上打印;等到你找到另一個詞。 – m69

回答

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 
+0

非常感謝,但需要將has_read_space重置爲false,否則代碼仍然相同,但是,謝謝您的想法! –

+0

是的。對不起,我修好了。 –