Advertisement
Ruslan_Isaev

Matrix

Dec 11th, 2020
924
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.00 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace Лабораторная_6а
  9. {
  10.     class Program
  11.     { // Все строки матрицы с нечетными номерами содержат одинаковое количество нулей.
  12.         static void Main(string[] args)
  13.         {
  14.             int[,] matr; bool flag = false;
  15.             InputMatrixFile(out matr);
  16.  
  17.             string choice = "0";
  18.             while (choice != "6")
  19.             {
  20.                 Console.ForegroundColor = ConsoleColor.DarkYellow;
  21.                 Console.WriteLine();
  22.                 Console.WriteLine(" 1. Ввод матрицы с клавиатуры.");
  23.                 Console.WriteLine(" 2. Ввод матрицы из файла.");
  24.                 Console.WriteLine(" 3. Вычисление характеристики.");
  25.                 Console.WriteLine(" 4. Нахождение куба матрицы. ");
  26.                 Console.WriteLine(" 5. Печать матрицы.");
  27.                 Console.WriteLine(" 6. Выход.");
  28.                 Console.WriteLine(" ");
  29.                 Console.WriteLine(" Введите номер пункта меню ");
  30.                 Console.ForegroundColor = ConsoleColor.Yellow;
  31.                 choice = Console.ReadLine();
  32.                 Console.ForegroundColor = ConsoleColor.White;
  33.                 switch (choice)
  34.                 {
  35.                     case "1": //Ввод с клавиатуры
  36.                         Console.ForegroundColor = ConsoleColor.Cyan;
  37.                         InputMatrix(out matr);
  38.                         flag = true;
  39.                         Console.ForegroundColor = ConsoleColor.White;
  40.                         break;
  41.                     case "2": //Ввод из файла
  42.                         Console.ForegroundColor = ConsoleColor.Cyan;
  43.                         InputMatrixFile(out matr);
  44.                         Console.WriteLine("Матрица успешно введена!");
  45.                         flag = true;
  46.                         Console.ForegroundColor = ConsoleColor.White;
  47.                         break;
  48.                     case "3": //Характеристика
  49.                         if (flag)
  50.                         {
  51.                             if (CharacteristicMatrix(ref matr))
  52.                             {
  53.                                 Console.ForegroundColor = ConsoleColor.Green;
  54.                                 Console.WriteLine("Характеристика выполняется :)");
  55.                                 Console.ForegroundColor = ConsoleColor.White;
  56.                             }
  57.                             else
  58.                             {
  59.                                 Console.ForegroundColor = ConsoleColor.Red;
  60.                                 Console.WriteLine("Характеристика не выполняется :(");
  61.                                 Console.ForegroundColor = ConsoleColor.White;
  62.                             }
  63.                         }
  64.                         else
  65.                         {
  66.                             Console.ForegroundColor = ConsoleColor.Red;
  67.                             Console.WriteLine("МАТРИЦА НЕ ВВЕДЕНА!");
  68.                             Console.ForegroundColor = ConsoleColor.White;
  69.                         }
  70.                         break;
  71.                     case "4":
  72.                         if (flag)
  73.                         {
  74.                             if (matr.GetLength(0) == matr.GetLength(1))
  75.                             {
  76.                                 Console.ForegroundColor = ConsoleColor.Cyan;
  77.                                 Console.WriteLine("Куб матрицы ");
  78.                                 PrintMatrix(MatrixStepen(ref matr, 3));
  79.                                 Console.ForegroundColor = ConsoleColor.White;
  80.                             }
  81.                             else
  82.                             {
  83.                                 Console.ForegroundColor = ConsoleColor.Red;
  84.                                 Console.WriteLine("ТАКУЮ МАТРИЦУ НЕЛЬЗЯ ВОЗВЕСТИ В КУБ!");
  85.                                 Console.ForegroundColor = ConsoleColor.White;
  86.                             }
  87.                         }
  88.                         else
  89.                         {
  90.                             Console.ForegroundColor = ConsoleColor.Red;
  91.                             Console.WriteLine("МАТРИЦА НЕ ВВЕДЕНА!");
  92.                             Console.ForegroundColor = ConsoleColor.White;
  93.                         }
  94.                         break;
  95.                     case "5": //Печать матрицы
  96.                         if (flag)
  97.                         {
  98.                             Console.ForegroundColor = ConsoleColor.Cyan;
  99.                             Console.WriteLine("Текущая матрица ");
  100.                             PrintMatrix(matr);
  101.                             Console.ForegroundColor = ConsoleColor.White;
  102.                         }
  103.                         else
  104.                         {
  105.                             Console.ForegroundColor = ConsoleColor.Red;
  106.                             Console.WriteLine("МАТРИЦА НЕ ВВЕДЕНА!");
  107.                             Console.ForegroundColor = ConsoleColor.White;
  108.                         }
  109.                         break;
  110.                     case "6": //Выход
  111.                         Console.ForegroundColor = ConsoleColor.Magenta;
  112.                         Console.WriteLine(" До свидания!");
  113.                         Console.ForegroundColor = ConsoleColor.White;
  114.                         break;
  115.                     default:
  116.                         Console.WriteLine(" Введите 1, 2, 3, 4 или 5");
  117.                         break;
  118.                 }
  119.             }
  120.             Console.ReadLine();
  121.         }
  122.         private static void InputMatrix(out int[,] matr)
  123.         {
  124.             Console.WriteLine("Введите размеры матрицы ");
  125.             string[] str = Console.ReadLine().Split(' ');
  126.             int n = int.Parse(str[0]);
  127.             int m = int.Parse(str[1]);
  128.             matr = new int[n, m];
  129.             Console.WriteLine("Введите матрицу ");
  130.             for (int i = 0; i < n; i++)
  131.             {
  132.                 str = Console.ReadLine().Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries);
  133.                 for (int j = 0; j < m; j++)
  134.                 {
  135.                     matr[i, j] = int.Parse(str[j]);
  136.                 }
  137.             }
  138.         }
  139.         private static void InputMatrixFile(out int[,] matr)
  140.         {
  141.             using (var sr = new StreamReader("text1.txt"))
  142.             {
  143.                 // ввод размерности
  144.                 string[] str = sr.ReadLine().Split(' ');
  145.                 var n = int.Parse(str[0]);
  146.                 var m = int.Parse(str[1]);
  147.                 matr = new int[n, m];
  148.                 // ввод матрицы
  149.                 int i;
  150.                 string line;
  151.                 for (i = 0; (i < n) && ((line = sr.ReadLine()) != null); i++)
  152.                 {
  153.                     string[] numbers = line.Split(new char[] { ' ', ',', '\t' });
  154.                     var j = 0;
  155.                     foreach (string numString in numbers)
  156.                     {
  157.                         int x;
  158.                         bool canConvert = int.TryParse(numString, out x);
  159.                         if (canConvert)
  160.                         {
  161.                             matr[i, j] = x;
  162.                             j++;
  163.                         }
  164.                     }
  165.                 }
  166.             }
  167.         }
  168.         static void PrintMatrix(int[,] a)
  169.         {
  170.             Console.ForegroundColor = ConsoleColor.Cyan;
  171.             for (int i = 0; i < a.GetLength(0); i++)
  172.             {
  173.                 for (int j = 0; j < a.GetLength(1); j++)
  174.                 {
  175.                     Console.Write("{0} \t", a[i, j]);
  176.  
  177.                 }
  178.                 Console.WriteLine();
  179.             }
  180.             Console.ForegroundColor = ConsoleColor.White;
  181.         }
  182.         static bool CharacteristicMatrix(ref int[,] matr)
  183.         {
  184.             int countNull = 0, countString = 0, countNull2 = 0, countString2 = 0;
  185.             if (matr.GetLength(0) < 2 && matr.GetLength(1) < 2)
  186.             {
  187.                 return true;
  188.             }
  189.             for (int i = 0; i < matr.GetLength(1); i++)//Нулей в первой строке
  190.             {
  191.                 if (matr[0, i] == 0)
  192.                 {
  193.                     countNull++;
  194.                 }
  195.             }
  196.             for (int i = 2; i < matr.GetLength(0); i += 2)
  197.             {
  198.                 countString++;
  199.                 for (int j = 0; j < matr.GetLength(1); j++)
  200.                 {
  201.                     if (matr[i, j] == 0)
  202.                     {
  203.                         countNull2++;
  204.                     }
  205.                 }
  206.                 if (countNull2 == countNull)
  207.                 {
  208.                     countString2++;
  209.                 }
  210.             }
  211.             if (countString == countString2)
  212.             {
  213.                 return true;
  214.             }
  215.             else return false;
  216.         }
  217.         static int[,] MatrixStepen(ref int[,] matr, int stepen)
  218.         {
  219.             /*
  220.             var matrB = new int[matr.GetLength(0), matr.GetLength(1)];
  221.             for (int i = 0; i < matr.GetLength(0); i++)
  222.             {
  223.                 for (int j = 0; j < matr.GetLength(0); j++)
  224.                 {
  225.                     for (int k = 0; k < matr.GetLength(1); k++)
  226.                     {
  227.                         matrB[i, j] += matr[i, k] * matr[k, j];
  228.                     }
  229.                 }
  230.             }
  231.             var matrC = new int[matr.GetLength(0), matr.GetLength(1)];
  232.             for (int i = 0; i < matr.GetLength(0); i++)
  233.             {
  234.                 for (int j = 0; j < matr.GetLength(0); j++)
  235.                 {
  236.                     for (int k = 0; k < matr.GetLength(1); k++)
  237.                     {
  238.                         matrC[i, j] += matr[i, k] * matrB[k, j];
  239.                     }
  240.                 }
  241.             }
  242.             matr = matrC;
  243.             return matr;
  244.              */
  245.             var firstMatr = matr;
  246.             for (int a = 1; a < stepen; a++)
  247.             {
  248.                 var matrB = new int[matr.GetLength(0), matr.GetLength(1)];
  249.                 for (int i = 0; i < matr.GetLength(0); i++)
  250.                 {
  251.                     for (int j = 0; j < matr.GetLength(0); j++)
  252.                     {
  253.                         for (int k = 0; k < matr.GetLength(1); k++)
  254.                         {
  255.                             matrB[i, j] += firstMatr[i, k] * matr[k, j];
  256.                         }
  257.                     }
  258.                 }
  259.                 matr = matrB;
  260.             }
  261.             return matr;
  262.  
  263.         }
  264.     }
  265. }
  266.  
  267.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement