0

我的任务是编写一个程序,它读取标准输入,存储文本直到遇到 EOF,然后使用凯撒分组密码对文本进行加密。

解决方案的步骤:

  1. 所以:将您的消息读入一个大缓冲区或字符串对象。
  2. 是否删除空格和标点符号
  3. 然后计算消息中的字符。
  4. 选择第一个大于消息长度的完美正方形,分配一个该大小的 char 数组。
  5. 从左到右,从上到下,将消息读入该大小的方形数组。
  6. 从上到下,从左到右写下信息,您就已经对其进行了加密。

这就是我到目前为止所拥有的......它编译但不做任何事情。我知道我一定错过了什么。任何帮助将不胜感激。

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <ctype.h>
#include <cstring>
#include <cmath>
#include <string>
using namespace std;

int main()
{
    // read in char from keyboard
    string buff;
    do
    {
        cin >> buff;
    } while ( ! cin.eof()) ;

    // delete spaces and punctuation
    for ( int i = 0 ; i < sizeof ( buff ) ; i++ )
    {
        if  ( !isalnum ( buff[i] )  )
        {
            buff.erase( i,1 );
            --i;
        }
    }

    // get length of edited string
    int static SIZE = buff.length(); //strlen (buff);

    // pick first perfect _square_ greater then the message length (ex:7x7)
    int  squared = static_cast <int> ( sqrt( static_cast <double> ( SIZE )) + .5f );

    // allocate an array of char that size
    char ** board;
    board = new char *[squared];      // array of 'squared' char pointers
    for ( int i = 0 ; i < squared ; i++ )
        board[i] = new char[squared];

    // read messsage into a square array of that size from left to right top to bottom
    for ( int c = 0 ; c < squared  ; c++ )
        for ( int r = 0 ; r < squared ; r++ )
            buff[r] = board[r][c];

    // write the message out top to bottom, left to right and its been encyphered
    for ( int r = 0 ; r < squared  ; r++ )
        for ( int c = 0 ; c < squared ; c++ )
            cout << board[r][c] << endl;

    // delete array
    delete [] board;
    for ( int i = 0 ; i < squared ; ++i )
        delete [] board[i] ;

} // end main
4

2 回答 2

1

我尝试编译您的代码并运行它 - 发现了许多问题。

1)您的“擦除”循环进入无限循环。从数组的开头开始,循环遍历所有元素,在遍历时删除元素,并期望事情在最后是正确的,这通常是非常糟糕的做法。从最后开始并向后工作要安​​全得多:

for(ii = buff.length() - 1; ii>=0; ii--) {
  if  ( !isalnum ( buff[i] )  )
    {
    cout << "erasing " << buff[i] << endl;
    buff.erase( i,1 );
    }
  }
}

你做

delete [] board;

在删除数组中的单个元素之前...不确定语法是什么,但它看起来不对我!

你的板子不够大。您必须使用该ceil功能来确保您真正获得“下一个最大”的数字:

square = ceil(sqrt(buff.length()));

最重要的是:您将(空)缓冲区复制到输入字符串中,而不是相反:

buff[r] = board[r][c];

应该是哪个

ii = 0;
for(( int c = 0 ; c < squared  ; c++ )
  for ( int r = 0 ; r < squared ; r++ )
    board[r][c] = buff[ii++];

可能还有其他人……仍在解决问题。

编辑:这是完整的工作代码。

    // read in char from keyboard
    int i;
    string buff;
    cout << "enter the text to be encoded" << endl;
    getline(cin,buff);
    cout << "done reading in the text!" << endl;
    cout << "buffer size is " << buff.length() << endl;
    cout << "the string is " << endl << buff << endl;
    // delete spaces and punctuation
    for ( i = buff.length()-1 ; i >=0 ; i--)
        {
            if  ( !isalnum ( buff[i] )  )
           {
           buff.erase( i,1 );
            }
        }

// get length of edited string
 int static SIZE = buff.length(); //strlen (buff);
cout << "the string length is " << SIZE << endl;

 // pick first perfect _square_ greater then the message length (ex:7x7)
int  squared = static_cast <int> ( ceil(sqrt( static_cast <double> ( SIZE ))));
cout << "size of board is " << squared << endl;
// allocate an array of char that size
char ** board;
board = new char *[squared];      // array of 'squared' char pointers
for ( i = 0 ; i < squared ; i++ )
    board[i] = new char[squared];

// read messsage into a square array of that size from left to right top to bottom
i = 0;
for ( int c = 0 ; c < squared  ; c++ )
    for ( int r = 0 ; r < squared ; r++ )
       board[r][c] = toupper(buff[i++]);

  // write the message out top to bottom, left to right and its been encyphered
 for ( int r = 0 ; r < squared  ; r++ ){
    for ( int c = 0 ; c < squared ; c++ ){
        cout << board[r][c];}
    cout << endl;
    }

   // delete array
    for ( i = 0 ; i < squared ; ++i )
     delete [] board[i] ;

    delete [] board;
} // end main

注意我改变了输入法是为了得到一个完整的句子,而不仅仅是一个单词;我将所有字符转换为大写(非常罗马 - 这意味着在句子开头、名称等处没有关于字符的提示),仅在完整的输出行(而不是每一行)的末尾添加了一个回车符,以及之前发现的问题。

快乐加密!哦 - 事后才想到。一旦您确认事情看起来不错(通过阅读列中的文本很容易做到),您将希望摆脱输出中的回车。它当然应该显示为没有这些中断的单行文本!

于 2013-04-07T21:49:43.753 回答
0

这很奇怪

string buff;
do
{
    cin >> buff;
} while ( ! cin.eof());

不知道你认为这会实现什么。为什么不只是这个?

string buff;
cin >> buff;

另一个错误

for ( int i = 0 ; i < sizeof ( buff ) ; i++ )

应该

for ( int i = 0 ; i < buff.length() ; i++ )

几行之后你就正确了,所以不知道为什么这里是错误的。

于 2013-04-07T21:30:26.350 回答