Facebook
From Sharp Cockroach, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 78
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7.  
  8. namespace huy
  9. {
  10.     class Program
  11.     {
  12.         static Random rnd = new Random();
  13.         static void Main(string[] args)
  14.         {
  15.             SelectingCommand();
  16.         }
  17.  
  18.         public static void RulesAndCommands()
  19.         {
  20.             Console.ForegroundColor = ConsoleColor.Yellow;
  21.             Console.WriteLine("╔════════════════════════════════════════════════════════════════════╗");
  22.             Console.WriteLine("║Команда для вычисления следа матрицы - trace                        ║");
  23.             Console.WriteLine("║Команда для транспонирования матрицы - trance                       ║");
  24.             Console.WriteLine("║Команда для получения суммы матриц двух - +                         ║");
  25.             Console.WriteLine("║Команда для произведения матрицы на введенное чсисло - *a           ║");
  26.             Console.WriteLine("║Команда для получения разности матриц - -                           ║");
  27.             Console.WriteLine("║Команда для поиска детерминанта - det                               ║");
  28.             Console.WriteLine("║Команда для выхода из программы - exit                              ║");
  29.             Console.WriteLine("║Команда для чистки консоли - clear                                  ║");
  30.             Console.WriteLine("╚════════════════════════════════════════════════════════════════════╝");
  31.             Console.ResetColor();
  32.         }
  33.         public static void SelectingCommand()
  34.         {
  35.             string comand;
  36.  
  37.             do
  38.             {
  39.                 Console.WriteLine("help для просмотра списка команд");
  40.                 Console.WriteLine("Выберите команду");
  41.                 comand = Console.ReadLine();
  42.                 switch (comand)
  43.                 {
  44.                     case "trace":
  45.                         SelectionForTrace();
  46.                         break;
  47.                     case "help":
  48.                         RulesAndCommands();
  49.                         break;
  50.                     case "exit":
  51.                         comand = "exit";
  52.                         Console.WriteLine("Сайонара...");
  53.                         break;
  54.                     case "clear":
  55.                         Console.Clear();
  56.                         break;
  57.                     case "+":
  58.                         SelectionForSumOfMatrix();
  59.                         break;
  60.                     case "-":
  61.                         SelectionForSubOfMatrix();
  62.                         break;
  63.                     case "trans":
  64.                         SelectionForTrasponing();
  65.                         break;
  66.                     case "*a":
  67.                         SelectionForComOfMatrixAndNum();
  68.                         break;
  69.                     case "*":
  70.                         SelectionForComOfMatrixAndMatrix();
  71.                         break;
  72.                     case "det":
  73.                         SelectionForDeterminant();
  74.                         break;
  75.                     default:
  76.                         Console.WriteLine("Ошибка");
  77.                         break;
  78.                 }
  79.             } while (comand != "exit");
  80.  
  81.         }
  82.         public static int[,] CreatingMatrix(int size, int column)
  83.         {
  84.             try
  85.             {
  86.                 Console.WriteLine("Вводите значения которые хотите ввести в виде строки");
  87.                 Console.WriteLine($"Пример: 1 2 3{Environment.NewLine}" +
  88.                                           $"\t1 2 3{Environment.NewLine}" +
  89.                                           $"\t1 2 3  зависит от выбранного вами размера");
  90.                 Console.WriteLine();
  91.                 int[,] squareMatrix = new int[size, column];
  92.                 for (int i = 0; i < size; i++)
  93.                 {
  94.                     string rowOfArray = Console.ReadLine();
  95.                     string[] numbersOfRow = rowOfArray.Split(new char[] { ' ' });
  96.                     if (numbersOfRow.Length < column)
  97.                     {
  98.                         Console.WriteLine("Заданный размер не совпадает с полученным");
  99.                         Console.WriteLine();
  100.                         return null;
  101.                     }
  102.                     int[] rowOfArrayInInt = new int[column];
  103.                     int numberOfArray;
  104.                     for (int u = 0, j = 0; u < numbersOfRow.Length; u++)
  105.                     {
  106.                         if (!int.TryParse(numbersOfRow[u], out numberOfArray))
  107.                         {
  108.                             Console.WriteLine("Ошибка при вводе");
  109.                             Console.WriteLine();
  110.                             return null;
  111.                         }
  112.                         rowOfArrayInInt[j] = numberOfArray;
  113.                         j++;
  114.                     }
  115.                     for (int v = 0; v < column; v++)
  116.                     {
  117.                         squareMatrix[i, v] = rowOfArrayInInt[v];
  118.                     }
  119.                 }
  120.                 return squareMatrix;
  121.  
  122.             }
  123.             catch (Exception)
  124.             {
  125.                 Console.WriteLine("Ошибка при вводе, некоректные данные");
  126.                 Console.WriteLine();
  127.                 return null;
  128.             }
  129.         }
  130.  
  131.         public static int[,] AnotherMatrix(int size, int column)
  132.         {
  133.             try
  134.             {
  135.                 int[,] squareMatrix = new int[size, column];
  136.                 for (int i = 0; i < size; i++)
  137.                 {
  138.                     string rowOfArray = Console.ReadLine();
  139.                     string[] numbersOfRow = rowOfArray.Split(new char[] { ' ' });
  140.                     if (numbersOfRow.Length < column)
  141.                     {
  142.                         Console.WriteLine("Заданный размер не совпадает с полученным");
  143.                         Console.WriteLine();
  144.                         return null;
  145.                     }
  146.                     int[] rowOfArrayInInt = new int[column];
  147.                     int numberOfArray;
  148.                     for (int u = 0, j = 0; u < numbersOfRow.Length; u++)
  149.                     {
  150.                         if (!int.TryParse(numbersOfRow[u], out numberOfArray))
  151.                         {
  152.                             Console.WriteLine("Ошибка при вводе");
  153.                             Console.WriteLine();
  154.                             return null;
  155.                         }
  156.                         rowOfArrayInInt[j] = numberOfArray;
  157.                         j++;
  158.                     }
  159.                     for (int v = 0; v < column; v++)
  160.                     {
  161.                         squareMatrix[i, v] = rowOfArrayInInt[v];
  162.                     }
  163.                 }
  164.                 return squareMatrix;
  165.  
  166.             }
  167.             catch (Exception)
  168.             {
  169.                 Console.WriteLine("Ошибка при вводе, некоректные данные");
  170.                 Console.WriteLine();
  171.                 return null;
  172.             }
  173.         }
  174.  
  175.         public static int[,] CreatingMatrixByRandom(int row, int column)
  176.         {
  177.             Console.WriteLine();
  178.             int[,] matrix = new int[row, column];
  179.             Console.WriteLine("Введите диапозон рандома");
  180.             Console.WriteLine("Минимальное значение");
  181.             int min, notMax;
  182.             while (!int.TryParse(Console.ReadLine(), out min))
  183.             {
  184.                 Console.WriteLine("Ошибкка при вводе");
  185.                 Console.WriteLine();
  186.             }
  187.             Console.WriteLine("Максимальное значение");
  188.             while (!int.TryParse(Console.ReadLine(), out notMax))
  189.             {
  190.                 Console.WriteLine("Ошибкка при вводе");
  191.                 Console.WriteLine();
  192.             }
  193.             int max = notMax + 1;
  194.             for (int i = 0; i < row; i++)
  195.             {
  196.                 for (int u = 0; u < column; u++)
  197.                 {
  198.                     matrix[i, u] = rnd.Next(min, max);
  199.                 }
  200.             }
  201.             Console.WriteLine("Заспавнившаяся матрица");
  202.             for (int i = 0; i < row; i++)
  203.             {
  204.                 for (int u = 0; u < column; u++)
  205.                 {
  206.                     Console.Write($"{matrix[i, u]} ");
  207.                 }
  208.                 Console.WriteLine();
  209.             }
  210.             Console.WriteLine();
  211.             return matrix;
  212.         }
  213.  
  214.         public static int[,] GettingMatrixFromFile()
  215.         {
  216.             try
  217.             {
  218.                 Console.WriteLine();
  219.                 Console.WriteLine("Значения должны быть записаны как в примере");
  220.                 Console.WriteLine($"Пример: 1 2 3{Environment.NewLine}" +
  221.                                           $"\t1 2 3{Environment.NewLine}" +
  222.                                           $"\t1 2 3  зависит от выбранного вами размера");
  223.                 Console.WriteLine("Введите полный путь к файлу");
  224.                 string path = Console.ReadLine();
  225.                 if (!File.Exists(path))
  226.                 {
  227.                     Console.WriteLine("Файла не существует");
  228.                     Console.WriteLine();
  229.                     return null;
  230.                 }
  231.                 StreamReader file = new StreamReader(path);
  232.                 string s = file.ReadToEnd();
  233.                 file.Close();
  234.                 string[] row = s.Split('\n');
  235.                 string[] column = row[0].Split(' ');
  236.                 int[,] squareMatrix = new int[row.Length, column.Length];
  237.                 int t;
  238.                 for (int i = 0; i < row.Length; i++)
  239.                 {
  240.                     column = row[i].Split(' ');
  241.                     for (int j = 0; j < column.Length; j++)
  242.                     {
  243.                         if (!int.TryParse(column[j], out t))
  244.                         {
  245.                             Console.WriteLine("Ошибка, неопознаный знак");
  246.                             Console.WriteLine();
  247.                             return null;
  248.                         }
  249.                         squareMatrix[i, j] = t;
  250.                     }
  251.                 }
  252.                 Console.WriteLine("Матрица в файле:");
  253.                 for (int i = 0; i < row.Length; i++)
  254.                 {
  255.                     for (int j  = 0; j < column.Length; j++)
  256.                     {
  257.                         Console.Write($"{squareMatrix[i, j]} ");
  258.                     }
  259.                     Console.WriteLine();
  260.                 }
  261.                 Console.WriteLine();
  262.                 return squareMatrix;
  263.  
  264.             }
  265.             catch (Exception)
  266.             {
  267.                 Console.WriteLine("Ошибка при вводе, некоректные данные");
  268.                 Console.WriteLine();
  269.                 return null;
  270.             }
  271.         }
  272.         public static void SelectionForTrace()
  273.         {
  274.             Console.WriteLine("Каким способом вы хотите получить матрицу");
  275.             Console.WriteLine("1 - Ввести вручную, 2 - Рандомные матрицы, 3 - загругить из файла");
  276.             string choise = Console.ReadLine();
  277.             Console.WriteLine();
  278.             switch (choise)
  279.             {
  280.                 case "1":
  281.                     SearchingMatrixTrace();
  282.                     break;
  283.                 case "2":
  284.                     MatrixTraceRandom();
  285.                     break;
  286.                 case "3":
  287.                     MatrixTraceFromFile();
  288.                     break;
  289.                 default:
  290.                     Console.WriteLine("Ошибка при выборе");
  291.                     Console.WriteLine();
  292.                     break;
  293.             }
  294.         }
  295.         public static void SearchingMatrixTrace()
  296.         {
  297.             Console.WriteLine("След находиться только в квадратных матрицах");
  298.             Console.WriteLine("Введите размерность матрицы (если будет введено например 3, матрица будет размера 3 на 3)");
  299.             Console.Write("Размерность = ");
  300.             int sizeOfMatrix;
  301.             while (!int.TryParse(Console.ReadLine(), out sizeOfMatrix) || sizeOfMatrix <= 0)
  302.             {
  303.                 Console.WriteLine("Ошибка при вводе!");
  304.                 Console.WriteLine();
  305.             }
  306.             int[,] matrix;
  307.             do
  308.             {
  309.                 matrix = CreatingMatrix(sizeOfMatrix, sizeOfMatrix);
  310.             } while (matrix == null);
  311.             int result = 0;
  312.             for (int i = 0; i < sizeOfMatrix; i++)
  313.             {
  314.                 result += matrix[i, i];
  315.             }
  316.             Console.WriteLine($"След матрицы равен = {result}");
  317.         }
  318.         public static void MatrixTraceRandom()
  319.         {
  320.             Console.WriteLine("След находиться только в квадратных матрицах");
  321.             Console.WriteLine("Введите размерность матрицы (если будет введено например 3, матрица будет размера 3 на 3)");
  322.             Console.Write("Размерность = ");
  323.             int sizeOfMatrix;
  324.             while (!int.TryParse(Console.ReadLine(), out sizeOfMatrix) || sizeOfMatrix <= 0)
  325.             {
  326.                 Console.WriteLine("Ошибка при вводе!");
  327.                 Console.WriteLine();
  328.             }
  329.             int[,] matrix = CreatingMatrixByRandom(sizeOfMatrix, sizeOfMatrix);
  330.             int result = 0;
  331.             for (int i = 0; i < sizeOfMatrix; i++)
  332.             {
  333.                 result += matrix[i, i];
  334.             }
  335.             Console.WriteLine($"След матрицы равен = {result}");
  336.         }
  337.  
  338.         public static void MatrixTraceFromFile()
  339.         {
  340.             Console.WriteLine("Замечание: След находиться только в квадратных матрицах");
  341.             int[,] matrix;
  342.             do
  343.             {
  344.                 matrix = GettingMatrixFromFile();
  345.             } while (matrix == null);
  346.             int result = 0;
  347.            
  348.             for (int i = 0; i < matrix.GetLength(0); i++)
  349.             {
  350.                 result += matrix[i, i];
  351.             }
  352.             Console.WriteLine($"След матрицы равен = {result}");
  353.         }
  354.         public static void SelectionForTrasponing()
  355.         {
  356.             Console.WriteLine("Каким способом вы хотите получить матрицу");
  357.             Console.WriteLine("1 - Ввести вручную, 2 - Рандомные матрицы, 3 - загругить из файла");
  358.             string choise = Console.ReadLine();
  359.             Console.WriteLine();
  360.             switch (choise)
  361.             {
  362.                 case "1":
  363.                     TransponingMatrix();
  364.                     break;
  365.                 case "2":
  366.                     TransponingByRandom();
  367.                     break;
  368.                 case "3":
  369.                     TransponingMatrixFromFile();
  370.                     break;
  371.                 default:
  372.                     Console.WriteLine("Ошибка при выборе");
  373.                     Console.WriteLine();
  374.                     break;
  375.             }
  376.         }
  377.         public static void TransponingMatrix()
  378.         {
  379.             Console.WriteLine("Введите количество строк и столбцов в матрице");
  380.             Console.Write("строк = ");
  381.             int row;
  382.             while (!int.TryParse(Console.ReadLine(), out row) || row <= 0)
  383.             {
  384.                 Console.WriteLine("Ошибка");
  385.                 Console.WriteLine();
  386.                 return;
  387.             }
  388.             int column;
  389.             Console.Write("cтолбцов = ");
  390.             while (!int.TryParse(Console.ReadLine(), out column) || column <= 0)
  391.             {
  392.                 Console.WriteLine("Ошибка");
  393.                 Console.WriteLine();
  394.                 return;
  395.             }
  396.             int[,] matrix;
  397.             do
  398.             {
  399.                 matrix = CreatingMatrix(row, column);
  400.             } while (matrix == null);
  401.             Console.WriteLine("Транспонированная матрица:");
  402.             for (int i = 0; i < column; i++)
  403.             {
  404.                 for (int u = 0; u < row; u++)
  405.                 {
  406.                     Console.Write($"{matrix[u, i]} ");
  407.                 }
  408.                 Console.WriteLine();
  409.             }
  410.             Console.WriteLine();
  411.         }
  412.  
  413.         public static void TransponingByRandom()
  414.         {
  415.             Console.WriteLine("Введите количество строк и столбцов в матрице");
  416.             Console.Write("строк = ");
  417.             int row;
  418.             while (!int.TryParse(Console.ReadLine(), out row) || row <= 0)
  419.             {
  420.                 Console.WriteLine("Ошибка");
  421.                 Console.WriteLine();
  422.                 return;
  423.             }
  424.             int column;
  425.             Console.Write("cтолбцов = ");
  426.             while (!int.TryParse(Console.ReadLine(), out column) || column <= 0)
  427.             {
  428.                 Console.WriteLine("Ошибка");
  429.                 Console.WriteLine();
  430.                 return;
  431.             }
  432.             int[,] matrix = CreatingMatrixByRandom(row, column);
  433.             Console.WriteLine("Транспонированная матрица:");
  434.             for (int i = 0; i < column; i++)
  435.             {
  436.                 for (int u = 0; u < row; u++)
  437.                 {
  438.                     Console.Write($"{matrix[u, i]} ");
  439.                 }
  440.                 Console.WriteLine();
  441.             }
  442.             Console.WriteLine();
  443.         }
  444.  
  445.         public static void TransponingMatrixFromFile()
  446.         {
  447.             int[,] matrix;
  448.             do
  449.             {
  450.                 matrix = GettingMatrixFromFile();
  451.             } while (matrix == null);
  452.             Console.WriteLine("Транспонированная матрица:");
  453.             for (int i = 0; i < matrix.GetLength(1); i++)
  454.             {
  455.                 for (int u = 0; u < matrix.GetLength(0); u++)
  456.                 {
  457.                     Console.Write($"{matrix[u, i]} ");
  458.                 }
  459.                 Console.WriteLine();
  460.             }
  461.             Console.WriteLine();
  462.         }
  463.         public static void SelectionForSumOfMatrix()
  464.         {
  465.             Console.WriteLine("Каким способом вы хотите получить матрицу");
  466.             Console.WriteLine("1 - Ввести вручную, 2 - Рандомные матрицы, 3 - загругить из файла");
  467.             string choise = Console.ReadLine();
  468.             Console.WriteLine();
  469.             switch (choise)
  470.             {
  471.                 case "1":
  472.                     SumOfMatrix();
  473.                     break;
  474.                 case "2":
  475.                     SumOfMatrixByRandom();
  476.                     break;
  477.                 case "3":
  478.                     SumOfMatrixFromFile();
  479.                     break;
  480.                 default:
  481.                     Console.WriteLine("Ошибка при выборе");
  482.                     Console.WriteLine();
  483.                     break;
  484.             }
  485.         }
  486.  
  487.         public static void SumOfMatrix()
  488.         {
  489.             Console.WriteLine("Действие - сложение матриц");
  490.             Console.WriteLine("Размерность матриц равна(поэтому)");
  491.             Console.WriteLine("Введите количество строк и столбцов в матрице");
  492.             Console.Write("строк = ");
  493.             int row;
  494.             while (!int.TryParse(Console.ReadLine(), out row) || row <= 0)
  495.             {
  496.                 Console.WriteLine("Ошибка");
  497.                 Console.WriteLine();
  498.                 return;
  499.             }
  500.             int column;
  501.             Console.Write("cтолбцов = ");
  502.             while (!int.TryParse(Console.ReadLine(), out column) || column <= 0)
  503.             {
  504.                 Console.WriteLine("Ошибка");
  505.                 Console.WriteLine();
  506.                 return;
  507.             }
  508.             Console.WriteLine("Введите первую матрицу");
  509.             int[,] matrix;
  510.             do
  511.             {
  512.                 matrix = CreatingMatrix(row, column);
  513.             } while (matrix == null);
  514.             Console.WriteLine("Введите вторую матрицу");
  515.             int[,] matrix1;
  516.             do
  517.             {
  518.                 matrix1 = CreatingMatrix(row, column);
  519.             } while (matrix1 == null);
  520.             SumOfMatrixPart2(matrix, matrix1, row, column);
  521.         }
  522.  
  523.         public static void SumOfMatrixPart2(int[,] matrix1, int[,] matrix2, int row, int column)
  524.         {
  525.             int[,] resultMatrix = new int[row, column];
  526.             for (int i = 0; i < row; i++)
  527.             {
  528.                 for (int uq = 0; uq < column; uq++)
  529.                 {
  530.                     resultMatrix[i, uq] = matrix1[i, uq] + matrix2[i, uq];
  531.                 }
  532.             }
  533.             Console.WriteLine("Сумма матриц");
  534.             for (int i = 0; i < row; i++)
  535.             {
  536.                 for (int uq = 0; uq < column; uq++)
  537.                 {
  538.                     Console.Write($"{resultMatrix[i, uq]} ");
  539.                 }
  540.                 Console.WriteLine();
  541.             }
  542.         }
  543.  
  544.         public static void SumOfMatrixByRandom()
  545.         {
  546.             Console.WriteLine("Действие - сложение матриц");
  547.             Console.WriteLine("Размерность матриц равна");
  548.             Console.WriteLine("Введите количество строк и столбцов в матрице");
  549.             Console.Write("строк = ");
  550.             int row;
  551.             while (!int.TryParse(Console.ReadLine(), out row) || row <= 0)
  552.             {
  553.                 Console.WriteLine("Ошибка");
  554.                 Console.WriteLine();
  555.                 return;
  556.             }
  557.             int column;
  558.             Console.Write("cтолбцов = ");
  559.             while (!int.TryParse(Console.ReadLine(), out column) || column <= 0)
  560.             {
  561.                 Console.WriteLine("Ошибка");
  562.                 Console.WriteLine();
  563.                 return;
  564.             }
  565.             Console.WriteLine("Первая матрица:");
  566.             int[,] matrix1 = CreatingMatrixByRandom(row, column);
  567.             Console.WriteLine("Вторая матрица:");
  568.             int[,] matrix2 = CreatingMatrixByRandom(row, column);
  569.             SumOfMatrixPart2(matrix1, matrix2, row, column);
  570.         }
  571.  
  572.         public static void SumOfMatrixFromFile()
  573.         {
  574.             Console.WriteLine("Действие - сложение матриц");
  575.             Console.WriteLine("Размерность матриц должна быть равна");
  576.             Console.WriteLine("Первая матрица");
  577.             int[,] matrix1;
  578.             do
  579.             {
  580.                 matrix1 = GettingMatrixFromFile();
  581.             } while (matrix1 == null);
  582.             Console.WriteLine("Вторая матрица");
  583.             int[,] matrix2;
  584.             do
  585.             {
  586.                 matrix2 = GettingMatrixFromFile();
  587.             } while (matrix2 == null);
  588.             if (matrix1.GetLength(0) != matrix2.GetLength(0) || matrix1.GetLength(1) != matrix2.GetLength(1)
  589.                 || matrix1.GetLength(0) != matrix1.GetLength(1) || matrix2.GetLength(0) != matrix2.GetLength(1))
  590.             {
  591.                 Console.WriteLine("Ошибка, не соответствует размеры");
  592.                 Console.WriteLine();
  593.                 return;
  594.             }
  595.             SumOfMatrixPart2(matrix1, matrix2, matrix1.GetLength(0), matrix1.GetLength(1));
  596.         }
  597.         public static void SelectionForSubOfMatrix()
  598.         {
  599.             Console.WriteLine("Каким способом вы хотите получить матрицу");
  600.             Console.WriteLine("1 - Ввести вручную, 2 - Рандомные матрицы, 3 - загругить из файла");
  601.             string choise = Console.ReadLine();
  602.             Console.WriteLine();
  603.             switch (choise)
  604.             {
  605.                 case "1":
  606.                     SubstracionOfMatrix();
  607.                     break;
  608.                 case "2":
  609.                     SubOfMatrixByRandom();
  610.                     break;
  611.                 case "3":
  612.                     SubOfMatrixFromFile();
  613.                     break;
  614.                 default:
  615.                     Console.WriteLine("Ошибка при выборе");
  616.                     break;
  617.             }
  618.         }
  619.         public static void SubstracionOfMatrix()
  620.         {
  621.             Console.WriteLine("Действие - разность матриц");
  622.             Console.WriteLine("Размерность матриц равна");
  623.             Console.WriteLine("Введите количество строк и столбцов в матрице");
  624.             Console.Write("строк = ");
  625.             int row;
  626.             while (!int.TryParse(Console.ReadLine(), out row) || row <= 0)
  627.             {
  628.                 Console.WriteLine("Ошибка");
  629.                 Console.WriteLine();
  630.                 return;
  631.             }
  632.             int column;
  633.             Console.Write("cтолбцов = ");
  634.             while (!int.TryParse(Console.ReadLine(), out column) || column <= 0)
  635.             {
  636.                 Console.WriteLine("Ошибка");
  637.                 Console.WriteLine();
  638.                 return;
  639.             }
  640.             Console.WriteLine("Введите первую матрицу");
  641.             int[,] matrix;
  642.             do
  643.             {
  644.                 matrix = CreatingMatrix(row, column);
  645.             } while (matrix == null);
  646.             Console.WriteLine("Введите вторую матрицу");
  647.             int[,] matrix1;
  648.             do
  649.             {
  650.                 matrix1 = CreatingMatrix(row, column);
  651.             } while (matrix1 == null);
  652.             SubstractionOfMatrixPart2(matrix, matrix1, row, column);
  653.         }
  654.  
  655.         public static void SubstractionOfMatrixPart2(int[,] matrix1, int[,] matrix2, int row, int column)
  656.         {
  657.             int[,] resultMatrix = new int[row, column];
  658.             for (int i = 0; i < row; i++)
  659.             {
  660.                 for (int uq = 0; uq < column; uq++)
  661.                 {
  662.                     resultMatrix[i, uq] = matrix1[i, uq] - matrix2[i, uq];
  663.                 }
  664.             }
  665.             Console.WriteLine("Разность матриц равна");
  666.             for (int i = 0; i < row; i++)
  667.             {
  668.                 for (int uq = 0; uq < column; uq++)
  669.                 {
  670.                     Console.Write($"{resultMatrix[i, uq]} ");
  671.                 }
  672.                 Console.WriteLine();
  673.             }
  674.         }
  675.         public static void SubOfMatrixByRandom()
  676.         {
  677.             Console.WriteLine("Действие - разность матриц");
  678.             Console.WriteLine("Размерность матриц равна");
  679.             Console.WriteLine("Введите количество строк и столбцов в матрице");
  680.             Console.Write("строк = ");
  681.             int row;
  682.             while (!int.TryParse(Console.ReadLine(), out row) || row <= 0)
  683.             {
  684.                 Console.WriteLine("Ошибка");
  685.                 Console.WriteLine();
  686.                 return;
  687.             }
  688.             int column;
  689.             Console.Write("cтолбцов = ");
  690.             while (!int.TryParse(Console.ReadLine(), out column) || column <= 0)
  691.             {
  692.                 Console.WriteLine("Ошибка");
  693.                 Console.WriteLine();
  694.                 return;
  695.             }
  696.             Console.WriteLine("Первая матрица:");
  697.             int[,] matrix1 = CreatingMatrixByRandom(row, column);
  698.             Console.WriteLine("Вторая матрица:");
  699.             int[,] matrix2 = CreatingMatrixByRandom(row, column);
  700.             SubstractionOfMatrixPart2(matrix1, matrix2, row, column);
  701.         }
  702.  
  703.         public static void SubOfMatrixFromFile()
  704.         {
  705.             Console.WriteLine("Действие - разность матриц");
  706.             Console.WriteLine("Размерность матриц должна быть равна");
  707.             Console.WriteLine("Первая матрица");
  708.             int[,] matrix1;
  709.             do
  710.             {
  711.                 matrix1 = GettingMatrixFromFile();
  712.             } while (matrix1 == null);
  713.             Console.WriteLine("Вторая матрица");
  714.             int[,] matrix2;
  715.             do
  716.             {
  717.                 matrix2 = GettingMatrixFromFile();
  718.             } while (matrix2 == null);
  719.             if (matrix1.GetLength(0) != matrix2.GetLength(0) || matrix1.GetLength(1) != matrix2.GetLength(1)
  720.                 || matrix1.GetLength(0) != matrix1.GetLength(1) || matrix2.GetLength(0) != matrix2.GetLength(1))
  721.             {
  722.                 Console.WriteLine("Ошибка, не соответствует размеры");
  723.                 Console.WriteLine();
  724.                 return;
  725.             }
  726.             SubstractionOfMatrixPart2(matrix1, matrix2, matrix1.GetLength(0), matrix1.GetLength(1));
  727.         }
  728.  
  729.         public static void SelectionForComOfMatrixAndNum()
  730.         {
  731.             Console.WriteLine("Каким способом вы хотите получить матрицу");
  732.             Console.WriteLine("1 - Ввести вручную, 2 - Рандомные матрицы, 3 - загругить из файла");
  733.             string choise = Console.ReadLine();
  734.             switch (choise)
  735.             {
  736.                 case "1":
  737.                     CompositionMatrixWithNumber();
  738.                     break;
  739.                 case "2":
  740.                     CompositionMatrixWithNumberByRandom();
  741.                     break;
  742.                 case "3":
  743.                     ComMatrixWithNumberFromFile();
  744.                     break;
  745.                 default:
  746.                     Console.WriteLine("Ошибка при выборе");
  747.                     Console.WriteLine();
  748.                     break;
  749.             }
  750.         }
  751.  
  752.         public static void CompositionMatrixWithNumber()
  753.         {
  754.             Console.WriteLine("Действие - умножение матрицы на число");
  755.             Console.WriteLine("Введите количество строк и столбцов в матрице");
  756.             Console.Write("строк = ");
  757.             int row;
  758.             while (!int.TryParse(Console.ReadLine(), out row) || row <= 0)
  759.             {
  760.                 Console.WriteLine("Ошибка");
  761.                 Console.WriteLine();
  762.                 return;
  763.             }
  764.             int column;
  765.             Console.Write("cтолбцов = ");
  766.             while (!int.TryParse(Console.ReadLine(), out column) || column <= 0)
  767.             {
  768.                 Console.WriteLine("Ошибка");
  769.                 Console.WriteLine();
  770.                 return;
  771.             }
  772.             Console.WriteLine("Введите матрицу");
  773.             int[,] matrix;
  774.             do
  775.             {
  776.                 matrix = CreatingMatrix(row, column);
  777.             } while (matrix == null);
  778.             Console.WriteLine("Введите число на которое надо умножить матрицу");
  779.             int lyamda;
  780.             while (!int.TryParse(Console.ReadLine(), out lyamda))
  781.             {
  782.                 Console.WriteLine("Ошибка");
  783.                 Console.WriteLine();
  784.             }
  785.             Console.WriteLine("Получившаяся матрица");
  786.             for (int i = 0; i < row; i++)
  787.             {
  788.                 for (int u = 0; u < column; u++)
  789.                 {
  790.                     Console.Write($"{matrix[i, u] * lyamda} ");
  791.                 }
  792.                 Console.WriteLine();
  793.             }
  794.             Console.WriteLine();
  795.         }
  796.  
  797.         public static void CompositionMatrixWithNumberByRandom()
  798.         {
  799.             Console.WriteLine("Действие - умножение матрицы на число");
  800.             Console.WriteLine("Введите количество строк и столбцов в матрице");
  801.             Console.Write("строк = ");
  802.             int row;
  803.             while (!int.TryParse(Console.ReadLine(), out row) || row <= 0)
  804.             {
  805.                 Console.WriteLine("Ошибка");
  806.                 Console.WriteLine();
  807.                 return;
  808.             }
  809.             int column;
  810.             Console.Write("cтолбцов = ");
  811.             while (!int.TryParse(Console.ReadLine(), out column) || column <= 0)
  812.             {
  813.                 Console.WriteLine("Ошибка");
  814.                 Console.WriteLine();
  815.                 return;
  816.             }
  817.             int[,] matrix = CreatingMatrixByRandom(row, column);
  818.             Console.WriteLine("Введите число на которое надо умножить матрицу");
  819.             int lyamda;
  820.             while (!int.TryParse(Console.ReadLine(), out lyamda))
  821.             {
  822.                 Console.WriteLine("Ошибка");
  823.                 Console.WriteLine();
  824.             }
  825.             Console.WriteLine("Получившаяся матрица");
  826.             for (int i = 0; i < row; i++)
  827.             {
  828.                 for (int u = 0; u < column; u++)
  829.                 {
  830.                     Console.Write($"{matrix[i, u] * lyamda} ");
  831.                 }
  832.                 Console.WriteLine();
  833.             }
  834.             Console.WriteLine();
  835.         }
  836.  
  837.         public static void ComMatrixWithNumberFromFile()
  838.         {
  839.             Console.WriteLine("Действие - умножение матрицы на число");
  840.             int[,] matrix;
  841.             do
  842.             {
  843.                 matrix = GettingMatrixFromFile();
  844.             } while (matrix == null);
  845.             Console.WriteLine("Введите число на которое надо умножить матрицу");
  846.             int lyamda;
  847.             while (!int.TryParse(Console.ReadLine(), out lyamda))
  848.             {
  849.                 Console.WriteLine("Ошибка");
  850.             }
  851.             Console.WriteLine("Получившаяся матрица");
  852.             for (int i = 0; i < matrix.GetLength(0); i++)
  853.             {
  854.                 for (int u = 0; u < matrix.GetLength(1); u++)
  855.                 {
  856.                     Console.Write($"{matrix[i, u] * lyamda} ");
  857.                 }
  858.                 Console.WriteLine();
  859.             }
  860.             Console.WriteLine();
  861.         }
  862.         public static void SelectionForComOfMatrixAndMatrix()
  863.         {
  864.             Console.WriteLine("Каким способом вы хотите получить матрицу");
  865.             Console.WriteLine("1 - Ввести вручную, 2 - Рандомные матрицы, 3 - загругить из файла");
  866.             string choise = Console.ReadLine();
  867.             switch (choise)
  868.             {
  869.                 case "1":
  870.                     CompositionWithAnotherMatrix();
  871.                     break;
  872.                 case "2":
  873.                     CompositionWithAnotherMatrixByRandom();
  874.                     break;
  875.                 case"3":
  876.                     ComMatrixesFromFile();
  877.                     break;
  878.                 default:
  879.                     Console.WriteLine("Ошибка при выборе");
  880.                     break;
  881.             }
  882.         }
  883.  
  884.         public static void CompositionWithAnotherMatrix()
  885.         {
  886.             Console.WriteLine("Действие - умножение двух матриц");
  887.             Console.WriteLine("Замечание: Количество столбцов в первой матрице равно количеству строк во второй");
  888.             Console.WriteLine("Введите количество строк первой матрицы");
  889.             Console.Write("строк первой матрицы = ");
  890.             int row1;
  891.             while (!int.TryParse(Console.ReadLine(), out row1) || row1 <= 0)
  892.             {
  893.                 Console.WriteLine("Ошибка");
  894.                 Console.WriteLine();
  895.                 return;
  896.             }
  897.             Console.WriteLine("Введите количество столбцов первой и строк второй матриц");
  898.             int column1row2;
  899.             Console.Write("количество столбцов первой и строк второй матриц = ");
  900.             while (!int.TryParse(Console.ReadLine(), out column1row2) || column1row2 <= 0)
  901.             {
  902.                 Console.WriteLine("Ошибка");
  903.                 Console.WriteLine();
  904.                 return;
  905.             }
  906.             Console.WriteLine("Введите количество столбцов второй матрицы");
  907.             int column2;
  908.             Console.Write("cтолбцов второй матрицы = ");
  909.             while (!int.TryParse(Console.ReadLine(), out column2) || column2 <= 0)
  910.             {
  911.                 Console.WriteLine("Ошибка");
  912.                 Console.WriteLine();
  913.                 return;
  914.             }
  915.             Console.WriteLine("Введите первую матрицу");
  916.             int[,] matrix1;
  917.             do
  918.             {
  919.                 matrix1 = CreatingMatrix(row1, column1row2);
  920.             } while (matrix1 == null);
  921.             Console.WriteLine("Введите вторую матрицу");
  922.             int[,] matrix2;
  923.             do
  924.             {
  925.                 matrix2 = CreatingMatrix(column1row2, column2);
  926.             } while (matrix2 == null);
  927.             CompositionWithAnotherMatrixPart2(matrix1, matrix2, row1, column1row2, column2);
  928.         }
  929.  
  930.         public static void CompositionWithAnotherMatrixPart2(int[,] matrix1, int[,] matrix2, int row1, int column1row2, int column2)
  931.         {
  932.             int[,] resultMatrix = new int[row1, column2];
  933.             for (int i = 0; i < row1; i++)
  934.             {
  935.                 for (int u = 0; u < column2; u++)
  936.                 {
  937.                     for (int j = 0; j < column1row2; j++)
  938.                     {
  939.                         resultMatrix[i, u] += matrix1[i, j] * matrix2[j, u];
  940.                     }
  941.                 }
  942.             }
  943.             Console.WriteLine("Произведение матриц:");
  944.             for (int i = 0; i < row1; i++)
  945.             {
  946.                 for (int u = 0; u < column2; u++)
  947.                 {
  948.                     Console.Write($"{resultMatrix[i, u]} ");
  949.                 }
  950.                 Console.WriteLine();
  951.             }
  952.             Console.WriteLine();
  953.         }
  954.  
  955.         public static void CompositionWithAnotherMatrixByRandom()
  956.         {
  957.             Console.WriteLine("Действие - умножение двух матриц");
  958.             Console.WriteLine("Замечание: Количество столбцов в первой матрице равно количеству строк во второй");
  959.             Console.WriteLine("Введите количество строк первой матрицы");
  960.             Console.Write("строк первой матрицы = ");
  961.             int row1;
  962.             while (!int.TryParse(Console.ReadLine(), out row1) || row1 <= 0)
  963.             {
  964.                 Console.WriteLine("Ошибка");
  965.                 Console.WriteLine();
  966.                 return;
  967.             }
  968.             Console.WriteLine("Введите количество столбцов первой и строк второй матриц");
  969.             int column1row2;
  970.             Console.Write("количество столбцов первой и строк второй матриц = ");
  971.             while (!int.TryParse(Console.ReadLine(), out column1row2) || column1row2 <= 0)
  972.             {
  973.                 Console.WriteLine("Ошибка");
  974.                 Console.WriteLine();
  975.                 return;
  976.             }
  977.             Console.WriteLine("Введите количество столбцов второй матрицы");
  978.             int column2;
  979.             Console.Write("cтолбцов второй матрицы = ");
  980.             while (!int.TryParse(Console.ReadLine(), out column2) || column2 <= 0)
  981.             {
  982.                 Console.WriteLine("Ошибка");
  983.                 Console.WriteLine();
  984.                 return;
  985.             }
  986.             Console.WriteLine("Первая матрица:");
  987.             int[,] matrix1 = CreatingMatrixByRandom(row1, column1row2);
  988.             Console.WriteLine("Вторая матрица:");
  989.             int[,] matrix2 = CreatingMatrixByRandom(column1row2, column2);
  990.             CompositionWithAnotherMatrixPart2(matrix1, matrix2, row1, column1row2, column2);
  991.         }
  992.  
  993.         public static void ComMatrixesFromFile()
  994.         {
  995.             Console.WriteLine("Действие - умножение двух матриц");
  996.             Console.WriteLine("Замечание: Количество столбцов в первой матрице равно количеству строк во второй");
  997.             int[,] matrix1;
  998.             Console.WriteLine("Первая матрица:");
  999.             do
  1000.             {
  1001.                 matrix1 = GettingMatrixFromFile();
  1002.             } while (matrix1 == null);
  1003.             int[,] matrix2;
  1004.             Console.WriteLine("Вторая матрица");
  1005.             do
  1006.             {
  1007.                 matrix2 = GettingMatrixFromFile();
  1008.             } while (matrix2 == null);
  1009.             CompositionWithAnotherMatrixPart2(matrix1, matrix2, matrix1.GetLength(0), matrix1.GetLength(1), matrix2.GetLength(1));
  1010.         }
  1011.  
  1012.         public static void SelectionForDeterminant()
  1013.         {
  1014.             Console.WriteLine("Каким способом вы хотите получить матрицу");
  1015.             Console.WriteLine("1 - Ввести вручную, 2 - Рандомные матрицы, 3 - загругить из файла");
  1016.             string choise = Console.ReadLine();
  1017.             switch (choise)
  1018.             {
  1019.                 case "1":
  1020.                     DeterminantByHands();
  1021.                     break;
  1022.                 case "2":
  1023.                     DeterminantByRandom();
  1024.                     break;
  1025.                 case "3":
  1026.                     DeterminantFromFile();
  1027.                     break;
  1028.                 default:
  1029.                     Console.WriteLine("Ошибка при выборе");
  1030.                     break;
  1031.             }
  1032.         }
  1033.  
  1034.         public static void DeterminantByHands()
  1035.         {
  1036.             try
  1037.             {
  1038.                 Console.WriteLine("Замечание: матрица должна быть квадратной");
  1039.                 Console.WriteLine("Введите размерность матрицы (если будет введено например 3, матрица будет размера 3 на 3)");
  1040.                 Console.Write("Размерность = ");
  1041.                 int sizeOfMatrix;
  1042.                 while (!int.TryParse(Console.ReadLine(), out sizeOfMatrix) || sizeOfMatrix <= 0)
  1043.                 {
  1044.                     Console.WriteLine("Ошибка при вводе!");
  1045.                     Console.WriteLine();
  1046.                 }
  1047.                 int[,] matrix;
  1048.                 do
  1049.                 {
  1050.                     matrix = CreatingMatrix(sizeOfMatrix, sizeOfMatrix);
  1051.                 } while (matrix == null);
  1052.                 int determinantMatrix = Determinant(matrix);
  1053.                 Console.WriteLine($"Детерминант матрицы = {determinantMatrix}");
  1054.                 Console.WriteLine();
  1055.             }
  1056.             catch(OverflowException)
  1057.             {
  1058.                 Console.WriteLine();
  1059.             }
  1060.             catch
  1061.             {
  1062.                 Console.WriteLine("Ошибка");
  1063.             }
  1064.         }
  1065.  
  1066.         public static void DeterminantByRandom()
  1067.         {
  1068.             try
  1069.             {
  1070.                 Console.WriteLine("Замечание: матрица должна быть квадратной");
  1071.                 Console.WriteLine("Введите размерность матрицы (если будет введено например 3, матрица будет размера 3 на 3)");
  1072.                 Console.Write("Размерность = ");
  1073.                 int sizeOfMatrix;
  1074.                 while (!int.TryParse(Console.ReadLine(), out sizeOfMatrix) || sizeOfMatrix <= 0)
  1075.                 {
  1076.                     Console.WriteLine("Ошибка при вводе!");
  1077.                 }
  1078.                 int[,] matrix = CreatingMatrixByRandom(sizeOfMatrix, sizeOfMatrix);
  1079.                 int determinantMatrix = Determinant(matrix);
  1080.                 Console.WriteLine($"Детерминант матрицы = {determinantMatrix}");
  1081.                 Console.WriteLine();
  1082.             }
  1083.             catch (OverflowException)
  1084.             {
  1085.                 Console.WriteLine();
  1086.             }
  1087.             catch
  1088.             {
  1089.                 Console.WriteLine("Ошибка");
  1090.             }
  1091.         }
  1092.  
  1093.         public static void DeterminantFromFile()
  1094.         {
  1095.             try
  1096.             {
  1097.                 Console.WriteLine("Детерминант находиться только в квадратных матрицах");
  1098.                 int[,] matrix;
  1099.                 do
  1100.                 {
  1101.                     matrix = GettingMatrixFromFile();
  1102.                 } while (matrix == null);
  1103.                 int detetrminant = Determinant(matrix);
  1104.                 Console.WriteLine($"Детерминант = {detetrminant}");
  1105.             }
  1106.             catch(InvalidOperationException)
  1107.             {
  1108.                 Console.WriteLine("Неверный размер матрицы");
  1109.             }
  1110.             catch (OverflowException)
  1111.             {
  1112.                 Console.WriteLine();
  1113.             }
  1114.             catch
  1115.             {
  1116.                 Console.WriteLine("Ошибка сенпай");
  1117.             }
  1118.         }
  1119.         public static int Determinant(int[,] matrix)
  1120.         {
  1121.             if (matrix.GetLength(1) == matrix.GetLength(0))
  1122.             {
  1123.                 // Если матрица 2х2
  1124.                 if (matrix.Length == 4)
  1125.                 {
  1126.                     Console.WriteLine(Environment.NewLine + "Определитель матрицы равен:");
  1127.                     Console.WriteLine(matrix[0, 0] * matrix[1, 1] - matrix[0, 1] * matrix[1, 0]);
  1128.                 }
  1129.                 int sign = 1;
  1130.                 int[,] minor = new int[matrix.GetLength(0) - 1, matrix.GetLength(0) - 1];
  1131.  
  1132.                 // Нахождение минора.
  1133.                 for (int i = 1; i < matrix.GetLength(1); i++)
  1134.                 {
  1135.                     for (int j = 0; j < matrix.GetLength(0) - 1; j++)
  1136.                     {
  1137.                         minor[i - 1, j] = matrix[i, j];
  1138.                     }
  1139.                     for (int j = matrix.GetLength(0); j < matrix.GetLength(1); j++)
  1140.                     {
  1141.                         minor[i - 1, j - 1] = matrix[i, j];
  1142.                     }
  1143.                 }
  1144.                 int det = 0;
  1145.                 // Нахождение определителя.
  1146.                 for (int i = 0; i < matrix.GetLength(0); i++)
  1147.                 {
  1148.                     det += sign * matrix[0, i] * Determinant(minor);
  1149.                     sign = -sign;
  1150.                 }
  1151.                 return det;
  1152.             }
  1153.             else
  1154.             {
  1155.                 int? nol = null;
  1156.                 return (int)nol;
  1157.             }
  1158.  
  1159.         }
  1160.     }
  1161. }