static void Main(string[] args) { while (true) { int[,] matrix; if (AskQuestion("считать с файла? y/n ")) { matrix = ReadArrFromFile(); } else { matrix = ReadArrFromConsole(); } ClassMatrix arr2 = new ClassMatrix(matrix); string result = DataConverter.Array2DToStr(arr2.CreateNewMatrix()); Console.WriteLine("Результат"); Console.WriteLine(result); if (AskQuestion("сохранить в файл? y/n ")) { SaveResultToFile(result); } } }
static void Main() { // You are free to change the sizes of the matrices. That is just example to preview how it works :) ClassMatrix matrix1 = new ClassMatrix(2, 2); for (int rows = 0; rows < matrix1.Rows; rows++) { for (int cols = 0; cols < matrix1.Cols; cols++) { Console.Write("matrix1 index[{0},{1}] = ", rows, cols); matrix1[rows, cols] = int.Parse(Console.ReadLine()); } } Console.WriteLine(); ClassMatrix matrix2 = new ClassMatrix(2, 2); for (int rows = 0; rows < matrix2.Rows; rows++) { for (int cols = 0; cols < matrix2.Cols; cols++) { Console.Write("matrix2 index[{0},{1}] = ", rows, cols); matrix2[rows, cols] = int.Parse(Console.ReadLine()); } } // You can change the names,sizes and actions of the matrices.This is just example to preview how it works ClassMatrix sumOfMatrices = matrix1 + matrix2; ClassMatrix substractMatrices = matrix1 - matrix2; ClassMatrix multipliedMatrices = matrix1 * matrix2; Console.WriteLine("\nThe new summed matrix is:\n"); Console.WriteLine(sumOfMatrices.ToString()); Console.WriteLine("\nThe new substracted matrix is:\n"); Console.WriteLine(substractMatrices.ToString()); Console.WriteLine("\nThe new multiplied matrix is:\n"); Console.WriteLine(multipliedMatrices.ToString()); }
// Create operator ' - ' public static ClassMatrix operator -(ClassMatrix matrixOne, ClassMatrix matrixTwo) { ClassMatrix resultMatrix = new ClassMatrix(matrixOne.Rows, matrixOne.Cols); if (matrixOne.Rows == matrixTwo.Rows && matrixOne.Cols == matrixTwo.Cols) { for (int rows = 0; rows < matrixOne.Rows; rows++) { for (int cols = 0; cols < matrixOne.Cols; cols++) { resultMatrix[rows, cols] = matrixOne[rows, cols] - matrixTwo[rows, cols]; } } } else { Console.WriteLine("\nThe matrices have different sizes"); } return resultMatrix; }
// Create operator ' - ' public static ClassMatrix operator -(ClassMatrix matrixOne, ClassMatrix matrixTwo) { ClassMatrix resultMatrix = new ClassMatrix(matrixOne.Rows, matrixOne.Cols); if (matrixOne.Rows == matrixTwo.Rows && matrixOne.Cols == matrixTwo.Cols) { for (int rows = 0; rows < matrixOne.Rows; rows++) { for (int cols = 0; cols < matrixOne.Cols; cols++) { resultMatrix[rows, cols] = matrixOne[rows, cols] - matrixTwo[rows, cols]; } } } else { Console.WriteLine("\nThe matrices have different sizes"); } return(resultMatrix); }
private void changebutton_Click(object sender, EventArgs e) { try { // Преобразуем содержимое нашего DataGridView в массив int[,] arr = DataGridViewUtils.GridToArray2 <int>(inputdataGridView); // Создаём объект класса Array2DUtils для выполнения // различных операций над двумерными массивами ClassMatrix arrayUtils = new ClassMatrix(arr); // Преобразуем результат выполнения метода IncreaseElementsValue // в содержимое DataGridView DataGridViewUtils.Array2ToGrid(outputdataGridView, arrayUtils.CreateNewMatrix()); } catch (Exception E) { // Если во время выполнения действий с массивом произошла ошибка, // то выводим её текст MessagesUtils.ShowError(E.Message); } }
static void Main() { // You are free to change the sizes of the matrices. That is just example to preview how it works :) ClassMatrix matrix1 = new ClassMatrix(2,2); for (int rows = 0; rows < matrix1.Rows; rows++) { for (int cols = 0; cols < matrix1.Cols; cols++) { Console.Write("matrix1 index[{0},{1}] = ",rows,cols); matrix1[rows, cols] = int.Parse(Console.ReadLine()); } } Console.WriteLine(); ClassMatrix matrix2 = new ClassMatrix(2,2); for (int rows = 0; rows < matrix2.Rows; rows++) { for (int cols = 0; cols < matrix2.Cols; cols++) { Console.Write("matrix2 index[{0},{1}] = ", rows, cols); matrix2[rows, cols] = int.Parse(Console.ReadLine()); } } // You can change the names,sizes and actions of the matrices.This is just example to preview how it works ClassMatrix sumOfMatrices = matrix1 + matrix2; ClassMatrix substractMatrices = matrix1 - matrix2; ClassMatrix multipliedMatrices = matrix1 * matrix2; Console.WriteLine("\nThe new summed matrix is:\n"); Console.WriteLine(sumOfMatrices.ToString()); Console.WriteLine("\nThe new substracted matrix is:\n"); Console.WriteLine(substractMatrices.ToString()); Console.WriteLine("\nThe new multiplied matrix is:\n"); Console.WriteLine(multipliedMatrices.ToString()); }
//Create operator for multiplying public static ClassMatrix operator *(ClassMatrix matrixOne, ClassMatrix matrixTwo) { ClassMatrix resultMatrix = new ClassMatrix(matrixOne.Rows, matrixTwo.Cols); if (matrixOne.Cols != matrixTwo.Rows) { Console.WriteLine("\nImposibble operation:\nThe colums of first matrice are not equal to rows of second matrice"); } else if (matrixOne.Cols == matrixTwo.Rows) { for (int rows = 0; rows < resultMatrix.Rows; rows++) { for (int cols = 0; cols < resultMatrix.Cols; cols++) { for (int i = 0; i < matrixOne.Cols; i++) { resultMatrix[rows, cols] += matrixOne[rows, i] * matrixTwo[i, cols]; } } } } return(resultMatrix); }
//Create operator for multiplying public static ClassMatrix operator *(ClassMatrix matrixOne, ClassMatrix matrixTwo) { ClassMatrix resultMatrix = new ClassMatrix(matrixOne.Rows, matrixTwo.Cols); if (matrixOne.Cols != matrixTwo.Rows) { Console.WriteLine("\nImposibble operation:\nThe colums of first matrice are not equal to rows of second matrice"); } else if (matrixOne.Cols == matrixTwo.Rows) { for (int rows = 0; rows < resultMatrix.Rows; rows++) { for (int cols = 0; cols < resultMatrix.Cols; cols++) { for (int i = 0; i < matrixOne.Cols; i++) { resultMatrix[rows, cols] += matrixOne[rows, i] * matrixTwo[i, cols]; } } } } return resultMatrix; }