我的任务是编写一个程序,它读取标准输入,存储文本直到遇到 EOF,然后使用凯撒分组密码对文本进行加密。
解决方案的步骤:
- 所以:将您的消息读入一个大缓冲区或字符串对象。
- 是否删除空格和标点符号
- 然后计算消息中的字符。
- 选择第一个大于消息长度的完美正方形,分配一个该大小的 char 数组。
- 从左到右,从上到下,将消息读入该大小的方形数组。
- 从上到下,从左到右写下信息,您就已经对其进行了加密。
这就是我到目前为止所拥有的......它编译但不做任何事情。我知道我一定错过了什么。任何帮助将不胜感激。
#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