Как передать массив List в класс - C#

Узнай цену своей работы

Формулировка задачи:

Всем привет Есть задание: Объявить класс "Matrix" и в нем сделать конструктор, который создает матрицу 3х3. Определить в классе "Matrix"

метод

, который принимает два параметра, а именно строку и столбец и позволяет вернуть элемент массива, находящийся по заданному индексу. Использую класс List<>, в него ложу сам массив матрицы, но теперь не знаю, как передать массив из List<> в метод, который описан выше и сделать перебор строк и столбцов, чтобы найти нужное мне значение. Надеюсь на вашу помощь

Решение задачи: «Как передать массив List в класс»

textual
Листинг программы
  1.         public class RMatrix
  2.         {
  3.             private int nRows;
  4.             private int nCols;
  5.             private double[,] matrix;
  6.  
  7.             public RMatrix(int nRows, int nCols)
  8.             {
  9.                 this.nRows = nRows;
  10.                 this.nCols = nCols;
  11.                 this.matrix = new double[nRows, nCols];
  12.                 for (int i = 0; i < nRows; i++)
  13.                 {
  14.                     for (int j = 0; j < nCols; j++)
  15.                     {
  16.                         matrix[i, j] = 0.0;
  17.                     }
  18.                 }
  19.             }
  20.  
  21.             public RMatrix(double[,] matrix)
  22.             {
  23.                 this.nRows = matrix.GetLength(0);
  24.                 this.nCols = matrix.GetLength(1);
  25.                 this.matrix = matrix;
  26.             }
  27.  
  28.             public RMatrix(RMatrix m)
  29.             {
  30.                 nRows = m.GetnRows;
  31.                 nCols = m.GetnCols;
  32.                 matrix = m.matrix;
  33.             }
  34.  
  35.             public double this[int m, int n]
  36.             {
  37.                 get
  38.                 {
  39.                     if (m < 0 || m > nRows)
  40.                     {
  41.                         throw new Exception("m-th row is out of range!");
  42.                     }
  43.                     if (n < 0 || n > nCols)
  44.                     {
  45.                         throw new Exception("n-th col is out of range!");
  46.                     }
  47.                     return matrix[m, n];
  48.                 }
  49.                 set { matrix[m, n] = value; }
  50.             }
  51.  
  52.             public int GetnRows
  53.             {
  54.                 get { return nRows; }
  55.             }
  56.  
  57.             public int GetnCols
  58.             {
  59.                 get { return nCols; }
  60.             }
  61.  
  62.             public override string ToString()
  63.             {
  64.                 string strMatrix = "(";
  65.                 for (int i = 0; i < nRows; i++)
  66.                 {
  67.                     string str = "";
  68.                     for (int j = 0; j < nCols - 1; j++)
  69.                     {
  70.                         str += matrix[i, j].ToString() + ", ";
  71.                     }
  72.                     str += matrix[i, nCols - 1].ToString();
  73.                     if (i != nRows - 1 && i == 0)
  74.                         strMatrix += str + "\n";
  75.                     else if (i != nRows - 1 && i != 0)
  76.                         strMatrix += " " + str + "\n";
  77.                     else
  78.                         strMatrix += " " + str + ")";
  79.                 }
  80.                 return strMatrix;
  81.             }
  82.  
  83.             public override bool Equals(object obj)
  84.             {
  85.                 return (obj is RMatrix) && this.Equals((RMatrix)obj);
  86.             }
  87.  
  88.             public bool Equals(RMatrix m)
  89.             {
  90.                 return matrix == m.matrix;
  91.             }
  92.  
  93.             public override int GetHashCode()
  94.             {
  95.                 return matrix.GetHashCode();
  96.             }
  97.  
  98.             public static RMatrix operator +(RMatrix m1, RMatrix m2)
  99.             {
  100.                 if (!RMatrix.CompareDimension(m1, m2))
  101.                 {
  102.                     throw new Exception("The dimensions of two matrices must be the same!");
  103.                 }
  104.                 RMatrix result = new RMatrix(m1.GetnRows, m1.GetnCols);
  105.                 for (int i = 0; i < m1.GetnRows; i++)
  106.                 {
  107.                     for (int j = 0; j < m1.GetnCols; j++)
  108.                     {
  109.                         result[i, j] = m1[i, j] + m2[i, j];
  110.                     }
  111.                 }
  112.                 return result;
  113.             }
  114.  
  115.             public static RMatrix operator -(RMatrix m)
  116.             {
  117.                 for (int i = 0; i < m.GetnRows; i++)
  118.                 {
  119.                     for (int j = 0; j < m.GetnCols; j++)
  120.                     {
  121.                         m[i, j] = -m[i, j];
  122.                     }
  123.                 }
  124.                 return m;
  125.             }
  126.  
  127.             public static RMatrix operator -(RMatrix m1, RMatrix m2)
  128.             {
  129.                 if (!RMatrix.CompareDimension(m1, m2))
  130.                 {
  131.                     throw new Exception("The dimensions of two matrices must be the same!");
  132.                 }
  133.                 RMatrix result = new RMatrix(m1.GetnRows, m1.GetnCols);
  134.                 for (int i = 0; i < m1.GetnRows; i++)
  135.                 {
  136.                     for (int j = 0; j < m1.GetnCols; j++)
  137.                     {
  138.                         result[i, j] = m1[i, j] - m2[i, j];
  139.                     }
  140.                 }
  141.                 return result;
  142.             }
  143.  
  144.             public static RMatrix operator *(RMatrix m, double d)
  145.             {
  146.                 RMatrix result = new RMatrix(m.GetnRows, m.GetnCols);
  147.                 for (int i = 0; i < m.GetnRows; i++)
  148.                 {
  149.                     for (int j = 0; j < m.GetnCols; j++)
  150.                     {
  151.                         result[i, j] = m[i, j] * d;
  152.                     }
  153.                 }
  154.                 return result;
  155.             }
  156.  
  157.             public static RMatrix operator *(double d, RMatrix m)
  158.             {
  159.                 RMatrix result = new RMatrix(m.GetnRows, m.GetnCols);
  160.                 for (int i = 0; i < m.GetnRows; i++)
  161.                 {
  162.                     for (int j = 0; j < m.GetnCols; j++)
  163.                     {
  164.                         result[i, j] = m[i, j] * d;
  165.                     }
  166.                 }
  167.                 return result;
  168.             }
  169.  
  170.             public static RMatrix operator /(RMatrix m, double d)
  171.             {
  172.                 RMatrix result = new RMatrix(m.GetnRows, m.GetnCols);
  173.                 for (int i = 0; i < m.GetnRows; i++)
  174.                 {
  175.                     for (int j = 0; j < m.GetnCols; j++)
  176.                     {
  177.                         result[i, j] = m[i, j] / d;
  178.                     }
  179.                 }
  180.                 return result;
  181.             }
  182.  
  183.             public static RMatrix operator /(double d, RMatrix m)
  184.             {
  185.                 RMatrix result = new RMatrix(m.GetnRows, m.GetnCols);
  186.                 for (int i = 0; i < m.GetnRows; i++)
  187.                 {
  188.                     for (int j = 0; j < m.GetnCols; j++)
  189.                     {
  190.                         result[i, j] = m[i, j] / d;
  191.                     }
  192.                 }
  193.                 return result;
  194.             }
  195.  
  196.             public static RMatrix operator *(RMatrix m1, RMatrix m2)
  197.             {
  198.                 if (m1.GetnCols != m2.GetnRows)
  199.                 {
  200.                     throw new Exception("The numbers of columns of the" +
  201.                      " first matrix must be equal to the number of " +
  202.                      " rows of the second matrix!");
  203.                 }
  204.                 double tmp;
  205.                 RMatrix result = new RMatrix(m1.GetnRows, m2.GetnCols);
  206.                 for (int i = 0; i < m1.GetnRows; i++)
  207.                 {
  208.                     for (int j = 0; j < m2.GetnCols; j++)
  209.                     {
  210.                         tmp = result[i, j];
  211.                         for (int k = 0; k < result.GetnRows; k++)
  212.                         {
  213.                             tmp += m1[i, k] * m2[k, j];
  214.                         }
  215.                         result[i, j] = tmp;
  216.                     }
  217.                 }
  218.                 return result;
  219.             }
  220.  
  221.             public bool IsSquared()
  222.             {
  223.                 if (nRows == nCols)
  224.                     return true;
  225.                 else
  226.                     return false;
  227.             }
  228.  
  229.             public static bool CompareDimension(RMatrix m1, RMatrix m2)
  230.             {
  231.                 if (m1.GetnRows == m2.GetnRows && m1.GetnCols == m2.GetnCols)
  232.                     return true;
  233.                 else
  234.                     return false;
  235.             }
  236.  
  237.             public static double Determinant(RMatrix mat)
  238.             {
  239.                 double result = 0.0;
  240.                 if (!mat.IsSquared())
  241.                 {
  242.                     throw new Exception("The matrix must be squared!");
  243.                 }
  244.                 if (mat.GetnRows == 1)
  245.                     result = mat[0, 0];
  246.                 else
  247.                 {
  248.                     for (int i = 0; i < mat.GetnRows; i++)
  249.                     {
  250.                         result += Math.Pow(-1, i) * mat[0, i] * Determinant(RMatrix.Minor(mat, 0, i));
  251.                     }
  252.                 }
  253.                 return result;
  254.             }
  255.  
  256.             public static RMatrix Minor(RMatrix mat, int row, int col)
  257.             {
  258.                 RMatrix mm = new RMatrix(mat.GetnRows - 1, mat.GetnCols - 1);
  259.                 int ii = 0, jj = 0;
  260.                 for (int i = 0; i < mat.GetnRows; i++)
  261.                 {
  262.                     if (i == row)
  263.                         continue;
  264.                     jj = 0;
  265.                     for (int j = 0; j < mat.GetnCols; j++)
  266.                     {
  267.                         if (j == col)
  268.                             continue;
  269.                         mm[ii, jj] = mat[i, j];
  270.                         jj++;
  271.                     }
  272.                     ii++;
  273.                 }
  274.                 return mm;
  275.             }
  276.  
  277.  
  278.         }
  279.  
  280.         static void Main(string[] args)
  281.         {
  282.             Console.WriteLine("\n\nЗадание Матрица\n\n");
  283.             // Создание матрицы А:
  284.             RMatrix A = new RMatrix(new double[3, 3] { {5.0, 1.0, 7.0},
  285.                                                        {10.0, -2.0, 1.0},
  286.                                                        {0.0, 1.0, 2.0}});
  287.             // Создание матрицы B:
  288.             RMatrix B = new RMatrix(new double[3, 3] { {2.0, 4.0, 1.0},
  289.                                                        {2.0, 1.0, 0.0},
  290.                                                        {7.0, 2.0, 1.0}});
  291.             // Создание матрицы C:
  292.             RMatrix C = new RMatrix(new double[3, 3] { {0.0, 0.0, 0.0},
  293.                                                        {0.0, 0.0, 0.0},
  294.                                                        {0.0, 0.0, 0.0}});
  295.             Console.WriteLine("\nМатрица: А = \n{0}", A);
  296.             Console.WriteLine("\nМатрица: B = \n{0}", B);
  297.  
  298.             Console.WriteLine("\nA + B = \n{0}", (A + B));
  299.             Console.WriteLine("\nA - B = \n{0}", (A - B));
  300.             Console.WriteLine("\nA * B = \n{0}", (A * B));
  301.             Console.WriteLine("\nB * A = \n{0}", (B * A));
  302.             Console.WriteLine("\nA * 5 = \n{0}", (A * 5));
  303.             Console.WriteLine("\nB * 4 = \n{0}", (B * 4));
  304.             Console.WriteLine("\nОпределитель A = {0}", RMatrix.Determinant(A));
  305.             Console.WriteLine("\nОпределитель B = {0}", RMatrix.Determinant(B));
  306.             C=2 * (A - B) * (A * A + B);
  307.             Console.WriteLine("\nС=2(A–B)(A^2 + B) = \n{0}",C);
  308.             Console.WriteLine("\nОпределитель C = {0}", RMatrix.Determinant(C));
  309.             Console.ReadLine();
  310.         }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

15   голосов , оценка 3.733 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы