Advertisement
Shokedbrain

matrix csharp

Jun 5th, 2022
1,152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.82 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.RegularExpressions;
  4.  
  5. namespace ConsoleApp2
  6. {
  7.     class Program
  8.     {
  9.         class Matrix
  10.         {
  11.             // Скрытые поля
  12.             private int _n;
  13.             private int[,] _mass;
  14.  
  15.             // Создаем конструкторы матрицы
  16.             public Matrix() { }
  17.             public int N
  18.             {
  19.                 get { return _n; }
  20.                 set { if (value > 0) _n = value; }
  21.             }
  22.  
  23.             // Задаем аксессоры для работы с полями вне класса Matrix
  24.             public Matrix(int n)
  25.             {
  26.                 this._n = n;
  27.                 _mass = new int[this._n, this._n];
  28.             }
  29.  
  30.             public int this[int i, int j]
  31.             {
  32.                 get
  33.                 {
  34.                     return _mass[i, j];
  35.                 }
  36.                 set
  37.                 {
  38.                     _mass[i, j] = value;
  39.                 }
  40.             }
  41.  
  42.             // Ввод матрицы с клавиатуры
  43.             public void Write()
  44.             {
  45.                 for (int i = 0; i < _n; i++)
  46.                 {
  47.                     for (int j = 0; j < _n; j++)
  48.                     {
  49.                         Console.WriteLine("Введите элемент матрицы {0}:{1}", i + 1, j + 1);
  50.                         _mass[i, j] = Convert.ToInt32(Console.ReadLine());
  51.                     }
  52.                 }
  53.             }
  54.  
  55.             // Ввод матрицы рандомно
  56.             public void WriteRand()
  57.             {
  58.                 Random rnd = new Random(777);
  59.                 for (int i = 0; i < _n; i++)
  60.                 {
  61.                     for (int j = 0; j < _n; j++)
  62.                     {
  63.                         _mass[i, j] = rnd.Next(0, 10);
  64.                     }
  65.                 }
  66.             }
  67.  
  68.  
  69.  
  70.             // Вывод матрицы с клавиатуры
  71.             public void Print()
  72.             {
  73.                 for (int i = 0; i < _n; i++)
  74.                 {
  75.                     for (int j = 0; j < _n; j++)
  76.                     {
  77.                         Console.Write(_mass[i, j] + "\t");
  78.                     }
  79.                     Console.WriteLine();
  80.                 }
  81.             }
  82.  
  83.            
  84.             // Сложение элементов матрицы с числом
  85.             public static Matrix addch(Matrix a, int ch)
  86.             {
  87.                 Matrix resMass = new Matrix(a.N);
  88.                 for (int i = 0; i < a.N; i++)
  89.                 {
  90.                     for (int j = 0; j < a.N; j++)
  91.                     {
  92.                         resMass[i, j] = a[i, j] + ch;
  93.                     }
  94.                 }
  95.                 return resMass;
  96.             }
  97.  
  98.  
  99.             // Вычитание из матрицы числа
  100.             public static Matrix subch(Matrix a, int ch)
  101.             {
  102.                 Matrix resMass = new Matrix(a.N);
  103.                 for (int i = 0; i < a.N; i++)
  104.                 {
  105.                     for (int j = 0; j < a.N; j++)
  106.                     {
  107.                         resMass[i, j] = a[i, j] - ch;
  108.                     }
  109.                 }
  110.                 return resMass;
  111.             }
  112.  
  113.  
  114.  
  115.  
  116.  
  117.             // Метод вычитания матрицы Б из матрицы А
  118.             public static Matrix razn(Matrix a, Matrix b)
  119.             {
  120.                 Matrix resMass = new Matrix(a.N);
  121.                 for (int i = 0; i < a.N; i++)
  122.                 {
  123.                     for (int j = 0; j < b.N; j++)
  124.                     {
  125.                         resMass[i, j] = a[i, j] - b[i, j];
  126.                     }
  127.                 }
  128.                 return resMass;
  129.             }
  130.  
  131.             // транспонированная матрица
  132.             public Matrix transpose()
  133.             {
  134.                 Matrix resMass = new Matrix(N);
  135.                 for (int i = 0; i < N; i++)
  136.                 {
  137.                     for (int j = 0; j < N; j++)
  138.                     {
  139.                         resMass[i, j] = _mass[j, i];
  140.                     }
  141.                 }
  142.                 return resMass;
  143.             }
  144.  
  145.  
  146.  
  147.             // Перегрузка оператора вычитания
  148.             public static Matrix operator -(Matrix a, Matrix b)
  149.             {
  150.                 return razn(a, b);
  151.             }
  152.  
  153.  
  154.             public static Matrix operator -(Matrix a, int b)
  155.             {
  156.                 return subch(a, b);
  157.             }
  158.  
  159.             public static Matrix Sum(Matrix a, Matrix b)
  160.             {
  161.                 Matrix resMass = new Matrix(a.N);
  162.                 for (int i = 0; i < a.N; i++)
  163.                 {
  164.                     for (int j = 0; j < b.N; j++)
  165.                     {
  166.                         resMass[i, j] = a[i, j] + b[i, j];
  167.                     }
  168.                 }
  169.                 return resMass;
  170.             }
  171.             // Перегрузка сложения
  172.             public static Matrix operator +(Matrix a, Matrix b)
  173.             {
  174.                 return Sum(a, b);
  175.             }
  176.  
  177.             public static Matrix operator +(Matrix a, int b)
  178.             {
  179.                 return addch(a, b);
  180.             }
  181.  
  182.             public static bool equals(Matrix a, Matrix b)
  183.             {
  184.                 bool equals = true;
  185.                 for (int i = 0; i < a.N; i++)
  186.                 {
  187.                     for (int j = 0; j < b.N; j++)
  188.                     {
  189.                         if (a[i, j] != b[i, j])
  190.                         {
  191.                             equals = false;
  192.                             break;
  193.                         }
  194.                     }
  195.                 }
  196.                 return equals;
  197.             }
  198.  
  199.             public static bool operator ==(Matrix a, Matrix b)
  200.             {
  201.                 return equals(a,b);
  202.             }
  203.  
  204.             public static bool operator !=(Matrix a, Matrix b)
  205.             {
  206.                 return !equals(a, b);
  207.             }
  208.  
  209.             // детерминант 2х2 матрицы
  210.             public double Det2X2()
  211.             {
  212.                 double det;
  213.                 det = _mass[0, 0] * _mass[1, 1] - _mass[0, 1] * _mass[1, 0];
  214.  
  215.                 return det;
  216.             }
  217.             // Минор матрицы
  218.             public Matrix Minor(int a, int b)
  219.             {
  220.                 int i, j, p, q;
  221.                 Matrix MEN = new Matrix(_n - 1);
  222.                 for (j = 0, q = 0; q < MEN.N; j++, q++)
  223.                 for (i = 0, p = 0; p < MEN.N; i++, p++)
  224.                 {
  225.                     if (i == a) i++;
  226.                     if (j == b) j++;
  227.                     MEN._mass[p, q] = _mass[i, j];
  228.                 }
  229.                 return MEN;
  230.             }
  231.  
  232.             // Вычисление определителя
  233.  
  234.             public static double Det(Matrix a)
  235.             {
  236.                 int n;
  237.                 int signo;
  238.                 double det = 0;
  239.                 if (a.N == 1)
  240.                     return a._mass[0, 0];
  241.                 if (a.N == 2)
  242.                 {
  243.                     return a.Det2X2();
  244.                 }
  245.                 else
  246.                 {
  247.                     for (n = 0; n < a.N; n++)
  248.                     {
  249.                         if ((n & 1) == 0)
  250.                         {
  251.                             signo = 1;
  252.                         }
  253.                         else
  254.                         {
  255.                             signo = -1;
  256.                         }
  257.  
  258.                         det += signo * a._mass[0, n] * Det(a.Minor(0, n));
  259.                     }
  260.                 }
  261.  
  262.  
  263.                 return det;
  264.             }
  265.  
  266.  
  267.  
  268.  
  269.             // Деструктор Matrix
  270.             ~Matrix()
  271.             {
  272.                 Console.WriteLine("Очистка");
  273.             }
  274.  
  275.  
  276.  
  277.             static void Main(string[] args)
  278.             {
  279.                 Matrix mass1 = new Matrix(2);
  280.                 Matrix mass2 = new Matrix(2);
  281.                 Console.WriteLine("ввод Матрица А: ");
  282.                 mass1.Write();
  283.                 Console.WriteLine("ввод Матрица B: ");
  284.                 mass2.Write();
  285.                 Console.WriteLine("Матрица А: ");
  286.                 mass1.Print();
  287.                 Console.WriteLine("Матрица B: ");
  288.                 mass2.Print();
  289.                 Console.WriteLine("Равенство двух матриц");
  290.                 Console.WriteLine(mass1 == mass2);
  291.  
  292.                 Console.WriteLine("Определитель М1 = {0}, M2 = {1}", Det(mass1), Det(mass2));
  293.                 Console.WriteLine("транспонированная матрица A");
  294.                 mass1.transpose().Print();
  295.             }
  296.         }
  297.     }
  298. }
  299.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement