Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Security.AccessControl;
- using System.Security.AccessControl;
- using System.Linq;
- using System.Drawing.Drawing2D;
- using System.Collections.Generic;
- using System.Xml.Linq;
- namespace lab
- {
- class Program
- {
- static void Print(string[,] arr)
- {
- for (int i = 0; i < arr.GetLength(0); i++)
- {
- for (int j = 0; j < arr.GetLength(1); j++)
- Console.Write(arr[i, j] + "\t");
- Console.WriteLine();
- }
- }
- static void Sort(string[,] arr)
- {
- for (int x = 0; x < arr.GetLength(0); x++)
- {
- for (int y = 0; y < arr.GetLength(1); y++)
- {
- for (int x2 = 0; x2 < arr.GetLength(0); x2++)
- {
- for (int y2 = 0; y2 < arr.GetLength(1); y2++)
- {
- if (x2 != x && y2 != y && string.CompareOrdinal(arr[x, y], arr[x2, y2]) < 0)
- {
- string temp = arr[x, y];
- arr[x, y] = arr[x2, y2];
- arr[x2, y2] = temp;
- }
- }
- }
- }
- }
- }
- public static string[,] RotateClockwise(string[,] m, int n, int numberHalfSquare)
- {
- var result = new string[2, 2];
- if(numberHalfSquare == 1)
- {
- for (int i = 0; i < n; i++)
- for (int j = 0; j < n; j++)
- result[i, j] = m[(n - j - 1), i];
- for (int i = 0; i < 2; i++)
- {
- for (int j = 0; j < 2; j++)
- {
- m[i, j] = result[i, j];
- }
- }
- }
- else if (numberHalfSquare == 2)
- {
- for (int i = 0; i < n; i++)
- for (int j = 0; j < n; j++)
- result[i, j] = m[(n - j - 1), i + 2];
- for (int i = 0; i < 2; i++)
- {
- for (int j = 0; j < 2; j++)
- {
- m[i, j + 2] = result[i, j];
- }
- }
- }
- else if (numberHalfSquare == 3)
- {
- for (int i = 0; i < n; i++)
- for (int j = 0; j < n; j++)
- result[i, j] = m[(n - j - 1) + 2, i];
- for (int i = 0; i < 2; i++)
- {
- for (int j = 0; j < 2; j++)
- {
- m[i+2, j] = result[i, j];
- }
- }
- }
- else if (numberHalfSquare == 4)
- {
- for (int i = 0; i < n; i++)
- for (int j = 0; j < n; j++)
- result[i, j] = m[(n - j - 1) + 2, i + 2];
- for (int i = 0; i < 2; i++)
- {
- for (int j = 0; j < 2; j++)
- {
- m[i+2, j + 2] = result[i, j];
- }
- }
- }
- else
- {
- throw new ArgumentException();
- }
- return m;
- }
- static void SwapElements(string[,] arr, string a, string b)
- {
- int AIndexI = 0, AIndexJ = 0;
- int BIndexI = 0, BIndexJ = 0;
- for (int i = 0; i < arr.GetLength(0); i++)
- {
- for (int j = 0; j < arr.GetLength(1); j++)
- {
- if (arr[i, j] == a)
- {
- AIndexI = i;
- AIndexJ = j;
- break;
- }
- }
- }
- for (int i = 0; i < arr.GetLength(0); i++)
- {
- for (int j = 0; j < arr.GetLength(1); j++)
- {
- if (arr[i, j] == b)
- {
- BIndexI = i;
- BIndexJ = j;
- break;
- }
- }
- }
- arr[AIndexI, AIndexJ] = b;
- arr[BIndexI, BIndexJ] = a;
- }
- static void Menu()
- {
- Console.WriteLine("1. Print matrix.");
- Console.WriteLine("2. Sort matrix.");
- Console.WriteLine("3. Rotate clockwise.");
- Console.WriteLine("4. Swap elements.");
- Console.WriteLine("0. Exit.");
- }
- static void Main()
- {
- try
- {
- Random rand = new Random();
- int n = 4;
- string[,] arr = new string[n, n];
- for (int i = 0; i < arr.GetLength(0); i++)
- {
- for (int j = 0; j < arr.GetLength(1); j++)
- {
- string temp = ((char)rand.Next('\u0041', '\u005A')).ToString().ToUpper();
- arr[i, j] = temp;
- }
- }
- int c = 0;
- do
- {
- Menu();
- Console.WriteLine();
- Console.Write(">> ");
- c = int.Parse(Console.ReadLine());
- switch (c)
- {
- case 1: { Print(arr); } break;
- case 2: { Sort(arr); Print(arr); } break;
- case 3:
- {
- Console.WriteLine("Number Half Square (1-4): ");
- int numberHalfSquare = int.Parse(Console.ReadLine());
- arr = RotateClockwise(arr, 2, numberHalfSquare);
- Print(arr);
- }
- break;
- case 4:
- {
- string a, b;
- Console.Write("First element = ");
- a = Console.ReadLine();
- Console.Write("Second element = ");
- b = Console.ReadLine();
- SwapElements(arr, a, b);
- Print(arr);
- }
- break;
- default: break;
- }
- Console.WriteLine();
- } while (c != 0);
- }
- catch(Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment