如何应用柱状换位? [英] how to apply columnar transposition?
问题描述
大家好,
我在 列转置密码
中遇到问题
我已经编写了一半的代码,但是我确实在for循环中遇到了严重的问题
这是代码
hey guys,
i have a problem in columnar transposition cipher
I''ve made half of the code but i really have a serious problem in for loop
and here is the code
string text = textBox1.Text;
double cols = double.Parse(textBox2.Text);
double len = text.Length;
string d;
if (cols > len)
{
label3.Text = "cols must be less than text";
}
else
{
double test = len / cols;
if (test % 1 == 0)
{
double row = test;
char[,] enc = new char[(int)cols, (int)row];
char[,] matrix = new char[(int)row, (int)cols];
char[] characters = text.ToCharArray();
for (double i = 0; i < row; i++)
{
for (double j = 0; j < cols; j++)
{
matrix[(int)i, (int)j] = characters[(int)(i + j + i * 2)];
MessageBox.Show(Convert.ToString(matrix[(int)i, (int)j]));
}
}
for (double c = 0; c < cols; c++)
{
for (double r = 0; r < row; r++)
{
MessageBox.Show(Convert.ToString(matrix[(int)c, (int)r]));
}
}
有谁能帮助我:(
can any one help me :(
推荐答案
从问题中给出的代码中可以看到
As seen from the code given in the question
double cols = double.Parse(textBox2.Text);
double len = text.Length;
cols
用于矩阵大小,因此最好将其作为int
.
属性text.Length
的类型为int
,可以按原样使用,而无需分配给局部变量.
cols
is used for the matrix size, so it is preferable to have it as int
.
The property text.Length
is of type int
and it can be used as it is and there is no need to assign to a local variable.
double test = len / cols;
if (test % 1 == 0)
可以像
一样以else if
的形式给出
can be given as an else if
like
else if (len % cols != 0)
label3.Text = "The text length shall be equal to rows * cols ";
换位可以在else
block
中完成
通过将row, cols
用作整型,可以避免强制转换(int)
.
进一步用于转置
1.可以使用text.Length /cols
作为行,cols
作为列来声明矩阵.
2.可以通过对像
这样的文本使用for循环来分配矩阵中的每个元素
The transposition can be done in the else
block
By taking row, cols
as int the cast (int)
can be avoided.
Further for transpose
1. The matrix can be declared with text.Length /cols
as rows and cols
as columns.
2. Each element in the matrix can be assigned by using for loop on text like
matrix[i/cols,i%cols]=text[i];
这篇关于如何应用柱状换位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!