【问题标题】:Lua - Cipher Logic Error Involving string.gsub, Ciphered Output is Not InputLua - 涉及 string.gsub 的密码逻辑错误,密码输出不是输入
【发布时间】:2014-07-13 10:07:42
【问题描述】:

我的程序似乎遇到了逻辑错误。我已经多次查看它,甚至编写了另一个类似于这个的程序(似乎也有同样的错误)。我不知道出了什么问题,尽管我认为这可能涉及我对 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()

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

样本运行(仅包含重要数据):

in:2
in:hi test
out:gb@safs
in:y
in:1
in:gb@safs
out:hq vjvv

【问题讨论】:

  • 编辑:发现其中一个逻辑错误 - q 变成 a 然后 j。可以安全地假设这是乱码文本的来源,但在我的其他程序中,我将符号(@#$%^# 等)加密为文本,但仍然失败。任何想法为什么或我需要明天发布它的代码?另外,这个错误有什么解决方法吗?
  • 我的眼睛,我的眼睛。你应该让电脑做重复的工作。在这项任务中,它比以往任何时候都好得多。我的意思是,它可以数到 2^32 而不会眨眼。
  • @owlstead 它被设计得尽可能简单,我想在让事情进一步复杂化之前修复这个错误:D

标签: string encryption lua gsub


【解决方案1】:
local decoded = 'abcdefghijklmnopqrstuvwxyz @'
local encoded = 'qehcjmrgbadyvzxkilfswtpuno@ '

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"

【讨论】:

  • 除了第 2 段和 '.' 外,我什么都懂in text27 = rawtextin:lower():gsub('.',enc),你介意解释一下吗?
  • . 是一个正则表达式,意思是“任何符号”。你在编码之前阅读过gsub 的文档吗?
  • 是的,我有,但是我错过了文档讨论模式的底部,只是寻找“。”尽管我假设 .不仅限于 gmatch。现在我知道了。 == * 。我想我可以自己弄清楚第2段的作用,谢谢!接受+赞成。还要感谢您没有提到我的代码中的极端重复,我说当我了解发生了什么时我会修复。
猜你喜欢
  • 1970-01-01
  • 2020-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-16
  • 2017-11-10
  • 2022-11-19
  • 2014-07-07
相关资源
最近更新 更多