我正在尝试编写一个程序,它接受一个编码消息,并解码Rot13和Rot6密码。Rot13部分运行良好,但Rot6部分仅在特定实例中工作(输入"Yngqkt,tuz yzoxxkj“应转换为"Shaken,not stirred”,但它返回为"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;
}发布于 2019-10-05 02:10:54
只有ROT13是可逆的,因为它移动了字母表大小的一半。
如果你摇动“摇动,不搅拌”,你会得到"Yngqkt,tuz yzoxxkj“,但当你再次ROT6它时,你不会得到”摇动,不搅拌“回来。检查https://rot13.com/。
而且您的ROT6实现也是错误的。您只需使用ROT13实现并将13更改为6。但ROT13的实现依赖于13是字母表大小的一半这一事实。但对于ROT6来说,情况并非如此。如果您想在ROT6实现中使用相同的模式,您必须将字母表不是一分为二,而是在a-t和u-z范围内。如果输入的字母属于第一个范围,则添加6;如果输入的字母属于第二个范围,则减去20。
https://stackoverflow.com/questions/58241043
复制相似问题