c++ - 凯撒密码删除数据

标签 c++ caesar-cipher

我想知道是否有人可以帮助我解决我的代码问题。它似乎一切正常,但是当我运行它时,它运行到一半就开始删除我的数据,即使我从来没有调用任何东西来删除它。

例如我的文件来自:

=======================
Rotated by 11 positions
=======================
Lnwypeyw lala sqz xa na lnwypeyw eb pdau sqz pwa w eppa
na pea bn znawec.
              -- F. L. IyAru**
to 
=======================
Rotated by 12 positions
=======================
Moxzqzx mbmb tra yb ob moxzqzx c qebv tra qxb x qqb
ob qb co aobxd.
              -- G. M. JzBsv

这是我的代码,我试过几次它在逻辑上都是有意义的,但不知道它会丢失数据。

#include <iostream>
#include <cctype>
#include <fstream>

using namespace std;

bool isUpper(char character){

    //this will test and see if it's upper or lowercase
    bool upper;

    if(character>= 'A' && character <= 'Z'){
        upper=true;
    }
    else {
        upper= false;
    }

   return upper;
}

bool isLower(char character){

    //this will test and see if it's upper or lowercase
    bool lower;

    if(character >= 'a' && character <= 'z'){
        lower=true;
    }

    else {
        lower= false;
    }

   return lower;
}

//Actual function that will rotate the character
char rotate(char character, int offset) {

    char next_letter;

    //Changes it if it's a lower case
    if (isLower(character)) {

        next_letter = character + offset;

        if (next_letter > 'z'){
            next_letter = (next_letter - 26);
            return next_letter;
        }

        return next_letter;
    }

    else if(isUpper(character)) {

        next_letter = character + offset;

        if (next_letter > 'Z'){
            next_letter = (next_letter - 26);
            return next_letter;
        }

        return next_letter;
    }

    else {
       return character;
    }
}


int main() {

    //variables for program
    char character = 'a';
    int offset = 0;

    while(offset < 26){

        //opens the file  
        ifstream fin;
        fin.open("secretMessage.txt");

        if(!fin.good()) {
            cout << "Please check your file name!!!";
            return 0; 
        }

        //report for reading       
        cout << "=======================" << endl  
        << "Rotated by " << offset << " positions" << endl
        << "=======================" << endl; 

        //Reads until it's at the end of the file
        while ((! fin.eof())){
            character = fin.get();

            cout << rotate(character,offset);

        }

    //makes it run 26 times  
    ++offset;
    fin.close();
    cout << endl << endl;
    }
    //Closes the file output.
    return 0;
}

最佳答案

在这个逻辑中:

    if (next_letter > 'z') {
        next_letter = (next_letter - 26);
        return next_letter;
    }

你没有想到如果 char 被签名会发生什么。测试一下:

assert(rotate('z', 0) == 'z');
assert(rotate('z', 1) == 'a');
assert(rotate('z', 2) == 'b');
assert(rotate('z', 3) == 'c');
assert(rotate('z', 4) == 'd');
assert(rotate('z', 5) == 'e');

到目前为止还不错,但是现在:

assert(rotate('z', 6) == 'f'); // WHOOPS

Ascii 'z'+5 是 127,'z'+6 是 128。但是,z+=6 结果为负,所以检查 > 'z'失败。

建议的简化:

bool isUpper(char ch) { return (ch >= 'A' && ch <= 'Z'); }
bool isLower(char ch) { return (ch >= 'a' && ch <= 'z'); }

// Actual function that will rotate the character
char rotate(char ch, int offset) {
    if (isLower(ch)) return (ch - 'a' + offset) % 26 + 'a';
    if (isUpper(ch)) return (ch - 'A' + offset) % 26 + 'A';
    return ch;
}

由于您包括 <cctype> , 你不妨使用 std::islowerstd::isupper .

Live On Coliru

#include <cctype>
#include <fstream>
#include <iostream>
#include <algorithm>

// Actual function that will rotate the character
char rotate(char ch, int offset) {
    if (std::islower(ch)) return (ch - 'a' + offset) % 26 + 'a';
    if (std::isupper(ch)) return (ch - 'A' + offset) % 26 + 'A';
    return ch;
}

std::string readFile(std::string const& fname) {
    std::ifstream fin;
    fin.exceptions(std::ios::failbit);
    fin.open(fname);
    return { std::istreambuf_iterator<char>(fin), {} };
}

int main() {

    auto const original = readFile("main.cpp");

    // makes it run 26 times
    for (int offset = 0; offset < 26; ++offset) {

        std::cout 
            << "\n"
            << "=======================\n"
            << "Rotated by " << offset << " positions\n"
            << "=======================\n";

        // Reads until it's at the end of the file
        std::transform(
                original.begin(), original.end(),
                std::ostreambuf_iterator<char>(std::cout),
                [offset](char ch) { return rotate(ch, offset); });
    }
}

奖金

实际上,您可以通过原地按 1 旋转 26 次来避免 main 中的复杂性:

Live On Coliru

auto text = readFile("main.cpp");

for (int offset = 0; offset < 26; ++offset) {
    std::cout 
        << "\n"
        << "=======================\n"
        << "Rotated by " << offset << " positions\n"
        << "=======================\n";

    std::cout << text;
    for (auto& ch : text) ch = rotate(ch, 1);
}

关于c++ - 凯撒密码删除数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46534881/

相关文章:

c++ - LPCSTR 函数的返回值变得无用

Python 凯撒密码更改给定输入字符串的大小写

python - 检查无效输入

c++ - SecByteBlock 不接受字符串作为自定义键

c++ - 在 C++ 中,setter 和 getter 以及构造函数之间有什么区别?

c++ - 将 vector 分配给多重集

c++ - 带for循环的递归函数: return value issue

java - 如何在凯撒密码中包含 "_"

c - 查找字符串中字符的结尾

谁能告诉我凯撒算法有什么问题吗?