Advertisement
mirozspace

pr6_MA_E_100

May 19th, 2019
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.31 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4.  
  5. public class Pr6StringMatrixRotation {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8.  
  9. List<String> list = new ArrayList<>();
  10. int biggestStringLength = 0;
  11. int stringLength;
  12.  
  13. String command = scanner.nextLine();
  14. String regex = "^(?<com>[A-Z][a-z]+)\\((?<degrees>[0-9]+)\\)$";
  15. Pattern pattern = Pattern.compile(regex);
  16. Matcher matcher = pattern.matcher(command);
  17.  
  18. if (matcher.find()) {
  19. //String commandMatrix = matcher.group("com");
  20. String degreesRotation = matcher.group("degrees");
  21.  
  22. degreesRotation = degreesCalculate(degreesRotation);
  23.  
  24. String line = scanner.nextLine();
  25.  
  26. while (!line.equals("END")) {
  27. stringLength = line.length();
  28. if (stringLength > biggestStringLength) {
  29. biggestStringLength = stringLength;
  30. }
  31. list.add(line);
  32. line = scanner.nextLine();
  33. }
  34.  
  35. String[][] matrix = new String[list.size()][biggestStringLength];
  36. //String[][] resultMatrix = new String[list.size()][biggestStringLength];
  37.  
  38. for (int i = 0; i < matrix.length; i++) {
  39. for (int j = 0; j < matrix[i].length; j++) {
  40. matrix[i][j] = " ";
  41. }
  42. }
  43. for (int i = 0; i < matrix.length; i++) {
  44. String lineInput = list.get(i);
  45. for (int j = 0; j < lineInput.length(); j++) {
  46. char symbol = lineInput.charAt(j);
  47. matrix[i][j] = symbol + "";
  48. }
  49. }
  50.  
  51. switch (degreesRotation) {
  52. case "0":
  53. printMatrix(matrix);
  54. break;
  55. case "90":
  56. matrix = rotateMatrix90(matrix);
  57. printMatrix(matrix);
  58. break;
  59. case "180":
  60. matrix = rotateMatrix180(matrix);
  61. printMatrix(matrix);
  62. break;
  63. case "270":
  64. matrix = rotateMatrix270(matrix);
  65. printMatrix(matrix);
  66. break;
  67. }
  68. }
  69. }
  70.  
  71. private static String degreesCalculate(String degreesRotation) {
  72. int degreesResult = 0;
  73. int degreesTemp = 0;
  74. int degreesRotationInt = Integer.parseInt(degreesRotation);
  75.  
  76. degreesTemp = degreesRotationInt;
  77. degreesResult = degreesRotationInt / 360;
  78. degreesRotationInt = degreesResult * 360;
  79. degreesRotationInt = degreesTemp - degreesRotationInt;
  80.  
  81. return degreesRotationInt + "";
  82. }
  83.  
  84.  
  85. private static String[][] rotateMatrix90(String[][] matrix) {
  86. String[][] resultMatrix = new String[matrix[0].length][matrix.length];
  87. StringBuilder sb = new StringBuilder();
  88. for (int i = 0; i < matrix[0].length; i++) {
  89. for (int j = 0; j < matrix.length; j++) {
  90. sb.append(matrix[j][i]);
  91. }
  92. sb.reverse();
  93. String sbStr = sb.toString();
  94. sb.delete(0, sb.length());
  95. String[] temp = new String[matrix.length];
  96. for (int j = 0; j < sbStr.length(); j++) {
  97. temp[j] = sbStr.charAt(j) + "";
  98. }
  99. resultMatrix[i] = temp;
  100. }
  101. return resultMatrix;
  102. }
  103.  
  104. private static String[][] rotateMatrix180(String[][] matrix) {
  105. String[][] resultMatrix = new String[matrix.length][matrix[0].length];
  106. StringBuilder sb = new StringBuilder();
  107. for (int i = 0; i < matrix.length; i++) {
  108. for (int j = 0; j < matrix[i].length; j++) {
  109. sb.append(matrix[i][j]);
  110. }
  111. sb.reverse();
  112. String sbStr = sb.toString();
  113. sb.delete(0, sb.length());
  114. String[] temp = new String[matrix[i].length];
  115. for (int j = 0; j < sbStr.length(); j++) {
  116. temp[j] = sbStr.charAt(j) + "";
  117. }
  118. resultMatrix[matrix.length - i - 1] = temp;
  119. }
  120. return resultMatrix;
  121. }
  122.  
  123. private static String[][] rotateMatrix270(String[][] matrix) {
  124. String[][] resultMatrix = new String[matrix[0].length][matrix.length];
  125. StringBuilder sb = new StringBuilder();
  126. for (int i = 0; i < matrix[0].length; i++) {
  127. for (int j = 0; j < matrix.length; j++) {
  128. sb.append(matrix[j][i]);
  129. }
  130. //sb.reverse();
  131. String sbStr = sb.toString();
  132. sb.delete(0, sb.length());
  133. String[] temp = new String[matrix.length];
  134. for (int j = 0; j < sbStr.length(); j++) {
  135. temp[j] = sbStr.charAt(j) + "";
  136. }
  137. resultMatrix[resultMatrix.length - i - 1] = temp;
  138. }
  139. return resultMatrix;
  140. }
  141.  
  142. private static void printMatrix(String[][] matrix) {
  143. for (String[] rows : matrix) {
  144. for (String s : rows) {
  145. System.out.print(s + "");
  146. }
  147. System.out.println();
  148. }
  149. }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement