Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- namespace lab1 {
- public class IMatrix
- {
- private int[,] _matrix;
- public int Height { get; private set; }
- public int Width { get; private set; }
- public IMatrix(int h, int w)
- {
- this.Height = h;
- this.Width = w;
- _matrix = new int[100, 100];
- }
- public int this[int i, int j] {
- get { return _matrix[i, j]; }
- set {
- if (i >= Height) {
- throw new ArgumentOutOfRangeException(nameof(i), i, "Height index out of range");
- }
- if (j >= Width) {
- throw new ArgumentOutOfRangeException(nameof(j), j, "Width index out of range");
- }
- _matrix[i, j] = value;
- }
- }
- public void Plus(int x)
- {
- for (int i = 0; i < Height; i++)
- {
- for (int j = 0; j < Width; j++)
- _matrix[i, j] += x;
- }
- }
- public void Out(int h1, int h2, int w1, int w2)
- {
- if (h1 < 0) {
- throw new ArgumentOutOfRangeException(nameof(h1), h1, "Height index out of range");
- }
- if (w1 < 0) {
- throw new ArgumentOutOfRangeException(nameof(w1), w1, "Width index out of range");
- }
- if (h2 >= Height) {
- throw new ArgumentOutOfRangeException(nameof(h2), h2, "Height index out of range");
- }
- if (w2 >= Width) {
- throw new ArgumentOutOfRangeException(nameof(w2), w2, "Width index out of range");
- }
- for (int i = h1; i <= h2 ; i++)
- {
- for (int j = w1; j <= w2; j++)
- {
- Console.Write("{0}\t", _matrix[i, j]);
- }
- Console.WriteLine();
- }
- }
- public void Out()
- {
- for (int i = 0; i < Height ; i++)
- {
- for (int j = 0; j < Width; j++)
- {
- Console.Write("{0}\t", _matrix[i, j]);
- }
- Console.WriteLine();
- }
- }
- public void Multiple(int x)
- {
- for (int i = 0; i < Height ; i++)
- {
- for (int j = 0; j < Width; j++)
- _matrix[i, j] *= x;
- }
- }
- public void Resize(int h, int w)
- {
- if (h < 100 && w < 100)
- {
- this.Height = h;
- this.Width = w;
- }
- }
- }
- public class Program
- {
- static void Main()
- {
- Console.WriteLine("Введите количество строк матрицы: ");
- int h = Int32.Parse(Console.ReadLine());
- Console.WriteLine("Введите количество столбцов матрицы: ");
- int w = Int32.Parse(Console.ReadLine());
- IMatrix matr = new IMatrix(h, w);
- while (true)
- {
- Console.WriteLine("=====- Menu -=====\n" +
- "1) Изменить размер матрицы\n" +
- "2) Вывести всю матрицу на экран\n" +
- "3) Вывести подматрицу на экран\n" +
- "4) Доступ к элементу матрицы\n" +
- "5) Сложение элементов матрицы\n" +
- "6) Умножение элементов матрицы\n" +
- "7) Выход");
- int num = Int16.Parse(Console.ReadLine());
- switch (num)
- {
- case 1:
- Console.WriteLine("Введите новое количество строк матрицы: ");
- h = Int32.Parse(Console.ReadLine());
- Console.WriteLine("Введите новое количество столбцов матрицы: ");
- w = Int32.Parse(Console.ReadLine());
- IMatrix matr2 = matr;
- matr = new IMatrix(h,w);
- break;
- case 2:
- matr.Out();
- break;
- case 3:
- Console.WriteLine("Введите размеры подматрицы в виде: высота 1, высота 2, ширина 1, ширина 2");
- int h1 = Int32.Parse(Console.ReadLine());
- int h2 = Int32.Parse(Console.ReadLine());
- int w1 = Int32.Parse(Console.ReadLine());
- int w2 = Int32.Parse(Console.ReadLine());
- matr.Out(h1,h2,w1,w2);
- break;
- case 4:
- Console.WriteLine("Введите строку и столбец: ");
- int hei = Int32.Parse(Console.ReadLine());
- int wei = Int32.Parse(Console.ReadLine());
- Console.WriteLine("[{0},{1}] = {2} \n Изменить? \n 1) Да \n 2) Нет", hei, wei, matr[hei, wei]);
- int nom = Int32.Parse(Console.ReadLine());
- if (nom == 1)
- {
- Console.WriteLine("Введите новое значение");
- int news = Int32.Parse(Console.ReadLine());
- matr[hei, wei] = news;
- Console.WriteLine("[{0},{1}] = {2}", hei, wei, news);
- }
- break;
- case 5:
- Console.Write("Введите число, которое нужно добавить: ");
- int pl = Int32.Parse(Console.ReadLine());
- matr.Plus(pl);
- break;
- case 6:
- Console.Write("Введите число, на которое нужно умножить: ");
- int multipler = Int32.Parse(Console.ReadLine());
- matr.Multiple(multipler);
- break;
- case 7:
- Environment.Exit(0);
- break;
- default:
- Console.WriteLine("!!! Данной категории MENU не существует !!!");
- break;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement