C++ 凯撒密码解密器
好吧,我已经“受命”写了一个程序来解密简单的凯撒密码 我的一切工作正常,只有我的输入停止约29个字符。它是从一个非常大的文件中读取的,文本停在“独立宣言”的中间句。你知道这是什么原因吗?我想我在这里滥用了什么东西。请随便用石头砸我的头。我相信我需要更多的“学习” 编辑:进一步调查后,我认为我的问题与我的循环和sizeof(myarray)有关 编辑2:我编辑了代码,现在它会因为“访问违规读取位置0x0024000”而中断:C++ 凯撒密码解密器,c++,encryption,C++,Encryption,好吧,我已经“受命”写了一个程序来解密简单的凯撒密码 我的一切工作正常,只有我的输入停止约29个字符。它是从一个非常大的文件中读取的,文本停在“独立宣言”的中间句。你知道这是什么原因吗?我想我在这里滥用了什么东西。请随便用石头砸我的头。我相信我需要更多的“学习” 编辑:进一步调查后,我认为我的问题与我的循环和sizeof(myarray)有关 编辑2:我编辑了代码,现在它会因为“访问违规读取位置0x0024000”而中断: for (int i = 0; i < myArray.leng
for (int i = 0; i < myArray.length(); i++)
{
// pritns each occurance cout << char(i + 'a') << " has " << count_Array[i] <<" occuarnces" <<endl;
if (count_Array[i] > tester)
{
//finds largest
tester = count_Array[i];
max_array_value = i;
}
}
// print
Thanks for any tips :]
code:
#include <iostream>
#include <string>
#include <cctype>
#include <fstream>
using namespace std;
int decipher(string myArray, string outputFileName);
int main ()
{
string outputFileName;
string inputFileName;
ifstream inputFile;
string reply;
char myArray;
string all_text = "";
//getting input and output files
cout << "Please input filename: ";
getline(cin,inputFileName);
cout <<"please enter output filename:";
getline(cin,outputFileName);
//opens file
inputFile.open(inputFileName);
if (!inputFile.is_open())
{
//file failed to open
cout <<"unable to open input file." <<endl << "Press enter to continue...";
getline(cin,reply);
exit(1);
}
//read file into all_text
while(inputFile.peek() != EOF)
{
inputFile.get(myArray);
all_text+=myArray;
}
// prints out file cout <<all_text;
inputFile.close();
//send to decipher
decipher(all_text, outputFileName);
cout << "press enter to continue";
getline(cin,reply);
return 0;
}
int decipher(string myArray, string outputFileName)
{
char default_alp[26] = {'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 count_Array[26] = { 0 };
int tester = count_Array[0];
int max_array_value = 0;
string my_Message;
char temp;
ofstream outputFile;
//gets count of occurances
for (int i = 0; i < myArray.length(); i++)
{
count_Array[tolower(myArray[i]) - 'a']++;
}
for (int i = 0; i < myArray.length(); i++)
{
// pritns each occurance cout << char(i + 'a') << " has " << count_Array[i] <<" occuarnces" <<endl;
if (count_Array[i] > tester)
{
//finds largest
tester = count_Array[i];
max_array_value = i;
}
}
// prints useful information cout << "Largest number of occurances " << default_alp[max_array_value] <<endl << "shift amount is: " << default_alp[max_array_value]-'e' <<endl;
for (int i = 0; i < myArray.length(); i++)
{
//prints out each letter cout << myArray[i];
}
for (int i = 0; i < myArray.length; i++)
{
cout <<myArray.length();
//shifts the text based on value in dec
int shift = default_alp[max_array_value]-'e';
temp = myArray[i];
if (isalpha(temp))
{
if(tolower(myArray[i])-shift >= 97)
{
my_Message += tolower(myArray[i]) - shift;
}
else
{
my_Message += tolower(myArray[i]) + 26-shift;
}
}
else
{
my_Message +=myArray[i];
}
}
outputFile.open(outputFileName);
outputFile << my_Message;
return 0;
}
for(int i=0;i //pritns每次发生都不能你在用什么编译器。对我来说,它不能编译是因为
//打开文件
打开(inputFileName)
需要
//打开文件
open(inputFileName.c_str())
但是当它编译时,一切似乎都正常。整个文件都被读取。在解密中,sizeof(myArray)
为您提供字符串
实例的大小。字符串
实例可能包含字符串的实际长度、非常小的字符串的一些空间和指向实际数据的指针,但它不包含数据,即字符串本身
使用myArray.length()
!而不是sizeof(myArray)
,读“是”,但它会在输出的早期停止。它似乎与我对sizeof(myArray)的使用有关;但是,当我将它传输到myArray.length()时代码在a06.exe中的0x013F828E处出现:handled exception:0xC0000005:访问冲突读取位置0x002A4000。sizeof(string)==sizeof(myarray)不管字符串有多大。您可能希望使用myarray.length()。请尝试以下操作:myarray.length=1000 count\u array[1000]导致访问冲突。哈哈!谢谢你指出这一点。这样做了…这解释了很多…我用更合适的数组长度(大小为26)替换了它;)我不能相信我错过了这一点。感谢所有其他反馈!是的,你使用的sizeof
是不正确的。这里不适用,请使用.length()
。您的另一个错误是,在为字母数组编制索引时,没有检查isalpha
。可能还有更多错误。是的,我刚刚将它们移动到了.length()。正在处理另一个错误。谢谢你的反馈。是的,我在发布这篇文章几分钟后做了更改,因为我意识到我在做什么,代码只是没有决定更新。