Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class Pr6StringMatrixRotation {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- List<String> list = new ArrayList<>();
- int biggestStringLength = 0;
- int stringLength;
- String command = scanner.nextLine();
- String regex = "^(?<com>[A-Z][a-z]+)\\((?<degrees>[0-9]+)\\)$";
- Pattern pattern = Pattern.compile(regex);
- Matcher matcher = pattern.matcher(command);
- if (matcher.find()) {
- //String commandMatrix = matcher.group("com");
- String degreesRotation = matcher.group("degrees");
- degreesRotation = degreesCalculate(degreesRotation);
- String line = scanner.nextLine();
- while (!line.equals("END")) {
- stringLength = line.length();
- if (stringLength > biggestStringLength) {
- biggestStringLength = stringLength;
- }
- list.add(line);
- line = scanner.nextLine();
- }
- String[][] matrix = new String[list.size()][biggestStringLength];
- //String[][] resultMatrix = new String[list.size()][biggestStringLength];
- for (int i = 0; i < matrix.length; i++) {
- for (int j = 0; j < matrix[i].length; j++) {
- matrix[i][j] = " ";
- }
- }
- for (int i = 0; i < matrix.length; i++) {
- String lineInput = list.get(i);
- for (int j = 0; j < lineInput.length(); j++) {
- char symbol = lineInput.charAt(j);
- matrix[i][j] = symbol + "";
- }
- }
- switch (degreesRotation) {
- case "0":
- printMatrix(matrix);
- break;
- case "90":
- matrix = rotateMatrix90(matrix);
- printMatrix(matrix);
- break;
- case "180":
- matrix = rotateMatrix180(matrix);
- printMatrix(matrix);
- break;
- case "270":
- matrix = rotateMatrix270(matrix);
- printMatrix(matrix);
- break;
- }
- }
- }
- private static String degreesCalculate(String degreesRotation) {
- int degreesResult = 0;
- int degreesTemp = 0;
- int degreesRotationInt = Integer.parseInt(degreesRotation);
- degreesTemp = degreesRotationInt;
- degreesResult = degreesRotationInt / 360;
- degreesRotationInt = degreesResult * 360;
- degreesRotationInt = degreesTemp - degreesRotationInt;
- return degreesRotationInt + "";
- }
- private static String[][] rotateMatrix90(String[][] matrix) {
- String[][] resultMatrix = new String[matrix[0].length][matrix.length];
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < matrix[0].length; i++) {
- for (int j = 0; j < matrix.length; j++) {
- sb.append(matrix[j][i]);
- }
- sb.reverse();
- String sbStr = sb.toString();
- sb.delete(0, sb.length());
- String[] temp = new String[matrix.length];
- for (int j = 0; j < sbStr.length(); j++) {
- temp[j] = sbStr.charAt(j) + "";
- }
- resultMatrix[i] = temp;
- }
- return resultMatrix;
- }
- private static String[][] rotateMatrix180(String[][] matrix) {
- String[][] resultMatrix = new String[matrix.length][matrix[0].length];
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < matrix.length; i++) {
- for (int j = 0; j < matrix[i].length; j++) {
- sb.append(matrix[i][j]);
- }
- sb.reverse();
- String sbStr = sb.toString();
- sb.delete(0, sb.length());
- String[] temp = new String[matrix[i].length];
- for (int j = 0; j < sbStr.length(); j++) {
- temp[j] = sbStr.charAt(j) + "";
- }
- resultMatrix[matrix.length - i - 1] = temp;
- }
- return resultMatrix;
- }
- private static String[][] rotateMatrix270(String[][] matrix) {
- String[][] resultMatrix = new String[matrix[0].length][matrix.length];
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < matrix[0].length; i++) {
- for (int j = 0; j < matrix.length; j++) {
- sb.append(matrix[j][i]);
- }
- //sb.reverse();
- String sbStr = sb.toString();
- sb.delete(0, sb.length());
- String[] temp = new String[matrix.length];
- for (int j = 0; j < sbStr.length(); j++) {
- temp[j] = sbStr.charAt(j) + "";
- }
- resultMatrix[resultMatrix.length - i - 1] = temp;
- }
- return resultMatrix;
- }
- private static void printMatrix(String[][] matrix) {
- for (String[] rows : matrix) {
- for (String s : rows) {
- System.out.print(s + "");
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement