Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Net.Mime;
- using System.Text;
- using System.Threading.Tasks;
- namespace OOP_CourseWork
- {
- //Создать класс матрица.Данный класс содержит размер строк
- //и столбцов матрицы.Определить конструктор без параметров,
- //конструктор с одним параметром и конструктор с двумя параметрами,
- //деструктор. Определить методы доступа: возвращать значение
- //элемента (𝑖, 𝑗) и адрес этого элемента.Определить функции
- //сложения и вычитания (матрицы с матрицей), умножение матрицы
- //на матрицу. Определить умножение матрицы на число.
- //Проверить работу этого класса.
- internal class Matrix
- {
- public readonly int Rows;
- public readonly int Columns;
- private int[,] data;
- ~Matrix()
- {
- data = null;
- }
- public Matrix()
- {
- Rows = Columns = 0;
- this.Initialize();
- }
- public Matrix(int dimension)
- {
- Rows = Columns = dimension;
- this.Initialize();
- }
- public Matrix(int Rows, int Columns)
- {
- this.Rows = Rows;
- this.Columns = Columns;
- this.Initialize();
- }
- private void Initialize()
- {
- data = new int[Rows, Columns];
- }
- public int this[int row, int column]
- {
- get
- {
- if (Columns > column && Rows > row) return data[row, column];
- throw new IndexOutOfRangeException("");
- }
- set
- {
- if (Columns > column && Rows > row)
- data[row, column] = value;
- }
- }
- public static Matrix operator +(Matrix matrix1, Matrix matrix2)
- {
- if (matrix1 == null || matrix2 == null)
- throw new ArgumentNullException();
- if (matrix1.Columns != matrix2.Columns || matrix1.Rows != matrix2.Rows)
- throw new ArgumentException("Matrices have different dimensions.");
- Matrix newMatrix = new Matrix(matrix1.Columns, matrix1.Columns);
- for (int i = 0; i < matrix1.Columns; i++)
- {
- for (int j = 0; j < matrix1.Rows; j++)
- {
- newMatrix.data[i, j] = matrix1.data[i, j] + matrix2[i, j];
- }
- }
- return newMatrix;
- }
- public static Matrix operator -(Matrix matrix1, Matrix matrix2)
- {
- if (matrix1 == null || matrix2 == null)
- throw new ArgumentNullException();
- if (matrix1.Columns != matrix2.Columns || matrix1.Rows != matrix2.Rows)
- throw new ArgumentException("Matrices have different dimensions.");
- Matrix newMatrix = new Matrix(matrix1.Columns, matrix1.Columns);
- for (int i = 0; i < matrix1.Columns; i++)
- {
- for (int j = 0; j < matrix1.Rows; j++)
- {
- newMatrix.data[i, j] = matrix1.data[i, j] - matrix2[i, j];
- }
- }
- return newMatrix;
- }
- public static Matrix operator *(Matrix matrix1, int ratio)
- {
- if (matrix1 == null)
- throw new ArgumentNullException();
- Matrix newMatrix = new Matrix(matrix1.Columns, matrix1.Columns);
- for (int i = 0; i < matrix1.Columns; i++)
- {
- for (int j = 0; j < matrix1.Rows; j++)
- {
- newMatrix.data[i, j] = matrix1.data[i, j] * ratio;
- }
- }
- return newMatrix;
- }
- public override string ToString()
- {
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < Columns; i++)
- {
- for (int j = 0; j < Rows; j++)
- {
- sb.Append(data[i, j] + " ");
- }
- sb.AppendLine();
- }
- return sb.ToString();
- }
- }
- internal class Program
- {
- private static void Main(string[] args)
- {
- int Rows = Convert.ToInt32(Console.ReadLine());
- int Columns = Convert.ToInt32(Console.ReadLine());
- Matrix matrix = new Matrix(Rows, Columns);
- for (int i = 0; i < Columns; i++)
- {
- for (int j = 0; j < Rows; j++)
- {
- matrix[i, j] = Convert.ToInt32(Console.ReadLine());
- }
- }
- Console.WriteLine(matrix.ToString());
- Matrix newMatrix = matrix + matrix;
- Console.WriteLine(newMatrix.ToString());
- newMatrix = newMatrix - matrix;
- Console.WriteLine(newMatrix.ToString());
- newMatrix = matrix * 2;
- Console.WriteLine(newMatrix.ToString());
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment