【问题标题】:caesar cypher decryptor凯撒密码解密器
【发布时间】:2013-02-26 06:02:06
【问题描述】:

好吧,所以我一直“受命”编写一个程序来解密简单的凯撒密码。

我的一切工作正常,只有我的输入停止了大约 29 个字符。它从一个非常大的文件中读取,文本停止在:“独立声明”中间句子。有什么想法可能导致这种情况吗?我假设我在这里滥用和误用了某些东西。随意向我的脑袋扔石头。我相信我可以使用更多的“学习”

编辑:经过进一步调查,我认为我的问题与我的循环和 sizeof(myarray)

有关

编辑 2:我已经编辑了代码,现在它因为 “访问冲突读取位置 0x002A4000”而中断:

 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;
}

【问题讨论】:

  • 是的,您对sizeof 的使用不正确。此处不适用,请使用.length()。您的另一个错误是您在索引字母数组时没有检查isalpha。可能还有更多错误。
  • 是的,我只是将它们移至 .length()。现在处理另一个错误。感谢您的反馈

标签: c++ encryption


【解决方案1】:

您使用的是什么编译器。对我来说,它不会编译,因为

//打开文件 inputFile.open(inputFileName);

需要 //打开文件 inputFile.open(inputFileName.c_str());

http://www.cplusplus.com/reference/fstream/ifstream/open/

但是当它编译时,似乎一切正常。读取整个文件。

【讨论】:

  • 读为是,但它会在输出早期停止。它似乎与我对 Sizeof(myarray) 的使用有关;但是,当我将其传输到 myArray.Length() 时,代码中断:在 a06.exe 中的 0x013F828E 处处理异常:0xC0000005:访问冲突读取位置 0x002A4000。
  • sizeof(string) == sizeof(myarray) 字符串的大小无关紧要。您可能想使用 myarray.length() 。试试这个:
  • myarray.length = 1000 count_array[1000] 导致访问冲突。
  • 哈哈!感谢您指出了这一点。做到了……这解释了很多……我用更合适的数组长度(大小为 26)替换了它;)我不敢相信我错过了。感谢所有其他反馈!
【解决方案2】:

decipher 中,sizeof(myArray) 为您提供string 实例的大小。 string 实例可能包含字符串的实际长度、非常小的字符串的一些空间和指向实际数据的指针,但它不包含数据,即字符串本身。

myArray.length()代替sizeof(myArray)

【讨论】:

  • 是的,我在发布此内容几分钟后进行了更改,因为我意识到自己在做什么,只是代码没有决定更新。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-03
  • 2016-08-30
  • 1970-01-01
  • 1970-01-01
  • 2012-01-20
  • 1970-01-01
相关资源
最近更新 更多