emodev

Untitled

Dec 22nd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.77 KB | None | 0 0
  1. package LinearDataStructures.Exercises_StartSeriously_21dec;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8.  
  9. public class e06_StringMatrix80Points {
  10.     public static void main(String[] args) throws IOException {
  11.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  12.         String rotation = reader.readLine();
  13.         rotation = rotation.substring(rotation.indexOf("(") + 1, rotation.length() - 1);
  14.         int rotateDegree = Integer.parseInt(rotation);
  15.  
  16.         char[][] matrix = null;
  17.         List<String> words = new ArrayList<>();
  18.         while (true) {
  19.             String input = reader.readLine();
  20.             if (input.equals("END")) {
  21.                 break;
  22.             }
  23.             words.add(input);
  24.         }
  25.  
  26.  
  27.         int maxLength = 0;
  28.         for (String word : words) {
  29.             if (word.length() > maxLength) {
  30.                 maxLength = word.length();
  31.             }
  32.         }
  33.  
  34.         matrix = new char[words.size()][maxLength];
  35.         for (int i = 0; i < words.size(); i++) {
  36.             String word = words.get(i);
  37.             for (int j = 0; j < word.length(); j++) {
  38.                 matrix[i][j] = word.charAt(j);
  39.             }
  40.             for (int j = word.length(); j < maxLength; j++) {
  41.                 matrix[i][j] = ' ';
  42.             }
  43.         }
  44. //        TODO: if statement for rotation degrees check
  45.         int rotationTimes = rotateDegree / 90;
  46.         if (rotateDegree % 360 == 0) {
  47.             matrix = matrix;
  48.         } else if (rotateDegree % 270 == 0 && rotationTimes % 3 == 0 && rotationTimes % 9 != 0 && rotationTimes % 2 != 0){
  49.             matrix = rotate270(matrix);
  50.         } else if (rotateDegree % 180 == 0 && rotationTimes % 2 == 0 && rotationTimes % 9 != 0 && rotationTimes % 10 != 0){
  51.             matrix = rotate180(matrix);
  52.         } else if(rotateDegree % 90 == 0 ){
  53.             matrix = rotate90(matrix);
  54.         }
  55.  
  56.         for (char[] chars : matrix) {
  57.             for (char letter : chars) {
  58.                 System.out.print(letter);
  59.             }
  60.             System.out.println();
  61.         }
  62.  
  63.     }
  64.  
  65.     private static char[][] rotate90(char[][] matrixToRotate) {
  66.         int row = matrixToRotate[0].length;
  67.         int col = matrixToRotate.length;
  68.  
  69.         char[][] matrix = new char[row][col];
  70.         int cols = 0;
  71.         for (int i = 0; i < row; i++) {
  72.             int colMatrixRotate = matrixToRotate.length - 1;
  73.             for (int j = 0; j < col; j++) {
  74.                 matrix[i][j] = matrixToRotate[colMatrixRotate][cols];
  75.                 colMatrixRotate--;
  76.             }
  77.             cols++;
  78.         }
  79.         return matrix;
  80.     }
  81.  
  82.     private static char[][] rotate180(char[][] matrixRotate) {
  83.         int row = matrixRotate.length;
  84.         int col = matrixRotate[0].length;
  85.  
  86.         char[][] matrix = new char[row][col];
  87.  
  88.         int rows = row - 1;
  89.         for (int i = 0; i < row; i++) {
  90.             int cols = col - 1;
  91.             for (int j = 0; j < col; j++) {
  92.                 matrix[i][cols] = matrixRotate[rows][j];
  93.                 cols--;
  94.             }
  95.             rows--;
  96.         }
  97.         return matrix;
  98.  
  99.     }
  100.  
  101.     private static char[][] rotate270(char[][] matrixRotate) {
  102.         int row = matrixRotate[0].length;
  103.         int col = matrixRotate.length;
  104.  
  105.         char[][] matrix = new char[row][col];
  106.  
  107.         int colMatrix = row - 1;
  108.         for (int i = 0; i < row; i++) {
  109.             int rowMatrix = 0;
  110.             for (int j = 0; j < col; j++) {
  111.                 matrix[i][j] = matrixRotate[rowMatrix][colMatrix];
  112.                 rowMatrix++;
  113.             }
  114.             colMatrix--;
  115.         }
  116.  
  117.         return matrix;
  118.  
  119.     }
  120. }
Add Comment
Please, Sign In to add comment