如何在数组中存储多个函数调用 [英] How to store multiple function calls in an array

查看:107
本文介绍了如何在数组中存储多个函数调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的函数返回一个带有2个值的int数组让我们说,res [0]和res [1]



当我在一个for中调用数组时循环多次,当我将结果存储在另一个数组中时,在每次迭代中结果都会覆盖新结果。



如果是第一个调用返回[0,1]数组将[0,1]存储在索引[0]和[1],这很好,但是当我再次调用该函数时,新结果存储在相同的索引中[ 0]和[1]我该如何避免?



代码为:



My function returns an int array with 2 values Let's say, res[0] and res[1]

When I'm calling the array in a for loop for multiple times, and when I'm storing the results in another array, in each iteration the results are over-written with the new results.

If the first call returns [0,1] the array will store [0,1] at index [0] and [1], which is fine, but when I'm calling the function again, the new results are stored at the same indexes [0] and [1] How can I avoid that?

The code is:

    class Hill_Cipher
    {
        
        string AtoZ="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        
 
        public string Hill_Cipher_Enc(string input, int[,] key)
        {
            int[] encChars  = new int[input.Length];
            string res = "";
            //Input to uppercase 
            input = input.ToUpper();
            //Remove Spaces
            input = input.Replace(" ", String.Empty);
            StringBuilder sb = new StringBuilder(input);
            //Make input dividable by the key length by adding x if necessary
            while (sb.Length % key.GetLength(0) != 0)
            {
                sb = sb.Append('X');
            }

            input = sb.ToString();

            //Dividing the input into chunks of the key length
            IEnumerable<string> chunks = Enumerable.Range(0, input.Length / key.GetLength(0))
    .Select(x => input.Substring(x * key.GetLength(0), key.GetLength(0)));
            string[] outPut = chunks.ToArray();

            //for every chunk, apply multiplication
            for (int i = 0; i < outPut.Length; i++)
            {
                if (i == 0)
                {
                    MatrixMul(outPut[i], key, encChars);
                }
                else
                MatrixMul(outPut[i], key,encChars);

            }

            for (int i = 0; i < encChars.Length; i++)
            {
                encChars[i] = encChars[i] % 26;
                
            }         

            return res;
        }

        //Matrices Multiplication
        private void MatrixMul(string subInput, int[,] key, int [] result)
        {
            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(subInput[col]);   
                }
            }
        }
}





例如,我的outPut包含以下:TH,IS,AT,当我用数组TH的第一个元素调用函数时,它将T转换为它的等价数并应用一些计算,并且与H相同。假设最终答案是T为20,H为30。问题是每次,encChars都会将值存储在索引0和1:encChars [0] = 20 encChars [1] = 30当我再次调用该函数时,它会将新值存储在0和1 ....那是因为我没有在每次调用时更改encChars的索引值,所以我该怎么做?



For example, my outPut contains the following: "TH","IS","AT" when I'm calling the function with the first element of array "TH", it converts "T" to its equivalent number and apply some calculations and same with "H". Let's say the final answer is 20 for "T" and 30 for "H". The problem is that every time, encChars will store the values at index 0 and 1: encChars[0]=20 encChars[1]=30 When I call the function again it will store the new values at 0 and 1.... That's because I'm not changing the index value for encChars on each call, so how do I do that?

推荐答案

看看这里:经典加密技术 [ ^ ]。在那里你会发现Hll Cipher算法的实现。它可能有助于解决您的问题。



MatrixMul 函数需要结果变量作为整数数组。为什么不将它用作输出?

Have a look here: Classical Encryption Techniques[^]. There you'll find Hll Cipher algorithm implementation. It might help to solve your issue.

MatrixMul function expects result variable as an array of integer. Why don't you use it as an output?
private void MatrixMul(string subInput, int[,] key, out int [] result)



现在,你是能够将重新计算的值添加到目标数组。



请参阅:

将数组作为参数传递(C#编程指南) [ ^ ]

使用ref和out传递数组(C#编程指南) [ ^ ]



知道了吗?


Now, you're able to add retruned values to the destination array.

Please, see:
Passing Arrays as Arguments (C# Programming Guide)[^]
Passing Arrays Using ref and out (C# Programming Guide)[^]

Got it?


这篇关于如何在数组中存储多个函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆