Организовать класс матрица - C#
Формулировка задачи:
1. Организовать класс квадратная матрица размерности произвольного размера. Методы: вывода матрицы в общепринятом виде, нахождения транспонированной матрицы и нахождения определителя матрицы, доступа по индексам к элементам матрицы.
С выводом и нахождением транспонированной матрицы я справился а вот определитель и доступ по индексам не удается, как реализовать ?
Вот то что я сделал
namespace Lab1
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
public class Matrix
{
private int width;
private int heith;
public Matrix()
{
width = 3;
heith = 3;
}
public int[,] mas = new int[100, 100];
public Matrix(int width, int heith)
{
this.width = width;
this.heith = heith;
}
public void Vvod() //Метод ввода матрицы
{
Random rand = new Random();
for (int i = 0; i < width; i++)
for (int j = 0; j < heith; j++)
{
mas[i, j] = rand.Next(1, 1000);
}
}
public void Vivod() //Метод вывода матрицы
{
for (int i = 0; i < width; i++)
{
for (int j = 0; j < heith; j++)
{
//mas[i, j] = rand.Next(1, 1000);
Console.Write(mas[i, j] + "\t");
}
Console.WriteLine("\n");
}
}
public void Transp() //Метод транспонирования матрицы
{
for (int j = 0; j < heith; j++)
{
for (int i = 0; i < width; i++)
{
Console.Write(mas[i, j] + "\t");
}
Console.WriteLine("\n");
}
}
public void Opr() //Метод нахождения определителя матрицы
{
}
public int DLINA
{ //установка свойства
set { if (width != value) width = value; }
get { return width; }
}
public int VISOTA
{ //установка свойства
set { if (heith != value) heith = value; }
get { return heith; }
}
/*public static Matrix operator --(Matrix a)
{
}*/
}
class Program
{
static void Main()
{
Matrix a = new Matrix();
Console.WriteLine("Начальные значения ");
a.Vvod();
a.Vivod();
Console.WriteLine("-------------------------");
Console.Write("Введите размерность:");
int n = int.Parse(Console.ReadLine());
int m = n;
Matrix b = new Matrix();
b.DLINA = n;
b.VISOTA = m;
b.Vvod();
b.Vivod();
Console.WriteLine("-------------------------");
Console.WriteLine("Транспонированная матрица ");
b.Transp();
Console.ReadLine();
}
}
}
}Решение задачи: «Организовать класс матрица»
textual
Листинг программы
class Program
{
public class Matrix
{
public float[,] matrix = null;
public int CountColumn { get; private set; }
public int CountRow { get; private set; }
public Matrix(int x = 1, int y = 1)
{
matrix = new float[x, y];
CountColumn = y;
CountRow = x;
}
public float this[int x, int y]
{
get { return matrix[x, y]; }
set { matrix[x, y] = value; }
}
public override string ToString()
{
StringBuilder ret = new StringBuilder();
if (matrix == null) return ret.ToString();
for (int i = 0; i < CountRow; i++)
{
for (int t = 0; t < CountColumn; t++)
{
ret.Append(matrix[i, t]);
ret.Append("\t");
}
ret.Append("\n");
}
return ret.ToString();
}
}
static void Main(string[] args)
{
Matrix m1 = new Matrix(2, 2);
m1.matrix[0,0] = 1;
m1.matrix[1,0] = 1;
m1.matrix[0,1] = 1;
m1.matrix[1,1] = 1;
Console.WriteLine("{0}", m1);
Console.ReadKey(true);
}
}