在C#中调用矩阵乘法函数时出错

时间:2014-10-09 03:38:45

标签: c# arrays matrix

我创建了这个函数来做一个简单的矩阵乘法

 int[] MatrixMul(string[] input, int[,] key)
        {
            int[] result = new int[input.Length];
            for (int row = 0; row < key.GetLength(0); row++)
            {
                for (int col = 0; col < key.GetLength(0); col++)
                {
                   result[row]+= key[row, col] * AtoZ.IndexOf(input[0][col]);

                }

            }
            return result;
        }

然而,当我试图打电话时,它给了我一些错误:

调用该函数:

总= MatrixMul(输出[I],密钥);

错误1'Hill_Cipher_CSharp.Hill_Cipher.MatrixMul(string [],int [])'的最佳重载方法匹配有一些无效的参数c:\ users \ wael \ documents \ visual studio 2013 \ Projects \ Hill_Cipher_CSharp \ Hill_Cipher.cs 34 23 Hill_Cipher_CSharp

错误2参数1:无法从'string'转换为'string []'c:\ users \ wael \ documents \ visual studio 2013 \ Projects \ Hill_Cipher_CSharp \ Hill_Cipher.cs 34 33 Hill_Cipher_CSharp

问题在于“outPut”,这里是outPut:

IEnumerable<string> output = Enumerable.Range(0, input.Length / key.GetLength(0))
    .Select(x => input.Substring(x * key.GetLength(0), key.GetLength(0)));
            string[] outPut = output.ToArray();

1 个答案:

答案 0 :(得分:0)

功能应该是:

 int[] MatrixMul(string input, int[,] key)
        {
            int[] result = new int[input.Length];
            for (int row = 0; row < key.GetLength(0); row++)
            {
                for (int col = 0; col < key.GetLength(0); col++)
                {
                   result[row]+= key[row, col] * AtoZ.IndexOf(input[col]);

                }

            }
            return result;
        }

调用函数:

for (int i = 0; i < outPut.Length; i++)
            {
                encChars = MatrixMul(outPut[i], key);
            }