2014-07-13 26 views
1

我的程序似乎遇到了逻辑错误。我已经多次查看它,甚至编写了与此类似的另一个程序(似乎也有相同的错误)。我无法弄清楚什么是错的,但我认为它可能涉及我string.gsub的使用...Lua - 涉及string.gsub的加密逻辑错误,加密输出不是输入

repeat 
local file = io.open("out.txt", "w") 
print("would you like to translate Cipher to English or English to Cipher?") 
print("enter 1 for translation to English. enter 2 for translation to Cipher") 
tchoice=io.read() 
if tchoice=="2" then 
print(" enter any text to translate it: ") 
rawtextin=io.read() 
text=string.lower(rawtextin) 
text1=string.gsub(text,"a","q") 
text2=string.gsub(text1,"b","e") 
text3=string.gsub(text2,"c","h") 
text4=string.gsub(text3,"d","c") 
text5=string.gsub(text4,"e","j") 
text6=string.gsub(text5,"f","m") 
text7=string.gsub(text6,"g","r") 
text8=string.gsub(text7,"h","g") 
text9=string.gsub(text8,"i","b") 
text10=string.gsub(text9,"j","a") 
text11=string.gsub(text10,"k","d") 
text12=string.gsub(text11,"l","y") 
text13=string.gsub(text12,"m","v") 
text14=string.gsub(text13,"n","z") 
text15=string.gsub(text14,"o","x") 
text16=string.gsub(text15,"p","k") 
text17=string.gsub(text16,"q","i") 
text18=string.gsub(text17,"r","l") 
text19=string.gsub(text18,"s","f") 
text20=string.gsub(text19,"t","s") 
text21=string.gsub(text20,"u","w") 
text22=string.gsub(text21,"v","t") 
text23=string.gsub(text22,"w","p") 
text24=string.gsub(text23,"x","u") 
text25=string.gsub(text24,"y","n") 
text26=string.gsub(text25,"z","o") 
text27=string.gsub(text26," ","@") 
print(text27) 
elseif tchoice=="1" then 
print("enter text!") 
rawtextin=io.read() 
text=string.lower(rawtextin) 
text1=string.gsub(text,"q","a") 
text2=string.gsub(text1,"e","b") 
text3=string.gsub(text2,"h","c") 
text4=string.gsub(text3,"c","d") 
text5=string.gsub(text4,"j","e") 
text6=string.gsub(text5,"m","f") 
text7=string.gsub(text6,"r","g") 
text8=string.gsub(text7,"g","h") 
text9=string.gsub(text8,"b","i") 
text10=string.gsub(text9,"a","j") 
text11=string.gsub(text10,"d","k") 
text12=string.gsub(text11,"y","l") 
text13=string.gsub(text12,"v","m") 
text14=string.gsub(text13,"z","n") 
text15=string.gsub(text14,"x","o") 
text16=string.gsub(text15,"k","p") 
text17=string.gsub(text16,"i","q") 
text18=string.gsub(text17,"l","r") 
text19=string.gsub(text18,"f","s") 
text20=string.gsub(text19,"s","t") 
text21=string.gsub(text20,"w","u") 
text22=string.gsub(text21,"t","v") 
text23=string.gsub(text22,"p","w") 
text24=string.gsub(text23,"u","x") 
text25=string.gsub(text24,"n","y") 
text26=string.gsub(text25,"o","z") 
text27=string.gsub(text26,"@"," ") 
print(text27) 
end 
print("writing to out.txt...") 
file:write(text27) 
file:close() 
print("done!") 
print("again? type y for yes or anything else for no.") 
again=io.read() 
until again~="y" 
x=io.read() 

在代码中没有错误 - 我在想什么?我意识到这不是最有效的方法,但我需要弄清楚在使用循环和表编写更高效的程序之前出了什么问题。

样品运行(只有显著数据附带):

+0

编辑:发现逻辑错误之一 - q被变成了和则j。假设这是乱码文本的来源是安全的,但在我的另一个程序中,我从符号(@#$%^#等)中对文本进行了加密,并且仍然失败。任何想法为什么或我需要明天发布它的代码?此外,这个错误的任何解决方法? – DivideByZero

+0

我的眼睛,我的眼睛。你应该让电脑做重复的工作。在这个任务中,这比你永远都要好得多。我的意思是,它可以不闪烁地计数到2^32。 –

+0

@owlstead它被设计得尽可能简单,我想在进一步复杂化之前修复bug:D – DivideByZero

回答

3
local decoded = 'abcdefghijklmnopqrstuvwxyz @' 
local encoded = '[email protected] ' 

local enc, dec = {}, {} 
for i = 1, #decoded do 
    local e, d = encoded:sub(i,i), decoded:sub(i,i) 
    enc[d] = e 
    dec[e] = d 
end 

repeat 
    local file = io.open("out.txt", "w") 
    local text27, rawtextin 
    print("would you like to translate Cipher to English or English to Cipher?") 
    print("enter 1 for translation to English. enter 2 for translation to Cipher") 
    local tchoice = io.read() 
    if tchoice == "2" then 
     print(" enter any text to translate it: ") 
     rawtextin = io.read() 
     text27 = rawtextin:lower():gsub('.',enc) 
     print(text27) 
    elseif tchoice == "1" then 
     print("enter text!") 
     rawtextin = io.read() 
     text27 = rawtextin:lower():gsub('.',dec) 
     print(text27) 
    end 
    print("writing to out.txt...") 
    file:write(text27) 
    file:close() 
    print("done!") 
    print("again? type y for yes or anything else for no.") 
    local again = io.read() 
until again ~= "y" 
+0

我了解除第二段和'。'以外的所有内容。在text27 = rawtextin:lower():gsub('。',enc),你会介意解释吗? – DivideByZero

+0

'.'是一个正则表达式,意思是“任何符号”。在编码之前,您是否阅读过有关'gsub'的文档? –

+0

+1仅用于创建循环。 –