需要帮助理解移位计算和修剪一些脂肪从我的代码
Need assistance understanding a shift calculation and trimming some fat off my code
我正在检查这段代码以读取我桌面上的一个文件,该文件解密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;
}
问题:
据我所知,Max e是频率最高的移位字母。由于e是英语中最常见的字母,程序试图将密码中频率最高的字符设置为英语中的"e"。就目前而言,这很好,但在许多短语中,e并不是最常见的字母,那么它就会被淘汰。那么,我怎样才能告诉程序在纯文本中猜测最频繁的密码字母是e,但如果不是,那么继续尝试将e移到文本中第二个最频繁的字母,以此类推,直到我找到它?
我认为如果我通过移位量移动字符,字符可能会或可能不会越界。'a' + 3是'd'可以,'x' + 3是'{'不可以。如果字符在'z'以上,去掉26,如果在'a'以下,加26。可以是一个可用的函数。但是,你能给我解释一下这个位移是如何在程序中计算并应用到文件中的吗?
计算转变:
首先,需要将字母"A"到"Z"(假设只有大写字母)映射到整数0到25。c++让你可以通过减法轻松地做到这一点:
n = c - 'A';
现在你可以用模数运算来执行移位:
n = (n + shift) % 26;
最后映射回一个字母:
p = n + 'A';
(注意,您需要对这些示例中使用的变量进行适当的声明。我建议您使用比我使用的单字母变量名称更有意义的名称。
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 我的字符计数代码计算错误.为什么
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 为什么我的代码在输出中增加了93天
- 我的简单if-else语句是如何无法访问的代码
- 0-1背包代码中的错误.我的代码中有什么错误
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 尝试链接我的着色器时,我收到错误代码"error c5145 must write to gl_position"
- 在我的代码中,获得最大的Pair Wise产品C++和输出并不总是正确的
- 代码在我的计算机上运行良好,但是在将其提交给coursera时遇到未知的信号11问题
- 为什么我需要C++中不同的排序格式来对这个USACO代码上的数组和优先级队列进行排序
- 为什么我的C++代码中出现'Segmentation Fault: 11'行?
- 是否值得降低我的代码的可读性,以便在出现内存不足错误时提供异常安全性?
- 为什么我的C++程序的程序集输出充满了 .ascii,没有汇编代码?
- 无法在我的堆排序代码中找到错误.无法正确执行.C++
- 如何重写全局方法名称以在调用原始方法之前将我的代码推到前面
- 为什么我的 BaseClass:Method 代码编译(带有单冒号)?
- 我的代码运行良好,但在游戏循环中中断
- 8谜题代码.我的队列没有填充,我找不到错误的位置