Vla_DOS

Untitled

Feb 11th, 2023
891
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.92 KB | None | 0 0
  1. using System;
  2. using System.Security.AccessControl;
  3. using System.Security.AccessControl;
  4. using System.Linq;
  5. using System.Drawing.Drawing2D;
  6. using System.Collections.Generic;
  7. using System.Xml.Linq;
  8.  
  9. namespace lab
  10. {
  11.     class Program
  12.     {
  13.         static void Print(string[,] arr)
  14.         {
  15.             for (int i = 0; i < arr.GetLength(0); i++)
  16.             {
  17.                 for (int j = 0; j < arr.GetLength(1); j++)
  18.                     Console.Write(arr[i, j] + "\t");
  19.                 Console.WriteLine();
  20.             }
  21.         }
  22.  
  23.         static void Sort(string[,] arr)
  24.         {
  25.             for (int x = 0; x < arr.GetLength(0); x++)
  26.             {
  27.                 for (int y = 0; y < arr.GetLength(1); y++)
  28.                 {
  29.                     for (int x2 = 0; x2 < arr.GetLength(0); x2++)
  30.                     {
  31.                         for (int y2 = 0; y2 < arr.GetLength(1); y2++)
  32.                         {
  33.                             if (x2 != x && y2 != y && string.CompareOrdinal(arr[x, y], arr[x2, y2]) < 0)
  34.                             {
  35.                                 string temp = arr[x, y];
  36.                                 arr[x, y] = arr[x2, y2];
  37.                                 arr[x2, y2] = temp;
  38.                             }
  39.                         }
  40.                     }
  41.                 }
  42.             }
  43.         }
  44.  
  45.         public static string[,] RotateClockwise(string[,] m, int n, int numberHalfSquare)
  46.         {
  47.             var result = new string[2, 2];
  48.  
  49.             if(numberHalfSquare == 1)
  50.             {
  51.                 for (int i = 0; i < n; i++)
  52.                     for (int j = 0; j < n; j++)
  53.                         result[i, j] = m[(n - j - 1), i];
  54.  
  55.                 for (int i = 0; i < 2; i++)
  56.                 {
  57.                     for (int j = 0; j < 2; j++)
  58.                     {
  59.                         m[i, j] = result[i, j];
  60.                     }
  61.                 }
  62.             }
  63.             else if (numberHalfSquare == 2)
  64.             {
  65.                 for (int i = 0; i < n; i++)
  66.                     for (int j = 0; j < n; j++)
  67.                         result[i, j] = m[(n - j - 1), i + 2];
  68.  
  69.                 for (int i = 0; i < 2; i++)
  70.                 {
  71.                     for (int j = 0; j < 2; j++)
  72.                     {
  73.                         m[i, j + 2] = result[i, j];
  74.                     }
  75.                 }
  76.             }
  77.             else if (numberHalfSquare == 3)
  78.             {
  79.                 for (int i = 0; i < n; i++)
  80.                     for (int j = 0; j < n; j++)
  81.                         result[i, j] = m[(n - j - 1) + 2, i];
  82.  
  83.                 for (int i = 0; i < 2; i++)
  84.                 {
  85.                     for (int j = 0; j < 2; j++)
  86.                     {
  87.                         m[i+2, j] = result[i, j];
  88.                     }
  89.                 }
  90.             }
  91.             else if (numberHalfSquare == 4)
  92.             {
  93.                 for (int i = 0; i < n; i++)
  94.                     for (int j = 0; j < n; j++)
  95.                         result[i, j] = m[(n - j - 1) + 2, i + 2];
  96.  
  97.                 for (int i = 0; i < 2; i++)
  98.                 {
  99.                     for (int j = 0; j < 2; j++)
  100.                     {
  101.                         m[i+2, j + 2] = result[i, j];
  102.                     }
  103.                 }
  104.             }
  105.             else
  106.             {
  107.                 throw new ArgumentException();
  108.             }
  109.  
  110.             return m;
  111.         }
  112.         static void SwapElements(string[,] arr, string a, string b)
  113.         {
  114.             int AIndexI = 0, AIndexJ = 0;
  115.             int BIndexI = 0, BIndexJ = 0;
  116.  
  117.             for (int i = 0; i < arr.GetLength(0); i++)
  118.             {
  119.                 for (int j = 0; j < arr.GetLength(1); j++)
  120.                 {
  121.                     if (arr[i, j] == a)
  122.                     {
  123.                         AIndexI = i;
  124.                         AIndexJ = j;
  125.                         break;
  126.                     }
  127.                 }
  128.             }
  129.             for (int i = 0; i < arr.GetLength(0); i++)
  130.             {
  131.                 for (int j = 0; j < arr.GetLength(1); j++)
  132.                 {
  133.                     if (arr[i, j] == b)
  134.                     {
  135.                         BIndexI = i;
  136.                         BIndexJ = j;
  137.                         break;
  138.                     }
  139.                 }
  140.             }
  141.             arr[AIndexI, AIndexJ] = b;
  142.             arr[BIndexI, BIndexJ] = a;
  143.         }
  144.  
  145.         static void Menu()
  146.         {
  147.             Console.WriteLine("1. Print matrix.");
  148.             Console.WriteLine("2. Sort matrix.");
  149.             Console.WriteLine("3. Rotate clockwise.");
  150.             Console.WriteLine("4. Swap elements.");
  151.             Console.WriteLine("0. Exit.");
  152.  
  153.         }
  154.         static void Main()
  155.         {
  156.             try
  157.             {
  158.                 Random rand = new Random();
  159.                 int n = 4;
  160.                 string[,] arr = new string[n, n];
  161.  
  162.                 for (int i = 0; i < arr.GetLength(0); i++)
  163.                 {
  164.                     for (int j = 0; j < arr.GetLength(1); j++)
  165.                     {
  166.                         string temp = ((char)rand.Next('\u0041', '\u005A')).ToString().ToUpper();
  167.  
  168.                         arr[i, j] = temp;
  169.  
  170.                     }
  171.                 }
  172.                 int c = 0;
  173.  
  174.                 do
  175.                 {
  176.                     Menu();
  177.                     Console.WriteLine();
  178.                     Console.Write(">> ");
  179.                     c = int.Parse(Console.ReadLine());
  180.                     switch (c)
  181.                     {
  182.                         case 1: { Print(arr); } break;
  183.                         case 2: { Sort(arr); Print(arr); } break;
  184.                         case 3:
  185.                             {
  186.                                 Console.WriteLine("Number Half Square (1-4): ");
  187.                                 int numberHalfSquare = int.Parse(Console.ReadLine());
  188.                                 arr = RotateClockwise(arr, 2, numberHalfSquare);
  189.                                 Print(arr);
  190.                             }
  191.                             break;
  192.                         case 4:
  193.                             {
  194.                                 string a, b;
  195.                                 Console.Write("First element = ");
  196.                                 a = Console.ReadLine();
  197.                                 Console.Write("Second element = ");
  198.                                 b = Console.ReadLine();
  199.                                 SwapElements(arr, a, b);
  200.                                 Print(arr);
  201.                             }
  202.                             break;
  203.  
  204.                         default: break;
  205.                     }
  206.                     Console.WriteLine();
  207.  
  208.                 } while (c != 0);
  209.             }
  210.             catch(Exception ex)
  211.             {
  212.                 Console.WriteLine(ex.Message);
  213.             }
  214.             Console.ReadKey();
  215.         }
  216.     }
  217. }
Advertisement
Add Comment
Please, Sign In to add comment