【问题标题】:I can't figure out what's wrong with my code我无法弄清楚我的代码有什么问题
【发布时间】:2019-10-04 17:44:38
【问题描述】:

我正在尝试制作一个程序,该程序采用编码消息并解码 Rot 13 和 Rot 6 密码。 Rot 13 部分工作得很好,但 Rot 6 部分只在特定情况下工作(输入“Yngqkt, tuz yzoxxkj”应该转换为“Shaken, not Stirring”,而是返回为“Yhmkqn not Stirrqp”)

const int lCaseA = 97; 
const int lCaseM = 109;
const int lCaseN = 110;
const int lCaseZ = 122;
const int uCaseA = 65;
const int uCaseM = 77;
const int uCaseN = 78;
const int uCaseY = 89;
const int uCaseZ = 90;


string rot6(string input) {
 int inputSize = input.size();
 int index{};

 while (index != inputSize) {
  if (input[index] >= lCaseA && input[index] <= lCaseM)    
   input[index] = input[index] + 6;    
  else if (input[index] >= lCaseN && input[index] <= lCaseZ)    
   input[index] = input[index] - 6;    
  else if (input[index] >= uCaseA && input[index] <= uCaseM)    
   input[index] = input[index] + 6;
  else if (input[index] <= uCaseN && input[index] <= uCaseZ)    
   input[index] = input[index] - 6;    
  index++;
 }    
 return input;
}

string rot13(string input) {  //Decodes into rot 13    
 int inputSize = input.size();    
 int index{};    
 while (index != inputSize) {    
  if (input[index] >= lCaseA && input[index] <= lCaseM)    
   input[index] = input[index] + 13;    
  else if (input[index] >= lCaseN && input[index] <= lCaseZ)    
   input[index] = input[index] - 13;    
  else if (input[index] >= uCaseA && input[index] <= uCaseM)    
   input[index] = input[index] + 13;    
  else if (input[index] <= uCaseN && input[index] <= uCaseZ)    
   input[index] = input[index] - 13;           
  index++;
 }        
 return input;
}


int main() {    
 string plaintext;    
 string ans13;    
 string ans6;    
 string ansCoffee;    

 cout << "Whats the message Spy Guy: ";    
 getline(cin, plaintext);    
 ans13 = rot13(plaintext);    
 ans6 = rot6(plaintext);

 cout << "One of these is your decoded message" << endl << "In Rot 13:  " << ans13 << endl << "In Rot 6:  " << ans6 << endl;
 return 0;
}

【问题讨论】:

  • 工具栏上有一个标题为“代码示例”的按钮,用于格式化选定的代码块。它看起来像{}
  • 您最好使用'a' 和其他字符文字,而不是定义自己的常量,例如lCaseA。文字 'a' 在大多数平台(显然是你的平台)上具有 97 的值。
  • input[index] &lt;= uCaseN 看起来可能是倒退了。
  • 只写一个rot 以旋转大小为参数的函数会更容易。

标签: c++


【解决方案1】:

只有 ROT13 是可逆的,因为它移动了字母大小的一半。

如果您 ROT6 “摇晃,未搅拌”,您会得到“Yngqkt,tuz yzoxxkj”,但当您再次 ROT6 时,您将不会再收到“摇晃,未搅拌”。检查https://rot13.com/

而且您对 ROT6 的实现也是错误的。您刚刚使用了 ROT13 实现并将 13 更改为 6。但是 ROT13 的实现依赖于 13 是字母大小的一半这一事实。这不适用于 ROT6。如果您想在 ROT6 实现中使用相同的模式,您必须将字母表分成两半,而不是在 a-tu-z 范围内。如果输入字母在第一个范围内,则添加6,如果输入字母在第二个范围内,则减去20

【讨论】:

    猜你喜欢
    • 2013-11-21
    • 2012-03-15
    • 1970-01-01
    • 2018-01-15
    • 2018-05-16
    • 1970-01-01
    • 2017-08-28
    相关资源
    最近更新 更多