【发布时间】:2013-03-29 21:05:47
【问题描述】:
我的任务是编写一个程序,它读取标准输入,存储文本直到遇到 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
【问题讨论】:
-
关闭
delete[]逻辑的顺序将使事情变得相当……有趣……在关机期间。 -
哈哈谢谢@WhozCraig。我把它换了。
标签: c++ arrays string encryption multidimensional-array