A
A
akass2015-12-08 18:04:43
.NET
akass, 2015-12-08 18:04:43

Is loop refactoring possible?

Is it possible to somehow refactor so as not to fence a bunch of similar cycles?
I thought it could be moved to a separate method, but it still turns out a lot of cycles, 2 for two directions and others for different parameters.
Maybe you can somehow make a cycle template?

private static void NonEqual()
        {
            
            if (_ncr > _nci)
            {
                for (int i = _nci; i < _stolbcov; i++)
                {


                    if (_matrix[_nsi, i] % _stolbcov == _ncr)
                    {
                        _way = _tempway;
                        _input = _matrix[_nsi, i];
                        Calculation();
                        EqualColumns();
                        break;
                    }
                    else
                    {
                        _tempway.Add(_matrix[_nsi, i]);
                    }


                }
            }
            else
            {
                for (int i = _nci; i >= 0; i--)
                {


                    if (_matrix[_nsi, i] % _stolbcov == _ncr)
                    {
                        _way = _tempway;
                        _input = _matrix[_nsi, i];
                        Calculation();
                        EqualColumns();
                        break;
                    }
                    else
                    {
                        _tempway.Add(_matrix[_nsi, i]);
                    }


                }
            }

            

        }

        private static void EqualColumns()
        {
            // Way.Add(Input);
            if (_receiver > _input)
                for (int j = _nsi; j < _strok; j++)
                {
                    if (_matrix[j, _nci] == _receiver)
                    {
                        //Way.Add(matrix[j, NCI]);
                        break;
                    }
                    else
                    {
                        _way.Add(_matrix[j, _nci]);
                    }
                }
            else
            {
                for (int j = _nsi; j >= 0; j--)
                {
                    if (_matrix[j, _nci] == _receiver)
                    {
                        //Way.Add(matrix[j, NCI]);
                        break;
                    }
                    else
                    {
                        _way.Add(_matrix[j, _nci]);
                    }
                }
            }
        }

        private static void EqualRows()
        {
            //  Way.Add(Input);
            if (_receiver > _input)
            {
                for (int j = _nci; j < _stolbcov; j++)
                {

                    if (_matrix[_nsi, j] == _receiver)
                    {
                        //Way.Add(matrix[NSI, j]);
                        break;
                    }
                    else
                    {
                        _way.Add(_matrix[_nsi, j]);
                    }
                }
            }
            else
            {
                for (int j = _nci; j >=0; j--)
                {
                    
                    if (_matrix[_nsi, j] == _receiver)
                    {
                        //Way.Add(matrix[NSI, j]);
                        break;
                    }
                    else
                    {
                        _way.Add(_matrix[_nsi, j]);
                    }
                }
            }

        }

        private static void FillingMatrix(int n, int m)
       {
           int stroka = n;
           int stolbec = m;
            _matrix = new int[stroka, stolbec];

           int cnt = 0;
           Console.WriteLine("исходная матрица:");

           for (int i = 0; i < stroka; i++)
           {
               for (int j = 0; j < stolbec; j++)
               {
                   _matrix[i, j] = cnt;
                   cnt++;
                   Console.Write(" "+_matrix[i, j] + "\t");

               }

               Console.WriteLine(Environment.NewLine);
           }

           
       }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mrrl, 2015-12-08
@Mrl

Move duplicate loop bodies into separate functions. The number of cycles will remain the same, but they will become shorter. Then you can see what else can be improved.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question