Advertisement
Guest User

test (C# Shell App Paste)

a guest
Aug 19th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.03 KB | None | 0 0
  1. //Disclaimer: The creator of 'C# Shell (C# Offline Compiler)' is in no way responsible for the code posted by any user.
  2. using System;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Collections.Generic;
  6.  
  7. namespace CSharp_Shell
  8. {
  9.     public enum Rotation {right, left};
  10.     public class Matrix
  11.     {
  12.         private int[,] arr;
  13.         private int length;
  14.        
  15.         public int this[int index1, int index2]
  16.         {
  17.             set{
  18.                 arr[index1, index2] = value;
  19.             }
  20.             get{
  21.                 return arr[index1, index2];
  22.             }
  23.         }
  24.         public Matrix(int length)
  25.         {
  26.             arr = new int[length, length];
  27.             this.length = length;
  28.         }
  29.         public void Print()
  30.         {
  31.             for(int a = 0; a < length; a++)
  32.             {
  33.                 for(int b = 0; b < length; b++)
  34.                     Console.Write(arr[a,b]);
  35.                 Console.WriteLine("");
  36.             }
  37.         }
  38.         public void RandomFill()
  39.         {
  40.             Random rand = new Random();
  41.             for(int i = 0; i < arr.Length; i++)
  42.             {
  43.                 arr[i/length, i%length] = rand.Next(9);
  44.             }
  45.         }
  46.         public void Rotate(Rotation rot=Rotation.right, int times=1)
  47.         {
  48.             for(int t = 0; t < times; t++)
  49.             {
  50.                 int[,] result = new int[length, length];
  51.  
  52.             for (int i = 0; i < length; i++)
  53.             for (int j = 0; j < length; j++)
  54.             result[i, j] = (rot == Rotation.right)?(arr[length - j - 1, i]):(arr[j, length - i - 1]);
  55.  
  56.             arr = result;
  57.             }
  58.         }
  59.     }
  60.  
  61.     public static class Program
  62.     {
  63.         public static void Main()
  64.         {
  65.             int num = Console.ReadLine<Int32>();
  66.             Matrix matrix = new Matrix(num);
  67.             string s;
  68.             do
  69.             {
  70.                 s = Console.ReadLine();
  71.                 switch(s)
  72.                 {
  73.                     case "right":
  74.                     case "left":
  75.                         matrix.Rotate((s == "right")?Rotation.right:Rotation.left, Console.ReadLine<Int32>());
  76.                         break;
  77.                     case "print":
  78.                         matrix.Print();
  79.                         break;
  80.                     case "random":
  81.                         matrix.RandomFill();
  82.                         break;
  83.                 }
  84.             } while(s != "exit");
  85.             matrix.RandomFill();
  86.             matrix.Print();
  87.             Console.WriteLine("-------");
  88.             matrix.Rotate();
  89.             matrix.Print();
  90.         }
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement