Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MatrixMultiply
- {
- public int[,] Do(int[,] left, int[,] right)
- {
- try
- {
- int str = left.GetLength(0);
- int stol = right.GetLength(1);
- int[,] C = new int[left.GetLength(0), right.GetLength(1)]; //Матрица - результат умножения
- if (CheckOnCorrect(left, right) == true) //Проверка
- {
- for (int i = 0; i < left.GetLength(0); i++)
- {
- for (int j = 0; j < right.GetLength(1); j++)
- {
- for (int k = 0; k < left.GetLength(0); k++)
- {
- C[i, j] = left[i, k] * right[k, j]; //Собсвтенно, умножение
- }
- }
- }
- }
- else
- {
- throw new Exception("Невозможно перемножить"); //Если неправильные матрицы
- }
- return C;
- }
- catch
- {
- throw new Exception("Ошибка"); //Если что-то пошло не так
- }
- }
- public bool CheckOnCorrect(int[,] left, int[,] right)
- {
- if (left.GetLength(0) == right.GetLength(1) && (left.GetLength(0) != 0 && left.GetLength(1) != 0) && (right.GetLength(0) != 0 && right.GetLength(1) != 0))
- {
- return true;
- }
- return false;
- }
- }
- class MatrixSum
- {
- public int[,] Do(int[,] left, int[,] right)
- {
- try
- {
- int str = left.GetLength(0);
- int stol = right.GetLength(1);
- int[,] D = new int[left.GetLength(0), right.GetLength(1)]; //Матрица - результат сложения
- if (CheckOnCorrect(left, right) == true) //Проверка
- {
- for (int i = 0; i < left.GetLength(0); i++)
- {
- for (int j = 0; j < right.GetLength(1); j++)
- {
- D[i, j] = left[i,j] + right[i, j]; //Собсвтенно, сложение
- }
- }
- }
- else
- {
- throw new Exception("Невозможно сложить"); //Если неправильные матрицы
- }
- return D;
- }
- catch
- {
- throw new Exception("Ошибка"); //Если что-то пошло не так
- }
- }
- public bool CheckOnCorrect(int[,] left, int[,] right)
- {
- 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)))
- {
- return true;
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment