Как передать массив List в класс - C#
Формулировка задачи:
Всем привет
Есть задание:
Объявить класс "Matrix" и в нем сделать конструктор, который создает матрицу 3х3. Определить в классе "Matrix"
метод
, который принимает два параметра, а именно строку и столбец и позволяет вернуть элемент массива, находящийся по заданному индексу. Использую класс List<>, в него ложу сам массив матрицы, но теперь не знаю, как передать массив из List<> в метод, который описан выше и сделать перебор строк и столбцов, чтобы найти нужное мне значение. Надеюсь на вашу помощьРешение задачи: «Как передать массив List в класс»
textual
Листинг программы
public class RMatrix
{
private int nRows;
private int nCols;
private double[,] matrix;
public RMatrix(int nRows, int nCols)
{
this.nRows = nRows;
this.nCols = nCols;
this.matrix = new double[nRows, nCols];
for (int i = 0; i < nRows; i++)
{
for (int j = 0; j < nCols; j++)
{
matrix[i, j] = 0.0;
}
}
}
public RMatrix(double[,] matrix)
{
this.nRows = matrix.GetLength(0);
this.nCols = matrix.GetLength(1);
this.matrix = matrix;
}
public RMatrix(RMatrix m)
{
nRows = m.GetnRows;
nCols = m.GetnCols;
matrix = m.matrix;
}
public double this[int m, int n]
{
get
{
if (m < 0 || m > nRows)
{
throw new Exception("m-th row is out of range!");
}
if (n < 0 || n > nCols)
{
throw new Exception("n-th col is out of range!");
}
return matrix[m, n];
}
set { matrix[m, n] = value; }
}
public int GetnRows
{
get { return nRows; }
}
public int GetnCols
{
get { return nCols; }
}
public override string ToString()
{
string strMatrix = "(";
for (int i = 0; i < nRows; i++)
{
string str = "";
for (int j = 0; j < nCols - 1; j++)
{
str += matrix[i, j].ToString() + ", ";
}
str += matrix[i, nCols - 1].ToString();
if (i != nRows - 1 && i == 0)
strMatrix += str + "\n";
else if (i != nRows - 1 && i != 0)
strMatrix += " " + str + "\n";
else
strMatrix += " " + str + ")";
}
return strMatrix;
}
public override bool Equals(object obj)
{
return (obj is RMatrix) && this.Equals((RMatrix)obj);
}
public bool Equals(RMatrix m)
{
return matrix == m.matrix;
}
public override int GetHashCode()
{
return matrix.GetHashCode();
}
public static RMatrix operator +(RMatrix m1, RMatrix m2)
{
if (!RMatrix.CompareDimension(m1, m2))
{
throw new Exception("The dimensions of two matrices must be the same!");
}
RMatrix result = new RMatrix(m1.GetnRows, m1.GetnCols);
for (int i = 0; i < m1.GetnRows; i++)
{
for (int j = 0; j < m1.GetnCols; j++)
{
result[i, j] = m1[i, j] + m2[i, j];
}
}
return result;
}
public static RMatrix operator -(RMatrix m)
{
for (int i = 0; i < m.GetnRows; i++)
{
for (int j = 0; j < m.GetnCols; j++)
{
m[i, j] = -m[i, j];
}
}
return m;
}
public static RMatrix operator -(RMatrix m1, RMatrix m2)
{
if (!RMatrix.CompareDimension(m1, m2))
{
throw new Exception("The dimensions of two matrices must be the same!");
}
RMatrix result = new RMatrix(m1.GetnRows, m1.GetnCols);
for (int i = 0; i < m1.GetnRows; i++)
{
for (int j = 0; j < m1.GetnCols; j++)
{
result[i, j] = m1[i, j] - m2[i, j];
}
}
return result;
}
public static RMatrix operator *(RMatrix m, double d)
{
RMatrix result = new RMatrix(m.GetnRows, m.GetnCols);
for (int i = 0; i < m.GetnRows; i++)
{
for (int j = 0; j < m.GetnCols; j++)
{
result[i, j] = m[i, j] * d;
}
}
return result;
}
public static RMatrix operator *(double d, RMatrix m)
{
RMatrix result = new RMatrix(m.GetnRows, m.GetnCols);
for (int i = 0; i < m.GetnRows; i++)
{
for (int j = 0; j < m.GetnCols; j++)
{
result[i, j] = m[i, j] * d;
}
}
return result;
}
public static RMatrix operator /(RMatrix m, double d)
{
RMatrix result = new RMatrix(m.GetnRows, m.GetnCols);
for (int i = 0; i < m.GetnRows; i++)
{
for (int j = 0; j < m.GetnCols; j++)
{
result[i, j] = m[i, j] / d;
}
}
return result;
}
public static RMatrix operator /(double d, RMatrix m)
{
RMatrix result = new RMatrix(m.GetnRows, m.GetnCols);
for (int i = 0; i < m.GetnRows; i++)
{
for (int j = 0; j < m.GetnCols; j++)
{
result[i, j] = m[i, j] / d;
}
}
return result;
}
public static RMatrix operator *(RMatrix m1, RMatrix m2)
{
if (m1.GetnCols != m2.GetnRows)
{
throw new Exception("The numbers of columns of the" +
" first matrix must be equal to the number of " +
" rows of the second matrix!");
}
double tmp;
RMatrix result = new RMatrix(m1.GetnRows, m2.GetnCols);
for (int i = 0; i < m1.GetnRows; i++)
{
for (int j = 0; j < m2.GetnCols; j++)
{
tmp = result[i, j];
for (int k = 0; k < result.GetnRows; k++)
{
tmp += m1[i, k] * m2[k, j];
}
result[i, j] = tmp;
}
}
return result;
}
public bool IsSquared()
{
if (nRows == nCols)
return true;
else
return false;
}
public static bool CompareDimension(RMatrix m1, RMatrix m2)
{
if (m1.GetnRows == m2.GetnRows && m1.GetnCols == m2.GetnCols)
return true;
else
return false;
}
public static double Determinant(RMatrix mat)
{
double result = 0.0;
if (!mat.IsSquared())
{
throw new Exception("The matrix must be squared!");
}
if (mat.GetnRows == 1)
result = mat[0, 0];
else
{
for (int i = 0; i < mat.GetnRows; i++)
{
result += Math.Pow(-1, i) * mat[0, i] * Determinant(RMatrix.Minor(mat, 0, i));
}
}
return result;
}
public static RMatrix Minor(RMatrix mat, int row, int col)
{
RMatrix mm = new RMatrix(mat.GetnRows - 1, mat.GetnCols - 1);
int ii = 0, jj = 0;
for (int i = 0; i < mat.GetnRows; i++)
{
if (i == row)
continue;
jj = 0;
for (int j = 0; j < mat.GetnCols; j++)
{
if (j == col)
continue;
mm[ii, jj] = mat[i, j];
jj++;
}
ii++;
}
return mm;
}
}
static void Main(string[] args)
{
Console.WriteLine("\n\nЗадание Матрица\n\n");
// Создание матрицы А:
RMatrix A = new RMatrix(new double[3, 3] { {5.0, 1.0, 7.0},
{10.0, -2.0, 1.0},
{0.0, 1.0, 2.0}});
// Создание матрицы B:
RMatrix B = new RMatrix(new double[3, 3] { {2.0, 4.0, 1.0},
{2.0, 1.0, 0.0},
{7.0, 2.0, 1.0}});
// Создание матрицы C:
RMatrix C = new RMatrix(new double[3, 3] { {0.0, 0.0, 0.0},
{0.0, 0.0, 0.0},
{0.0, 0.0, 0.0}});
Console.WriteLine("\nМатрица: А = \n{0}", A);
Console.WriteLine("\nМатрица: B = \n{0}", B);
Console.WriteLine("\nA + B = \n{0}", (A + B));
Console.WriteLine("\nA - B = \n{0}", (A - B));
Console.WriteLine("\nA * B = \n{0}", (A * B));
Console.WriteLine("\nB * A = \n{0}", (B * A));
Console.WriteLine("\nA * 5 = \n{0}", (A * 5));
Console.WriteLine("\nB * 4 = \n{0}", (B * 4));
Console.WriteLine("\nОпределитель A = {0}", RMatrix.Determinant(A));
Console.WriteLine("\nОпределитель B = {0}", RMatrix.Determinant(B));
C=2 * (A - B) * (A * A + B);
Console.WriteLine("\nС=2(A–B)(A^2 + B) = \n{0}",C);
Console.WriteLine("\nОпределитель C = {0}", RMatrix.Determinant(C));
Console.ReadLine();
}