需要帮助理解移位计算和修剪一些脂肪从我的代码

Need assistance understanding a shift calculation and trimming some fat off my code

本文关键字:代码 我的 修剪 助理 帮助 计算      更新时间:2023-10-16

我正在检查这段代码以读取我桌面上的一个文件,该文件解密cesar密码,我试图弄清楚如何在此程序中计算移位。

据我所知,Max e是频率最高的移位字母。由于e是英语中最常见的字母,程序试图将密码中频率最高的字符设置为英语中的"e"。就目前而言,这很好,但在许多短语中,e并不是最常见的字母,那么它就会被淘汰。

那么,我怎样才能告诉程序在纯文本中猜测最频繁的密码字母是e,但如果不是,那么继续尝试将e移到文本中第二个最频繁的字母,以此类推,直到我找到它?

一个朋友帮我做了那部分,但他的英语很差,所以很难向我解释。有人能详细说明一下吗?非常感谢您的协助!让我知道你的想法:

#include <iostream>
#include <string>
#include <cctype> // isalpha, islower, isupper, functions
#include <fstream>
using namespace std;
string caesarShift(string text, int shift);
int main()
{
    int maxEs = 0;       // # of e's in maxString
    int currentEs = 0;    // # of e'sin currentString
    string maxString;       // decrypted caesar shift with most e's
    string currentString;    //decrypted caesar shift
    string cipher;       // Stores cipher text
    char ch;    // Stores currentcharacter for reading
    ifstream fin("/Users/jasonrodriguez/Desktop/encrypted.txt");   //opens "encrypted.txt" file
    while( fin.get(ch) )    // readseach char into the cipher till EOF
    {
        cipher += ch;
    }
    fin.close();    // be safe andclose file
    for(int i=0; i < 26; i++)
    {
        currentEs =0;    // Reset counter
        currentString =caesarShift(cipher, i);    // get shifted text
        for(unsigned int x=0; x <currentString.size(); x++) // check each character of stringarray
        {
            if(currentString[x] == 'e' || currentString[x] == 'E') // check fore's
            {
                currentEs++;    // increment Ecounter
            }
        }
        if(currentEs > maxEs) //if currentEs is greater than maxEs, replace max with current
        {
            maxEs =currentEs;
            maxString= currentString;
        }
    }
    cout << maxString << endl;
    return 0;
}
/**
 string caesarShift(string text, int shift)
 Decrypts Caesar Shift using text and shift
 */
string caesarShift(string text, int shift)
{
    shift = shift % 26;    // Morethan 26 is redundant and unneeded
    char ch = 0; // holds current character
    char chs = 0;    // holds shiftedcharacter
    for(unsigned int i=0; i < text.size();i++)
    {
        ch = text[i];
        if( isalpha(ch) )
        {
            chs = ch -shift; // reverse shifting
            if( (islower(ch) && chs < 'a' )  // If is lowercase andshifted value is lower than 'a'
               ||
               ( isupper(ch) && chs < 'A' ) ) // Ifis uppercase and shifted value is lower than 'A'
            {
                chs += 26;    // Add 26(number ofletters) to get back to the correct place in alphabet
            }
            text[i] =chs;    // Set character to shifted character
        }
    }
    return text;
}

问题:

  1. 据我所知,Max e是频率最高的移位字母。由于e是英语中最常见的字母,程序试图将密码中频率最高的字符设置为英语中的"e"。就目前而言,这很好,但在许多短语中,e并不是最常见的字母,那么它就会被淘汰。那么,我怎样才能告诉程序在纯文本中猜测最频繁的密码字母是e,但如果不是,那么继续尝试将e移到文本中第二个最频繁的字母,以此类推,直到我找到它?

  2. 我认为如果我通过移位量移动字符,字符可能会或可能不会越界。'a' + 3是'd'可以,'x' + 3是'{'不可以。如果字符在'z'以上,去掉26,如果在'a'以下,加26。可以是一个可用的函数。但是,你能给我解释一下这个位移是如何在程序中计算并应用到文件中的吗?

计算转变:

首先,需要将字母"A"到"Z"(假设只有大写字母)映射到整数0到25。c++让你可以通过减法轻松地做到这一点:

n = c - 'A';

现在你可以用模数运算来执行移位:

n = (n + shift) % 26;

最后映射回一个字母:

p = n + 'A';

(注意,您需要对这些示例中使用的变量进行适当的声明。我建议您使用比我使用的单字母变量名称更有意义的名称。

相关文章: