首页 > 解决方案 > 我的解码程序的两个部分没有按预期工作

问题描述

几天前我来这里寻求帮助(有更具体的部分),但我得到的解决方案不太奏效。基本上,我正在编写一个用于 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++

解决方案


了解您在做什么的最佳机会是阅读有关 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;
}

推荐阅读