首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我找不出我的代码出了什么问题

我找不出我的代码出了什么问题
EN

Stack Overflow用户
提问于 2019-10-05 01:44:39
回答 1查看 122关注 0票数 1

我正在尝试编写一个程序,它接受一个编码消息,并解码Rot13和Rot6密码。Rot13部分运行良好,但Rot6部分仅在特定实例中工作(输入"Yngqkt,tuz yzoxxkj“应转换为"Shaken,not stirred”,但它返回为"Yhmkqn not stirrqp")

代码语言:javascript
复制
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;
}
EN

回答 1

Stack Overflow用户

发布于 2019-10-05 02:10:54

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

如果你摇动“摇动,不搅拌”,你会得到"Yngqkt,tuz yzoxxkj“,但当你再次ROT6它时,你不会得到”摇动,不搅拌“回来。检查https://rot13.com/

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58241043

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档