Hamikadze

Untitled

Oct 20th, 2019
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.44 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Net.Mime;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace OOP_CourseWork
  10. {
  11.     //Создать класс матрица.Данный класс содержит размер строк
  12.     //и столбцов матрицы.Определить конструктор без параметров,
  13.     //конструктор с одним параметром и конструктор с двумя параметрами,
  14.     //деструктор. Определить методы доступа: возвращать значение
  15.     //элемента (𝑖, 𝑗) и адрес этого элемента.Определить функции
  16.     //сложения и вычитания (матрицы с матрицей), умножение матрицы
  17.     //на матрицу. Определить умножение матрицы на число.
  18.     //Проверить работу этого класса.
  19.  
  20.     internal class Matrix
  21.     {
  22.         public readonly int Rows;
  23.         public readonly int Columns;
  24.         private int[,] data;
  25.  
  26.         ~Matrix()
  27.         {
  28.             data = null;
  29.         }
  30.  
  31.         public Matrix()
  32.         {
  33.             Rows = Columns = 0;
  34.             this.Initialize();
  35.         }
  36.  
  37.         public Matrix(int dimension)
  38.         {
  39.             Rows = Columns = dimension;
  40.             this.Initialize();
  41.         }
  42.  
  43.         public Matrix(int Rows, int Columns)
  44.         {
  45.             this.Rows = Rows;
  46.             this.Columns = Columns;
  47.             this.Initialize();
  48.         }
  49.  
  50.         private void Initialize()
  51.         {
  52.             data = new int[Rows, Columns];
  53.         }
  54.  
  55.         public int this[int row, int column]
  56.         {
  57.             get
  58.             {
  59.                 if (Columns > column && Rows > row) return data[row, column];
  60.                 throw new IndexOutOfRangeException("");
  61.             }
  62.             set
  63.             {
  64.                 if (Columns > column && Rows > row)
  65.                     data[row, column] = value;
  66.             }
  67.         }
  68.  
  69.         public static Matrix operator +(Matrix matrix1, Matrix matrix2)
  70.         {
  71.             if (matrix1 == null || matrix2 == null)
  72.                 throw new ArgumentNullException();
  73.  
  74.             if (matrix1.Columns != matrix2.Columns || matrix1.Rows != matrix2.Rows)
  75.                 throw new ArgumentException("Matrices have different dimensions.");
  76.  
  77.             Matrix newMatrix = new Matrix(matrix1.Columns, matrix1.Columns);
  78.             for (int i = 0; i < matrix1.Columns; i++)
  79.             {
  80.                 for (int j = 0; j < matrix1.Rows; j++)
  81.                 {
  82.                     newMatrix.data[i, j] = matrix1.data[i, j] + matrix2[i, j];
  83.                 }
  84.             }
  85.             return newMatrix;
  86.         }
  87.  
  88.         public static Matrix operator -(Matrix matrix1, Matrix matrix2)
  89.         {
  90.             if (matrix1 == null || matrix2 == null)
  91.                 throw new ArgumentNullException();
  92.  
  93.             if (matrix1.Columns != matrix2.Columns || matrix1.Rows != matrix2.Rows)
  94.                 throw new ArgumentException("Matrices have different dimensions.");
  95.  
  96.             Matrix newMatrix = new Matrix(matrix1.Columns, matrix1.Columns);
  97.             for (int i = 0; i < matrix1.Columns; i++)
  98.             {
  99.                 for (int j = 0; j < matrix1.Rows; j++)
  100.                 {
  101.                     newMatrix.data[i, j] = matrix1.data[i, j] - matrix2[i, j];
  102.                 }
  103.             }
  104.             return newMatrix;
  105.         }
  106.  
  107.         public static Matrix operator *(Matrix matrix1, int ratio)
  108.         {
  109.             if (matrix1 == null)
  110.                 throw new ArgumentNullException();
  111.  
  112.             Matrix newMatrix = new Matrix(matrix1.Columns, matrix1.Columns);
  113.             for (int i = 0; i < matrix1.Columns; i++)
  114.             {
  115.                 for (int j = 0; j < matrix1.Rows; j++)
  116.                 {
  117.                     newMatrix.data[i, j] = matrix1.data[i, j] * ratio;
  118.                 }
  119.             }
  120.             return newMatrix;
  121.         }
  122.  
  123.         public override string ToString()
  124.         {
  125.             StringBuilder sb = new StringBuilder();
  126.             for (int i = 0; i < Columns; i++)
  127.             {
  128.                 for (int j = 0; j < Rows; j++)
  129.                 {
  130.                     sb.Append(data[i, j] + " ");
  131.                 }
  132.  
  133.                 sb.AppendLine();
  134.             }
  135.  
  136.             return sb.ToString();
  137.         }
  138.     }
  139.  
  140.     internal class Program
  141.     {
  142.         private static void Main(string[] args)
  143.         {
  144.             int Rows = Convert.ToInt32(Console.ReadLine());
  145.             int Columns = Convert.ToInt32(Console.ReadLine());
  146.             Matrix matrix = new Matrix(Rows, Columns);
  147.             for (int i = 0; i < Columns; i++)
  148.             {
  149.                 for (int j = 0; j < Rows; j++)
  150.                 {
  151.                     matrix[i, j] = Convert.ToInt32(Console.ReadLine());
  152.                 }
  153.             }
  154.  
  155.             Console.WriteLine(matrix.ToString());
  156.  
  157.             Matrix newMatrix = matrix + matrix;
  158.  
  159.             Console.WriteLine(newMatrix.ToString());
  160.  
  161.             newMatrix = newMatrix - matrix;
  162.  
  163.             Console.WriteLine(newMatrix.ToString());
  164.  
  165.             newMatrix = matrix * 2;
  166.  
  167.             Console.WriteLine(newMatrix.ToString());
  168.  
  169.             Console.ReadKey();
  170.         }
  171.     }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment