Advertisement
Guest User

Untitled

a guest
Jan 26th, 2020
962
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.25 KB | None | 0 0
  1.  
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Scanner;
  5. import java.util.regex.Matcher;
  6. import java.util.regex.Pattern;
  7.  
  8.  
  9. public class asd {
  10.  
  11. public static void main(String[] args) {
  12. Scanner scanner = new Scanner(System.in);
  13.  
  14. String command = scanner.nextLine();
  15.  
  16. ArrayList<String> input = new ArrayList<>();
  17.  
  18. int counter = getInputAndCounter(scanner, command, input);
  19.  
  20. String rotationCommand = input.get(0);
  21.  
  22. int degrees = getDegrees(rotationCommand);
  23.  
  24. input.remove(0);
  25.  
  26. int length = getMatrixLength(input);
  27.  
  28. char[][] matrix = getMatrix(input, counter, length);
  29.  
  30.  
  31. if (degrees == 90) {
  32. oneRotation(input, length, matrix);
  33. } else if (degrees == 0) {
  34. print(input, length, matrix);
  35. } else if (degrees == 180) {
  36. twoRotations(input, counter, length, matrix);
  37. } else if (degrees == 270) {
  38. threeRotations(input, length, matrix);
  39. }
  40. }
  41.  
  42. private static int getDegrees(String rotationCommand) {
  43. String regex = "([A-Za-z]+)(\\()([0-9]+)(\\))";
  44. Pattern pattern = Pattern.compile(regex);
  45. Matcher matcher = pattern.matcher(rotationCommand);
  46.  
  47. boolean matches = matcher.matches();
  48.  
  49. int degrees = Integer.parseInt(matcher.group(3));
  50.  
  51. while (degrees >= 360){
  52. degrees = degrees % 360;
  53. }
  54. return degrees;
  55. }
  56.  
  57. private static void threeRotations(ArrayList<String> list, int length, char[][] matrix) {
  58. char[][] newMatrix = new char[length][list.size()];
  59. int i = 0;
  60. for (int c = 0; c < newMatrix[list.size()].length; c++) {
  61. int j = 0;
  62. for (int r = newMatrix.length - 1; r >= 0; r--) {
  63. if (j < matrix[i].length) {
  64. newMatrix[r][c] = matrix[i][j];
  65. }
  66. j++;
  67. }
  68. i++;
  69. }
  70. print(list, length, newMatrix);
  71. }
  72.  
  73. private static void twoRotations(ArrayList<String> list, int counter, int length, char[][] matrix) {
  74. char[][] newMatrix = new char[counter][length];
  75. for (int r = 0; r < matrix.length; r++) {
  76. for (int c = matrix[r].length - 1; c >= 0; c--) {
  77. newMatrix[matrix.length - 1 - r][c] = matrix[r][matrix[r].length - 1 - c];
  78. }
  79. }
  80. print(list, length, newMatrix);
  81. }
  82.  
  83. private static void print(ArrayList<String> list, int length, char[][] matrix) {
  84. for (int r = 0; r < matrix.length; r++) {
  85. for (int c = 0; c < matrix[r].length; c++) {
  86. if (matrix[r][c] == 0) { // You missed this condition!!!!!!!!!!!!!!!!!!!!!
  87. matrix[r][c] = ' ';
  88. }
  89. System.out.print(matrix[r][c]);
  90. }
  91. System.out.println();
  92. }
  93. }
  94.  
  95. private static void oneRotation(ArrayList<String> list, int length, char[][] matrix) {
  96. char[][] newMatrix = new char[length][list.size()];
  97. int i = 0;
  98. for (int c = matrix.length - 1; c >= 0; c--) {
  99. int j = 0;
  100. for (int r = 0; r < matrix[i].length; r++) {
  101. newMatrix[r][c] = matrix[i][j];
  102. j++;
  103. }
  104. i++;
  105. }
  106. print(list, length, newMatrix);
  107. }
  108.  
  109. private static char[][] getMatrix(ArrayList<String> list, int counter, int length) {
  110. char[][] matrix = new char[counter][length];
  111. for (int r = 0; r < matrix.length; r++) {
  112. char[] arr = list.get(r).toCharArray();
  113. matrix[r] = arr;
  114. }
  115. return matrix;
  116. }
  117.  
  118. private static int getMatrixLength(ArrayList<String> list) {
  119. int max = Integer.MIN_VALUE;
  120. for (String s : list) {
  121. if (s.length() > max) {
  122. max = s.length();
  123. }
  124. }
  125. return max;
  126. }
  127.  
  128. private static int getInputAndCounter(Scanner scanner, String command, ArrayList<String> list) {
  129. int counter = 0;
  130. while (!command.equalsIgnoreCase("END")) {
  131. list.add(command);
  132. command = scanner.nextLine();
  133. counter++;
  134. }
  135. return counter - 1;
  136. }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement