simeon3000

String Matrix Rotation

Jan 31st, 2018
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.66 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5.  
  6. namespace P12.StringMatrixRotation
  7. {
  8.     class StringMatrixRotation
  9.     {
  10.         private static char[,] matrix;
  11.        
  12.         static void Main()
  13.         {
  14.             int degrees = int.Parse(Regex.Match(Console.ReadLine(), "[0-9]+").ToString()) % 360;
  15.  
  16.             var tempList = new List<List<char>>();
  17.             int rows = 0;
  18.             int cols = 0;
  19.            
  20.             string inputRow;
  21.             while ((inputRow = Console.ReadLine()) != "END")
  22.             {                
  23.                 if (inputRow.Length > cols)
  24.                 {
  25.                     cols = inputRow.Length;
  26.                 }
  27.                 tempList.Add(inputRow.ToList());
  28.                 rows++;
  29.             }
  30.  
  31.             //fill matrix
  32.             matrix = new char[rows, cols];
  33.             for (int r = 0; r < rows; r++)
  34.             {
  35.                 for (int c = 0; c < cols; c++)
  36.                 {
  37.                     try
  38.                     {
  39.                         matrix[r, c] = tempList[r][c];
  40.                     }
  41.                     catch (Exception)
  42.                     {
  43.                         matrix[r, c] = ' ';
  44.                     }
  45.                 }
  46.             }
  47.  
  48.             switch (degrees)
  49.             {                
  50.                 case 90:
  51.                     matrix = rotate90degrees(matrix);
  52.                     break;
  53.                 case 180:
  54.                     matrix = rotate90degrees(matrix);
  55.                     matrix = rotate90degrees(matrix);
  56.                     break;
  57.                 case 270:
  58.                     matrix = rotate90degrees(matrix);
  59.                     matrix = rotate90degrees(matrix);
  60.                     matrix = rotate90degrees(matrix);
  61.                     break;
  62.             }
  63.  
  64.             //print
  65.             for (int r = 0; r < matrix.GetLength(0); r++)
  66.             {
  67.                 for (int c = 0; c < matrix.GetLength(1); c++)
  68.                 {
  69.                     Console.Write(matrix[r, c]);
  70.                 }
  71.                 Console.WriteLine();
  72.             }          
  73.         }
  74.        
  75.         private static char[,] rotate90degrees(char[,] input)
  76.         {
  77.  
  78.             int rows = input.GetLength(0);
  79.             int cols = input.GetLength(1);
  80.             char[,] output = new char[cols, rows];
  81.  
  82.             for (int r = 0; r < rows; r++)
  83.             {
  84.                 for (int c = 0; c < cols; c++)
  85.                 {
  86.                     output[c, rows - 1 - r] = input[r, c];
  87.                 }
  88.             }
  89.             return output;
  90.         }
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment