Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.93 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4.  
  5. namespace lab1 {
  6.    
  7.     public class IMatrix
  8.     {
  9.         private int[,] _matrix;
  10.         public int Height { get; private set; }
  11.         public int Width { get; private set; }
  12.         public IMatrix(int h, int w)
  13.         {
  14.             this.Height = h;
  15.             this.Width = w;
  16.             _matrix = new int[100, 100];
  17.         }
  18.  
  19.         public int this[int i, int j] {
  20.             get { return _matrix[i, j]; }
  21.             set {
  22.                 if (i >= Height) {
  23.                     throw new ArgumentOutOfRangeException(nameof(i), i, "Height index out of range");
  24.                 }
  25.                 if (j >= Width) {
  26.                     throw new ArgumentOutOfRangeException(nameof(j), j, "Width index out of range");
  27.                 }
  28.                 _matrix[i, j] = value;
  29.             }
  30.         }
  31.  
  32.         public void Plus(int x)
  33.         {
  34.             for (int i = 0; i < Height; i++)
  35.             {
  36.                 for (int j = 0; j < Width; j++)
  37.                     _matrix[i, j] += x;
  38.             }
  39.         }
  40.        
  41.         public void Out(int h1, int h2, int w1, int w2)
  42.         {
  43.             if (h1 < 0) {
  44.                 throw new ArgumentOutOfRangeException(nameof(h1), h1, "Height index out of range");
  45.             }
  46.             if (w1 < 0) {
  47.                 throw new ArgumentOutOfRangeException(nameof(w1), w1, "Width index out of range");
  48.             }
  49.             if (h2 >= Height) {
  50.                 throw new ArgumentOutOfRangeException(nameof(h2), h2, "Height index out of range");
  51.             }
  52.             if (w2 >= Width) {
  53.                 throw new ArgumentOutOfRangeException(nameof(w2), w2, "Width index out of range");
  54.             }
  55.             for (int i = h1; i <= h2 ; i++)
  56.             {
  57.                 for (int j = w1; j <= w2; j++)
  58.                 {
  59.                     Console.Write("{0}\t", _matrix[i, j]);
  60.                 }
  61.                 Console.WriteLine();
  62.             }
  63.         }
  64.  
  65.         public void Out()
  66.         {
  67.             for (int i = 0; i < Height ; i++)
  68.             {
  69.                 for (int j = 0; j < Width; j++)
  70.                 {
  71.                     Console.Write("{0}\t", _matrix[i, j]);
  72.                 }
  73.                 Console.WriteLine();
  74.             }
  75.         }
  76.  
  77.         public void Multiple(int x)
  78.         {
  79.             for (int i = 0; i < Height ; i++)
  80.             {
  81.                 for (int j = 0; j < Width; j++)
  82.                     _matrix[i, j] *= x;
  83.             }
  84.         }
  85.  
  86.         public void Resize(int h, int w)
  87.         {
  88.             if (h < 100 && w < 100)
  89.             {
  90.                 this.Height = h;
  91.                 this.Width = w;
  92.             }
  93.         }
  94.  
  95.     }
  96.  
  97.     public class Program
  98.     {
  99.         static void Main()
  100.         {
  101.             Console.WriteLine("Введите количество строк матрицы: ");
  102.             int h = Int32.Parse(Console.ReadLine());
  103.             Console.WriteLine("Введите количество столбцов матрицы: ");
  104.             int w = Int32.Parse(Console.ReadLine());
  105.  
  106.             IMatrix matr = new IMatrix(h, w);
  107.  
  108.             while (true)
  109.             {
  110.                 Console.WriteLine("=====- Menu -=====\n" +
  111.                                   "1) Изменить размер матрицы\n" +
  112.                                   "2) Вывести всю матрицу на экран\n" +
  113.                                   "3) Вывести подматрицу на экран\n" +
  114.                                   "4) Доступ к элементу матрицы\n" +
  115.                                   "5) Сложение элементов матрицы\n" +
  116.                                   "6) Умножение элементов матрицы\n" +
  117.                                   "7) Выход");
  118.  
  119.                 int num = Int16.Parse(Console.ReadLine());
  120.                 switch (num)
  121.                 {
  122.                     case 1:
  123.                         Console.WriteLine("Введите новое количество строк матрицы: ");
  124.                         h = Int32.Parse(Console.ReadLine());
  125.                         Console.WriteLine("Введите новое количество столбцов матрицы: ");
  126.                         w = Int32.Parse(Console.ReadLine());
  127.                         IMatrix matr2 = matr;
  128.                         matr = new IMatrix(h,w);
  129.                         break;
  130.                     case 2:
  131.                         matr.Out();
  132.                         break;
  133.                     case 3:
  134.                         Console.WriteLine("Введите размеры подматрицы в виде: высота 1, высота 2, ширина 1, ширина 2");
  135.                         int h1 = Int32.Parse(Console.ReadLine());
  136.                         int h2 = Int32.Parse(Console.ReadLine());
  137.                         int w1 = Int32.Parse(Console.ReadLine());
  138.                         int w2 = Int32.Parse(Console.ReadLine());
  139.                         matr.Out(h1,h2,w1,w2);
  140.                         break;
  141.                     case 4:
  142.                         Console.WriteLine("Введите строку и столбец: ");
  143.                         int hei = Int32.Parse(Console.ReadLine());
  144.                         int wei = Int32.Parse(Console.ReadLine());
  145.                         Console.WriteLine("[{0},{1}] = {2} \n Изменить? \n 1) Да \n 2) Нет", hei, wei, matr[hei, wei]);
  146.                         int nom = Int32.Parse(Console.ReadLine());
  147.                         if (nom == 1)
  148.                         {
  149.                             Console.WriteLine("Введите новое значение");
  150.                             int news = Int32.Parse(Console.ReadLine());
  151.                             matr[hei, wei] = news;
  152.                             Console.WriteLine("[{0},{1}] = {2}", hei, wei, news);
  153.                         }
  154.                         break;
  155.                     case 5:
  156.                         Console.Write("Введите число, которое нужно добавить: ");
  157.                         int pl = Int32.Parse(Console.ReadLine());
  158.                         matr.Plus(pl);
  159.                         break;
  160.                     case 6:
  161.                         Console.Write("Введите число, на которое нужно умножить: ");
  162.                         int multipler = Int32.Parse(Console.ReadLine());
  163.                         matr.Multiple(multipler);
  164.                         break;
  165.                     case 7:
  166.                         Environment.Exit(0);
  167.                         break;
  168.                     default:
  169.                         Console.WriteLine("!!! Данной категории MENU не существует !!!");
  170.                         break;
  171.                 }
  172.             }
  173.         }
  174.     }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement