我的解码程序的两个部分没有按预期工作

  • 本文关键字:工作 两个 程序 解码 c++
  • 更新时间 :
  • 英文 :


几天前我来这里寻求帮助(更具体的部分(,但我得到的解决方案不太有效。基本上,我正在编写一个用于3个目的的程序:解码Rot 13密码,解码Rot 6密码,并通过等式"x=2n-1"输入用户输入,其中n是用户输入。

Rot 13 工作正常,但 Rot 6 输出乱码,方程输出一个字母(输入"8"会给你 o 而不是 15(

我知道这可以用更少的功能来完成,我可能不需要列表,但这是为了作业,我需要它们

我知道我不擅长这个,但任何帮助都会很棒

#include <iostream>
#include <string>
#include <list>
#include <array>
using namespace std;
string coffeeCode(string input) {   //Coffee code= 2n-1 where n=a number in a string
double index{};
input[index] = 2*input[index]-1;

return input;
};
string rot6(string input) {
int lower[] = { 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',' ' };
int upper[] = { 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z' };
int inputSize = input.size();  // rot 6 rotates letters so that a{0}->g[6]
int index{};      //         m[12]->s[18]
//         n->t
while (index != inputSize) {  //         z->f
if (input[index] >= lower[0] && input[index] <= lower[19])
input[index] = input[index] + 6;
else if (input[index] >= lower[20] && input[index] <= lower[25])
input[index] = input[index] - 20;
else if (input[index] >= upper[0] && input[index] <= upper[19])
input[index] = input[index] + 6;
else if (input[index] <= upper[20] && input[index] <= upper[25])
input[index] = input[index] - 20;
index++;
}
return input;
}
string rot13(string input) {  //Decodes into rot 13
int lower[] = { 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',' ' };
int upper[] = { 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z' };
int inputSize = input.size();
int index{};
while (index != inputSize) {
if (input[index] >= lower[0] && input[index] <= lower[12])
input[index] = input[index] + 13;
else if (input[index] >= lower[13] && input[index] <= lower[25])
input[index] = input[index] - 13;
else if (input[index] >= upper[0] && input[index] <= upper[12])
input[index] = input[index] + 13;
else if (input[index] <= upper[13] && input[index] <= upper[25])
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);
ansCoffee = coffeeCode(plaintext);
cout << "One of these is your decoded message" << endl << "In Rot 13:  " << ans13 << endl << "In Rot 6:  " << ans6 << endl
<< "In Coffee Code:  " << ansCoffee << endl;
return 0;
}

了解您正在做什么的最佳机会是阅读 c++ 如何处理字符以及 ASCII 是什么。

[http://www.asciitable.com/]

只是一个快速总结:

ASCII 表为英文字母表中的每个字符分配一个数字(或代码(,该数字是C++实际存储在内存中的数字。 因此,当您执行char c = 'a'时,这相当于执行char c = 97

ASCII表也非常有条理,因此所有大写字母都按字母顺序排列,从65(即A(到90(即Z(。非大写字母也是如此,从 97 到 122,数字字母从 48 到 57。 这可用于确定变量是哪种字符:

if ('a' <= input[index] && input[index] <= 'z') {
// It's lower case
}
if('A' <= input[index] && input[index] <= 'Z') {
// It's upper case
}

请注意,当您将单字符放在单引号中时,编译器会将其替换为其 ASCII 代码,因此您实际上不需要记住该表。考虑如何构造 if 来确定字符是否为数字。

以下是我将如何实现 rot6。它可能不是最好的,但我认为没关系。

string rot6(string input) {
for (int index = 0; index < input.size(); ++ index) {
if ('a' <= input[index] && input[index] <= 'z') { // It's lower case
input[index] = 'a' + ((input[index] - 'a') + 6) % 26;
}
else if ('A' <= input[index] && input[index] <= 'Z') { // It's upper case
input[index] = 'A' + ((input[index] - 'A') + 6) % 26;
}
else if ('0' <= input[index] && input[index] <= '9') { // It's a digit
input[index] = '0' + ((input[index] - '0') + 6) % 10;
}
else { // It's an error
return "Error, bad input!";
}
}
return input;
}

最新更新