2012-10-22 94 views
2

将文本从输入文件转换为莫尔斯码,然后将结果放入输出txt文件。文件本身正在创建,但没有输出。文件没有得到输出

MAXLINELENGTH = 40 
codes = ['.-', '-...', '-.-.', '-..', '.', '..-.', 
     '--.', '....', '..', '.---', '-.-', '.-..', 
     '--', '-.', '---', '.--.', '--.-', '.-.', 
     '...', '-', '..-', '...-', '.--', '-..-', 
     '-.--', '--..','.----', '..---', '...--', 
     '....-', '.....', '-....', '--...', '---..', 
     '----.', '-----'] 
chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" 
fin = File.open("input.txt", "r") 
fout = File.open("output.txt", "w") 
line_length = 0 
while character = fin.getc 
    if index = chars.index(character.upcase) 
    morse = codes[index] 
    elsif character == " " 
    fout.print " " 
    line_length = line_length + 4 
    end 
    if line_length >= MAXLINELENGTH 
    fout.print "\n" 
    line_length = 0 
    end 
end 
fin.close 
fout.close 

回答

5

你从来没有真正打印变量morse,你只要给它分配的if语句来的第一线。

+0

:D没有注意到哈哈非常感谢 – RubyNovice

+0

认为我已经很抱歉stackoverflow新手:P – RubyNovice