cuốn sách gpt4 ai đã làm

c++ - block 密码

In lại Tác giả: Walker 123 更新时间:2023-11-28 07:40:56 26 4
mua khóa gpt4 Nike

我的任务是编写一个程序来读取标准输入,存储文本直到遇到 EOF,然后使用 Caesar block 密码加密文本。

解决方案的步骤:

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

这是我目前所拥有的...它编译但不执行任何操作。我知道我一定错过了什么。任何帮助将不胜感激。

#include 
#include
#include
#include
#include
#include
#include
#include
#include
sử dụng không gian tên std;

int chính()
{
// read in char from keyboard
string buff;
LÀM
{
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 ( sqrt( static_cast ( 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

câu trả lời hay nhất

我尝试编译并运行您的代码 - 发现了一些问题。

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 tô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 ( ceil(sqrt( static_cast ( 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

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

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

关于c++ - block 密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15867988/

26 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress