Denis1098

Untitled

Nov 14th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.27 KB | None | 0 0
  1.     class MatrixMultiply
  2.     {
  3.         public int[,] Do(int[,] left, int[,] right)
  4.         {
  5.             try
  6.             {
  7.                 int str = left.GetLength(0);
  8.                 int stol = right.GetLength(1);
  9.                 int[,] C = new int[left.GetLength(0), right.GetLength(1)]; //Матрица - результат умножения
  10.                 if (CheckOnCorrect(left, right) == true) //Проверка
  11.                 {
  12.                     for (int i = 0; i < left.GetLength(0); i++)
  13.                     {
  14.                         for (int j = 0; j < right.GetLength(1); j++)
  15.                         {
  16.                             for (int k = 0; k < left.GetLength(0); k++)
  17.                             {
  18.                                 C[i, j] = left[i, k] * right[k, j]; //Собсвтенно, умножение
  19.                             }
  20.                         }
  21.                     }
  22.                 }
  23.                 else
  24.                 {
  25.                     throw new Exception("Невозможно перемножить"); //Если неправильные матрицы
  26.  
  27.                 }
  28.                 return C;
  29.             }
  30.             catch
  31.             {
  32.                 throw new Exception("Ошибка"); //Если что-то пошло не так
  33.             }
  34.         }
  35.         public bool CheckOnCorrect(int[,] left, int[,] right)
  36.         {
  37.             if (left.GetLength(0) == right.GetLength(1) && (left.GetLength(0) != 0 && left.GetLength(1) != 0) && (right.GetLength(0) != 0 && right.GetLength(1) != 0))
  38.             {
  39.                 return true;
  40.             }
  41.             return false;
  42.         }
  43.  
  44.     }
  45.     class MatrixSum
  46.     {
  47.         public int[,] Do(int[,] left, int[,] right)
  48.         {
  49.             try
  50.             {
  51.                 int str = left.GetLength(0);
  52.                 int stol = right.GetLength(1);
  53.                 int[,] D = new int[left.GetLength(0), right.GetLength(1)]; //Матрица - результат сложения
  54.                 if (CheckOnCorrect(left, right) == true) //Проверка
  55.                 {
  56.                     for (int i = 0; i < left.GetLength(0); i++)
  57.                     {
  58.                         for (int j = 0; j < right.GetLength(1); j++)
  59.                         {
  60.                                 D[i, j] = left[i,j] + right[i, j]; //Собсвтенно, сложение
  61.                         }
  62.                     }
  63.                 }
  64.                 else
  65.                 {
  66.                     throw new Exception("Невозможно сложить"); //Если неправильные матрицы
  67.  
  68.                 }
  69.                 return D;
  70.             }
  71.             catch
  72.             {
  73.                 throw new Exception("Ошибка"); //Если что-то пошло не так
  74.             }
  75.         }
  76.         public bool CheckOnCorrect(int[,] left, int[,] right)
  77.         {
  78.             if (left.GetLength(0) == right.GetLength(1) && (left.GetLength(0) != 0 && left.GetLength(1) != 0) && (right.GetLength(0) != 0 && right.GetLength(1) != 0) && (right.GetLength(0) == right.GetLength(1)) && (left.GetLength(0) == left.GetLength(1)))
  79.             {
  80.                 return true;
  81.             }
  82.             return false;
  83.         }
  84.  
  85.     }
Advertisement
Add Comment
Please, Sign In to add comment