2014-07-13 28 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僅用於創建循環。 –