【发布时间】: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] <= uCaseN看起来可能是倒退了。 -
只写一个
rot以旋转大小为参数的函数会更容易。
标签: c++